blob: 21efb0b949a6c710ee76f7aa9da74c83a6717dbb [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 Jiang3e148a32019-06-19 17:30:46 +0800128static int rdev_init_wb(struct md_rdev *rdev)
129{
130 if (rdev->bdev->bd_queue->nr_hw_queues == 1)
131 return 0;
132
133 spin_lock_init(&rdev->wb_list_lock);
134 INIT_LIST_HEAD(&rdev->wb_list);
135 init_waitqueue_head(&rdev->wb_io_wait);
136 set_bit(WBCollisionCheck, &rdev->flags);
137
138 return 1;
139}
140
Guoqing Jiang963c5552019-06-14 17:10:36 +0800141/*
142 * Create wb_info_pool if rdev is the first multi-queue device flaged
143 * with writemostly, also write-behind mode is enabled.
144 */
145void mddev_create_wb_pool(struct mddev *mddev, struct md_rdev *rdev,
146 bool is_suspend)
147{
148 if (mddev->bitmap_info.max_write_behind == 0)
149 return;
150
151 if (!test_bit(WriteMostly, &rdev->flags) || !rdev_init_wb(rdev))
152 return;
153
154 if (mddev->wb_info_pool == NULL) {
155 unsigned int noio_flag;
156
157 if (!is_suspend)
158 mddev_suspend(mddev);
159 noio_flag = memalloc_noio_save();
160 mddev->wb_info_pool = mempool_create_kmalloc_pool(NR_WB_INFOS,
161 sizeof(struct wb_info));
162 memalloc_noio_restore(noio_flag);
163 if (!mddev->wb_info_pool)
164 pr_err("can't alloc memory pool for writemostly\n");
165 if (!is_suspend)
166 mddev_resume(mddev);
167 }
168}
169EXPORT_SYMBOL_GPL(mddev_create_wb_pool);
170
171/*
172 * destroy wb_info_pool if rdev is the last device flaged with WBCollisionCheck.
173 */
174static void mddev_destroy_wb_pool(struct mddev *mddev, struct md_rdev *rdev)
175{
176 if (!test_and_clear_bit(WBCollisionCheck, &rdev->flags))
177 return;
178
179 if (mddev->wb_info_pool) {
180 struct md_rdev *temp;
181 int num = 0;
182
183 /*
184 * Check if other rdevs need wb_info_pool.
185 */
186 rdev_for_each(temp, mddev)
187 if (temp != rdev &&
188 test_bit(WBCollisionCheck, &temp->flags))
189 num++;
190 if (!num) {
191 mddev_suspend(rdev->mddev);
192 mempool_destroy(mddev->wb_info_pool);
193 mddev->wb_info_pool = NULL;
194 mddev_resume(rdev->mddev);
195 }
196 }
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static struct ctl_table_header *raid_table_header;
200
Joe Perches82592c32013-11-14 15:16:18 +1100201static struct ctl_table raid_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 .procname = "speed_limit_min",
204 .data = &sysctl_speed_limit_min,
205 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700206 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800207 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 },
209 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 .procname = "speed_limit_max",
211 .data = &sysctl_speed_limit_max,
212 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700213 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800214 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800216 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
Joe Perches82592c32013-11-14 15:16:18 +1100219static struct ctl_table raid_dir_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .procname = "raid",
222 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700223 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 .child = raid_table,
225 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800226 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227};
228
Joe Perches82592c32013-11-14 15:16:18 +1100229static struct ctl_table raid_root_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 .procname = "dev",
232 .maxlen = 0,
233 .mode = 0555,
234 .child = raid_dir_table,
235 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800236 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237};
238
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700239static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
NeilBrownf91de922005-11-08 21:39:36 -0800241static int start_readonly;
242
NeilBrown78b63502017-04-12 16:26:13 +1000243/*
244 * The original mechanism for creating an md device is to create
245 * a device node in /dev and to open it. This causes races with device-close.
246 * The preferred method is to write to the "new_array" module parameter.
247 * This can avoid races.
248 * Setting create_on_open to false disables the original mechanism
249 * so all the races disappear.
250 */
251static bool create_on_open = true;
252
NeilBrowna167f662010-10-26 18:31:13 +1100253struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100254 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100255{
Kent Overstreetafeee512018-05-20 18:25:52 -0400256 if (!mddev || !bioset_initialized(&mddev->bio_set))
NeilBrowna167f662010-10-26 18:31:13 +1100257 return bio_alloc(gfp_mask, nr_iovecs);
258
Marcos Paulo de Souza62516912019-01-14 06:31:56 -0700259 return bio_alloc_bioset(gfp_mask, nr_iovecs, &mddev->bio_set);
NeilBrowna167f662010-10-26 18:31:13 +1100260}
261EXPORT_SYMBOL_GPL(bio_alloc_mddev);
262
NeilBrown5a850712017-06-21 09:12:21 +1000263static struct bio *md_bio_alloc_sync(struct mddev *mddev)
264{
Kent Overstreetafeee512018-05-20 18:25:52 -0400265 if (!mddev || !bioset_initialized(&mddev->sync_set))
NeilBrown5a850712017-06-21 09:12:21 +1000266 return bio_alloc(GFP_NOIO, 1);
267
Kent Overstreetafeee512018-05-20 18:25:52 -0400268 return bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set);
NeilBrown5a850712017-06-21 09:12:21 +1000269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/*
NeilBrownd7603b72006-01-06 00:20:30 -0800272 * We have a system wide 'event count' that is incremented
273 * on any 'interesting' event, and readers of /proc/mdstat
274 * can use 'poll' or 'select' to find out when the event
275 * count increases.
276 *
277 * Events are:
278 * start array, stop array, error, add device, remove device,
279 * start build, activate spare
280 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800281static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800282static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100283void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800284{
285 atomic_inc(&md_event_count);
286 wake_up(&md_event_waiters);
287}
NeilBrown29269552006-03-27 01:18:10 -0800288EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800289
290/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * Enables to iterate over all existing md arrays
292 * all_mddevs_lock protects this list.
293 */
294static LIST_HEAD(all_mddevs);
295static DEFINE_SPINLOCK(all_mddevs_lock);
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/*
298 * iterates through all used mddevs in the system.
299 * We take care to grab the all_mddevs_lock whenever navigating
300 * the list, and to always hold a refcount when unlocked.
301 * Any code which breaks out of this loop while own
302 * a reference to the current mddev and must mddev_put it.
303 */
NeilBrownfd01b882011-10-11 16:47:53 +1100304#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 \
NeilBrownf72ffdd2014-09-30 14:23:59 +1000306 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100307 _tmp = all_mddevs.next; \
308 _mddev = NULL;}); \
309 ({ if (_tmp != &all_mddevs) \
310 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100312 if (_mddev) mddev_put(_mddev); \
313 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
314 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100316 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 )
318
NeilBrown409c57f2009-03-31 14:39:39 +1100319/* Rather than calling directly into the personality make_request function,
320 * IO requests come here first so that we can check if the device is
321 * being suspended pending a reconfiguration.
322 * We hold a refcount over the call to ->make_request. By the time that
323 * call has finished, the bio has been linked into some internal structure
324 * and so is visible to ->quiesce(), so we don't need the refcount any more.
325 */
NeilBrownb3143b92017-10-17 13:46:43 +1100326static bool is_suspended(struct mddev *mddev, struct bio *bio)
327{
328 if (mddev->suspended)
329 return true;
330 if (bio_data_dir(bio) != WRITE)
331 return false;
332 if (mddev->suspend_lo >= mddev->suspend_hi)
333 return false;
334 if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
335 return false;
336 if (bio_end_sector(bio) < mddev->suspend_lo)
337 return false;
338 return true;
339}
340
Shaohua Li393debc2017-09-21 10:23:35 -0700341void md_handle_request(struct mddev *mddev, struct bio *bio)
342{
343check_suspended:
344 rcu_read_lock();
NeilBrownb3143b92017-10-17 13:46:43 +1100345 if (is_suspended(mddev, bio)) {
Shaohua Li393debc2017-09-21 10:23:35 -0700346 DEFINE_WAIT(__wait);
347 for (;;) {
348 prepare_to_wait(&mddev->sb_wait, &__wait,
349 TASK_UNINTERRUPTIBLE);
NeilBrownb3143b92017-10-17 13:46:43 +1100350 if (!is_suspended(mddev, bio))
Shaohua Li393debc2017-09-21 10:23:35 -0700351 break;
352 rcu_read_unlock();
353 schedule();
354 rcu_read_lock();
355 }
356 finish_wait(&mddev->sb_wait, &__wait);
357 }
358 atomic_inc(&mddev->active_io);
359 rcu_read_unlock();
360
361 if (!mddev->pers->make_request(mddev, bio)) {
362 atomic_dec(&mddev->active_io);
363 wake_up(&mddev->sb_wait);
364 goto check_suspended;
365 }
366
367 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
368 wake_up(&mddev->sb_wait);
369}
370EXPORT_SYMBOL(md_handle_request);
371
Jens Axboedece1632015-11-05 10:41:16 -0700372static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
NeilBrown49077322010-03-25 16:20:56 +1100374 const int rw = bio_data_dir(bio);
Michael Callahanddcf35d2018-07-18 04:47:39 -0700375 const int sgrp = op_stat_group(bio_op(bio));
NeilBrownfd01b882011-10-11 16:47:53 +1100376 struct mddev *mddev = q->queuedata;
Chris Masone91ece52011-02-07 19:21:48 -0500377 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100378
NeilBrownaf67c312017-06-18 14:38:57 +1000379 blk_queue_split(q, &bio);
Kent Overstreet54efd502015-04-23 22:37:18 -0700380
NeilBrown274d8cb2016-01-04 16:16:58 +1100381 if (mddev == NULL || mddev->pers == NULL) {
NeilBrown409c57f2009-03-31 14:39:39 +1100382 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700383 return BLK_QC_T_NONE;
NeilBrown409c57f2009-03-31 14:39:39 +1100384 }
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100385 if (mddev->ro == 1 && unlikely(rw == WRITE)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200386 if (bio_sectors(bio) != 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200387 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200388 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700389 return BLK_QC_T_NONE;
Sebastian Riemerbbfa57c2013-02-21 13:28:09 +1100390 }
NeilBrown49077322010-03-25 16:20:56 +1100391
Chris Masone91ece52011-02-07 19:21:48 -0500392 /*
393 * save the sectors now since our bio can
394 * go away inside make_request
395 */
396 sectors = bio_sectors(bio);
Shaohua Li9c573de2016-04-25 16:52:38 -0700397 /* bio could be mergeable after passing to underlayer */
Jens Axboe1eff9d32016-08-05 15:35:16 -0600398 bio->bi_opf &= ~REQ_NOMERGE;
Shaohua Li393debc2017-09-21 10:23:35 -0700399
400 md_handle_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100401
Mike Snitzer112f1582018-12-06 11:41:18 -0500402 part_stat_lock();
403 part_stat_inc(&mddev->gendisk->part0, ios[sgrp]);
404 part_stat_add(&mddev->gendisk->part0, sectors[sgrp], sectors);
Gu Zheng74672d02015-04-03 08:44:47 +0800405 part_stat_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100406
Jens Axboedece1632015-11-05 10:41:16 -0700407 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
NeilBrown9e35b992010-04-06 14:23:02 +1000410/* mddev_suspend makes sure no new requests are submitted
411 * to the device, and that any requests that have been submitted
412 * are completely handled.
NeilBrownafa0f552014-12-15 12:56:58 +1100413 * Once mddev_detach() is called and completes, the module will be
414 * completely unused.
NeilBrown9e35b992010-04-06 14:23:02 +1000415 */
NeilBrownfd01b882011-10-11 16:47:53 +1100416void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100417{
Heinz Mauelshagen092398d2016-05-03 19:43:57 +0200418 WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
NeilBrown4d5324f2017-10-19 12:17:16 +1100419 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100420 if (mddev->suspended++)
421 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100422 synchronize_rcu();
NeilBrowncc27b0c2017-06-05 16:49:39 +1000423 wake_up(&mddev->sb_wait);
NeilBrown35bfc522017-10-17 13:46:43 +1100424 set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
425 smp_mb__after_atomic();
NeilBrown409c57f2009-03-31 14:39:39 +1100426 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
427 mddev->pers->quiesce(mddev, 1);
NeilBrown35bfc522017-10-17 13:46:43 +1100428 clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
429 wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
Jonathan Brassow0d9f4f12012-05-16 04:06:14 -0500430
431 del_timer_sync(&mddev->safemode_timer);
NeilBrown409c57f2009-03-31 14:39:39 +1100432}
NeilBrown390ee602010-06-01 19:37:27 +1000433EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100434
NeilBrownfd01b882011-10-11 16:47:53 +1100435void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100436{
NeilBrown4d5324f2017-10-19 12:17:16 +1100437 lockdep_assert_held(&mddev->reconfig_mutex);
Mikulas Patocka0dc10e52015-12-18 15:19:16 +1100438 if (--mddev->suspended)
439 return;
NeilBrown409c57f2009-03-31 14:39:39 +1100440 wake_up(&mddev->sb_wait);
441 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500442
Jonathan Brassow47525e52012-05-22 13:55:29 +1000443 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500444 md_wakeup_thread(mddev->thread);
445 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100446}
NeilBrown390ee602010-06-01 19:37:27 +1000447EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100448
NeilBrownfd01b882011-10-11 16:47:53 +1100449int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000450{
NeilBrown5c675f82014-12-15 12:56:56 +1100451 struct md_personality *pers = mddev->pers;
452 int ret = 0;
453
454 rcu_read_lock();
455 if (mddev->suspended)
456 ret = 1;
457 else if (pers && pers->congested)
458 ret = pers->congested(mddev, bits);
459 rcu_read_unlock();
460 return ret;
NeilBrown3fa841d2009-09-23 18:10:29 +1000461}
NeilBrown5c675f82014-12-15 12:56:56 +1100462EXPORT_SYMBOL_GPL(mddev_congested);
463static int md_congested(void *data, int bits)
464{
465 struct mddev *mddev = data;
466 return mddev_congested(mddev, bits);
467}
NeilBrown3fa841d2009-09-23 18:10:29 +1000468
NeilBrowna2826aa2009-12-14 12:49:49 +1100469/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200470 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100471 */
NeilBrown4bc034d2019-03-29 10:46:16 -0700472
473static void md_end_flush(struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100474{
NeilBrown4bc034d2019-03-29 10:46:16 -0700475 struct md_rdev *rdev = bio->bi_private;
476 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100477
478 rdev_dec_pending(rdev, mddev);
479
NeilBrown4bc034d2019-03-29 10:46:16 -0700480 if (atomic_dec_and_test(&mddev->flush_pending)) {
481 /* The pre-request flush has finished */
482 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100483 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700484 bio_put(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100485}
486
NeilBrown4bc034d2019-03-29 10:46:16 -0700487static void md_submit_flush_data(struct work_struct *ws);
488
489static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100490{
NeilBrown4bc034d2019-03-29 10:46:16 -0700491 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100492 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100493
NeilBrown2bc13b82019-03-29 10:46:17 -0700494 mddev->start_flush = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700495 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
496 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100497 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +1100498 rdev_for_each_rcu(rdev, mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100499 if (rdev->raid_disk >= 0 &&
500 !test_bit(Faulty, &rdev->flags)) {
501 /* Take two references, one is dropped
502 * when request finishes, one after
503 * we reclaim rcu_read_lock
504 */
505 struct bio *bi;
506 atomic_inc(&rdev->nr_pending);
507 atomic_inc(&rdev->nr_pending);
508 rcu_read_unlock();
Shaohua Lib5e1b8c2012-05-21 09:26:59 +1000509 bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
Xiao Ni5a409b42018-05-21 11:49:54 +0800510 bi->bi_end_io = md_end_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700511 bi->bi_private = rdev;
512 bio_set_dev(bi, rdev->bdev);
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600513 bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
NeilBrown4bc034d2019-03-29 10:46:16 -0700514 atomic_inc(&mddev->flush_pending);
Mike Christie4e49ea42016-06-05 14:31:41 -0500515 submit_bio(bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100516 rcu_read_lock();
517 rdev_dec_pending(rdev, mddev);
518 }
519 rcu_read_unlock();
NeilBrown4bc034d2019-03-29 10:46:16 -0700520 if (atomic_dec_and_test(&mddev->flush_pending))
521 queue_work(md_wq, &mddev->flush_work);
522}
NeilBrowna2826aa2009-12-14 12:49:49 +1100523
NeilBrown4bc034d2019-03-29 10:46:16 -0700524static void md_submit_flush_data(struct work_struct *ws)
525{
526 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
527 struct bio *bio = mddev->flush_bio;
528
529 /*
530 * must reset flush_bio before calling into md_handle_request to avoid a
531 * deadlock, because other bios passed md_handle_request suspend check
532 * could wait for this and below md_handle_request could wait for those
533 * bios because of suspend check
534 */
NeilBrown2bc13b82019-03-29 10:46:17 -0700535 mddev->last_flush = mddev->start_flush;
NeilBrown4bc034d2019-03-29 10:46:16 -0700536 mddev->flush_bio = NULL;
537 wake_up(&mddev->sb_wait);
538
539 if (bio->bi_iter.bi_size == 0) {
540 /* an empty barrier - all done */
541 bio_endio(bio);
542 } else {
543 bio->bi_opf &= ~REQ_PREFLUSH;
544 md_handle_request(mddev, bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100545 }
NeilBrowna2826aa2009-12-14 12:49:49 +1100546}
NeilBrown4bc034d2019-03-29 10:46:16 -0700547
548void md_flush_request(struct mddev *mddev, struct bio *bio)
549{
NeilBrown2bc13b82019-03-29 10:46:17 -0700550 ktime_t start = ktime_get_boottime();
NeilBrown4bc034d2019-03-29 10:46:16 -0700551 spin_lock_irq(&mddev->lock);
552 wait_event_lock_irq(mddev->sb_wait,
NeilBrown2bc13b82019-03-29 10:46:17 -0700553 !mddev->flush_bio ||
554 ktime_after(mddev->last_flush, start),
NeilBrown4bc034d2019-03-29 10:46:16 -0700555 mddev->lock);
NeilBrown2bc13b82019-03-29 10:46:17 -0700556 if (!ktime_after(mddev->last_flush, start)) {
557 WARN_ON(mddev->flush_bio);
558 mddev->flush_bio = bio;
559 bio = NULL;
560 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700561 spin_unlock_irq(&mddev->lock);
562
NeilBrown2bc13b82019-03-29 10:46:17 -0700563 if (!bio) {
564 INIT_WORK(&mddev->flush_work, submit_flushes);
565 queue_work(md_wq, &mddev->flush_work);
566 } else {
567 /* flush was performed for some other bio while we waited. */
568 if (bio->bi_iter.bi_size == 0)
569 /* an empty barrier - all done */
570 bio_endio(bio);
571 else {
572 bio->bi_opf &= ~REQ_PREFLUSH;
573 mddev->pers->make_request(mddev, bio);
574 }
575 }
NeilBrown4bc034d2019-03-29 10:46:16 -0700576}
Tejun Heoe9c74692010-09-03 11:56:18 +0200577EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100578
NeilBrownfd01b882011-10-11 16:47:53 +1100579static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581 atomic_inc(&mddev->active);
582 return mddev;
583}
584
Dan Williams5fd3a172009-03-04 00:57:25 -0700585static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100586
NeilBrownfd01b882011-10-11 16:47:53 +1100587static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
590 return;
NeilBrownd3374822009-01-09 08:31:10 +1100591 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100592 mddev->ctime == 0 && !mddev->hold_active) {
593 /* Array is not configured at all, and not held active,
594 * so destroy it */
NeilBrownaf8a2432011-12-08 15:49:46 +1100595 list_del_init(&mddev->all_mddevs);
Kent Overstreet28dec872018-06-07 20:52:54 -0400596
597 /*
598 * Call queue_work inside the spinlock so that
599 * flush_workqueue() after mddev_find will succeed in waiting
600 * for the work to be done.
601 */
602 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
603 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100604 }
605 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Kees Cook8376d3c2017-10-16 17:01:48 -0700608static void md_safemode_timeout(struct timer_list *t);
Sasha Levin25b2edf2015-07-24 18:19:58 -0400609
NeilBrownfd01b882011-10-11 16:47:53 +1100610void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100611{
Kent Overstreet28dec872018-06-07 20:52:54 -0400612 kobject_init(&mddev->kobj, &md_ktype);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100613 mutex_init(&mddev->open_mutex);
614 mutex_init(&mddev->reconfig_mutex);
615 mutex_init(&mddev->bitmap_info.mutex);
616 INIT_LIST_HEAD(&mddev->disks);
617 INIT_LIST_HEAD(&mddev->all_mddevs);
Kees Cook8376d3c2017-10-16 17:01:48 -0700618 timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100619 atomic_set(&mddev->active, 1);
620 atomic_set(&mddev->openers, 0);
621 atomic_set(&mddev->active_io, 0);
NeilBrown85572d72014-12-15 12:56:56 +1100622 spin_lock_init(&mddev->lock);
NeilBrown4bc034d2019-03-29 10:46:16 -0700623 atomic_set(&mddev->flush_pending, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100624 init_waitqueue_head(&mddev->sb_wait);
625 init_waitqueue_head(&mddev->recovery_wait);
626 mddev->reshape_position = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +1000627 mddev->reshape_backwards = 0;
Jonathan Brassowc4a39552013-06-25 01:23:59 -0500628 mddev->last_sync_action = "none";
NeilBrownfafd7fb2010-04-01 15:55:30 +1100629 mddev->resync_min = 0;
630 mddev->resync_max = MaxSector;
631 mddev->level = LEVEL_NONE;
632}
NeilBrown390ee602010-06-01 19:37:27 +1000633EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100634
NeilBrownf72ffdd2014-09-30 14:23:59 +1000635static struct mddev *mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
NeilBrownfd01b882011-10-11 16:47:53 +1100637 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
NeilBrown8f5f02c2011-02-16 13:58:51 +1100639 if (unit && MAJOR(unit) != MD_MAJOR)
640 unit &= ~((1<<MdpMinorShift)-1);
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 retry:
643 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
NeilBrownefeb53c2009-01-09 08:31:10 +1100645 if (unit) {
646 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
647 if (mddev->unit == unit) {
648 mddev_get(mddev);
649 spin_unlock(&all_mddevs_lock);
650 kfree(new);
651 return mddev;
652 }
653
654 if (new) {
655 list_add(&new->all_mddevs, &all_mddevs);
656 spin_unlock(&all_mddevs_lock);
657 new->hold_active = UNTIL_IOCTL;
658 return new;
659 }
660 } else if (new) {
661 /* find an unused unit number */
662 static int next_minor = 512;
663 int start = next_minor;
664 int is_free = 0;
665 int dev = 0;
666 while (!is_free) {
667 dev = MKDEV(MD_MAJOR, next_minor);
668 next_minor++;
669 if (next_minor > MINORMASK)
670 next_minor = 0;
671 if (next_minor == start) {
672 /* Oh dear, all in use. */
673 spin_unlock(&all_mddevs_lock);
674 kfree(new);
675 return NULL;
676 }
NeilBrownf72ffdd2014-09-30 14:23:59 +1000677
NeilBrownefeb53c2009-01-09 08:31:10 +1100678 is_free = 1;
679 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
680 if (mddev->unit == dev) {
681 is_free = 0;
682 break;
683 }
684 }
685 new->unit = dev;
686 new->md_minor = MINOR(dev);
687 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 list_add(&new->all_mddevs, &all_mddevs);
689 spin_unlock(&all_mddevs_lock);
690 return new;
691 }
692 spin_unlock(&all_mddevs_lock);
693
NeilBrown9ffae0c2006-01-06 00:20:32 -0800694 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (!new)
696 return NULL;
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 new->unit = unit;
699 if (MAJOR(unit) == MD_MAJOR)
700 new->md_minor = MINOR(unit);
701 else
702 new->md_minor = MINOR(unit) >> MdpMinorShift;
703
NeilBrownfafd7fb2010-04-01 15:55:30 +1100704 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 goto retry;
707}
708
NeilBrownb6eb1272010-04-15 10:13:47 +1000709static struct attribute_group md_redundancy_group;
710
NeilBrown5c47daf2014-12-15 12:57:01 +1100711void mddev_unlock(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
NeilBrowna64c8762010-04-14 17:15:37 +1000713 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000714 /* These cannot be removed under reconfig_mutex as
715 * an access to the files will try to take reconfig_mutex
716 * while holding the file unremovable, which leads to
717 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000718 * So hold set sysfs_active while the remove in happeing,
719 * and anything else which might set ->to_remove or my
720 * otherwise change the sysfs namespace will fail with
721 * -EBUSY if sysfs_active is still set.
722 * We set sysfs_active under reconfig_mutex and elsewhere
723 * test it under the same mutex to ensure its correct value
724 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000725 */
NeilBrowna64c8762010-04-14 17:15:37 +1000726 struct attribute_group *to_remove = mddev->to_remove;
727 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000728 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000729 mutex_unlock(&mddev->reconfig_mutex);
730
NeilBrown00bcb4a2010-06-01 19:37:23 +1000731 if (mddev->kobj.sd) {
732 if (to_remove != &md_redundancy_group)
733 sysfs_remove_group(&mddev->kobj, to_remove);
734 if (mddev->pers == NULL ||
735 mddev->pers->sync_request == NULL) {
736 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
737 if (mddev->sysfs_action)
738 sysfs_put(mddev->sysfs_action);
739 mddev->sysfs_action = NULL;
740 }
NeilBrowna64c8762010-04-14 17:15:37 +1000741 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000742 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000743 } else
744 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Chris Dunlop751e67c2011-10-19 16:48:26 +1100746 /* As we've dropped the mutex we need a spinlock to
747 * make sure the thread doesn't disappear
NeilBrown01f96c02011-09-21 15:30:20 +1000748 */
749 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700750 md_wakeup_thread(mddev->thread);
NeilBrown4d5324f2017-10-19 12:17:16 +1100751 wake_up(&mddev->sb_wait);
NeilBrown01f96c02011-09-21 15:30:20 +1000752 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
NeilBrown5c47daf2014-12-15 12:57:01 +1100754EXPORT_SYMBOL_GPL(mddev_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500756struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
NeilBrown1ca69c42012-10-11 13:37:33 +1100757{
758 struct md_rdev *rdev;
759
760 rdev_for_each_rcu(rdev, mddev)
761 if (rdev->desc_nr == nr)
762 return rdev;
763
764 return NULL;
765}
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -0500766EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100767
768static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
NeilBrown3cb03002011-10-11 16:45:26 +1100770 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
NeilBrowndafb20f2012-03-19 12:46:39 +1100772 rdev_for_each(rdev, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (rdev->bdev->bd_dev == dev)
774 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return NULL;
777}
778
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100779struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev)
NeilBrown1ca69c42012-10-11 13:37:33 +1100780{
781 struct md_rdev *rdev;
782
783 rdev_for_each_rcu(rdev, mddev)
784 if (rdev->bdev->bd_dev == dev)
785 return rdev;
786
787 return NULL;
788}
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +0100789EXPORT_SYMBOL_GPL(md_find_rdev_rcu);
NeilBrown1ca69c42012-10-11 13:37:33 +1100790
NeilBrown84fc4b52011-10-11 16:49:58 +1100791static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800792{
NeilBrown84fc4b52011-10-11 16:49:58 +1100793 struct md_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800794 list_for_each_entry(pers, &pers_list, list) {
795 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800796 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800797 if (strcmp(pers->name, clevel)==0)
798 return pers;
799 }
NeilBrown2604b702006-01-06 00:20:36 -0800800 return NULL;
801}
802
Andre Nollb73df2d2008-07-11 22:02:23 +1000803/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100804static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100806 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000807 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
NeilBrownf72ffdd2014-09-30 14:23:59 +1000810static int alloc_disk_sb(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 rdev->sb_page = alloc_page(GFP_KERNEL);
NeilBrown7f0f0d82016-11-02 14:16:49 +1100813 if (!rdev->sb_page)
Andre Nollebc24332008-07-11 22:02:20 +1000814 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return 0;
816}
817
NeilBrown545c8792012-05-22 13:54:30 +1000818void md_rdev_clear(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800821 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 rdev->sb_loaded = 0;
823 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000824 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100825 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
NeilBrown2699b672011-07-28 11:31:47 +1000827 if (rdev->bb_page) {
828 put_page(rdev->bb_page);
829 rdev->bb_page = NULL;
830 }
Dan Williamsd3b407fb2016-01-06 12:19:22 -0800831 badblocks_exit(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
NeilBrown545c8792012-05-22 13:54:30 +1000833EXPORT_SYMBOL_GPL(md_rdev_clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200835static void super_written(struct bio *bio)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700836{
NeilBrown3cb03002011-10-11 16:45:26 +1100837 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100838 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700839
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200840 if (bio->bi_status) {
841 pr_err("md: super_written gets error=%d\n", bio->bi_status);
NeilBrowna9701a32005-11-08 21:39:34 -0800842 md_error(mddev, rdev);
NeilBrown46533ff2016-11-18 16:16:11 +1100843 if (!test_bit(Faulty, &rdev->flags)
844 && (bio->bi_opf & MD_FAILFAST)) {
Shaohua Li29530792016-12-08 15:48:19 -0800845 set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
NeilBrown46533ff2016-11-18 16:16:11 +1100846 set_bit(LastDev, &rdev->flags);
847 }
848 } else
849 clear_bit(LastDev, &rdev->flags);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700850
NeilBrowna9701a32005-11-08 21:39:34 -0800851 if (atomic_dec_and_test(&mddev->pending_writes))
852 wake_up(&mddev->sb_wait);
Shaohua Lied3b98c2016-03-29 14:00:19 -0700853 rdev_dec_pending(rdev, mddev);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700854 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700855}
856
NeilBrownfd01b882011-10-11 16:47:53 +1100857void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700858 sector_t sector, int size, struct page *page)
859{
860 /* write first size bytes of page to sector of rdev
861 * Increment mddev->pending_writes before returning
862 * and decrement it on completion, waking up sb_wait
863 * if zero is reached.
864 * If an error occurred, call md_error
865 */
NeilBrown46533ff2016-11-18 16:16:11 +1100866 struct bio *bio;
867 int ff = 0;
868
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +0100869 if (!page)
870 return;
871
NeilBrown46533ff2016-11-18 16:16:11 +1100872 if (test_bit(Faulty, &rdev->flags))
873 return;
874
NeilBrown5a850712017-06-21 09:12:21 +1000875 bio = md_bio_alloc_sync(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700876
Shaohua Lied3b98c2016-03-29 14:00:19 -0700877 atomic_inc(&rdev->nr_pending);
878
Christoph Hellwig74d46992017-08-23 19:10:32 +0200879 bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700880 bio->bi_iter.bi_sector = sector;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700881 bio_add_page(bio, page, size, 0);
882 bio->bi_private = rdev;
883 bio->bi_end_io = super_written;
NeilBrown46533ff2016-11-18 16:16:11 +1100884
885 if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
886 test_bit(FailFast, &rdev->flags) &&
887 !test_bit(LastDev, &rdev->flags))
888 ff = MD_FAILFAST;
Jan Kara5a8948f2017-05-31 09:44:33 +0200889 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
NeilBrowna9701a32005-11-08 21:39:34 -0800890
NeilBrown7bfa19f2005-06-21 17:17:28 -0700891 atomic_inc(&mddev->pending_writes);
Mike Christie4e49ea42016-06-05 14:31:41 -0500892 submit_bio(bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800893}
894
NeilBrown46533ff2016-11-18 16:16:11 +1100895int md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800896{
Tejun Heoe9c74692010-09-03 11:56:18 +0200897 /* wait for all superblock writes that were scheduled to complete */
NeilBrown1967cd52014-09-09 14:20:28 +1000898 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
Shaohua Li29530792016-12-08 15:48:19 -0800899 if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
NeilBrown46533ff2016-11-18 16:16:11 +1100900 return -EAGAIN;
901 return 0;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700902}
903
NeilBrown3cb03002011-10-11 16:45:26 +1100904int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Mike Christie796a5cf2016-06-05 14:32:07 -0500905 struct page *page, int op, int op_flags, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
NeilBrown5a850712017-06-21 09:12:21 +1000907 struct bio *bio = md_bio_alloc_sync(rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 int ret;
909
Christoph Hellwig74d46992017-08-23 19:10:32 +0200910 if (metadata_op && rdev->meta_bdev)
911 bio_set_dev(bio, rdev->meta_bdev);
912 else
913 bio_set_dev(bio, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -0500914 bio_set_op_attrs(bio, op, op_flags);
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100915 if (metadata_op)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700916 bio->bi_iter.bi_sector = sector + rdev->sb_start;
NeilBrown1fdd6fc92012-05-21 09:28:32 +1000917 else if (rdev->mddev->reshape_position != MaxSector &&
918 (rdev->mddev->reshape_backwards ==
919 (sector >= rdev->mddev->reshape_position)))
Kent Overstreet4f024f32013-10-11 15:44:27 -0700920 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100921 else
Kent Overstreet4f024f32013-10-11 15:44:27 -0700922 bio->bi_iter.bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 bio_add_page(bio, page, size, 0);
Mike Christie4e49ea42016-06-05 14:31:41 -0500924
925 submit_bio_wait(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200927 ret = !bio->bi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 bio_put(bio);
929 return ret;
930}
NeilBrowna8745db2006-01-06 00:20:34 -0800931EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
NeilBrownf72ffdd2014-09-30 14:23:59 +1000933static int read_disk_sb(struct md_rdev *rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +1000936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (rdev->sb_loaded)
938 return 0;
939
Mike Christie796a5cf2016-06-05 14:32:07 -0500940 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 goto fail;
942 rdev->sb_loaded = 1;
943 return 0;
944
945fail:
NeilBrown9d487392016-11-02 14:16:49 +1100946 pr_err("md: disabled device %s, could not read superblock.\n",
947 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return -EINVAL;
949}
950
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300951static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
NeilBrownf72ffdd2014-09-30 14:23:59 +1000953 return sb1->set_uuid0 == sb2->set_uuid0 &&
Andre Noll05710462008-07-11 22:02:20 +1000954 sb1->set_uuid1 == sb2->set_uuid1 &&
955 sb1->set_uuid2 == sb2->set_uuid2 &&
956 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Amir Goldsteine6fd2092017-05-04 16:26:20 +0300959static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 int ret;
962 mdp_super_t *tmp1, *tmp2;
963
964 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
965 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
966
967 if (!tmp1 || !tmp2) {
968 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 goto abort;
970 }
971
972 *tmp1 = *sb1;
973 *tmp2 = *sb2;
974
975 /*
976 * nr_disks is not constant
977 */
978 tmp1->nr_disks = 0;
979 tmp2->nr_disks = 0;
980
Andre Nollce0c8e02008-07-11 22:02:20 +1000981 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700983 kfree(tmp1);
984 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return ret;
986}
987
NeilBrown4d167f02007-05-09 02:35:37 -0700988static u32 md_csum_fold(u32 csum)
989{
990 csum = (csum & 0xffff) + (csum >> 16);
991 return (csum & 0xffff) + (csum >> 16);
992}
993
NeilBrownf72ffdd2014-09-30 14:23:59 +1000994static unsigned int calc_sb_csum(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
NeilBrown4d167f02007-05-09 02:35:37 -0700996 u64 newcsum = 0;
997 u32 *sb32 = (u32*)sb;
998 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 unsigned int disk_csum, csum;
1000
1001 disk_csum = sb->sb_csum;
1002 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -07001003
1004 for (i = 0; i < MD_SB_BYTES/4 ; i++)
1005 newcsum += sb32[i];
1006 csum = (newcsum & 0xffffffff) + (newcsum>>32);
1007
NeilBrown4d167f02007-05-09 02:35:37 -07001008#ifdef CONFIG_ALPHA
1009 /* This used to use csum_partial, which was wrong for several
1010 * reasons including that different results are returned on
1011 * different architectures. It isn't critical that we get exactly
1012 * the same return value as before (we always csum_fold before
1013 * testing, and that removes any differences). However as we
1014 * know that csum_partial always returned a 16bit value on
1015 * alphas, do a fold to maximise conformity to previous behaviour.
1016 */
1017 sb->sb_csum = md_csum_fold(disk_csum);
1018#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -07001020#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return csum;
1022}
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024/*
1025 * Handle superblock details.
1026 * We want to be able to handle multiple superblock formats
1027 * so we have a common interface to them all, and an array of
1028 * different handlers.
1029 * We rely on user-space to write the initial superblock, and support
1030 * reading and updating of superblocks.
1031 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +11001032 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 * loads and validates a superblock on dev.
1034 * if refdev != NULL, compare superblocks on both devices
1035 * Return:
1036 * 0 - dev has a superblock that is compatible with refdev
1037 * 1 - dev has a superblock that is compatible and newer than refdev
1038 * so dev should be used as the refdev in future
1039 * -EINVAL superblock incompatible or invalid
1040 * -othererror e.g. -EIO
1041 *
NeilBrownfd01b882011-10-11 16:47:53 +11001042 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 * Verify that dev is acceptable into mddev.
1044 * The first time, mddev->raid_disks will be 0, and data from
1045 * dev should be merged in. Subsequent calls check that dev
1046 * is new enough. Return 0 or -EINVAL
1047 *
NeilBrownfd01b882011-10-11 16:47:53 +11001048 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 * Update the superblock for rdev with data in mddev
1050 * This does not write to disc.
1051 *
1052 */
1053
1054struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001055 char *name;
1056 struct module *owner;
NeilBrownc6563a82012-05-21 09:27:00 +10001057 int (*load_super)(struct md_rdev *rdev,
1058 struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001059 int minor_version);
NeilBrownc6563a82012-05-21 09:27:00 +10001060 int (*validate_super)(struct mddev *mddev,
1061 struct md_rdev *rdev);
1062 void (*sync_super)(struct mddev *mddev,
1063 struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001064 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001065 sector_t num_sectors);
NeilBrownc6563a82012-05-21 09:27:00 +10001066 int (*allow_new_offset)(struct md_rdev *rdev,
1067 unsigned long long new_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068};
1069
1070/*
Andre Noll0894cc32009-06-18 08:49:23 +10001071 * Check that the given mddev has no bitmap.
1072 *
1073 * This function is called from the run method of all personalities that do not
1074 * support bitmaps. It prints an error message and returns non-zero if mddev
1075 * has a bitmap. Otherwise, it returns 0.
1076 *
1077 */
NeilBrownfd01b882011-10-11 16:47:53 +11001078int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001079{
NeilBrownc3d97142009-12-14 12:49:52 +11001080 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001081 return 0;
NeilBrown9d487392016-11-02 14:16:49 +11001082 pr_warn("%s: bitmaps are not supported for %s\n",
Andre Noll0894cc32009-06-18 08:49:23 +10001083 mdname(mddev), mddev->pers->name);
1084 return 1;
1085}
1086EXPORT_SYMBOL(md_check_no_bitmap);
1087
1088/*
NeilBrownf72ffdd2014-09-30 14:23:59 +10001089 * load_super for 0.90.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 */
NeilBrown3cb03002011-10-11 16:45:26 +11001091static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1094 mdp_super_t *sb;
1095 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 /*
Andre Noll0f420352008-07-11 22:02:23 +10001098 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 * it's at the end of the disk.
1100 *
1101 * It also happens to be a multiple of 4Kb.
1102 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001103 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
NeilBrown0002b272005-09-09 16:23:53 -07001105 ret = read_disk_sb(rdev, MD_SB_BYTES);
NeilBrown9d487392016-11-02 14:16:49 +11001106 if (ret)
1107 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 ret = -EINVAL;
1110
1111 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001112 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 if (sb->md_magic != MD_SB_MAGIC) {
NeilBrown9d487392016-11-02 14:16:49 +11001115 pr_warn("md: invalid raid superblock magic on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 goto abort;
1117 }
1118
1119 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001120 sb->minor_version < 90 ||
1121 sb->minor_version > 91) {
NeilBrown9d487392016-11-02 14:16:49 +11001122 pr_warn("Bad version number %d.%d on %s\n",
1123 sb->major_version, sb->minor_version, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 goto abort;
1125 }
1126
1127 if (sb->raid_disks <= 0)
1128 goto abort;
1129
NeilBrown4d167f02007-05-09 02:35:37 -07001130 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
NeilBrown9d487392016-11-02 14:16:49 +11001131 pr_warn("md: invalid superblock checksum on %s\n", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 goto abort;
1133 }
1134
1135 rdev->preferred_minor = sb->md_minor;
1136 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10001137 rdev->new_data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001138 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001139 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (sb->level == LEVEL_MULTIPATH)
1142 rdev->desc_nr = -1;
1143 else
1144 rdev->desc_nr = sb->this_disk.number;
1145
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001146 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001148 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001150 mdp_super_t *refsb = page_address(refdev->sb_page);
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001151 if (!md_uuid_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001152 pr_warn("md: %s has different UUID to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 b, bdevname(refdev->bdev,b2));
1154 goto abort;
1155 }
Amir Goldsteine6fd2092017-05-04 16:26:20 +03001156 if (!md_sb_equal(refsb, sb)) {
NeilBrown9d487392016-11-02 14:16:49 +11001157 pr_warn("md: %s has same UUID but different superblock to %s\n",
1158 b, bdevname(refdev->bdev, b2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 goto abort;
1160 }
1161 ev1 = md_event(sb);
1162 ev2 = md_event(refsb);
1163 if (ev1 > ev2)
1164 ret = 1;
NeilBrownf72ffdd2014-09-30 14:23:59 +10001165 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 ret = 0;
1167 }
NeilBrown8190e752009-06-18 08:48:58 +10001168 rdev->sectors = rdev->sb_start;
NeilBrown667a5312012-08-16 16:46:12 +10001169 /* Limit to 4TB as metadata cannot record more than that.
1170 * (not needed for Linear and RAID0 as metadata doesn't
1171 * record this size)
1172 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001173 if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001174 rdev->sectors = (sector_t)(2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
NeilBrown27a7b262011-09-10 17:21:28 +10001176 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001177 /* "this cannot possibly happen" ... */
1178 ret = -EINVAL;
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 abort:
1181 return ret;
1182}
1183
1184/*
1185 * validate_super for 0.90.0
1186 */
NeilBrownfd01b882011-10-11 16:47:53 +11001187static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001190 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001191 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
NeilBrown41158c72005-06-21 17:17:25 -07001193 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001194 clear_bit(Faulty, &rdev->flags);
1195 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001196 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001197 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (mddev->raid_disks == 0) {
1200 mddev->major_version = 0;
1201 mddev->minor_version = sb->minor_version;
1202 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001203 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001204 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 mddev->ctime = sb->ctime;
1206 mddev->utime = sb->utime;
1207 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001208 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 mddev->layout = sb->layout;
1210 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001211 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001212 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001213 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001214 mddev->bitmap_info.space = 0;
1215 /* bitmap can use 60 K after the 4K superblocks */
NeilBrownc3d97142009-12-14 12:49:52 +11001216 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001217 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrown2c810cd2012-05-21 09:27:00 +10001218 mddev->reshape_backwards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
NeilBrownf6705572006-03-27 01:18:11 -08001220 if (mddev->minor_version >= 91) {
1221 mddev->reshape_position = sb->reshape_position;
1222 mddev->delta_disks = sb->delta_disks;
1223 mddev->new_level = sb->new_level;
1224 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001225 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001226 if (mddev->delta_disks < 0)
1227 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001228 } else {
1229 mddev->reshape_position = MaxSector;
1230 mddev->delta_disks = 0;
1231 mddev->new_level = mddev->level;
1232 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001233 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001234 }
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (sb->state & (1<<MD_SB_CLEAN))
1237 mddev->recovery_cp = MaxSector;
1238 else {
NeilBrownf72ffdd2014-09-30 14:23:59 +10001239 if (sb->events_hi == sb->cp_events_hi &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 sb->events_lo == sb->cp_events_lo) {
1241 mddev->recovery_cp = sb->recovery_cp;
1242 } else
1243 mddev->recovery_cp = 0;
1244 }
1245
1246 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1247 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1248 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1249 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1250
1251 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001252
1253 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001254 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001255 mddev->bitmap_info.offset =
1256 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10001257 mddev->bitmap_info.space =
Dave Jonesc9ad0202013-08-19 22:26:32 -04001258 mddev->bitmap_info.default_space;
NeilBrown6409bb02012-05-22 13:55:07 +10001259 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001260
NeilBrown41158c72005-06-21 17:17:25 -07001261 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001262 /* Insist on good event counter while assembling, except
1263 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001265 if (sb->disks[rdev->desc_nr].state & (
1266 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
NeilBrownf72ffdd2014-09-30 14:23:59 +10001267 if (ev1 < mddev->events)
NeilBrownbe6800a2010-05-18 10:17:09 +10001268 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001269 } else if (mddev->bitmap) {
1270 /* if adding to array with a bitmap, then we can accept an
1271 * older device ... but not too old.
1272 */
NeilBrown41158c72005-06-21 17:17:25 -07001273 if (ev1 < mddev->bitmap->events_cleared)
1274 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001275 if (ev1 < mddev->events)
1276 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001277 } else {
1278 if (ev1 < mddev->events)
1279 /* just a hot-add of a new device, leave raid_disk at -1 */
1280 return 0;
1281 }
NeilBrown41158c72005-06-21 17:17:25 -07001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 desc = sb->disks + rdev->desc_nr;
1285
1286 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001287 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001288 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1289 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001290 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 rdev->raid_disk = desc->raid_disk;
NeilBrownf4667222013-12-09 12:04:56 +11001292 rdev->saved_raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001293 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1294 /* active but not in sync implies recovery up to
1295 * reshape position. We don't know exactly where
1296 * that is, so set to zero for now */
1297 if (mddev->minor_version >= 91) {
1298 rdev->recovery_offset = 0;
1299 rdev->raid_disk = desc->raid_disk;
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001302 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1303 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001304 if (desc->state & (1<<MD_DISK_FAILFAST))
1305 set_bit(FailFast, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001306 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001307 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return 0;
1309}
1310
1311/*
1312 * sync_super for 0.90.0
1313 */
NeilBrownfd01b882011-10-11 16:47:53 +11001314static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001317 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /* make rdev->sb match mddev data..
1321 *
1322 * 1/ zero out disks
1323 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1324 * 3/ any empty disks < next_spare become removed
1325 *
1326 * disks[0] gets initialised to REMOVED because
1327 * we cannot be sure from other fields if it has
1328 * been initialised or not.
1329 */
1330 int i;
1331 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1332
NeilBrown61181562005-09-09 16:24:02 -07001333 rdev->sb_size = MD_SB_BYTES;
1334
Namhyung Kim65a06f062011-07-27 11:00:36 +10001335 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 memset(sb, 0, sizeof(*sb));
1338
1339 sb->md_magic = MD_SB_MAGIC;
1340 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 sb->patch_version = mddev->patch_version;
1342 sb->gvalid_words = 0; /* ignored */
1343 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1344 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1345 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1346 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1347
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001348 sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001350 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 sb->raid_disks = mddev->raid_disks;
1352 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001353 sb->not_persistent = 0;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001354 sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 sb->state = 0;
1356 sb->events_hi = (mddev->events>>32);
1357 sb->events_lo = (u32)mddev->events;
1358
NeilBrownf6705572006-03-27 01:18:11 -08001359 if (mddev->reshape_position == MaxSector)
1360 sb->minor_version = 90;
1361 else {
1362 sb->minor_version = 91;
1363 sb->reshape_position = mddev->reshape_position;
1364 sb->new_level = mddev->new_level;
1365 sb->delta_disks = mddev->delta_disks;
1366 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001367 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001368 }
1369 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (mddev->in_sync)
1371 {
1372 sb->recovery_cp = mddev->recovery_cp;
1373 sb->cp_events_hi = (mddev->events>>32);
1374 sb->cp_events_lo = (u32)mddev->events;
1375 if (mddev->recovery_cp == MaxSector)
1376 sb->state = (1<< MD_SB_CLEAN);
1377 } else
1378 sb->recovery_cp = 0;
1379
1380 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001381 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
NeilBrownc3d97142009-12-14 12:49:52 +11001383 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001384 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrowndafb20f2012-03-19 12:46:39 +11001387 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001389 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001390 int is_active = test_bit(In_sync, &rdev2->flags);
1391
1392 if (rdev2->raid_disk >= 0 &&
1393 sb->minor_version >= 91)
1394 /* we have nowhere to store the recovery_offset,
1395 * but if it is not below the reshape_position,
1396 * we can piggy-back on that.
1397 */
1398 is_active = 1;
1399 if (rdev2->raid_disk < 0 ||
1400 test_bit(Faulty, &rdev2->flags))
1401 is_active = 0;
1402 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001403 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001405 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001406 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 d = &sb->disks[rdev2->desc_nr];
1408 nr_disks++;
1409 d->number = rdev2->desc_nr;
1410 d->major = MAJOR(rdev2->bdev->bd_dev);
1411 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001412 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 d->raid_disk = rdev2->raid_disk;
1414 else
1415 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001416 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001418 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001420 if (test_bit(In_sync, &rdev2->flags))
1421 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 active++;
1423 working++;
1424 } else {
1425 d->state = 0;
1426 spare++;
1427 working++;
1428 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001429 if (test_bit(WriteMostly, &rdev2->flags))
1430 d->state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11001431 if (test_bit(FailFast, &rdev2->flags))
1432 d->state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /* now set the "removed" and "faulty" bits on any missing devices */
1435 for (i=0 ; i < mddev->raid_disks ; i++) {
1436 mdp_disk_t *d = &sb->disks[i];
1437 if (d->state == 0 && d->number == 0) {
1438 d->number = i;
1439 d->raid_disk = i;
1440 d->state = (1<<MD_DISK_REMOVED);
1441 d->state |= (1<<MD_DISK_FAULTY);
1442 failed++;
1443 }
1444 }
1445 sb->nr_disks = nr_disks;
1446 sb->active_disks = active;
1447 sb->working_disks = working;
1448 sb->failed_disks = failed;
1449 sb->spare_disks = spare;
1450
1451 sb->this_disk = sb->disks[rdev->desc_nr];
1452 sb->sb_csum = calc_sb_csum(sb);
1453}
1454
1455/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001456 * rdev_size_change for 0.90.0
1457 */
1458static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001459super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001460{
Andre Noll58c0fed2009-03-31 14:33:13 +11001461 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001462 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001463 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001464 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001465 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001466 if (!num_sectors || num_sectors > rdev->sb_start)
1467 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001468 /* Limit to 4TB as metadata cannot record more than that.
1469 * 4TB == 2^32 KB, or 2*2^32 sectors.
1470 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02001471 if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
Arnd Bergmann3312c952015-12-21 10:51:01 +11001472 num_sectors = (sector_t)(2ULL << 32) - 2;
NeilBrown46533ff2016-11-18 16:16:11 +11001473 do {
1474 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001475 rdev->sb_page);
NeilBrown46533ff2016-11-18 16:16:11 +11001476 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001477 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001478}
1479
NeilBrownc6563a82012-05-21 09:27:00 +10001480static int
1481super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1482{
1483 /* non-zero offset changes not possible with v0.90 */
1484 return new_offset == 0;
1485}
Chris Webb0cd17fe2008-06-28 08:31:46 +10001486
1487/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 * version 1 superblock
1489 */
1490
NeilBrownf72ffdd2014-09-30 14:23:59 +10001491static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001493 __le32 disk_csum;
1494 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 unsigned long long newcsum;
1496 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001497 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 disk_csum = sb->sb_csum;
1500 sb->sb_csum = 0;
1501 newcsum = 0;
NeilBrown1f3c9902012-12-11 13:09:00 +11001502 for (; size >= 4; size -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 newcsum += le32_to_cpu(*isuper++);
1504
1505 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001506 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1509 sb->sb_csum = disk_csum;
1510 return cpu_to_le32(csum);
1511}
1512
NeilBrown3cb03002011-10-11 16:45:26 +11001513static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 struct mdp_superblock_1 *sb;
1516 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001517 sector_t sb_start;
NeilBrownc6563a82012-05-21 09:27:00 +10001518 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001520 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 /*
Andre Noll0f420352008-07-11 22:02:23 +10001523 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 * It is always aligned to a 4K boundary and
1525 * depeding on minor_version, it can be:
1526 * 0: At least 8K, but less than 12K, from end of device
1527 * 1: At start of device
1528 * 2: 4K from start of device.
1529 */
1530 switch(minor_version) {
1531 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001532 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001533 sb_start -= 8*2;
1534 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 break;
1536 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001537 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 break;
1539 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001540 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 break;
1542 default:
1543 return -EINVAL;
1544 }
Andre Noll0f420352008-07-11 22:02:23 +10001545 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
NeilBrown0002b272005-09-09 16:23:53 -07001547 /* superblock is rarely larger than 1K, but it can be larger,
1548 * and it is safe to read 4k, so we do that
1549 */
1550 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (ret) return ret;
1552
Namhyung Kim65a06f062011-07-27 11:00:36 +10001553 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1556 sb->major_version != cpu_to_le32(1) ||
1557 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001558 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001559 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return -EINVAL;
1561
1562 if (calc_sb_1_csum(sb) != sb->sb_csum) {
NeilBrown9d487392016-11-02 14:16:49 +11001563 pr_warn("md: invalid superblock checksum on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 bdevname(rdev->bdev,b));
1565 return -EINVAL;
1566 }
1567 if (le64_to_cpu(sb->data_size) < 10) {
NeilBrown9d487392016-11-02 14:16:49 +11001568 pr_warn("md: data_size too small on %s\n",
1569 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return -EINVAL;
1571 }
NeilBrownc6563a82012-05-21 09:27:00 +10001572 if (sb->pad0 ||
1573 sb->pad3[0] ||
1574 memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1575 /* Some padding is non-zero, might be a new feature */
1576 return -EINVAL;
NeilBrowne11e93f2007-05-09 02:35:36 -07001577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 rdev->preferred_minor = 0xffff;
1579 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrownc6563a82012-05-21 09:27:00 +10001580 rdev->new_data_offset = rdev->data_offset;
1581 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1582 (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1583 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001584 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
NeilBrown0002b272005-09-09 16:23:53 -07001586 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001587 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001588 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001589 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1590
1591 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001592 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001593 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10001594 if (minor_version
1595 && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1596 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001597
NeilBrown31b65a02006-07-10 04:44:14 -07001598 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1599 rdev->desc_nr = -1;
1600 else
1601 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1602
NeilBrown2699b672011-07-28 11:31:47 +10001603 if (!rdev->bb_page) {
1604 rdev->bb_page = alloc_page(GFP_KERNEL);
1605 if (!rdev->bb_page)
1606 return -ENOMEM;
1607 }
1608 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1609 rdev->badblocks.count == 0) {
1610 /* need to load the bad block list.
1611 * Currently we limit it to one page.
1612 */
1613 s32 offset;
1614 sector_t bb_sector;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001615 __le64 *bbp;
NeilBrown2699b672011-07-28 11:31:47 +10001616 int i;
1617 int sectors = le16_to_cpu(sb->bblog_size);
1618 if (sectors > (PAGE_SIZE / 512))
1619 return -EINVAL;
1620 offset = le32_to_cpu(sb->bblog_offset);
1621 if (offset == 0)
1622 return -EINVAL;
1623 bb_sector = (long long)offset;
1624 if (!sync_page_io(rdev, bb_sector, sectors << 9,
Mike Christie796a5cf2016-06-05 14:32:07 -05001625 rdev->bb_page, REQ_OP_READ, 0, true))
NeilBrown2699b672011-07-28 11:31:47 +10001626 return -EIO;
Christoph Hellwig00485d02019-04-04 18:56:12 +02001627 bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10001628 rdev->badblocks.shift = sb->bblog_shift;
1629 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1630 u64 bb = le64_to_cpu(*bbp);
1631 int count = bb & (0x3ff);
1632 u64 sector = bb >> 10;
1633 sector <<= sb->bblog_shift;
1634 count <<= sb->bblog_shift;
1635 if (bb + 1 == 0)
1636 break;
Vishal Vermafc974ee2015-12-24 19:20:34 -07001637 if (badblocks_set(&rdev->badblocks, sector, count, 1))
NeilBrown2699b672011-07-28 11:31:47 +10001638 return -EINVAL;
1639 }
NeilBrown486adf72013-04-24 11:42:44 +10001640 } else if (sb->bblog_offset != 0)
1641 rdev->badblocks.shift = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001642
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001643 if ((le32_to_cpu(sb->feature_map) &
1644 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001645 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1646 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1647 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1648 }
1649
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001650 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001651 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001652 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001654 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1657 sb->level != refsb->level ||
1658 sb->layout != refsb->layout ||
1659 sb->chunksize != refsb->chunksize) {
NeilBrown9d487392016-11-02 14:16:49 +11001660 pr_warn("md: %s has strangely different superblock to %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 bdevname(rdev->bdev,b),
1662 bdevname(refdev->bdev,b2));
1663 return -EINVAL;
1664 }
1665 ev1 = le64_to_cpu(sb->events);
1666 ev2 = le64_to_cpu(refsb->events);
1667
1668 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001669 ret = 1;
1670 else
1671 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
NeilBrownc6563a82012-05-21 09:27:00 +10001673 if (minor_version) {
1674 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1675 sectors -= rdev->data_offset;
1676 } else
1677 sectors = rdev->sb_start;
1678 if (sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001680 rdev->sectors = le64_to_cpu(sb->data_size);
NeilBrown8ed75462006-02-03 03:03:41 -08001681 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
NeilBrownfd01b882011-10-11 16:47:53 +11001684static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001686 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001687 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
NeilBrown41158c72005-06-21 17:17:25 -07001689 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001690 clear_bit(Faulty, &rdev->flags);
1691 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11001692 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001693 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (mddev->raid_disks == 0) {
1696 mddev->major_version = 1;
1697 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001698 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001699 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11001700 mddev->ctime = le64_to_cpu(sb->ctime);
1701 mddev->utime = le64_to_cpu(sb->utime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001703 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 mddev->layout = le32_to_cpu(sb->layout);
1705 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001706 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001707 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001708 mddev->bitmap_info.offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10001709 mddev->bitmap_info.space = 0;
1710 /* Default location for bitmap is 1K after superblock
1711 * using 3K - total of 4K
1712 */
NeilBrownc3d97142009-12-14 12:49:52 +11001713 mddev->bitmap_info.default_offset = 1024 >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10001714 mddev->bitmap_info.default_space = (4096-1024) >> 9;
NeilBrown2c810cd2012-05-21 09:27:00 +10001715 mddev->reshape_backwards = 0;
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1718 memcpy(mddev->uuid, sb->set_uuid, 16);
1719
1720 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001721
NeilBrown71c08052005-09-09 16:23:51 -07001722 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrown6409bb02012-05-22 13:55:07 +10001723 mddev->bitmap_info.file == NULL) {
NeilBrownc3d97142009-12-14 12:49:52 +11001724 mddev->bitmap_info.offset =
1725 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrown6409bb02012-05-22 13:55:07 +10001726 /* Metadata doesn't record how much space is available.
1727 * For 1.0, we assume we can use up to the superblock
1728 * if before, else to 4K beyond superblock.
1729 * For others, assume no change is possible.
1730 */
1731 if (mddev->minor_version > 0)
1732 mddev->bitmap_info.space = 0;
1733 else if (mddev->bitmap_info.offset > 0)
1734 mddev->bitmap_info.space =
1735 8 - mddev->bitmap_info.offset;
1736 else
1737 mddev->bitmap_info.space =
1738 -mddev->bitmap_info.offset;
1739 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001740
NeilBrownf6705572006-03-27 01:18:11 -08001741 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1742 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1743 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1744 mddev->new_level = le32_to_cpu(sb->new_level);
1745 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001746 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrown2c810cd2012-05-21 09:27:00 +10001747 if (mddev->delta_disks < 0 ||
1748 (mddev->delta_disks == 0 &&
1749 (le32_to_cpu(sb->feature_map)
1750 & MD_FEATURE_RESHAPE_BACKWARDS)))
1751 mddev->reshape_backwards = 1;
NeilBrownf6705572006-03-27 01:18:11 -08001752 } else {
1753 mddev->reshape_position = MaxSector;
1754 mddev->delta_disks = 0;
1755 mddev->new_level = mddev->level;
1756 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001757 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001758 }
1759
Song Liu486b0f72016-08-19 15:34:01 -07001760 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
Shaohua Lia62ab492016-01-06 14:37:13 -08001761 set_bit(MD_HAS_JOURNAL, &mddev->flags);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001762
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001763 if (le32_to_cpu(sb->feature_map) &
1764 (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001765 if (le32_to_cpu(sb->feature_map) &
1766 (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1767 return -EINVAL;
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001768 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1769 (le32_to_cpu(sb->feature_map) &
1770 MD_FEATURE_MULTIPLE_PPLS))
1771 return -EINVAL;
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001772 set_bit(MD_HAS_PPL, &mddev->flags);
1773 }
NeilBrown41158c72005-06-21 17:17:25 -07001774 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001775 /* Insist of good event counter while assembling, except for
1776 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001778 if (rdev->desc_nr >= 0 &&
1779 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
Song Liua3dfbda2015-10-08 21:54:11 -07001780 (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1781 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
NeilBrownbe6800a2010-05-18 10:17:09 +10001782 if (ev1 < mddev->events)
1783 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001784 } else if (mddev->bitmap) {
1785 /* If adding to array with a bitmap, then we can accept an
1786 * older device, but not too old.
1787 */
NeilBrown41158c72005-06-21 17:17:25 -07001788 if (ev1 < mddev->bitmap->events_cleared)
1789 return 0;
NeilBrown8313b8e2013-12-12 10:13:33 +11001790 if (ev1 < mddev->events)
1791 set_bit(Bitmap_sync, &rdev->flags);
NeilBrown07d84d102006-06-26 00:27:56 -07001792 } else {
1793 if (ev1 < mddev->events)
1794 /* just a hot-add of a new device, leave raid_disk at -1 */
1795 return 0;
1796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (mddev->level != LEVEL_MULTIPATH) {
1798 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001799 if (rdev->desc_nr < 0 ||
1800 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
Song Liuc4d4c912015-08-13 14:31:54 -07001801 role = MD_DISK_ROLE_SPARE;
NeilBrown3673f302009-08-03 10:59:56 +10001802 rdev->desc_nr = -1;
1803 } else
1804 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 switch(role) {
Song Liuc4d4c912015-08-13 14:31:54 -07001806 case MD_DISK_ROLE_SPARE: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 break;
Song Liuc4d4c912015-08-13 14:31:54 -07001808 case MD_DISK_ROLE_FAULTY: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001809 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 break;
Song Liubac624f2015-08-13 14:31:55 -07001811 case MD_DISK_ROLE_JOURNAL: /* journal device */
1812 if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1813 /* journal device without journal feature */
NeilBrown9d487392016-11-02 14:16:49 +11001814 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
Song Liubac624f2015-08-13 14:31:55 -07001815 return -EINVAL;
1816 }
1817 set_bit(Journal, &rdev->flags);
Shaohua Li3069aa82015-08-13 14:31:56 -07001818 rdev->journal_tail = le64_to_cpu(sb->journal_tail);
Shaohua Li9b156032015-12-18 15:19:16 +11001819 rdev->raid_disk = 0;
Song Liubac624f2015-08-13 14:31:55 -07001820 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 default:
NeilBrownf4667222013-12-09 12:04:56 +11001822 rdev->saved_raid_disk = role;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001823 if ((le32_to_cpu(sb->feature_map) &
NeilBrownf4667222013-12-09 12:04:56 +11001824 MD_FEATURE_RECOVERY_OFFSET)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001825 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001826 if (!(le32_to_cpu(sb->feature_map) &
1827 MD_FEATURE_RECOVERY_BITMAP))
1828 rdev->saved_raid_disk = -1;
Guoqing Jiang062f5b2a2019-07-24 11:09:20 +02001829 } else {
1830 /*
1831 * If the array is FROZEN, then the device can't
1832 * be in_sync with rest of array.
1833 */
1834 if (!test_bit(MD_RECOVERY_FROZEN,
1835 &mddev->recovery))
1836 set_bit(In_sync, &rdev->flags);
1837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 rdev->raid_disk = role;
1839 break;
1840 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001841 if (sb->devflags & WriteMostly1)
1842 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11001843 if (sb->devflags & FailFast1)
1844 set_bit(FailFast, &rdev->flags);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001845 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1846 set_bit(Replacement, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001847 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001848 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return 0;
1851}
1852
NeilBrownfd01b882011-10-11 16:47:53 +11001853static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001856 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 int max_dev, i;
1858 /* make rdev->sb match mddev and rdev data. */
1859
Namhyung Kim65a06f062011-07-27 11:00:36 +10001860 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 sb->feature_map = 0;
1863 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001864 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 memset(sb->pad3, 0, sizeof(sb->pad3));
1866
1867 sb->utime = cpu_to_le64((__u64)mddev->utime);
1868 sb->events = cpu_to_le64(mddev->events);
1869 if (mddev->in_sync)
1870 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
Shaohua Libd18f642015-09-02 13:49:50 -07001871 else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1872 sb->resync_offset = cpu_to_le64(MaxSector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 else
1874 sb->resync_offset = cpu_to_le64(0);
1875
NeilBrown1c05b4b2006-10-21 10:24:08 -07001876 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001877
NeilBrownf0ca3402006-02-02 14:28:04 -08001878 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001879 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001880 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001881 sb->level = cpu_to_le32(mddev->level);
1882 sb->layout = cpu_to_le32(mddev->layout);
NeilBrown688834e2016-11-18 16:16:11 +11001883 if (test_bit(FailFast, &rdev->flags))
1884 sb->devflags |= FailFast1;
1885 else
1886 sb->devflags &= ~FailFast1;
NeilBrownf0ca3402006-02-02 14:28:04 -08001887
NeilBrownaeb9b2112011-08-25 14:43:08 +10001888 if (test_bit(WriteMostly, &rdev->flags))
1889 sb->devflags |= WriteMostly1;
1890 else
1891 sb->devflags &= ~WriteMostly1;
NeilBrownc6563a82012-05-21 09:27:00 +10001892 sb->data_offset = cpu_to_le64(rdev->data_offset);
1893 sb->data_size = cpu_to_le64(rdev->sectors);
NeilBrownaeb9b2112011-08-25 14:43:08 +10001894
NeilBrownc3d97142009-12-14 12:49:52 +11001895 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1896 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001897 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001898 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001899
Shaohua Lif2076e72015-10-08 21:54:12 -07001900 if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
NeilBrown97e4f422009-03-31 14:33:13 +11001901 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001902 sb->feature_map |=
1903 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1904 sb->recovery_offset =
1905 cpu_to_le64(rdev->recovery_offset);
NeilBrownf4667222013-12-09 12:04:56 +11001906 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1907 sb->feature_map |=
1908 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001909 }
Shaohua Li3069aa82015-08-13 14:31:56 -07001910 /* Note: recovery_offset and journal_tail share space */
1911 if (test_bit(Journal, &rdev->flags))
1912 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
NeilBrown2d78f8c2011-12-23 10:17:51 +11001913 if (test_bit(Replacement, &rdev->flags))
1914 sb->feature_map |=
1915 cpu_to_le32(MD_FEATURE_REPLACEMENT);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001916
NeilBrownf6705572006-03-27 01:18:11 -08001917 if (mddev->reshape_position != MaxSector) {
1918 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1919 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1920 sb->new_layout = cpu_to_le32(mddev->new_layout);
1921 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1922 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001923 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrown2c810cd2012-05-21 09:27:00 +10001924 if (mddev->delta_disks == 0 &&
1925 mddev->reshape_backwards)
1926 sb->feature_map
1927 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
NeilBrownc6563a82012-05-21 09:27:00 +10001928 if (rdev->new_data_offset != rdev->data_offset) {
1929 sb->feature_map
1930 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1931 sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1932 - rdev->data_offset));
1933 }
NeilBrownf6705572006-03-27 01:18:11 -08001934 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001935
Goldwyn Rodrigues3c462c82015-08-19 07:35:54 +10001936 if (mddev_is_clustered(mddev))
1937 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1938
NeilBrown2699b672011-07-28 11:31:47 +10001939 if (rdev->badblocks.count == 0)
1940 /* Nothing to do for bad blocks*/ ;
1941 else if (sb->bblog_offset == 0)
1942 /* Cannot record bad blocks on this device */
1943 md_error(mddev, rdev);
1944 else {
1945 struct badblocks *bb = &rdev->badblocks;
Christoph Hellwigae506402019-04-04 18:56:13 +02001946 __le64 *bbp = (__le64 *)page_address(rdev->bb_page);
NeilBrown2699b672011-07-28 11:31:47 +10001947 u64 *p = bb->page;
1948 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1949 if (bb->changed) {
1950 unsigned seq;
1951
1952retry:
1953 seq = read_seqbegin(&bb->lock);
1954
1955 memset(bbp, 0xff, PAGE_SIZE);
1956
1957 for (i = 0 ; i < bb->count ; i++) {
majianpeng35f9ac22012-11-08 08:56:27 +08001958 u64 internal_bb = p[i];
NeilBrown2699b672011-07-28 11:31:47 +10001959 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1960 | BB_LEN(internal_bb));
majianpeng35f9ac22012-11-08 08:56:27 +08001961 bbp[i] = cpu_to_le64(store_bb);
NeilBrown2699b672011-07-28 11:31:47 +10001962 }
NeilBrownd0962932012-03-19 12:46:41 +11001963 bb->changed = 0;
NeilBrown2699b672011-07-28 11:31:47 +10001964 if (read_seqretry(&bb->lock, seq))
1965 goto retry;
1966
1967 bb->sector = (rdev->sb_start +
1968 (int)le32_to_cpu(sb->bblog_offset));
1969 bb->size = le16_to_cpu(sb->bblog_size);
NeilBrown2699b672011-07-28 11:31:47 +10001970 }
1971 }
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 max_dev = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11001974 rdev_for_each(rdev2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (rdev2->desc_nr+1 > max_dev)
1976 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001977
NeilBrown70471da2009-08-03 10:59:57 +10001978 if (max_dev > le32_to_cpu(sb->max_dev)) {
1979 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001980 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001981 rdev->sb_size = max_dev * 2 + 256;
1982 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1983 if (rdev->sb_size & bmask)
1984 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001985 } else
1986 max_dev = le32_to_cpu(sb->max_dev);
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 for (i=0; i<max_dev;i++)
Lidong Zhong8df72022017-06-12 10:45:55 +08001989 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
NeilBrownf72ffdd2014-09-30 14:23:59 +10001990
Song Liua97b7892015-10-08 21:54:09 -07001991 if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1992 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01001994 if (test_bit(MD_HAS_PPL, &mddev->flags)) {
Pawel Baldysiakddc08822017-08-16 17:13:45 +02001995 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
1996 sb->feature_map |=
1997 cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
1998 else
1999 sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
Artur Paszkiewiczea0213e2017-03-09 09:59:57 +01002000 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
2001 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
2002 }
2003
NeilBrowndafb20f2012-03-19 12:46:39 +11002004 rdev_for_each(rdev2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002006 if (test_bit(Faulty, &rdev2->flags))
Song Liuc4d4c912015-08-13 14:31:54 -07002007 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08002008 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Song Liua97b7892015-10-08 21:54:09 -07002010 else if (test_bit(Journal, &rdev2->flags))
Song Liubac624f2015-08-13 14:31:55 -07002011 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
NeilBrown93be75f2009-12-14 12:50:06 +11002012 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002013 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 else
Song Liuc4d4c912015-08-13 14:31:54 -07002015 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 sb->sb_csum = calc_sb_1_csum(sb);
2019}
2020
Chris Webb0cd17fe2008-06-28 08:31:46 +10002021static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11002022super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002023{
2024 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002025 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11002026 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002027 return 0; /* component must fit device */
NeilBrownc6563a82012-05-21 09:27:00 +10002028 if (rdev->data_offset != rdev->new_data_offset)
2029 return 0; /* too confusing */
Andre Noll0f420352008-07-11 22:02:23 +10002030 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002031 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01002032 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002033 max_sectors -= rdev->data_offset;
2034 if (!num_sectors || num_sectors > max_sectors)
2035 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11002036 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10002037 /* minor version 0 with bitmap we can't move */
2038 return 0;
2039 } else {
2040 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10002041 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01002042 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10002043 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11002044 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10002045 if (!num_sectors || num_sectors > max_sectors)
2046 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10002047 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002048 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10002049 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10002050 sb->data_size = cpu_to_le64(num_sectors);
Jason Yan3fb632e2017-03-10 11:27:23 +08002051 sb->super_offset = cpu_to_le64(rdev->sb_start);
Chris Webb0cd17fe2008-06-28 08:31:46 +10002052 sb->sb_csum = calc_sb_1_csum(sb);
NeilBrown46533ff2016-11-18 16:16:11 +11002053 do {
2054 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
2055 rdev->sb_page);
2056 } while (md_super_wait(rdev->mddev) < 0);
Justin Maggardc26a44e2010-11-24 16:36:17 +11002057 return num_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10002058
2059}
2060
2061static int
2062super_1_allow_new_offset(struct md_rdev *rdev,
2063 unsigned long long new_offset)
2064{
2065 /* All necessary checks on new >= old have been done */
2066 struct bitmap *bitmap;
2067 if (new_offset >= rdev->data_offset)
2068 return 1;
2069
2070 /* with 1.0 metadata, there is no metadata to tread on
2071 * so we can always move back */
2072 if (rdev->mddev->minor_version == 0)
2073 return 1;
2074
2075 /* otherwise we must be sure not to step on
2076 * any metadata, so stay:
2077 * 36K beyond start of superblock
2078 * beyond end of badblocks
2079 * beyond write-intent bitmap
2080 */
2081 if (rdev->sb_start + (32+4)*2 > new_offset)
2082 return 0;
2083 bitmap = rdev->mddev->bitmap;
2084 if (bitmap && !rdev->mddev->bitmap_info.file &&
2085 rdev->sb_start + rdev->mddev->bitmap_info.offset +
NeilBrown1ec885c2012-05-22 13:55:10 +10002086 bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
NeilBrownc6563a82012-05-21 09:27:00 +10002087 return 0;
2088 if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2089 return 0;
2090
2091 return 1;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Adrian Bunk75c96f82005-05-05 16:16:09 -07002094static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 [0] = {
2096 .name = "0.90.0",
2097 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002098 .load_super = super_90_load,
2099 .validate_super = super_90_validate,
2100 .sync_super = super_90_sync,
2101 .rdev_size_change = super_90_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002102 .allow_new_offset = super_90_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 },
2104 [1] = {
2105 .name = "md-1",
2106 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10002107 .load_super = super_1_load,
2108 .validate_super = super_1_validate,
2109 .sync_super = super_1_sync,
2110 .rdev_size_change = super_1_rdev_size_change,
NeilBrownc6563a82012-05-21 09:27:00 +10002111 .allow_new_offset = super_1_allow_new_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 },
2113};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
NeilBrownfd01b882011-10-11 16:47:53 +11002115static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05002116{
2117 if (mddev->sync_super) {
2118 mddev->sync_super(mddev, rdev);
2119 return;
2120 }
2121
2122 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2123
2124 super_types[mddev->major_version].sync_super(mddev, rdev);
2125}
2126
NeilBrownfd01b882011-10-11 16:47:53 +11002127static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
NeilBrown3cb03002011-10-11 16:45:26 +11002129 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
NeilBrown4b809912008-07-21 17:05:25 +10002131 rcu_read_lock();
Song Liu0b020e82015-09-03 23:00:35 -07002132 rdev_for_each_rcu(rdev, mddev1) {
2133 if (test_bit(Faulty, &rdev->flags) ||
2134 test_bit(Journal, &rdev->flags) ||
2135 rdev->raid_disk == -1)
2136 continue;
2137 rdev_for_each_rcu(rdev2, mddev2) {
2138 if (test_bit(Faulty, &rdev2->flags) ||
2139 test_bit(Journal, &rdev2->flags) ||
2140 rdev2->raid_disk == -1)
2141 continue;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002142 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10002143 rdev2->bdev->bd_contains) {
2144 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002145 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10002146 }
Song Liu0b020e82015-09-03 23:00:35 -07002147 }
2148 }
NeilBrown4b809912008-07-21 17:05:25 +10002149 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return 0;
2151}
2152
2153static LIST_HEAD(pending_raid_disks);
2154
Andre Nollac5e7112009-08-03 10:59:47 +10002155/*
2156 * Try to register data integrity profile for an mddev
2157 *
2158 * This is called when an array is started and after a disk has been kicked
2159 * from the array. It only succeeds if all working and active component devices
2160 * are integrity capable with matching profiles.
2161 */
NeilBrownfd01b882011-10-11 16:47:53 +11002162int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002163{
NeilBrown3cb03002011-10-11 16:45:26 +11002164 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002165
Andre Nollac5e7112009-08-03 10:59:47 +10002166 if (list_empty(&mddev->disks))
2167 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10002168 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2169 return 0; /* shouldn't register, or already is */
NeilBrowndafb20f2012-03-19 12:46:39 +11002170 rdev_for_each(rdev, mddev) {
Andre Nollac5e7112009-08-03 10:59:47 +10002171 /* skip spares and non-functional disks */
2172 if (test_bit(Faulty, &rdev->flags))
2173 continue;
2174 if (rdev->raid_disk < 0)
2175 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10002176 if (!reference) {
2177 /* Use the first rdev as the reference */
2178 reference = rdev;
2179 continue;
2180 }
2181 /* does this rdev's profile match the reference profile? */
2182 if (blk_integrity_compare(reference->bdev->bd_disk,
2183 rdev->bdev->bd_disk) < 0)
2184 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002185 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04002186 if (!reference || !bdev_get_integrity(reference->bdev))
2187 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002188 /*
2189 * All component devices are integrity capable and have matching
2190 * profiles, register the common profile for the md device.
2191 */
Martin K. Petersen25520d52015-10-21 13:19:49 -04002192 blk_integrity_register(mddev->gendisk,
2193 bdev_get_integrity(reference->bdev));
2194
NeilBrown9d487392016-11-02 14:16:49 +11002195 pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
Kent Overstreetafeee512018-05-20 18:25:52 -04002196 if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) {
NeilBrown9d487392016-11-02 14:16:49 +11002197 pr_err("md: failed to create integrity pool for %s\n",
Martin K. Petersena91a2782011-03-17 11:11:05 +01002198 mdname(mddev));
2199 return -EINVAL;
2200 }
Andre Nollac5e7112009-08-03 10:59:47 +10002201 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002202}
Andre Nollac5e7112009-08-03 10:59:47 +10002203EXPORT_SYMBOL(md_integrity_register);
2204
Dan Williams1501efa2016-01-13 16:00:07 -08002205/*
2206 * Attempt to add an rdev, but only if it is consistent with the current
2207 * integrity profile
2208 */
2209int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10002210{
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002211 struct blk_integrity *bi_mddev;
Dan Williams1501efa2016-01-13 16:00:07 -08002212 char name[BDEVNAME_SIZE];
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002213
2214 if (!mddev->gendisk)
Dan Williams1501efa2016-01-13 16:00:07 -08002215 return 0;
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002216
Jonathan Brassow2863b9e2012-10-11 13:38:58 +11002217 bi_mddev = blk_get_integrity(mddev->gendisk);
Andre Nollac5e7112009-08-03 10:59:47 +10002218
2219 if (!bi_mddev) /* nothing to do */
Dan Williams1501efa2016-01-13 16:00:07 -08002220 return 0;
2221
2222 if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11002223 pr_err("%s: incompatible integrity profile for %s\n",
2224 mdname(mddev), bdevname(rdev->bdev, name));
Dan Williams1501efa2016-01-13 16:00:07 -08002225 return -ENXIO;
2226 }
2227
2228 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10002229}
2230EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002231
NeilBrownf72ffdd2014-09-30 14:23:59 +10002232static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08002234 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002235 struct kobject *ko;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002236 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Dan Williams11e2ede2008-04-30 00:52:32 -07002238 /* prevent duplicates */
2239 if (find_rdev(mddev, rdev->bdev->bd_dev))
2240 return -EEXIST;
2241
NeilBrown97b20ef2017-04-13 08:53:48 +10002242 if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2243 mddev->pers)
2244 return -EROFS;
2245
Andre Nolldd8ac332009-03-31 14:33:13 +11002246 /* make sure rdev->sectors exceeds mddev->dev_sectors */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002247 if (!test_bit(Journal, &rdev->flags) &&
2248 rdev->sectors &&
2249 (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002250 if (mddev->pers) {
2251 /* Cannot change size, so fail
2252 * If mddev->level <= 0, then we don't care
2253 * about aligning sizes (e.g. linear)
2254 */
2255 if (mddev->level > 0)
2256 return -ENOSPC;
2257 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002258 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 /* Verify rdev->desc_nr is unique.
2262 * If it is -1, assign a free number, else
2263 * check number is not in use
2264 */
NeilBrown4878e9e2014-09-25 17:00:11 +10002265 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (rdev->desc_nr < 0) {
2267 int choice = 0;
NeilBrown4878e9e2014-09-25 17:00:11 +10002268 if (mddev->pers)
2269 choice = mddev->raid_disks;
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002270 while (md_find_rdev_nr_rcu(mddev, choice))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 choice++;
2272 rdev->desc_nr = choice;
2273 } else {
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05002274 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
NeilBrown4878e9e2014-09-25 17:00:11 +10002275 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return -EBUSY;
NeilBrown4878e9e2014-09-25 17:00:11 +10002277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
NeilBrown4878e9e2014-09-25 17:00:11 +10002279 rcu_read_unlock();
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002280 if (!test_bit(Journal, &rdev->flags) &&
2281 mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11002282 pr_warn("md: %s: array is limited to %d devices\n",
2283 mdname(mddev), mddev->max_disks);
NeilBrownde01dfa2009-02-06 18:02:46 +11002284 return -EBUSY;
2285 }
NeilBrown19133a42005-11-08 21:39:35 -08002286 bdevname(rdev->bdev,b);
Rasmus Villemoes90a9bef2015-06-25 15:02:36 -07002287 strreplace(b, '/', '!');
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 rdev->mddev = mddev;
NeilBrown9d487392016-11-02 14:16:49 +11002290 pr_debug("md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002291
Guoqing Jiang963c5552019-06-14 17:10:36 +08002292 if (mddev->raid_disks)
2293 mddev_create_wb_pool(mddev, rdev, false);
2294
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002295 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002296 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002297
Tejun Heo0762b8b2008-08-25 19:56:12 +09002298 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002299 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2300 /* failure here is OK */;
2301 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002302
NeilBrown4b809912008-07-21 17:05:25 +10002303 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002304 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002305
2306 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002307 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002310
2311 fail:
NeilBrown9d487392016-11-02 14:16:49 +11002312 pr_warn("md: failed to register dev-%s for %s\n",
2313 b, mdname(mddev));
NeilBrown5e55e2f2007-03-26 21:32:14 -08002314 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
NeilBrown177a99b2008-02-06 01:39:56 -08002317static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002318{
NeilBrown3cb03002011-10-11 16:45:26 +11002319 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002320 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002321 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002322}
2323
NeilBrownf72ffdd2014-09-30 14:23:59 +10002324static void unbind_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
2326 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002327
Tejun Heo49731ba2011-01-14 18:43:57 +01002328 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002329 list_del_rcu(&rdev->same_set);
NeilBrown9d487392016-11-02 14:16:49 +11002330 pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
Guoqing Jiang963c5552019-06-14 17:10:36 +08002331 mddev_destroy_wb_pool(rdev->mddev, rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002333 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002334 sysfs_put(rdev->sysfs_state);
2335 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002336 rdev->badblocks.count = 0;
NeilBrown5792a282007-04-04 19:08:18 -07002337 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002338 * writing to 'remove' to "dev/state". We also need
2339 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002340 */
NeilBrown4b809912008-07-21 17:05:25 +10002341 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002342 INIT_WORK(&rdev->del_work, md_delayed_delete);
2343 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002344 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
2346
2347/*
2348 * prevent the device from being mounted, repartitioned or
2349 * otherwise reused by a RAID array (or any other kernel
2350 * subsystem), by bd_claiming the device.
2351 */
NeilBrown3cb03002011-10-11 16:45:26 +11002352static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
2354 int err = 0;
2355 struct block_device *bdev;
2356 char b[BDEVNAME_SIZE];
2357
Tejun Heod4d77622010-11-13 11:55:18 +01002358 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002359 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (IS_ERR(bdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11002361 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return PTR_ERR(bdev);
2363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 rdev->bdev = bdev;
2365 return err;
2366}
2367
NeilBrown3cb03002011-10-11 16:45:26 +11002368static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
2370 struct block_device *bdev = rdev->bdev;
2371 rdev->bdev = NULL;
Tejun Heoe525fd82010-11-13 11:55:17 +01002372 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
2375void md_autodetect_dev(dev_t dev);
2376
NeilBrownf72ffdd2014-09-30 14:23:59 +10002377static void export_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
2379 char b[BDEVNAME_SIZE];
NeilBrown403df472014-09-30 15:52:29 +10002380
NeilBrown9d487392016-11-02 14:16:49 +11002381 pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
NeilBrown545c8792012-05-22 13:54:30 +10002382 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002384 if (test_bit(AutoDetected, &rdev->flags))
2385 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386#endif
2387 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002388 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002391void md_kick_rdev_from_array(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
2393 unbind_rdev_from_array(rdev);
2394 export_rdev(rdev);
2395}
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002396EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
NeilBrownfd01b882011-10-11 16:47:53 +11002398static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
NeilBrown0638bb02014-09-25 17:43:47 +10002400 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
NeilBrown0638bb02014-09-25 17:43:47 +10002402 while (!list_empty(&mddev->disks)) {
2403 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2404 same_set);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05002405 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 mddev->raid_disks = 0;
2408 mddev->major_version = 0;
2409}
2410
NeilBrown6497709b2017-03-15 14:05:14 +11002411static bool set_in_sync(struct mddev *mddev)
2412{
Shaohua Liefa4b772017-10-18 22:08:13 -07002413 lockdep_assert_held(&mddev->lock);
NeilBrown4ad23a972017-03-15 14:05:14 +11002414 if (!mddev->in_sync) {
2415 mddev->sync_checkers++;
2416 spin_unlock(&mddev->lock);
2417 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2418 spin_lock(&mddev->lock);
2419 if (!mddev->in_sync &&
2420 percpu_ref_is_zero(&mddev->writes_pending)) {
NeilBrown6497709b2017-03-15 14:05:14 +11002421 mddev->in_sync = 1;
NeilBrown4ad23a972017-03-15 14:05:14 +11002422 /*
2423 * Ensure ->in_sync is visible before we clear
2424 * ->sync_checkers.
2425 */
NeilBrown55cc39f2017-03-15 14:05:14 +11002426 smp_mb();
NeilBrown6497709b2017-03-15 14:05:14 +11002427 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2428 sysfs_notify_dirent_safe(mddev->sysfs_state);
2429 }
NeilBrown4ad23a972017-03-15 14:05:14 +11002430 if (--mddev->sync_checkers == 0)
2431 percpu_ref_switch_to_percpu(&mddev->writes_pending);
NeilBrown6497709b2017-03-15 14:05:14 +11002432 }
2433 if (mddev->safemode == 1)
2434 mddev->safemode = 0;
2435 return mddev->in_sync;
2436}
2437
NeilBrownf72ffdd2014-09-30 14:23:59 +10002438static void sync_sbs(struct mddev *mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
NeilBrown42543762006-06-26 00:27:57 -07002440 /* Update each superblock (in-memory image), but
2441 * if we are allowed to, skip spares which already
2442 * have the right event counter, or have one earlier
2443 * (which would mean they aren't being marked as dirty
2444 * with the rest of the array)
2445 */
NeilBrown3cb03002011-10-11 16:45:26 +11002446 struct md_rdev *rdev;
NeilBrowndafb20f2012-03-19 12:46:39 +11002447 rdev_for_each(rdev, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07002448 if (rdev->sb_events == mddev->events ||
2449 (nospares &&
2450 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002451 rdev->sb_events+1 == mddev->events)) {
2452 /* Don't update this superblock */
2453 rdev->sb_loaded = 2;
2454 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002455 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002456 rdev->sb_loaded = 1;
2457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 }
2459}
2460
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002461static bool does_sb_need_changing(struct mddev *mddev)
2462{
2463 struct md_rdev *rdev;
2464 struct mdp_superblock_1 *sb;
2465 int role;
2466
2467 /* Find a good rdev */
2468 rdev_for_each(rdev, mddev)
2469 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2470 break;
2471
2472 /* No good device found. */
2473 if (!rdev)
2474 return false;
2475
2476 sb = page_address(rdev->sb_page);
2477 /* Check if a device has become faulty or a spare become active */
2478 rdev_for_each(rdev, mddev) {
2479 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2480 /* Device activated? */
2481 if (role == 0xffff && rdev->raid_disk >=0 &&
2482 !test_bit(Faulty, &rdev->flags))
2483 return true;
2484 /* Device turned faulty? */
2485 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2486 return true;
2487 }
2488
2489 /* Check if any mddev parameters have changed */
2490 if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2491 (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
Jason Yan13459212017-03-10 11:49:12 +08002492 (mddev->layout != le32_to_cpu(sb->layout)) ||
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002493 (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2494 (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2495 return true;
2496
2497 return false;
2498}
2499
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002500void md_update_sb(struct mddev *mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
NeilBrown3cb03002011-10-11 16:45:26 +11002502 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002503 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002504 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002505 int any_badblocks_changed = 0;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002506 int ret = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
NeilBrownd87f0642013-04-24 11:42:40 +10002508 if (mddev->ro) {
2509 if (force_change)
Shaohua Li29530792016-12-08 15:48:19 -08002510 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownd87f0642013-04-24 11:42:40 +10002511 return;
2512 }
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002513
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002514repeat:
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002515 if (mddev_is_clustered(mddev)) {
Shaohua Li29530792016-12-08 15:48:19 -08002516 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002517 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002518 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002519 nospares = 1;
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002520 ret = md_cluster_ops->metadata_update_start(mddev);
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002521 /* Has someone else has updated the sb */
2522 if (!does_sb_need_changing(mddev)) {
Guoqing Jiang23b63f92015-10-12 17:21:30 +08002523 if (ret == 0)
2524 md_cluster_ops->metadata_update_cancel(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08002525 bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2526 BIT(MD_SB_CHANGE_DEVS) |
2527 BIT(MD_SB_CHANGE_CLEAN));
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05002528 return;
2529 }
2530 }
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002531
NeilBrowndb0505d2017-10-17 16:18:36 +11002532 /*
2533 * First make sure individual recovery_offsets are correct
2534 * curr_resync_completed can only be used during recovery.
2535 * During reshape/resync it might use array-addresses rather
2536 * that device addresses.
2537 */
NeilBrowndafb20f2012-03-19 12:46:39 +11002538 rdev_for_each(rdev, mddev) {
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002539 if (rdev->raid_disk >= 0 &&
2540 mddev->delta_disks >= 0 &&
NeilBrowndb0505d2017-10-17 16:18:36 +11002541 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2542 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2543 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002544 !test_bit(Journal, &rdev->flags) &&
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002545 !test_bit(In_sync, &rdev->flags) &&
2546 mddev->curr_resync_completed > rdev->recovery_offset)
2547 rdev->recovery_offset = mddev->curr_resync_completed;
2548
NeilBrownf72ffdd2014-09-30 14:23:59 +10002549 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002550 if (!mddev->persistent) {
Shaohua Li29530792016-12-08 15:48:19 -08002551 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2552 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002553 if (!mddev->external) {
Shaohua Li29530792016-12-08 15:48:19 -08002554 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrowndafb20f2012-03-19 12:46:39 +11002555 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002556 if (rdev->badblocks.changed) {
NeilBrownd0962932012-03-19 12:46:41 +11002557 rdev->badblocks.changed = 0;
Vishal Vermafc974ee2015-12-24 19:20:34 -07002558 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002559 md_error(mddev, rdev);
2560 }
2561 clear_bit(Blocked, &rdev->flags);
2562 clear_bit(BlockedBadBlocks, &rdev->flags);
2563 wake_up(&rdev->blocked_wait);
2564 }
2565 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002566 wake_up(&mddev->sb_wait);
2567 return;
2568 }
2569
NeilBrown85572d72014-12-15 12:56:56 +11002570 spin_lock(&mddev->lock);
NeilBrown84692192006-08-27 01:23:49 -07002571
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11002572 mddev->utime = ktime_get_real_seconds();
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002573
Shaohua Li29530792016-12-08 15:48:19 -08002574 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002575 force_change = 1;
Shaohua Li29530792016-12-08 15:48:19 -08002576 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
NeilBrown850b2b422006-10-03 01:15:46 -07002577 /* just a clean<-> dirty transition, possibly leave spares alone,
2578 * though if events isn't the right even/odd, we will have to do
2579 * spares after all
2580 */
2581 nospares = 1;
2582 if (force_change)
2583 nospares = 0;
2584 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002585 /* If the array is degraded, then skipping spares is both
2586 * dangerous and fairly pointless.
2587 * Dangerous because a device that was removed from the array
2588 * might have a event_count that still looks up-to-date,
2589 * so it can be re-added without a resync.
2590 * Pointless because if there are any spares to skip,
2591 * then a recovery will happen and soon that array won't
2592 * be degraded any more and the spare can go back to sleep then.
2593 */
NeilBrown850b2b422006-10-03 01:15:46 -07002594 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002595
NeilBrown06d91a52005-06-21 17:17:12 -07002596 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002597
2598 /* If this is just a dirty<->clean transition, and the array is clean
2599 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002600 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002601 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002602 && mddev->can_decrease_events
2603 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002604 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002605 mddev->can_decrease_events = 0;
2606 } else {
NeilBrown42543762006-06-26 00:27:57 -07002607 /* otherwise we have to go forward and ... */
2608 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002609 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
NeilBrown403df472014-09-30 15:52:29 +10002612 /*
2613 * This 64-bit counter should never wrap.
2614 * Either we are in around ~1 trillion A.C., assuming
2615 * 1 reboot per second, or we have a bug...
2616 */
2617 WARN_ON(mddev->events == 0);
NeilBrown2699b672011-07-28 11:31:47 +10002618
NeilBrowndafb20f2012-03-19 12:46:39 +11002619 rdev_for_each(rdev, mddev) {
NeilBrown2699b672011-07-28 11:31:47 +10002620 if (rdev->badblocks.changed)
2621 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002622 if (test_bit(Faulty, &rdev->flags))
2623 set_bit(FaultRecorded, &rdev->flags);
2624 }
NeilBrown2699b672011-07-28 11:31:47 +10002625
NeilBrowne6910632008-02-06 01:39:51 -08002626 sync_sbs(mddev, nospares);
NeilBrown85572d72014-12-15 12:56:56 +11002627 spin_unlock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
NeilBrown36a4e1f2011-10-07 14:23:17 +11002629 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2630 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Shaohua Li504634f2016-11-18 09:44:08 -08002632 if (mddev->queue)
2633 blk_add_trace_msg(mddev->queue, "md md_update_sb");
NeilBrown46533ff2016-11-18 16:16:11 +11002634rewrite:
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07002635 md_bitmap_update_sb(mddev->bitmap);
NeilBrowndafb20f2012-03-19 12:46:39 +11002636 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002638
NeilBrown42543762006-06-26 00:27:57 -07002639 if (rdev->sb_loaded != 1)
2640 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
NeilBrownf4667222013-12-09 12:04:56 +11002642 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002643 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002644 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002645 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002646 pr_debug("md: (write) %s's sb offset: %llu\n",
2647 bdevname(rdev->bdev, b),
2648 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002649 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002650 if (rdev->badblocks.size) {
2651 md_super_write(mddev, rdev,
2652 rdev->badblocks.sector,
2653 rdev->badblocks.size << 9,
2654 rdev->bb_page);
2655 rdev->badblocks.size = 0;
2656 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002657
NeilBrownf4667222013-12-09 12:04:56 +11002658 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002659 pr_debug("md: %s (skipping faulty)\n",
2660 bdevname(rdev->bdev, b));
Andrei Warkentind70ed2e2011-10-18 12:16:48 +11002661
NeilBrown7bfa19f2005-06-21 17:17:28 -07002662 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 /* only need to write one superblock... */
2664 break;
2665 }
NeilBrown46533ff2016-11-18 16:16:11 +11002666 if (md_super_wait(mddev) < 0)
2667 goto rewrite;
Shaohua Li29530792016-12-08 15:48:19 -08002668 /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002669
Guoqing Jiang2c97cf12016-05-02 11:33:09 -04002670 if (mddev_is_clustered(mddev) && ret == 0)
2671 md_cluster_ops->metadata_update_finish(mddev);
2672
NeilBrown850b2b422006-10-03 01:15:46 -07002673 if (mddev->in_sync != sync_req ||
Shaohua Li29530792016-12-08 15:48:19 -08002674 !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2675 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
NeilBrown06d91a52005-06-21 17:17:12 -07002676 /* have to write it out again */
NeilBrown06d91a52005-06-21 17:17:12 -07002677 goto repeat;
NeilBrown3d310eb2005-06-21 17:17:26 -07002678 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002679 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2680 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002681
NeilBrowndafb20f2012-03-19 12:46:39 +11002682 rdev_for_each(rdev, mddev) {
NeilBrownde393cd2011-07-28 11:31:48 +10002683 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2684 clear_bit(Blocked, &rdev->flags);
2685
2686 if (any_badblocks_changed)
Vishal Vermafc974ee2015-12-24 19:20:34 -07002687 ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002688 clear_bit(BlockedBadBlocks, &rdev->flags);
2689 wake_up(&rdev->blocked_wait);
2690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691}
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05002692EXPORT_SYMBOL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002694static int add_bound_rdev(struct md_rdev *rdev)
2695{
2696 struct mddev *mddev = rdev->mddev;
2697 int err = 0;
Shaohua Li87d4d912016-01-06 14:37:14 -08002698 bool add_journal = test_bit(Journal, &rdev->flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002699
Shaohua Li87d4d912016-01-06 14:37:14 -08002700 if (!mddev->pers->hot_remove_disk || add_journal) {
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002701 /* If there is hot_add_disk but no hot_remove_disk
2702 * then added disks for geometry changes,
2703 * and should be added immediately.
2704 */
2705 super_types[mddev->major_version].
2706 validate_super(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002707 if (add_journal)
2708 mddev_suspend(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002709 err = mddev->pers->hot_add_disk(mddev, rdev);
Shaohua Li87d4d912016-01-06 14:37:14 -08002710 if (add_journal)
2711 mddev_resume(mddev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002712 if (err) {
Guoqing Jiangdb767672016-06-02 23:32:05 -04002713 md_kick_rdev_from_array(rdev);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002714 return err;
2715 }
2716 }
2717 sysfs_notify_dirent_safe(rdev->sysfs_state);
2718
Shaohua Li29530792016-12-08 15:48:19 -08002719 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002720 if (mddev->degraded)
2721 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2722 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2723 md_new_event(mddev);
2724 md_wakeup_thread(mddev->thread);
2725 return 0;
2726}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Andre Noll7f6ce762008-03-23 18:34:54 +01002728/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002729 * We want to accept with case. For this we use cmd_match.
2730 */
2731static int cmd_match(const char *cmd, const char *str)
2732{
2733 /* See if cmd, written into a sysfs file, matches
2734 * str. They must either be the same, or cmd can
2735 * have a trailing newline
2736 */
2737 while (*cmd && *str && *cmd == *str) {
2738 cmd++;
2739 str++;
2740 }
2741 if (*cmd == '\n')
2742 cmd++;
2743 if (*str || *cmd)
2744 return 0;
2745 return 1;
2746}
2747
NeilBrown86e6ffd2005-11-08 21:39:24 -08002748struct rdev_sysfs_entry {
2749 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002750 ssize_t (*show)(struct md_rdev *, char *);
2751 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002752};
2753
2754static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002755state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002756{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002757 char *sep = ",";
NeilBrown20a49ff2008-02-06 01:39:57 -08002758 size_t len = 0;
Mark Rutland6aa7de02017-10-23 14:07:29 -07002759 unsigned long flags = READ_ONCE(rdev->flags);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002760
NeilBrown758bfc82014-12-15 12:56:59 +11002761 if (test_bit(Faulty, &flags) ||
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002762 (!test_bit(ExternalBbl, &flags) &&
2763 rdev->badblocks.unacked_exist))
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002764 len += sprintf(page+len, "faulty%s", sep);
2765 if (test_bit(In_sync, &flags))
2766 len += sprintf(page+len, "in_sync%s", sep);
2767 if (test_bit(Journal, &flags))
2768 len += sprintf(page+len, "journal%s", sep);
2769 if (test_bit(WriteMostly, &flags))
2770 len += sprintf(page+len, "write_mostly%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002771 if (test_bit(Blocked, &flags) ||
NeilBrown52c64152011-12-08 16:22:48 +11002772 (rdev->badblocks.unacked_exist
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002773 && !test_bit(Faulty, &flags)))
2774 len += sprintf(page+len, "blocked%s", sep);
NeilBrown758bfc82014-12-15 12:56:59 +11002775 if (!test_bit(Faulty, &flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002776 !test_bit(Journal, &flags) &&
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002777 !test_bit(In_sync, &flags))
2778 len += sprintf(page+len, "spare%s", sep);
2779 if (test_bit(WriteErrorSeen, &flags))
2780 len += sprintf(page+len, "write_error%s", sep);
2781 if (test_bit(WantReplacement, &flags))
2782 len += sprintf(page+len, "want_replacement%s", sep);
2783 if (test_bit(Replacement, &flags))
2784 len += sprintf(page+len, "replacement%s", sep);
2785 if (test_bit(ExternalBbl, &flags))
2786 len += sprintf(page+len, "external_bbl%s", sep);
NeilBrown688834e2016-11-18 16:16:11 +11002787 if (test_bit(FailFast, &flags))
2788 len += sprintf(page+len, "failfast%s", sep);
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002789
2790 if (len)
2791 len -= strlen(sep);
NeilBrown2d78f8c2011-12-23 10:17:51 +11002792
NeilBrown86e6ffd2005-11-08 21:39:24 -08002793 return len+sprintf(page+len, "\n");
2794}
2795
NeilBrown45dc2de2006-06-26 00:27:58 -07002796static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002797state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002798{
2799 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002800 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002801 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002802 * writemostly - sets write_mostly
2803 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002804 * blocked - sets the Blocked flags
2805 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002806 * insync - sets Insync providing device isn't active
NeilBrownf4667222013-12-09 12:04:56 +11002807 * -insync - clear Insync for a device with a slot assigned,
2808 * so that it gets rebuilt based on bitmap
NeilBrownd7a9d442011-07-28 11:31:48 +10002809 * write_error - sets WriteErrorSeen
2810 * -write_error - clears WriteErrorSeen
NeilBrown688834e2016-11-18 16:16:11 +11002811 * {,-}failfast - set/clear FailFast
NeilBrown45dc2de2006-06-26 00:27:58 -07002812 */
2813 int err = -EINVAL;
2814 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2815 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002816 if (test_bit(Faulty, &rdev->flags))
2817 err = 0;
2818 else
2819 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002820 } else if (cmd_match(buf, "remove")) {
Shaohua Li5d881782016-07-28 09:06:34 -07002821 if (rdev->mddev->pers) {
2822 clear_bit(Blocked, &rdev->flags);
2823 remove_and_add_spares(rdev->mddev, rdev);
2824 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002825 if (rdev->raid_disk >= 0)
2826 err = -EBUSY;
2827 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002828 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002829 err = 0;
Guoqing Jianga9720902015-10-12 17:21:27 +08002830 if (mddev_is_clustered(mddev))
2831 err = md_cluster_ops->remove_disk(mddev, rdev);
2832
2833 if (err == 0) {
2834 md_kick_rdev_from_array(rdev);
NeilBrown060b0682016-11-04 16:46:03 +11002835 if (mddev->pers) {
Shaohua Li29530792016-12-08 15:48:19 -08002836 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11002837 md_wakeup_thread(mddev->thread);
2838 }
Guoqing Jianga9720902015-10-12 17:21:27 +08002839 md_new_event(mddev);
2840 }
NeilBrown45dc2de2006-06-26 00:27:58 -07002841 }
NeilBrownf6556752006-06-26 00:28:01 -07002842 } else if (cmd_match(buf, "writemostly")) {
2843 set_bit(WriteMostly, &rdev->flags);
Guoqing Jiang963c5552019-06-14 17:10:36 +08002844 mddev_create_wb_pool(rdev->mddev, rdev, false);
NeilBrownf6556752006-06-26 00:28:01 -07002845 err = 0;
2846 } else if (cmd_match(buf, "-writemostly")) {
Guoqing Jiang963c5552019-06-14 17:10:36 +08002847 mddev_destroy_wb_pool(rdev->mddev, rdev);
NeilBrownf6556752006-06-26 00:28:01 -07002848 clear_bit(WriteMostly, &rdev->flags);
2849 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002850 } else if (cmd_match(buf, "blocked")) {
2851 set_bit(Blocked, &rdev->flags);
2852 err = 0;
2853 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002854 if (!test_bit(Faulty, &rdev->flags) &&
Tomasz Majchrzakdcbcb482016-10-21 16:27:08 +02002855 !test_bit(ExternalBbl, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002856 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002857 /* metadata handler doesn't understand badblocks,
2858 * so we need to fail the device
2859 */
2860 md_error(rdev->mddev, rdev);
2861 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002862 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002863 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002864 wake_up(&rdev->blocked_wait);
2865 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2866 md_wakeup_thread(rdev->mddev->thread);
2867
2868 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002869 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2870 set_bit(In_sync, &rdev->flags);
2871 err = 0;
NeilBrown688834e2016-11-18 16:16:11 +11002872 } else if (cmd_match(buf, "failfast")) {
2873 set_bit(FailFast, &rdev->flags);
2874 err = 0;
2875 } else if (cmd_match(buf, "-failfast")) {
2876 clear_bit(FailFast, &rdev->flags);
2877 err = 0;
Shaohua Lif2076e72015-10-08 21:54:12 -07002878 } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2879 !test_bit(Journal, &rdev->flags)) {
NeilBrowne1960f82014-09-30 15:24:25 +10002880 if (rdev->mddev->pers == NULL) {
2881 clear_bit(In_sync, &rdev->flags);
2882 rdev->saved_raid_disk = rdev->raid_disk;
2883 rdev->raid_disk = -1;
2884 err = 0;
2885 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002886 } else if (cmd_match(buf, "write_error")) {
2887 set_bit(WriteErrorSeen, &rdev->flags);
2888 err = 0;
2889 } else if (cmd_match(buf, "-write_error")) {
2890 clear_bit(WriteErrorSeen, &rdev->flags);
2891 err = 0;
NeilBrown2d78f8c2011-12-23 10:17:51 +11002892 } else if (cmd_match(buf, "want_replacement")) {
2893 /* Any non-spare device that is not a replacement can
2894 * become want_replacement at any time, but we then need to
2895 * check if recovery is needed.
2896 */
2897 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07002898 !test_bit(Journal, &rdev->flags) &&
NeilBrown2d78f8c2011-12-23 10:17:51 +11002899 !test_bit(Replacement, &rdev->flags))
2900 set_bit(WantReplacement, &rdev->flags);
2901 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2902 md_wakeup_thread(rdev->mddev->thread);
2903 err = 0;
2904 } else if (cmd_match(buf, "-want_replacement")) {
2905 /* Clearing 'want_replacement' is always allowed.
2906 * Once replacements starts it is too late though.
2907 */
2908 err = 0;
2909 clear_bit(WantReplacement, &rdev->flags);
2910 } else if (cmd_match(buf, "replacement")) {
2911 /* Can only set a device as a replacement when array has not
2912 * yet been started. Once running, replacement is automatic
2913 * from spares, or by assigning 'slot'.
2914 */
2915 if (rdev->mddev->pers)
2916 err = -EBUSY;
2917 else {
2918 set_bit(Replacement, &rdev->flags);
2919 err = 0;
2920 }
2921 } else if (cmd_match(buf, "-replacement")) {
2922 /* Similarly, can only clear Replacement before start */
2923 if (rdev->mddev->pers)
2924 err = -EBUSY;
2925 else {
2926 clear_bit(Replacement, &rdev->flags);
2927 err = 0;
2928 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002929 } else if (cmd_match(buf, "re-add")) {
Yufen Yuee37e622019-04-02 14:22:14 +08002930 if (!rdev->mddev->pers)
2931 err = -EINVAL;
2932 else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) &&
2933 rdev->saved_raid_disk >= 0) {
Goldwyn Rodrigues97f6cd32015-04-14 10:45:42 -05002934 /* clear_bit is performed _after_ all the devices
2935 * have their local Faulty bit cleared. If any writes
2936 * happen in the meantime in the local node, they
2937 * will land in the local bitmap, which will be synced
2938 * by this node eventually
2939 */
2940 if (!mddev_is_clustered(rdev->mddev) ||
2941 (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2942 clear_bit(Faulty, &rdev->flags);
2943 err = add_bound_rdev(rdev);
2944 }
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05002945 } else
2946 err = -EBUSY;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02002947 } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2948 set_bit(ExternalBbl, &rdev->flags);
2949 rdev->badblocks.shift = 0;
2950 err = 0;
2951 } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2952 clear_bit(ExternalBbl, &rdev->flags);
2953 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002954 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002955 if (!err)
2956 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002957 return err ? err : len;
2958}
NeilBrown80ca3a42006-07-10 04:44:18 -07002959static struct rdev_sysfs_entry rdev_state =
NeilBrown750f1992014-09-30 08:53:05 +10002960__ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002961
2962static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002963errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002964{
2965 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2966}
2967
2968static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002969errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002970{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002971 unsigned int n;
2972 int rv;
2973
2974 rv = kstrtouint(buf, 10, &n);
2975 if (rv < 0)
2976 return rv;
2977 atomic_set(&rdev->corrected_errors, n);
2978 return len;
NeilBrown4dbcdc72006-01-06 00:20:52 -08002979}
2980static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002981__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002982
NeilBrown014236d2006-01-06 00:20:55 -08002983static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002984slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002985{
Shaohua Lif2076e72015-10-08 21:54:12 -07002986 if (test_bit(Journal, &rdev->flags))
2987 return sprintf(page, "journal\n");
2988 else if (rdev->raid_disk < 0)
NeilBrown014236d2006-01-06 00:20:55 -08002989 return sprintf(page, "none\n");
2990 else
2991 return sprintf(page, "%d\n", rdev->raid_disk);
2992}
2993
2994static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002995slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002996{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002997 int slot;
NeilBrownc303da62008-02-06 01:39:51 -08002998 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03002999
Shaohua Lif2076e72015-10-08 21:54:12 -07003000 if (test_bit(Journal, &rdev->flags))
3001 return -EBUSY;
NeilBrown014236d2006-01-06 00:20:55 -08003002 if (strncmp(buf, "none", 4)==0)
3003 slot = -1;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003004 else {
3005 err = kstrtouint(buf, 10, (unsigned int *)&slot);
3006 if (err < 0)
3007 return err;
3008 }
Neil Brown6c2fce22008-06-28 08:31:31 +10003009 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08003010 /* Setting 'slot' on an active array requires also
3011 * updating the 'rd%d' link, and communicating
3012 * with the personality with ->hot_*_disk.
3013 * For now we only support removing
3014 * failed/spare devices. This normally happens automatically,
3015 * but not when the metadata is externally managed.
3016 */
NeilBrownc303da62008-02-06 01:39:51 -08003017 if (rdev->raid_disk == -1)
3018 return -EEXIST;
3019 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10003020 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08003021 return -EINVAL;
NeilBrown746d3202013-04-24 11:42:41 +10003022 clear_bit(Blocked, &rdev->flags);
3023 remove_and_add_spares(rdev->mddev, rdev);
3024 if (rdev->raid_disk >= 0)
3025 return -EBUSY;
NeilBrownc303da62008-02-06 01:39:51 -08003026 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
3027 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10003028 } else if (rdev->mddev->pers) {
Neil Brown6c2fce22008-06-28 08:31:31 +10003029 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10003030 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10003031 */
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003032 int err;
Neil Brown6c2fce22008-06-28 08:31:31 +10003033
3034 if (rdev->raid_disk != -1)
3035 return -EBUSY;
3036
NeilBrownc6751b22011-02-02 11:57:13 +11003037 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
3038 return -EBUSY;
3039
Neil Brown6c2fce22008-06-28 08:31:31 +10003040 if (rdev->mddev->pers->hot_add_disk == NULL)
3041 return -EINVAL;
3042
NeilBrownba1b41b2011-01-14 09:14:34 +11003043 if (slot >= rdev->mddev->raid_disks &&
3044 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
3045 return -ENOSPC;
3046
Neil Brown6c2fce22008-06-28 08:31:31 +10003047 rdev->raid_disk = slot;
3048 if (test_bit(In_sync, &rdev->flags))
3049 rdev->saved_raid_disk = slot;
3050 else
3051 rdev->saved_raid_disk = -1;
NeilBrownd30519f2011-10-18 12:13:47 +11003052 clear_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11003053 clear_bit(Bitmap_sync, &rdev->flags);
Goldwyn Rodriguescb01c542015-12-18 15:19:16 +11003054 err = rdev->mddev->pers->
3055 hot_add_disk(rdev->mddev, rdev);
3056 if (err) {
3057 rdev->raid_disk = -1;
3058 return err;
3059 } else
3060 sysfs_notify_dirent_safe(rdev->sysfs_state);
3061 if (sysfs_link_rdev(rdev->mddev, rdev))
3062 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10003063 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08003064 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11003065 if (slot >= rdev->mddev->raid_disks &&
3066 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08003067 return -ENOSPC;
3068 rdev->raid_disk = slot;
3069 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08003070 clear_bit(Faulty, &rdev->flags);
3071 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08003072 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003073 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08003074 }
NeilBrown014236d2006-01-06 00:20:55 -08003075 return len;
3076}
3077
NeilBrown014236d2006-01-06 00:20:55 -08003078static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07003079__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08003080
NeilBrown93c8cad2006-01-06 00:20:56 -08003081static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003082offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08003083{
Andrew Morton6961ece2006-01-06 00:20:59 -08003084 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08003085}
3086
3087static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003088offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08003089{
NeilBrownc6563a82012-05-21 09:27:00 +10003090 unsigned long long offset;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003091 if (kstrtoull(buf, 10, &offset) < 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003092 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10003093 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08003094 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003095 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08003096 /* Must set offset before size, so overlap checks
3097 * can be sane */
3098 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08003099 rdev->data_offset = offset;
NeilBrown25f7fd42012-07-19 15:59:18 +10003100 rdev->new_data_offset = offset;
NeilBrown93c8cad2006-01-06 00:20:56 -08003101 return len;
3102}
3103
3104static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07003105__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08003106
NeilBrownc6563a82012-05-21 09:27:00 +10003107static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3108{
3109 return sprintf(page, "%llu\n",
3110 (unsigned long long)rdev->new_data_offset);
3111}
3112
3113static ssize_t new_offset_store(struct md_rdev *rdev,
3114 const char *buf, size_t len)
3115{
3116 unsigned long long new_offset;
3117 struct mddev *mddev = rdev->mddev;
3118
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003119 if (kstrtoull(buf, 10, &new_offset) < 0)
NeilBrownc6563a82012-05-21 09:27:00 +10003120 return -EINVAL;
3121
NeilBrownf851b602014-12-11 10:02:10 +11003122 if (mddev->sync_thread ||
3123 test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
NeilBrownc6563a82012-05-21 09:27:00 +10003124 return -EBUSY;
3125 if (new_offset == rdev->data_offset)
3126 /* reset is always permitted */
3127 ;
3128 else if (new_offset > rdev->data_offset) {
3129 /* must not push array size beyond rdev_sectors */
3130 if (new_offset - rdev->data_offset
3131 + mddev->dev_sectors > rdev->sectors)
3132 return -E2BIG;
3133 }
3134 /* Metadata worries about other space details. */
3135
3136 /* decreasing the offset is inconsistent with a backwards
3137 * reshape.
3138 */
3139 if (new_offset < rdev->data_offset &&
3140 mddev->reshape_backwards)
3141 return -EINVAL;
3142 /* Increasing offset is inconsistent with forwards
3143 * reshape. reshape_direction should be set to
3144 * 'backwards' first.
3145 */
3146 if (new_offset > rdev->data_offset &&
3147 !mddev->reshape_backwards)
3148 return -EINVAL;
3149
3150 if (mddev->pers && mddev->persistent &&
3151 !super_types[mddev->major_version]
3152 .allow_new_offset(rdev, new_offset))
3153 return -E2BIG;
3154 rdev->new_data_offset = new_offset;
3155 if (new_offset > rdev->data_offset)
3156 mddev->reshape_backwards = 1;
3157 else if (new_offset < rdev->data_offset)
3158 mddev->reshape_backwards = 0;
3159
3160 return len;
3161}
3162static struct rdev_sysfs_entry rdev_new_offset =
3163__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3164
NeilBrown83303b62006-01-06 00:21:06 -08003165static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003166rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08003167{
Andre Nolldd8ac332009-03-31 14:33:13 +11003168 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08003169}
3170
NeilBrownc5d79ad2008-02-06 01:39:54 -08003171static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3172{
3173 /* check if two start/length pairs overlap */
3174 if (s1+l1 <= s2)
3175 return 0;
3176 if (s2+l2 <= s1)
3177 return 0;
3178 return 1;
3179}
3180
Dan Williamsb522adc2009-03-31 15:00:31 +11003181static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3182{
3183 unsigned long long blocks;
3184 sector_t new;
3185
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003186 if (kstrtoull(buf, 10, &blocks) < 0)
Dan Williamsb522adc2009-03-31 15:00:31 +11003187 return -EINVAL;
3188
3189 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3190 return -EINVAL; /* sector conversion overflow */
3191
3192 new = blocks * 2;
3193 if (new != blocks * 2)
3194 return -EINVAL; /* unsigned long long to sector_t overflow */
3195
3196 *sectors = new;
3197 return 0;
3198}
3199
NeilBrown83303b62006-01-06 00:21:06 -08003200static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11003201rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08003202{
NeilBrownfd01b882011-10-11 16:47:53 +11003203 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11003204 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11003205 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08003206
Shaohua Lif2076e72015-10-08 21:54:12 -07003207 if (test_bit(Journal, &rdev->flags))
3208 return -EBUSY;
Dan Williamsb522adc2009-03-31 15:00:31 +11003209 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10003210 return -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10003211 if (rdev->data_offset != rdev->new_data_offset)
3212 return -EINVAL; /* too confusing */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003213 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10003214 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11003215 sectors = super_types[my_mddev->major_version].
3216 rdev_size_change(rdev, sectors);
3217 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10003218 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11003219 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01003220 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11003221 rdev->data_offset;
NeilBrowna6468532013-02-21 14:33:17 +11003222 if (!my_mddev->pers->resize)
3223 /* Cannot change size for RAID0 or Linear etc */
3224 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10003225 }
Andre Nolldd8ac332009-03-31 14:33:13 +11003226 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11003227 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10003228
Andre Nolldd8ac332009-03-31 14:33:13 +11003229 rdev->sectors = sectors;
3230 if (sectors > oldsectors && my_mddev->external) {
NeilBrown8b1afc32014-09-29 15:33:20 +10003231 /* Need to check that all other rdevs with the same
3232 * ->bdev do not overlap. 'rcu' is sufficient to walk
3233 * the rdev lists safely.
3234 * This check does not provide a hard guarantee, it
3235 * just helps avoid dangerous mistakes.
NeilBrownc5d79ad2008-02-06 01:39:54 -08003236 */
NeilBrownfd01b882011-10-11 16:47:53 +11003237 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003238 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003239 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003240
NeilBrown8b1afc32014-09-29 15:33:20 +10003241 rcu_read_lock();
NeilBrown29ac4aa2008-02-06 01:39:58 -08003242 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11003243 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003244
NeilBrowndafb20f2012-03-19 12:46:39 +11003245 rdev_for_each(rdev2, mddev)
NeilBrownf21e9ff2011-01-31 12:10:09 +11003246 if (rdev->bdev == rdev2->bdev &&
3247 rdev != rdev2 &&
3248 overlaps(rdev->data_offset, rdev->sectors,
3249 rdev2->data_offset,
3250 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08003251 overlap = 1;
3252 break;
3253 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003254 if (overlap) {
3255 mddev_put(mddev);
3256 break;
3257 }
3258 }
NeilBrown8b1afc32014-09-29 15:33:20 +10003259 rcu_read_unlock();
NeilBrownc5d79ad2008-02-06 01:39:54 -08003260 if (overlap) {
3261 /* Someone else could have slipped in a size
3262 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11003263 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08003264 * safe, and trust userspace not to race with
3265 * itself
3266 */
Andre Nolldd8ac332009-03-31 14:33:13 +11003267 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08003268 return -EBUSY;
3269 }
3270 }
NeilBrown83303b62006-01-06 00:21:06 -08003271 return len;
3272}
3273
3274static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003275__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08003276
NeilBrown3cb03002011-10-11 16:45:26 +11003277static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07003278{
3279 unsigned long long recovery_start = rdev->recovery_offset;
3280
3281 if (test_bit(In_sync, &rdev->flags) ||
3282 recovery_start == MaxSector)
3283 return sprintf(page, "none\n");
3284
3285 return sprintf(page, "%llu\n", recovery_start);
3286}
3287
NeilBrown3cb03002011-10-11 16:45:26 +11003288static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07003289{
3290 unsigned long long recovery_start;
3291
3292 if (cmd_match(buf, "none"))
3293 recovery_start = MaxSector;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003294 else if (kstrtoull(buf, 10, &recovery_start))
Dan Williams06e3c812009-12-12 21:17:12 -07003295 return -EINVAL;
3296
3297 if (rdev->mddev->pers &&
3298 rdev->raid_disk >= 0)
3299 return -EBUSY;
3300
3301 rdev->recovery_offset = recovery_start;
3302 if (recovery_start == MaxSector)
3303 set_bit(In_sync, &rdev->flags);
3304 else
3305 clear_bit(In_sync, &rdev->flags);
3306 return len;
3307}
3308
3309static struct rdev_sysfs_entry rdev_recovery_start =
3310__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3311
Vishal Vermafc974ee2015-12-24 19:20:34 -07003312/* sysfs access to bad-blocks list.
3313 * We present two files.
3314 * 'bad-blocks' lists sector numbers and lengths of ranges that
3315 * are recorded as bad. The list is truncated to fit within
3316 * the one-page limit of sysfs.
3317 * Writing "sector length" to this file adds an acknowledged
3318 * bad block list.
3319 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3320 * been acknowledged. Writing to this file adds bad blocks
3321 * without acknowledging them. This is largely for testing.
3322 */
NeilBrown3cb03002011-10-11 16:45:26 +11003323static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003324{
3325 return badblocks_show(&rdev->badblocks, page, 0);
3326}
NeilBrown3cb03002011-10-11 16:45:26 +11003327static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003328{
NeilBrownde393cd2011-07-28 11:31:48 +10003329 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3330 /* Maybe that ack was all we needed */
3331 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3332 wake_up(&rdev->blocked_wait);
3333 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10003334}
3335static struct rdev_sysfs_entry rdev_bad_blocks =
3336__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3337
NeilBrown3cb03002011-10-11 16:45:26 +11003338static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10003339{
3340 return badblocks_show(&rdev->badblocks, page, 1);
3341}
NeilBrown3cb03002011-10-11 16:45:26 +11003342static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10003343{
3344 return badblocks_store(&rdev->badblocks, page, len, 1);
3345}
3346static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3347__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3348
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003349static ssize_t
3350ppl_sector_show(struct md_rdev *rdev, char *page)
3351{
3352 return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3353}
3354
3355static ssize_t
3356ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3357{
3358 unsigned long long sector;
3359
3360 if (kstrtoull(buf, 10, &sector) < 0)
3361 return -EINVAL;
3362 if (sector != (sector_t)sector)
3363 return -EINVAL;
3364
3365 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3366 rdev->raid_disk >= 0)
3367 return -EBUSY;
3368
3369 if (rdev->mddev->persistent) {
3370 if (rdev->mddev->major_version == 0)
3371 return -EINVAL;
3372 if ((sector > rdev->sb_start &&
3373 sector - rdev->sb_start > S16_MAX) ||
3374 (sector < rdev->sb_start &&
3375 rdev->sb_start - sector > -S16_MIN))
3376 return -EINVAL;
3377 rdev->ppl.offset = sector - rdev->sb_start;
3378 } else if (!rdev->mddev->external) {
3379 return -EBUSY;
3380 }
3381 rdev->ppl.sector = sector;
3382 return len;
3383}
3384
3385static struct rdev_sysfs_entry rdev_ppl_sector =
3386__ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3387
3388static ssize_t
3389ppl_size_show(struct md_rdev *rdev, char *page)
3390{
3391 return sprintf(page, "%u\n", rdev->ppl.size);
3392}
3393
3394static ssize_t
3395ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3396{
3397 unsigned int size;
3398
3399 if (kstrtouint(buf, 10, &size) < 0)
3400 return -EINVAL;
3401
3402 if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3403 rdev->raid_disk >= 0)
3404 return -EBUSY;
3405
3406 if (rdev->mddev->persistent) {
3407 if (rdev->mddev->major_version == 0)
3408 return -EINVAL;
3409 if (size > U16_MAX)
3410 return -EINVAL;
3411 } else if (!rdev->mddev->external) {
3412 return -EBUSY;
3413 }
3414 rdev->ppl.size = size;
3415 return len;
3416}
3417
3418static struct rdev_sysfs_entry rdev_ppl_size =
3419__ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3420
NeilBrown86e6ffd2005-11-08 21:39:24 -08003421static struct attribute *rdev_default_attrs[] = {
3422 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08003423 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08003424 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08003425 &rdev_offset.attr,
NeilBrownc6563a82012-05-21 09:27:00 +10003426 &rdev_new_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08003427 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07003428 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10003429 &rdev_bad_blocks.attr,
3430 &rdev_unack_bad_blocks.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01003431 &rdev_ppl_sector.attr,
3432 &rdev_ppl_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08003433 NULL,
3434};
3435static ssize_t
3436rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3437{
3438 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003439 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003440
3441 if (!entry->show)
3442 return -EIO;
NeilBrown758bfc82014-12-15 12:56:59 +11003443 if (!rdev->mddev)
Marcos Paulo de Souza168b3052019-06-14 15:41:06 -07003444 return -ENODEV;
NeilBrown758bfc82014-12-15 12:56:59 +11003445 return entry->show(rdev, page);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003446}
3447
3448static ssize_t
3449rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3450 const char *page, size_t length)
3451{
3452 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11003453 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08003454 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11003455 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003456
3457 if (!entry->store)
3458 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003459 if (!capable(CAP_SYS_ADMIN))
3460 return -EACCES;
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003461 rv = mddev ? mddev_lock(mddev) : -ENODEV;
NeilBrownca388052008-02-06 01:39:55 -08003462 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003463 if (rdev->mddev == NULL)
Pawel Baldysiakc42d3242019-03-27 13:48:21 +01003464 rv = -ENODEV;
NeilBrown27c529b2008-03-04 14:29:33 -08003465 else
3466 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003467 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003468 }
3469 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003470}
3471
3472static void rdev_free(struct kobject *ko)
3473{
NeilBrown3cb03002011-10-11 16:45:26 +11003474 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003475 kfree(rdev);
3476}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003477static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003478 .show = rdev_attr_show,
3479 .store = rdev_attr_store,
3480};
3481static struct kobj_type rdev_ktype = {
3482 .release = rdev_free,
3483 .sysfs_ops = &rdev_sysfs_ops,
3484 .default_attrs = rdev_default_attrs,
3485};
3486
NeilBrown3cb03002011-10-11 16:45:26 +11003487int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003488{
3489 rdev->desc_nr = -1;
3490 rdev->saved_raid_disk = -1;
3491 rdev->raid_disk = -1;
3492 rdev->flags = 0;
3493 rdev->data_offset = 0;
NeilBrownc6563a82012-05-21 09:27:00 +10003494 rdev->new_data_offset = 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003495 rdev->sb_events = 0;
Arnd Bergmann0e3ef492016-06-17 17:33:10 +02003496 rdev->last_read_error = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003497 rdev->sb_loaded = 0;
3498 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003499 atomic_set(&rdev->nr_pending, 0);
3500 atomic_set(&rdev->read_errors, 0);
3501 atomic_set(&rdev->corrected_errors, 0);
3502
3503 INIT_LIST_HEAD(&rdev->same_set);
3504 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003505
3506 /* Add space to store bad block list.
3507 * This reserves the space even on arrays where it cannot
3508 * be used - I wonder if that matters
3509 */
Vishal Vermafc974ee2015-12-24 19:20:34 -07003510 return badblocks_init(&rdev->badblocks, 0);
NeilBrowne8bb9a82010-06-01 19:37:26 +10003511}
3512EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513/*
3514 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3515 *
3516 * mark the device faulty if:
3517 *
3518 * - the device is nonexistent (zero size)
3519 * - the device has no valid superblock
3520 *
3521 * a faulty rdev _never_ has rdev->sb set.
3522 */
NeilBrown3cb03002011-10-11 16:45:26 +11003523static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524{
3525 char b[BDEVNAME_SIZE];
3526 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003527 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 sector_t size;
3529
NeilBrown9ffae0c2006-01-06 00:20:32 -08003530 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
NeilBrown9d487392016-11-02 14:16:49 +11003531 if (!rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
NeilBrown2230dfe2011-07-28 11:31:46 +10003534 err = md_rdev_init(rdev);
3535 if (err)
3536 goto abort_free;
3537 err = alloc_disk_sb(rdev);
3538 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 goto abort_free;
3540
NeilBrownc5d79ad2008-02-06 01:39:54 -08003541 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 if (err)
3543 goto abort_free;
3544
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003545 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003546
Mike Snitzer77304d22010-11-08 14:39:12 +01003547 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 if (!size) {
NeilBrown9d487392016-11-02 14:16:49 +11003549 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 bdevname(rdev->bdev,b));
3551 err = -EINVAL;
3552 goto abort_free;
3553 }
3554
3555 if (super_format >= 0) {
3556 err = super_types[super_format].
3557 load_super(rdev, NULL, super_minor);
3558 if (err == -EINVAL) {
NeilBrown9d487392016-11-02 14:16:49 +11003559 pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
NeilBrowndf968c42007-07-17 04:06:11 -07003560 bdevname(rdev->bdev,b),
NeilBrown9d487392016-11-02 14:16:49 +11003561 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 goto abort_free;
3563 }
3564 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11003565 pr_warn("md: could not read %s's sb, not importing!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 bdevname(rdev->bdev,b));
3567 goto abort_free;
3568 }
3569 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 return rdev;
3572
3573abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003574 if (rdev->bdev)
3575 unlock_rdev(rdev);
NeilBrown545c8792012-05-22 13:54:30 +10003576 md_rdev_clear(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 kfree(rdev);
3578 return ERR_PTR(err);
3579}
3580
3581/*
3582 * Check a full RAID array for plausibility
3583 */
3584
NeilBrownf72ffdd2014-09-30 14:23:59 +10003585static void analyze_sbs(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
3587 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003588 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 char b[BDEVNAME_SIZE];
3590
3591 freshest = NULL;
NeilBrowndafb20f2012-03-19 12:46:39 +11003592 rdev_for_each_safe(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 switch (super_types[mddev->major_version].
3594 load_super(rdev, freshest, mddev->minor_version)) {
3595 case 1:
3596 freshest = rdev;
3597 break;
3598 case 0:
3599 break;
3600 default:
NeilBrown9d487392016-11-02 14:16:49 +11003601 pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003603 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
3605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 super_types[mddev->major_version].
3607 validate_super(mddev, freshest);
3608
3609 i = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +11003610 rdev_for_each_safe(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003611 if (mddev->max_disks &&
3612 (rdev->desc_nr >= mddev->max_disks ||
3613 i > mddev->max_disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11003614 pr_warn("md: %s: %s: only %d devices permitted\n",
3615 mdname(mddev), bdevname(rdev->bdev, b),
3616 mddev->max_disks);
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003617 md_kick_rdev_from_array(rdev);
NeilBrownde01dfa2009-02-06 18:02:46 +11003618 continue;
3619 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003620 if (rdev != freshest) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 if (super_types[mddev->major_version].
3622 validate_super(mddev, rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003623 pr_warn("md: kicking non-fresh %s from array!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 bdevname(rdev->bdev,b));
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05003625 md_kick_rdev_from_array(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 continue;
3627 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05003628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 if (mddev->level == LEVEL_MULTIPATH) {
3630 rdev->desc_nr = i++;
3631 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003632 set_bit(In_sync, &rdev->flags);
Shaohua Lif2076e72015-10-08 21:54:12 -07003633 } else if (rdev->raid_disk >=
3634 (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3635 !test_bit(Journal, &rdev->flags)) {
NeilBrowna778b732007-05-23 13:58:10 -07003636 rdev->raid_disk = -1;
3637 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 }
3639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640}
3641
NeilBrown72e02072009-12-14 12:49:55 +11003642/* Read a fixed-point number.
3643 * Numbers in sysfs attributes should be in "standard" units where
3644 * possible, so time should be in seconds.
NeilBrownf72ffdd2014-09-30 14:23:59 +10003645 * However we internally use a a much smaller unit such as
NeilBrown72e02072009-12-14 12:49:55 +11003646 * milliseconds or jiffies.
3647 * This function takes a decimal number with a possible fractional
3648 * component, and produces an integer which is the result of
3649 * multiplying that number by 10^'scale'.
3650 * all without any floating-point arithmetic.
3651 */
3652int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3653{
3654 unsigned long result = 0;
3655 long decimals = -1;
3656 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3657 if (*cp == '.')
3658 decimals = 0;
3659 else if (decimals < scale) {
3660 unsigned int value;
3661 value = *cp - '0';
3662 result = result * 10 + value;
3663 if (decimals >= 0)
3664 decimals++;
3665 }
3666 cp++;
3667 }
3668 if (*cp == '\n')
3669 cp++;
3670 if (*cp)
3671 return -EINVAL;
3672 if (decimals < 0)
3673 decimals = 0;
Andy Shevchenkocf891602019-07-23 23:41:55 +03003674 *res = result * int_pow(10, scale - decimals);
NeilBrown72e02072009-12-14 12:49:55 +11003675 return 0;
3676}
3677
NeilBrowneae17012005-11-08 21:39:23 -08003678static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003679safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003680{
3681 int msec = (mddev->safemode_delay*1000)/HZ;
3682 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3683}
3684static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003685safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003686{
NeilBrown16f17b32006-06-26 00:27:37 -07003687 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003688
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003689 if (mddev_is_clustered(mddev)) {
NeilBrown9d487392016-11-02 14:16:49 +11003690 pr_warn("md: Safemode is disabled for clustered mode\n");
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11003691 return -EINVAL;
3692 }
3693
NeilBrown72e02072009-12-14 12:49:55 +11003694 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003695 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003696 if (msec == 0)
3697 mddev->safemode_delay = 0;
3698 else {
NeilBrown19052c02008-08-05 15:54:13 +10003699 unsigned long old_delay = mddev->safemode_delay;
NeilBrown1b30e662014-12-15 12:57:00 +11003700 unsigned long new_delay = (msec*HZ)/1000;
3701
3702 if (new_delay == 0)
3703 new_delay = 1;
3704 mddev->safemode_delay = new_delay;
3705 if (new_delay < old_delay || old_delay == 0)
3706 mod_timer(&mddev->safemode_timer, jiffies+1);
NeilBrown16f17b32006-06-26 00:27:37 -07003707 }
3708 return len;
3709}
3710static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003711__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003712
3713static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003714level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003715{
NeilBrown36d091f2014-12-15 12:56:58 +11003716 struct md_personality *p;
3717 int ret;
3718 spin_lock(&mddev->lock);
3719 p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003720 if (p)
NeilBrown36d091f2014-12-15 12:56:58 +11003721 ret = sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003722 else if (mddev->clevel[0])
NeilBrown36d091f2014-12-15 12:56:58 +11003723 ret = sprintf(page, "%s\n", mddev->clevel);
NeilBrownd9d166c2006-01-06 00:20:51 -08003724 else if (mddev->level != LEVEL_NONE)
NeilBrown36d091f2014-12-15 12:56:58 +11003725 ret = sprintf(page, "%d\n", mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08003726 else
NeilBrown36d091f2014-12-15 12:56:58 +11003727 ret = 0;
3728 spin_unlock(&mddev->lock);
3729 return ret;
NeilBrowneae17012005-11-08 21:39:23 -08003730}
3731
NeilBrownd9d166c2006-01-06 00:20:51 -08003732static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003733level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003734{
Dan Williamsf2859af2010-05-02 10:04:16 -07003735 char clevel[16];
NeilBrown67918752014-12-15 12:57:01 +11003736 ssize_t rv;
3737 size_t slen = len;
NeilBrowndb721d32014-12-15 12:56:58 +11003738 struct md_personality *pers, *oldpers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003739 long level;
NeilBrowndb721d32014-12-15 12:56:58 +11003740 void *priv, *oldpriv;
NeilBrown3cb03002011-10-11 16:45:26 +11003741 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003742
NeilBrown67918752014-12-15 12:57:01 +11003743 if (slen == 0 || slen >= sizeof(clevel))
3744 return -EINVAL;
3745
3746 rv = mddev_lock(mddev);
3747 if (rv)
NeilBrown245f46c2009-03-31 14:39:39 +11003748 return rv;
NeilBrown67918752014-12-15 12:57:01 +11003749
3750 if (mddev->pers == NULL) {
3751 strncpy(mddev->clevel, buf, slen);
3752 if (mddev->clevel[slen-1] == '\n')
3753 slen--;
3754 mddev->clevel[slen] = 0;
3755 mddev->level = LEVEL_NONE;
3756 rv = len;
3757 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003758 }
NeilBrown67918752014-12-15 12:57:01 +11003759 rv = -EROFS;
NeilBrownbd8839e2014-05-28 13:39:21 +10003760 if (mddev->ro)
NeilBrown67918752014-12-15 12:57:01 +11003761 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003762
3763 /* request to change the personality. Need to ensure:
3764 * - array is not engaged in resync/recovery/reshape
3765 * - old personality can be suspended
3766 * - new personality will access other array.
3767 */
3768
NeilBrown67918752014-12-15 12:57:01 +11003769 rv = -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10003770 if (mddev->sync_thread ||
NeilBrownf851b602014-12-11 10:02:10 +11003771 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrownbb4f1e92010-08-08 21:18:03 +10003772 mddev->reshape_position != MaxSector ||
3773 mddev->sysfs_active)
NeilBrown67918752014-12-15 12:57:01 +11003774 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003775
NeilBrown67918752014-12-15 12:57:01 +11003776 rv = -EINVAL;
NeilBrown245f46c2009-03-31 14:39:39 +11003777 if (!mddev->pers->quiesce) {
NeilBrown9d487392016-11-02 14:16:49 +11003778 pr_warn("md: %s: %s does not support online personality change\n",
3779 mdname(mddev), mddev->pers->name);
NeilBrown67918752014-12-15 12:57:01 +11003780 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003781 }
3782
3783 /* Now find the new personality */
NeilBrown67918752014-12-15 12:57:01 +11003784 strncpy(clevel, buf, slen);
3785 if (clevel[slen-1] == '\n')
3786 slen--;
3787 clevel[slen] = 0;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09003788 if (kstrtol(clevel, 10, &level))
Dan Williamsf2859af2010-05-02 10:04:16 -07003789 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003790
Dan Williamsf2859af2010-05-02 10:04:16 -07003791 if (request_module("md-%s", clevel) != 0)
3792 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003793 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003794 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003795 if (!pers || !try_module_get(pers->owner)) {
3796 spin_unlock(&pers_lock);
NeilBrown9d487392016-11-02 14:16:49 +11003797 pr_warn("md: personality %s not loaded\n", clevel);
NeilBrown67918752014-12-15 12:57:01 +11003798 rv = -EINVAL;
3799 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003800 }
3801 spin_unlock(&pers_lock);
3802
3803 if (pers == mddev->pers) {
3804 /* Nothing to do! */
3805 module_put(pers->owner);
NeilBrown67918752014-12-15 12:57:01 +11003806 rv = len;
3807 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003808 }
3809 if (!pers->takeover) {
3810 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003811 pr_warn("md: %s: %s does not support personality takeover\n",
3812 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003813 rv = -EINVAL;
3814 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003815 }
3816
NeilBrowndafb20f2012-03-19 12:46:39 +11003817 rdev_for_each(rdev, mddev)
NeilBrowne93f68a2010-06-15 09:36:03 +01003818 rdev->new_raid_disk = rdev->raid_disk;
3819
NeilBrown245f46c2009-03-31 14:39:39 +11003820 /* ->takeover must set new_* and/or delta_disks
3821 * if it succeeds, and may set them when it fails.
3822 */
3823 priv = pers->takeover(mddev);
3824 if (IS_ERR(priv)) {
3825 mddev->new_level = mddev->level;
3826 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003827 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003828 mddev->raid_disks -= mddev->delta_disks;
3829 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10003830 mddev->reshape_backwards = 0;
NeilBrown245f46c2009-03-31 14:39:39 +11003831 module_put(pers->owner);
NeilBrown9d487392016-11-02 14:16:49 +11003832 pr_warn("md: %s: %s would not accept array\n",
3833 mdname(mddev), clevel);
NeilBrown67918752014-12-15 12:57:01 +11003834 rv = PTR_ERR(priv);
3835 goto out_unlock;
NeilBrown245f46c2009-03-31 14:39:39 +11003836 }
3837
3838 /* Looks like we have a winner */
3839 mddev_suspend(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11003840 mddev_detach(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11003841
3842 spin_lock(&mddev->lock);
NeilBrowndb721d32014-12-15 12:56:58 +11003843 oldpers = mddev->pers;
3844 oldpriv = mddev->private;
3845 mddev->pers = pers;
3846 mddev->private = priv;
3847 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3848 mddev->level = mddev->new_level;
3849 mddev->layout = mddev->new_layout;
3850 mddev->chunk_sectors = mddev->new_chunk_sectors;
3851 mddev->delta_disks = 0;
3852 mddev->reshape_backwards = 0;
3853 mddev->degraded = 0;
NeilBrown36d091f2014-12-15 12:56:58 +11003854 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10003855
NeilBrowndb721d32014-12-15 12:56:58 +11003856 if (oldpers->sync_request == NULL &&
Trela Maciej54071b32010-03-08 16:02:42 +11003857 mddev->external) {
3858 /* We are converting from a no-redundancy array
3859 * to a redundancy array and metadata is managed
3860 * externally so we need to be sure that writes
3861 * won't block due to a need to transition
3862 * clean->dirty
3863 * until external management is started.
3864 */
3865 mddev->in_sync = 0;
3866 mddev->safemode_delay = 0;
3867 mddev->safemode = 0;
3868 }
3869
NeilBrowndb721d32014-12-15 12:56:58 +11003870 oldpers->free(mddev, oldpriv);
3871
3872 if (oldpers->sync_request == NULL &&
3873 pers->sync_request != NULL) {
3874 /* need to add the md_redundancy_group */
3875 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11003876 pr_warn("md: cannot register extra attributes for %s\n",
3877 mdname(mddev));
NeilBrowndb721d32014-12-15 12:56:58 +11003878 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3879 }
3880 if (oldpers->sync_request != NULL &&
3881 pers->sync_request == NULL) {
3882 /* need to remove the md_redundancy_group */
3883 if (mddev->to_remove == NULL)
3884 mddev->to_remove = &md_redundancy_group;
3885 }
3886
Alexey Obitotskiy4cb9da72016-06-23 12:11:01 +02003887 module_put(oldpers->owner);
3888
NeilBrowndafb20f2012-03-19 12:46:39 +11003889 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003890 if (rdev->raid_disk < 0)
3891 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003892 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003893 rdev->new_raid_disk = -1;
3894 if (rdev->new_raid_disk == rdev->raid_disk)
3895 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003896 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003897 }
NeilBrowndafb20f2012-03-19 12:46:39 +11003898 rdev_for_each(rdev, mddev) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003899 if (rdev->raid_disk < 0)
3900 continue;
3901 if (rdev->new_raid_disk == rdev->raid_disk)
3902 continue;
3903 rdev->raid_disk = rdev->new_raid_disk;
3904 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003905 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003906 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003907 if (sysfs_link_rdev(mddev, rdev))
NeilBrown9d487392016-11-02 14:16:49 +11003908 pr_warn("md: cannot register rd%d for %s after level change\n",
3909 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003910 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003911 }
3912
NeilBrowndb721d32014-12-15 12:56:58 +11003913 if (pers->sync_request == NULL) {
Trela, Maciej9af204c2010-03-08 16:02:44 +11003914 /* this is now an array without redundancy, so
3915 * it must always be in_sync
3916 */
3917 mddev->in_sync = 1;
3918 del_timer_sync(&mddev->safemode_timer);
3919 }
NeilBrown02e5f5c2013-11-14 15:16:15 +11003920 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown245f46c2009-03-31 14:39:39 +11003921 pers->run(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08003922 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassow47525e52012-05-22 13:55:29 +10003923 mddev_resume(mddev);
NeilBrown830778a2014-01-14 15:17:03 +11003924 if (!mddev->thread)
3925 md_update_sb(mddev, 1);
Maciej Trela5cac78612010-04-14 17:17:39 +10003926 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003927 md_new_event(mddev);
NeilBrown67918752014-12-15 12:57:01 +11003928 rv = len;
3929out_unlock:
3930 mddev_unlock(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003931 return rv;
3932}
3933
3934static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003935__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003936
NeilBrownd4dbd022006-06-26 00:27:59 -07003937static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003938layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003939{
3940 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003941 if (mddev->reshape_position != MaxSector &&
3942 mddev->layout != mddev->new_layout)
3943 return sprintf(page, "%d (%d)\n",
3944 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003945 return sprintf(page, "%d\n", mddev->layout);
3946}
3947
3948static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003949layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003950{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003951 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11003952 int err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003953
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03003954 err = kstrtouint(buf, 10, &n);
3955 if (err < 0)
3956 return err;
NeilBrown67918752014-12-15 12:57:01 +11003957 err = mddev_lock(mddev);
3958 if (err)
3959 return err;
NeilBrownd4dbd022006-06-26 00:27:59 -07003960
NeilBrownb3546032009-03-31 14:56:41 +11003961 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10003962 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11003963 err = -EBUSY;
3964 else if (mddev->ro)
3965 err = -EROFS;
3966 else {
3967 mddev->new_layout = n;
3968 err = mddev->pers->check_reshape(mddev);
3969 if (err)
3970 mddev->new_layout = mddev->layout;
NeilBrown597a7112009-06-18 08:47:42 +10003971 }
NeilBrownb3546032009-03-31 14:56:41 +11003972 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003973 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003974 if (mddev->reshape_position == MaxSector)
3975 mddev->layout = n;
3976 }
NeilBrown67918752014-12-15 12:57:01 +11003977 mddev_unlock(mddev);
3978 return err ?: len;
NeilBrownd4dbd022006-06-26 00:27:59 -07003979}
3980static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003981__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003982
NeilBrowneae17012005-11-08 21:39:23 -08003983static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003984raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003985{
NeilBrownbb636542005-11-08 21:39:45 -08003986 if (mddev->raid_disks == 0)
3987 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003988 if (mddev->reshape_position != MaxSector &&
3989 mddev->delta_disks != 0)
3990 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3991 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003992 return sprintf(page, "%d\n", mddev->raid_disks);
3993}
3994
NeilBrownfd01b882011-10-11 16:47:53 +11003995static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b992006-01-06 00:20:54 -08003996
3997static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003998raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b992006-01-06 00:20:54 -08003999{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004000 unsigned int n;
NeilBrown67918752014-12-15 12:57:01 +11004001 int err;
NeilBrownda943b992006-01-06 00:20:54 -08004002
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004003 err = kstrtouint(buf, 10, &n);
4004 if (err < 0)
4005 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004006
NeilBrown67918752014-12-15 12:57:01 +11004007 err = mddev_lock(mddev);
4008 if (err)
4009 return err;
NeilBrownda943b992006-01-06 00:20:54 -08004010 if (mddev->pers)
NeilBrown67918752014-12-15 12:57:01 +11004011 err = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07004012 else if (mddev->reshape_position != MaxSector) {
NeilBrownc6563a82012-05-21 09:27:00 +10004013 struct md_rdev *rdev;
NeilBrown08a02ec2007-05-09 02:35:38 -07004014 int olddisks = mddev->raid_disks - mddev->delta_disks;
NeilBrownc6563a82012-05-21 09:27:00 +10004015
NeilBrown67918752014-12-15 12:57:01 +11004016 err = -EINVAL;
NeilBrownc6563a82012-05-21 09:27:00 +10004017 rdev_for_each(rdev, mddev) {
4018 if (olddisks < n &&
4019 rdev->data_offset < rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004020 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004021 if (olddisks > n &&
4022 rdev->data_offset > rdev->new_data_offset)
NeilBrown67918752014-12-15 12:57:01 +11004023 goto out_unlock;
NeilBrownc6563a82012-05-21 09:27:00 +10004024 }
NeilBrown67918752014-12-15 12:57:01 +11004025 err = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07004026 mddev->delta_disks = n - olddisks;
4027 mddev->raid_disks = n;
NeilBrown2c810cd2012-05-21 09:27:00 +10004028 mddev->reshape_backwards = (mddev->delta_disks < 0);
NeilBrown08a02ec2007-05-09 02:35:38 -07004029 } else
NeilBrownda943b992006-01-06 00:20:54 -08004030 mddev->raid_disks = n;
NeilBrown67918752014-12-15 12:57:01 +11004031out_unlock:
4032 mddev_unlock(mddev);
4033 return err ? err : len;
NeilBrownda943b992006-01-06 00:20:54 -08004034}
4035static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07004036__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08004037
NeilBrown24dd4692005-11-08 21:39:26 -08004038static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004039chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08004040{
NeilBrown08a02ec2007-05-09 02:35:38 -07004041 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10004042 mddev->chunk_sectors != mddev->new_chunk_sectors)
4043 return sprintf(page, "%d (%d)\n",
4044 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10004045 mddev->chunk_sectors << 9);
4046 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08004047}
4048
4049static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004050chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08004051{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004052 unsigned long n;
NeilBrown67918752014-12-15 12:57:01 +11004053 int err;
NeilBrown3b343802006-01-06 00:20:47 -08004054
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004055 err = kstrtoul(buf, 10, &n);
4056 if (err < 0)
4057 return err;
NeilBrown3b343802006-01-06 00:20:47 -08004058
NeilBrown67918752014-12-15 12:57:01 +11004059 err = mddev_lock(mddev);
4060 if (err)
4061 return err;
NeilBrownb3546032009-03-31 14:56:41 +11004062 if (mddev->pers) {
NeilBrown50ac1682009-06-18 08:47:55 +10004063 if (mddev->pers->check_reshape == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004064 err = -EBUSY;
4065 else if (mddev->ro)
4066 err = -EROFS;
4067 else {
4068 mddev->new_chunk_sectors = n >> 9;
4069 err = mddev->pers->check_reshape(mddev);
4070 if (err)
4071 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown597a7112009-06-18 08:47:42 +10004072 }
NeilBrownb3546032009-03-31 14:56:41 +11004073 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10004074 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004075 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10004076 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11004077 }
NeilBrown67918752014-12-15 12:57:01 +11004078 mddev_unlock(mddev);
4079 return err ?: len;
NeilBrown3b343802006-01-06 00:20:47 -08004080}
4081static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004082__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08004083
NeilBrowna94213b2006-06-26 00:28:00 -07004084static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004085resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07004086{
NeilBrownd1a7c502009-03-31 15:24:32 +11004087 if (mddev->recovery_cp == MaxSector)
4088 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07004089 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
4090}
4091
4092static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004093resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07004094{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004095 unsigned long long n;
NeilBrown67918752014-12-15 12:57:01 +11004096 int err;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004097
4098 if (cmd_match(buf, "none"))
4099 n = MaxSector;
4100 else {
4101 err = kstrtoull(buf, 10, &n);
4102 if (err < 0)
4103 return err;
4104 if (n != (sector_t)n)
4105 return -EINVAL;
4106 }
NeilBrowna94213b2006-06-26 00:28:00 -07004107
NeilBrown67918752014-12-15 12:57:01 +11004108 err = mddev_lock(mddev);
4109 if (err)
4110 return err;
NeilBrownb0986362011-05-11 15:52:21 +10004111 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrown67918752014-12-15 12:57:01 +11004112 err = -EBUSY;
NeilBrowna94213b2006-06-26 00:28:00 -07004113
NeilBrown67918752014-12-15 12:57:01 +11004114 if (!err) {
4115 mddev->recovery_cp = n;
4116 if (mddev->pers)
Shaohua Li29530792016-12-08 15:48:19 -08004117 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrown67918752014-12-15 12:57:01 +11004118 }
4119 mddev_unlock(mddev);
4120 return err ?: len;
NeilBrowna94213b2006-06-26 00:28:00 -07004121}
4122static struct md_sysfs_entry md_resync_start =
NeilBrown750f1992014-09-30 08:53:05 +10004123__ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR,
4124 resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07004125
NeilBrown9e653b62006-06-26 00:27:58 -07004126/*
4127 * The array state can be:
4128 *
4129 * clear
4130 * No devices, no size, no level
4131 * Equivalent to STOP_ARRAY ioctl
4132 * inactive
4133 * May have some settings, but array is not active
4134 * all IO results in error
4135 * When written, doesn't tear down array, but just stops it
4136 * suspended (not supported yet)
4137 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01004138 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07004139 * readonly
4140 * no resync can happen. no superblocks get written.
4141 * write requests fail
4142 * read-auto
4143 * like readonly, but behaves like 'clean' on a write request.
4144 *
4145 * clean - no pending writes, but otherwise active.
4146 * When written to inactive array, starts without resync
4147 * If a write request arrives then
4148 * if metadata is known, mark 'dirty' and switch to 'active'.
4149 * if not known, block and switch to write-pending
4150 * If written to an active array that has pending writes, then fails.
4151 * active
4152 * fully active: IO and resync can be happening.
4153 * When written to inactive array, starts with resync
4154 *
4155 * write-pending
4156 * clean, but writes are blocked waiting for 'active' to be written.
4157 *
4158 * active-idle
4159 * like active, but no writes have been seen for a while (100msec).
4160 *
4161 */
4162enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
4163 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07004164static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07004165 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
4166 "write-pending", "active-idle", NULL };
4167
4168static int match_word(const char *word, char **list)
4169{
4170 int n;
4171 for (n=0; list[n]; n++)
4172 if (cmd_match(word, list[n]))
4173 break;
4174 return n;
4175}
4176
4177static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004178array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07004179{
4180 enum array_state st = inactive;
4181
4182 if (mddev->pers)
4183 switch(mddev->ro) {
4184 case 1:
4185 st = readonly;
4186 break;
4187 case 2:
4188 st = read_auto;
4189 break;
4190 case 0:
NeilBrown55cc39f2017-03-15 14:05:14 +11004191 spin_lock(&mddev->lock);
Shaohua Li29530792016-12-08 15:48:19 -08004192 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrowne6910632008-02-06 01:39:51 -08004193 st = write_pending;
Tomasz Majchrzak16f88942016-10-24 12:47:28 +02004194 else if (mddev->in_sync)
4195 st = clean;
NeilBrown9e653b62006-06-26 00:27:58 -07004196 else if (mddev->safemode)
4197 st = active_idle;
4198 else
4199 st = active;
NeilBrown55cc39f2017-03-15 14:05:14 +11004200 spin_unlock(&mddev->lock);
NeilBrown9e653b62006-06-26 00:27:58 -07004201 }
4202 else {
4203 if (list_empty(&mddev->disks) &&
4204 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11004205 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07004206 st = clear;
4207 else
4208 st = inactive;
4209 }
4210 return sprintf(page, "%s\n", array_states[st]);
4211}
4212
NeilBrownf72ffdd2014-09-30 14:23:59 +10004213static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev);
4214static int md_set_readonly(struct mddev *mddev, struct block_device *bdev);
4215static int do_md_run(struct mddev *mddev);
NeilBrownfd01b882011-10-11 16:47:53 +11004216static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07004217
4218static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004219array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07004220{
NeilBrown6497709b2017-03-15 14:05:14 +11004221 int err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004222 enum array_state st = match_word(buf, array_states);
NeilBrown67918752014-12-15 12:57:01 +11004223
4224 if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) {
4225 /* don't take reconfig_mutex when toggling between
4226 * clean and active
4227 */
4228 spin_lock(&mddev->lock);
4229 if (st == active) {
4230 restart_array(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08004231 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
Tomasz Majchrzak91a6c4a2016-10-25 17:07:08 +02004232 md_wakeup_thread(mddev->thread);
NeilBrown67918752014-12-15 12:57:01 +11004233 wake_up(&mddev->sb_wait);
NeilBrown67918752014-12-15 12:57:01 +11004234 } else /* st == clean */ {
4235 restart_array(mddev);
NeilBrown6497709b2017-03-15 14:05:14 +11004236 if (!set_in_sync(mddev))
NeilBrown67918752014-12-15 12:57:01 +11004237 err = -EBUSY;
4238 }
Tomasz Majchrzak573275b2016-06-30 10:47:09 +02004239 if (!err)
4240 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown67918752014-12-15 12:57:01 +11004241 spin_unlock(&mddev->lock);
NeilBrownc008f1d2015-06-12 19:46:44 +10004242 return err ?: len;
NeilBrown67918752014-12-15 12:57:01 +11004243 }
4244 err = mddev_lock(mddev);
4245 if (err)
4246 return err;
4247 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004248 switch(st) {
4249 case bad_word:
4250 break;
4251 case clear:
4252 /* stopping an active array */
NeilBrowna05b7ea2012-07-19 15:59:18 +10004253 err = do_md_stop(mddev, 0, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004254 break;
4255 case inactive:
4256 /* stopping an active array */
NeilBrown90cf1952012-07-31 10:04:55 +10004257 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004258 err = do_md_stop(mddev, 2, NULL);
NeilBrown90cf1952012-07-31 10:04:55 +10004259 else
NeilBrowne6910632008-02-06 01:39:51 -08004260 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07004261 break;
4262 case suspended:
4263 break; /* not supported yet */
4264 case readonly:
4265 if (mddev->pers)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004266 err = md_set_readonly(mddev, NULL);
NeilBrown9e653b62006-06-26 00:27:58 -07004267 else {
4268 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07004269 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07004270 err = do_md_run(mddev);
4271 }
4272 break;
4273 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07004274 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11004275 if (mddev->ro == 0)
NeilBrowna05b7ea2012-07-19 15:59:18 +10004276 err = md_set_readonly(mddev, NULL);
NeilBrown80268ee2008-10-13 11:55:12 +11004277 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07004278 err = restart_array(mddev);
4279 if (err == 0) {
4280 mddev->ro = 2;
4281 set_disk_ro(mddev->gendisk, 0);
4282 }
NeilBrown9e653b62006-06-26 00:27:58 -07004283 } else {
4284 mddev->ro = 2;
4285 err = do_md_run(mddev);
4286 }
4287 break;
4288 case clean:
4289 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004290 err = restart_array(mddev);
4291 if (err)
4292 break;
NeilBrown85572d72014-12-15 12:56:56 +11004293 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11004294 if (!set_in_sync(mddev))
NeilBrowne6910632008-02-06 01:39:51 -08004295 err = -EBUSY;
NeilBrown85572d72014-12-15 12:56:56 +11004296 spin_unlock(&mddev->lock);
NeilBrown5bf29592009-05-07 12:50:57 +10004297 } else
4298 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07004299 break;
4300 case active:
4301 if (mddev->pers) {
Song Liu339421d2015-10-08 21:54:13 -07004302 err = restart_array(mddev);
4303 if (err)
4304 break;
Shaohua Li29530792016-12-08 15:48:19 -08004305 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown9e653b62006-06-26 00:27:58 -07004306 wake_up(&mddev->sb_wait);
4307 err = 0;
4308 } else {
4309 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07004310 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07004311 err = do_md_run(mddev);
4312 }
4313 break;
4314 case write_pending:
4315 case active_idle:
4316 /* these cannot be set */
4317 break;
4318 }
NeilBrown67918752014-12-15 12:57:01 +11004319
4320 if (!err) {
NeilBrown1d23f172011-12-08 15:49:12 +11004321 if (mddev->hold_active == UNTIL_IOCTL)
4322 mddev->hold_active = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10004323 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004324 }
NeilBrown67918752014-12-15 12:57:01 +11004325 mddev_unlock(mddev);
4326 return err ?: len;
NeilBrown9e653b62006-06-26 00:27:58 -07004327}
NeilBrown80ca3a42006-07-10 04:44:18 -07004328static struct md_sysfs_entry md_array_state =
NeilBrown750f1992014-09-30 08:53:05 +10004329__ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07004330
NeilBrown6d7ff7382006-01-06 00:21:16 -08004331static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004332max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11004333 return sprintf(page, "%d\n",
4334 atomic_read(&mddev->max_corr_read_errors));
4335}
4336
4337static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004338max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11004339{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004340 unsigned int n;
4341 int rv;
Robert Becker1e509152009-12-14 12:49:58 +11004342
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004343 rv = kstrtouint(buf, 10, &n);
4344 if (rv < 0)
4345 return rv;
4346 atomic_set(&mddev->max_corr_read_errors, n);
4347 return len;
Robert Becker1e509152009-12-14 12:49:58 +11004348}
4349
4350static struct md_sysfs_entry max_corr_read_errors =
4351__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4352 max_corrected_read_errors_store);
4353
4354static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004355null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004356{
4357 return -EINVAL;
4358}
4359
4360static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004361new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff7382006-01-06 00:21:16 -08004362{
4363 /* buf must be %d:%d\n? giving major and minor numbers */
4364 /* The new device is added to the array.
4365 * If the array has a persistent superblock, we read the
4366 * superblock to initialise info and check validity.
4367 * Otherwise, only checking done is that in bind_rdev_to_array,
4368 * which mainly checks size.
4369 */
4370 char *e;
4371 int major = simple_strtoul(buf, &e, 10);
4372 int minor;
4373 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11004374 struct md_rdev *rdev;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004375 int err;
4376
4377 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4378 return -EINVAL;
4379 minor = simple_strtoul(e+1, &e, 10);
4380 if (*e && *e != '\n')
4381 return -EINVAL;
4382 dev = MKDEV(major, minor);
4383 if (major != MAJOR(dev) ||
4384 minor != MINOR(dev))
4385 return -EOVERFLOW;
4386
NeilBrown67918752014-12-15 12:57:01 +11004387 flush_workqueue(md_misc_wq);
4388
4389 err = mddev_lock(mddev);
4390 if (err)
4391 return err;
NeilBrown6d7ff7382006-01-06 00:21:16 -08004392 if (mddev->persistent) {
4393 rdev = md_import_device(dev, mddev->major_version,
4394 mddev->minor_version);
4395 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11004396 struct md_rdev *rdev0
4397 = list_entry(mddev->disks.next,
4398 struct md_rdev, same_set);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004399 err = super_types[mddev->major_version]
4400 .load_super(rdev, rdev0, mddev->minor_version);
4401 if (err < 0)
4402 goto out;
4403 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08004404 } else if (mddev->external)
4405 rdev = md_import_device(dev, -2, -1);
4406 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08004407 rdev = md_import_device(dev, -1, -1);
4408
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004409 if (IS_ERR(rdev)) {
4410 mddev_unlock(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004411 return PTR_ERR(rdev);
NeilBrown9a8c0fa2015-06-25 17:06:40 +10004412 }
NeilBrown6d7ff7382006-01-06 00:21:16 -08004413 err = bind_rdev_to_array(rdev, mddev);
4414 out:
4415 if (err)
4416 export_rdev(rdev);
NeilBrown67918752014-12-15 12:57:01 +11004417 mddev_unlock(mddev);
Alexey Obitotskiy5492c462017-07-28 15:49:25 +02004418 if (!err)
4419 md_new_event(mddev);
NeilBrown6d7ff7382006-01-06 00:21:16 -08004420 return err ? err : len;
4421}
4422
4423static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07004424__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08004425
4426static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004427bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07004428{
4429 char *end;
4430 unsigned long chunk, end_chunk;
NeilBrown67918752014-12-15 12:57:01 +11004431 int err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004432
NeilBrown67918752014-12-15 12:57:01 +11004433 err = mddev_lock(mddev);
4434 if (err)
4435 return err;
Paul Clements9b1d1da2006-10-03 01:15:49 -07004436 if (!mddev->bitmap)
4437 goto out;
4438 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4439 while (*buf) {
4440 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4441 if (buf == end) break;
4442 if (*end == '-') { /* range */
4443 buf = end + 1;
4444 end_chunk = simple_strtoul(buf, &end, 0);
4445 if (buf == end) break;
4446 }
4447 if (*end && !isspace(*end)) break;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004448 md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08004449 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004450 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004451 md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
Paul Clements9b1d1da2006-10-03 01:15:49 -07004452out:
NeilBrown67918752014-12-15 12:57:01 +11004453 mddev_unlock(mddev);
Paul Clements9b1d1da2006-10-03 01:15:49 -07004454 return len;
4455}
4456
4457static struct md_sysfs_entry md_bitmap =
4458__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4459
4460static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004461size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08004462{
Andre Noll58c0fed2009-03-31 14:33:13 +11004463 return sprintf(page, "%llu\n",
4464 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08004465}
4466
NeilBrownfd01b882011-10-11 16:47:53 +11004467static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004468
4469static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004470size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08004471{
4472 /* If array is inactive, we can reduce the component size, but
4473 * not increase it (except from 0).
4474 * If array is active, we can try an on-line resize
4475 */
Dan Williamsb522adc2009-03-31 15:00:31 +11004476 sector_t sectors;
4477 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004478
Andre Noll58c0fed2009-03-31 14:33:13 +11004479 if (err < 0)
4480 return err;
NeilBrown67918752014-12-15 12:57:01 +11004481 err = mddev_lock(mddev);
4482 if (err)
4483 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08004484 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004485 err = update_size(mddev, sectors);
Xiao Ni4ba1e782016-06-12 17:18:00 +08004486 if (err == 0)
4487 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08004488 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11004489 if (mddev->dev_sectors == 0 ||
4490 mddev->dev_sectors > sectors)
4491 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08004492 else
4493 err = -ENOSPC;
4494 }
NeilBrown67918752014-12-15 12:57:01 +11004495 mddev_unlock(mddev);
NeilBrowna35b0d62006-01-06 00:20:49 -08004496 return err ? err : len;
4497}
4498
4499static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07004500__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08004501
Masanari Iida83f0d772012-10-30 00:18:08 +09004502/* Metadata version.
NeilBrowne6910632008-02-06 01:39:51 -08004503 * This is one of
4504 * 'none' for arrays with no metadata (good luck...)
4505 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004506 * or N.M for internally known formats
4507 */
4508static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004509metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004510{
4511 if (mddev->persistent)
4512 return sprintf(page, "%d.%d\n",
4513 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08004514 else if (mddev->external)
4515 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004516 else
4517 return sprintf(page, "none\n");
4518}
4519
4520static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004521metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004522{
4523 int major, minor;
4524 char *e;
NeilBrown67918752014-12-15 12:57:01 +11004525 int err;
NeilBrownea43ddd2008-10-13 11:55:11 +11004526 /* Changing the details of 'external' metadata is
4527 * always permitted. Otherwise there must be
4528 * no devices attached to the array.
4529 */
NeilBrown67918752014-12-15 12:57:01 +11004530
4531 err = mddev_lock(mddev);
4532 if (err)
4533 return err;
4534 err = -EBUSY;
NeilBrownea43ddd2008-10-13 11:55:11 +11004535 if (mddev->external && strncmp(buf, "external:", 9) == 0)
4536 ;
4537 else if (!list_empty(&mddev->disks))
NeilBrown67918752014-12-15 12:57:01 +11004538 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004539
NeilBrown67918752014-12-15 12:57:01 +11004540 err = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004541 if (cmd_match(buf, "none")) {
4542 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08004543 mddev->external = 0;
4544 mddev->major_version = 0;
4545 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004546 goto out_unlock;
NeilBrowne6910632008-02-06 01:39:51 -08004547 }
4548 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08004549 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08004550 if (namelen >= sizeof(mddev->metadata_type))
4551 namelen = sizeof(mddev->metadata_type)-1;
4552 strncpy(mddev->metadata_type, buf+9, namelen);
4553 mddev->metadata_type[namelen] = 0;
4554 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4555 mddev->metadata_type[--namelen] = 0;
4556 mddev->persistent = 0;
4557 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004558 mddev->major_version = 0;
4559 mddev->minor_version = 90;
NeilBrown67918752014-12-15 12:57:01 +11004560 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004561 }
4562 major = simple_strtoul(buf, &e, 10);
NeilBrown67918752014-12-15 12:57:01 +11004563 err = -EINVAL;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004564 if (e==buf || *e != '.')
NeilBrown67918752014-12-15 12:57:01 +11004565 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004566 buf = e+1;
4567 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004568 if (e==buf || (*e && *e != '\n') )
NeilBrown67918752014-12-15 12:57:01 +11004569 goto out_unlock;
4570 err = -ENOENT;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004571 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004572 goto out_unlock;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004573 mddev->major_version = major;
4574 mddev->minor_version = minor;
4575 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004576 mddev->external = 0;
NeilBrown67918752014-12-15 12:57:01 +11004577 err = 0;
4578out_unlock:
4579 mddev_unlock(mddev);
4580 return err ?: len;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004581}
4582
4583static struct md_sysfs_entry md_metadata =
NeilBrown750f1992014-09-30 08:53:05 +10004584__ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004585
NeilBrowna35b0d62006-01-06 00:20:49 -08004586static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004587action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004588{
NeilBrown7eec3142005-11-08 21:39:44 -08004589 char *type = "idle";
NeilBrownb7b17c92014-12-15 12:56:59 +11004590 unsigned long recovery = mddev->recovery;
4591 if (test_bit(MD_RECOVERY_FROZEN, &recovery))
NeilBrownb6a9ce62009-05-26 09:41:17 +10004592 type = "frozen";
NeilBrownb7b17c92014-12-15 12:56:59 +11004593 else if (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
4594 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) {
4595 if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
NeilBrownccfcc3c2006-03-27 01:18:09 -08004596 type = "reshape";
NeilBrownb7b17c92014-12-15 12:56:59 +11004597 else if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
4598 if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004599 type = "resync";
NeilBrownb7b17c92014-12-15 12:56:59 +11004600 else if (test_bit(MD_RECOVERY_CHECK, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004601 type = "check";
4602 else
4603 type = "repair";
NeilBrownb7b17c92014-12-15 12:56:59 +11004604 } else if (test_bit(MD_RECOVERY_RECOVER, &recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004605 type = "recover";
NeilBrown985ca972015-07-06 12:26:57 +10004606 else if (mddev->reshape_position != MaxSector)
4607 type = "reshape";
NeilBrown24dd4692005-11-08 21:39:26 -08004608 }
4609 return sprintf(page, "%s\n", type);
4610}
4611
4612static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004613action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004614{
NeilBrown7eec3142005-11-08 21:39:44 -08004615 if (!mddev->pers || !mddev->pers->sync_request)
4616 return -EINVAL;
4617
NeilBrownb6a9ce62009-05-26 09:41:17 +10004618
4619 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004620 if (cmd_match(page, "frozen"))
4621 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4622 else
4623 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown8e8e2512015-06-12 19:51:27 +10004624 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4625 mddev_lock(mddev) == 0) {
4626 flush_workqueue(md_misc_wq);
4627 if (mddev->sync_thread) {
4628 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown67918752014-12-15 12:57:01 +11004629 md_reap_sync_thread(mddev);
NeilBrown67918752014-12-15 12:57:01 +11004630 }
NeilBrown8e8e2512015-06-12 19:51:27 +10004631 mddev_unlock(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004632 }
NeilBrown312045e2015-12-21 11:01:21 +11004633 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004634 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004635 else if (cmd_match(page, "resync"))
NeilBrown56ccc112015-05-28 17:53:29 +10004636 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004637 else if (cmd_match(page, "recover")) {
NeilBrown56ccc112015-05-28 17:53:29 +10004638 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004639 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004640 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004641 int err;
4642 if (mddev->pers->start_reshape == NULL)
4643 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11004644 err = mddev_lock(mddev);
4645 if (!err) {
NeilBrown312045e2015-12-21 11:01:21 +11004646 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4647 err = -EBUSY;
4648 else {
4649 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4650 err = mddev->pers->start_reshape(mddev);
4651 }
NeilBrown67918752014-12-15 12:57:01 +11004652 mddev_unlock(mddev);
4653 }
NeilBrown16484bf2006-03-27 01:18:13 -08004654 if (err)
4655 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004656 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004657 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004658 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004659 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004660 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004661 return -EINVAL;
NeilBrown56ccc112015-05-28 17:53:29 +10004662 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004663 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4664 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004665 }
NeilBrown48c26dd2012-10-11 14:19:39 +11004666 if (mddev->ro == 2) {
4667 /* A write to sync_action is enough to justify
4668 * canceling read-auto mode
4669 */
4670 mddev->ro = 0;
4671 md_wakeup_thread(mddev->sync_thread);
4672 }
NeilBrown03c902e2006-01-06 00:20:46 -08004673 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004674 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004675 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004676 return len;
4677}
4678
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004679static struct md_sysfs_entry md_scan_mode =
NeilBrown750f1992014-09-30 08:53:05 +10004680__ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
Jonathan Brassowc4a39552013-06-25 01:23:59 -05004681
4682static ssize_t
4683last_sync_action_show(struct mddev *mddev, char *page)
4684{
4685 return sprintf(page, "%s\n", mddev->last_sync_action);
4686}
4687
4688static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4689
NeilBrown9d888832005-11-08 21:39:26 -08004690static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004691mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004692{
4693 return sprintf(page, "%llu\n",
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004694 (unsigned long long)
4695 atomic64_read(&mddev->resync_mismatches));
NeilBrown9d888832005-11-08 21:39:26 -08004696}
4697
NeilBrown80ca3a42006-07-10 04:44:18 -07004698static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004699
NeilBrown88202a02006-01-06 00:21:36 -08004700static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004701sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004702{
4703 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4704 mddev->sync_speed_min ? "local": "system");
4705}
4706
4707static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004708sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004709{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004710 unsigned int min;
4711 int rv;
4712
NeilBrown88202a02006-01-06 00:21:36 -08004713 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004714 min = 0;
4715 } else {
4716 rv = kstrtouint(buf, 10, &min);
4717 if (rv < 0)
4718 return rv;
4719 if (min == 0)
4720 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004721 }
NeilBrown88202a02006-01-06 00:21:36 -08004722 mddev->sync_speed_min = min;
4723 return len;
4724}
4725
4726static struct md_sysfs_entry md_sync_min =
4727__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4728
4729static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004730sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004731{
4732 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4733 mddev->sync_speed_max ? "local": "system");
4734}
4735
4736static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004737sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004738{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004739 unsigned int max;
4740 int rv;
4741
NeilBrown88202a02006-01-06 00:21:36 -08004742 if (strncmp(buf, "system", 6)==0) {
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004743 max = 0;
4744 } else {
4745 rv = kstrtouint(buf, 10, &max);
4746 if (rv < 0)
4747 return rv;
4748 if (max == 0)
4749 return -EINVAL;
NeilBrown88202a02006-01-06 00:21:36 -08004750 }
NeilBrown88202a02006-01-06 00:21:36 -08004751 mddev->sync_speed_max = max;
4752 return len;
4753}
4754
4755static struct md_sysfs_entry md_sync_max =
4756__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4757
Iustin Popd7f3d292007-10-16 23:30:54 -07004758static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004759degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004760{
4761 return sprintf(page, "%d\n", mddev->degraded);
4762}
4763static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004764
4765static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004766sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004767{
4768 return sprintf(page, "%d\n", mddev->parallel_resync);
4769}
4770
4771static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004772sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004773{
4774 long n;
4775
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004776 if (kstrtol(buf, 10, &n))
Bernd Schubert90b08712008-05-23 13:04:38 -07004777 return -EINVAL;
4778
4779 if (n != 0 && n != 1)
4780 return -EINVAL;
4781
4782 mddev->parallel_resync = n;
4783
4784 if (mddev->sync_thread)
4785 wake_up(&resync_wait);
4786
4787 return len;
4788}
4789
4790/* force parallel resync, even with shared block devices */
4791static struct md_sysfs_entry md_sync_force_parallel =
4792__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4793 sync_force_parallel_show, sync_force_parallel_store);
4794
4795static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004796sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004797{
4798 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004799 if (mddev->curr_resync == 0)
4800 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004801 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4802 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004803 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004804 db = resync - mddev->resync_mark_cnt;
4805 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004806}
4807
NeilBrown80ca3a42006-07-10 04:44:18 -07004808static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004809
4810static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004811sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004812{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004813 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004814
NeilBrownacb180b2009-04-14 16:28:34 +10004815 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4816 return sprintf(page, "none\n");
4817
NeilBrown72f36d52012-10-11 14:25:57 +11004818 if (mddev->curr_resync == 1 ||
4819 mddev->curr_resync == 2)
4820 return sprintf(page, "delayed\n");
4821
NeilBrownc804cde2012-05-21 09:28:33 +10004822 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4823 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004824 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004825 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004826 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004827
NeilBrownacb180b2009-04-14 16:28:34 +10004828 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004829 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004830}
4831
NeilBrown750f1992014-09-30 08:53:05 +10004832static struct md_sysfs_entry md_sync_completed =
4833 __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL);
NeilBrown88202a02006-01-06 00:21:36 -08004834
NeilBrowne464eaf2006-03-27 01:18:14 -08004835static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004836min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004837{
4838 return sprintf(page, "%llu\n",
4839 (unsigned long long)mddev->resync_min);
4840}
4841static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004842min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004843{
4844 unsigned long long min;
NeilBrown23da4222014-12-15 12:57:01 +11004845 int err;
NeilBrown23da4222014-12-15 12:57:01 +11004846
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004847 if (kstrtoull(buf, 10, &min))
Neil Brown5e96ee62008-06-28 08:31:24 +10004848 return -EINVAL;
NeilBrown23da4222014-12-15 12:57:01 +11004849
4850 spin_lock(&mddev->lock);
4851 err = -EINVAL;
Neil Brown5e96ee62008-06-28 08:31:24 +10004852 if (min > mddev->resync_max)
NeilBrown23da4222014-12-15 12:57:01 +11004853 goto out_unlock;
4854
4855 err = -EBUSY;
Neil Brown5e96ee62008-06-28 08:31:24 +10004856 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004857 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004858
NeilBrown50c37b12015-03-23 17:36:38 +11004859 /* Round down to multiple of 4K for safety */
4860 mddev->resync_min = round_down(min, 8);
NeilBrown23da4222014-12-15 12:57:01 +11004861 err = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004862
NeilBrown23da4222014-12-15 12:57:01 +11004863out_unlock:
4864 spin_unlock(&mddev->lock);
4865 return err ?: len;
Neil Brown5e96ee62008-06-28 08:31:24 +10004866}
4867
4868static struct md_sysfs_entry md_min_sync =
4869__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4870
4871static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004872max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004873{
4874 if (mddev->resync_max == MaxSector)
4875 return sprintf(page, "max\n");
4876 else
4877 return sprintf(page, "%llu\n",
4878 (unsigned long long)mddev->resync_max);
4879}
4880static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004881max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004882{
NeilBrown23da4222014-12-15 12:57:01 +11004883 int err;
4884 spin_lock(&mddev->lock);
NeilBrownc6207272008-02-06 01:39:52 -08004885 if (strncmp(buf, "max", 3) == 0)
4886 mddev->resync_max = MaxSector;
4887 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004888 unsigned long long max;
NeilBrown23da4222014-12-15 12:57:01 +11004889 int chunk;
4890
4891 err = -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09004892 if (kstrtoull(buf, 10, &max))
NeilBrown23da4222014-12-15 12:57:01 +11004893 goto out_unlock;
Neil Brown5e96ee62008-06-28 08:31:24 +10004894 if (max < mddev->resync_min)
NeilBrown23da4222014-12-15 12:57:01 +11004895 goto out_unlock;
4896
4897 err = -EBUSY;
NeilBrownc6207272008-02-06 01:39:52 -08004898 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004899 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004900 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown23da4222014-12-15 12:57:01 +11004901 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004902
4903 /* Must be a multiple of chunk_size */
NeilBrown23da4222014-12-15 12:57:01 +11004904 chunk = mddev->chunk_sectors;
4905 if (chunk) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004906 sector_t temp = max;
NeilBrown23da4222014-12-15 12:57:01 +11004907
4908 err = -EINVAL;
4909 if (sector_div(temp, chunk))
4910 goto out_unlock;
NeilBrownc6207272008-02-06 01:39:52 -08004911 }
4912 mddev->resync_max = max;
4913 }
4914 wake_up(&mddev->recovery_wait);
NeilBrown23da4222014-12-15 12:57:01 +11004915 err = 0;
4916out_unlock:
4917 spin_unlock(&mddev->lock);
4918 return err ?: len;
NeilBrownc6207272008-02-06 01:39:52 -08004919}
4920
4921static struct md_sysfs_entry md_max_sync =
4922__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4923
4924static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004925suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004926{
4927 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4928}
4929
4930static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004931suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004932{
NeilBrownb03e0cc2017-10-19 12:49:15 +11004933 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004934 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004935
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004936 err = kstrtoull(buf, 10, &new);
4937 if (err < 0)
4938 return err;
4939 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004940 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004941
NeilBrown67918752014-12-15 12:57:01 +11004942 err = mddev_lock(mddev);
4943 if (err)
4944 return err;
4945 err = -EINVAL;
4946 if (mddev->pers == NULL ||
4947 mddev->pers->quiesce == NULL)
4948 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004949 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004950 mddev->suspend_lo = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004951 mddev_resume(mddev);
4952
NeilBrown67918752014-12-15 12:57:01 +11004953 err = 0;
4954unlock:
4955 mddev_unlock(mddev);
4956 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004957}
4958static struct md_sysfs_entry md_suspend_lo =
4959__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4960
NeilBrowne464eaf2006-03-27 01:18:14 -08004961static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004962suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004963{
4964 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4965}
4966
4967static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004968suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004969{
NeilBrownb03e0cc2017-10-19 12:49:15 +11004970 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11004971 int err;
NeilBrowne464eaf2006-03-27 01:18:14 -08004972
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03004973 err = kstrtoull(buf, 10, &new);
4974 if (err < 0)
4975 return err;
4976 if (new != (sector_t)new)
NeilBrowne464eaf2006-03-27 01:18:14 -08004977 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004978
NeilBrown67918752014-12-15 12:57:01 +11004979 err = mddev_lock(mddev);
4980 if (err)
4981 return err;
4982 err = -EINVAL;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004983 if (mddev->pers == NULL)
NeilBrown67918752014-12-15 12:57:01 +11004984 goto unlock;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004985
4986 mddev_suspend(mddev);
NeilBrown23ddff32011-01-14 09:14:34 +11004987 mddev->suspend_hi = new;
NeilBrownb03e0cc2017-10-19 12:49:15 +11004988 mddev_resume(mddev);
4989
NeilBrown67918752014-12-15 12:57:01 +11004990 err = 0;
4991unlock:
4992 mddev_unlock(mddev);
4993 return err ?: len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004994}
4995static struct md_sysfs_entry md_suspend_hi =
4996__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4997
NeilBrown08a02ec2007-05-09 02:35:38 -07004998static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004999reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07005000{
5001 if (mddev->reshape_position != MaxSector)
5002 return sprintf(page, "%llu\n",
5003 (unsigned long long)mddev->reshape_position);
5004 strcpy(page, "none\n");
5005 return 5;
5006}
5007
5008static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005009reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07005010{
NeilBrownc6563a82012-05-21 09:27:00 +10005011 struct md_rdev *rdev;
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005012 unsigned long long new;
NeilBrown67918752014-12-15 12:57:01 +11005013 int err;
NeilBrown67918752014-12-15 12:57:01 +11005014
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03005015 err = kstrtoull(buf, 10, &new);
5016 if (err < 0)
5017 return err;
5018 if (new != (sector_t)new)
NeilBrown08a02ec2007-05-09 02:35:38 -07005019 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11005020 err = mddev_lock(mddev);
5021 if (err)
5022 return err;
5023 err = -EBUSY;
5024 if (mddev->pers)
5025 goto unlock;
NeilBrown08a02ec2007-05-09 02:35:38 -07005026 mddev->reshape_position = new;
5027 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005028 mddev->reshape_backwards = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07005029 mddev->new_level = mddev->level;
5030 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10005031 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownc6563a82012-05-21 09:27:00 +10005032 rdev_for_each(rdev, mddev)
5033 rdev->new_data_offset = rdev->data_offset;
NeilBrown67918752014-12-15 12:57:01 +11005034 err = 0;
5035unlock:
5036 mddev_unlock(mddev);
5037 return err ?: len;
NeilBrown08a02ec2007-05-09 02:35:38 -07005038}
5039
5040static struct md_sysfs_entry md_reshape_position =
5041__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
5042 reshape_position_store);
5043
Dan Williamsb522adc2009-03-31 15:00:31 +11005044static ssize_t
NeilBrown2c810cd2012-05-21 09:27:00 +10005045reshape_direction_show(struct mddev *mddev, char *page)
5046{
5047 return sprintf(page, "%s\n",
5048 mddev->reshape_backwards ? "backwards" : "forwards");
5049}
5050
5051static ssize_t
5052reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
5053{
5054 int backwards = 0;
NeilBrown67918752014-12-15 12:57:01 +11005055 int err;
5056
NeilBrown2c810cd2012-05-21 09:27:00 +10005057 if (cmd_match(buf, "forwards"))
5058 backwards = 0;
5059 else if (cmd_match(buf, "backwards"))
5060 backwards = 1;
5061 else
5062 return -EINVAL;
5063 if (mddev->reshape_backwards == backwards)
5064 return len;
5065
NeilBrown67918752014-12-15 12:57:01 +11005066 err = mddev_lock(mddev);
5067 if (err)
5068 return err;
NeilBrown2c810cd2012-05-21 09:27:00 +10005069 /* check if we are allowed to change */
5070 if (mddev->delta_disks)
NeilBrown67918752014-12-15 12:57:01 +11005071 err = -EBUSY;
5072 else if (mddev->persistent &&
NeilBrown2c810cd2012-05-21 09:27:00 +10005073 mddev->major_version == 0)
NeilBrown67918752014-12-15 12:57:01 +11005074 err = -EINVAL;
5075 else
5076 mddev->reshape_backwards = backwards;
5077 mddev_unlock(mddev);
5078 return err ?: len;
NeilBrown2c810cd2012-05-21 09:27:00 +10005079}
5080
5081static struct md_sysfs_entry md_reshape_direction =
5082__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
5083 reshape_direction_store);
5084
5085static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005086array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11005087{
5088 if (mddev->external_size)
5089 return sprintf(page, "%llu\n",
5090 (unsigned long long)mddev->array_sectors/2);
5091 else
5092 return sprintf(page, "default\n");
5093}
5094
5095static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005096array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11005097{
5098 sector_t sectors;
NeilBrown67918752014-12-15 12:57:01 +11005099 int err;
5100
5101 err = mddev_lock(mddev);
5102 if (err)
5103 return err;
Dan Williamsb522adc2009-03-31 15:00:31 +11005104
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005105 /* cluster raid doesn't support change array_sectors */
Zhilong Liub6708832017-04-10 14:15:55 +08005106 if (mddev_is_clustered(mddev)) {
5107 mddev_unlock(mddev);
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005108 return -EINVAL;
Zhilong Liub6708832017-04-10 14:15:55 +08005109 }
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04005110
Dan Williamsb522adc2009-03-31 15:00:31 +11005111 if (strncmp(buf, "default", 7) == 0) {
5112 if (mddev->pers)
5113 sectors = mddev->pers->size(mddev, 0, 0);
5114 else
5115 sectors = mddev->array_sectors;
5116
5117 mddev->external_size = 0;
5118 } else {
5119 if (strict_blocks_to_sectors(buf, &sectors) < 0)
NeilBrown67918752014-12-15 12:57:01 +11005120 err = -EINVAL;
5121 else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
5122 err = -E2BIG;
5123 else
5124 mddev->external_size = 1;
Dan Williamsb522adc2009-03-31 15:00:31 +11005125 }
5126
NeilBrown67918752014-12-15 12:57:01 +11005127 if (!err) {
5128 mddev->array_sectors = sectors;
5129 if (mddev->pers) {
5130 set_capacity(mddev->gendisk, mddev->array_sectors);
5131 revalidate_disk(mddev->gendisk);
5132 }
NeilBrowncbe6ef12011-02-16 13:58:38 +11005133 }
NeilBrown67918752014-12-15 12:57:01 +11005134 mddev_unlock(mddev);
5135 return err ?: len;
Dan Williamsb522adc2009-03-31 15:00:31 +11005136}
5137
5138static struct md_sysfs_entry md_array_size =
5139__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
5140 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08005141
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005142static ssize_t
5143consistency_policy_show(struct mddev *mddev, char *page)
5144{
5145 int ret;
5146
5147 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
5148 ret = sprintf(page, "journal\n");
5149 } else if (test_bit(MD_HAS_PPL, &mddev->flags)) {
5150 ret = sprintf(page, "ppl\n");
5151 } else if (mddev->bitmap) {
5152 ret = sprintf(page, "bitmap\n");
5153 } else if (mddev->pers) {
5154 if (mddev->pers->sync_request)
5155 ret = sprintf(page, "resync\n");
5156 else
5157 ret = sprintf(page, "none\n");
5158 } else {
5159 ret = sprintf(page, "unknown\n");
5160 }
5161
5162 return ret;
5163}
5164
5165static ssize_t
5166consistency_policy_store(struct mddev *mddev, const char *buf, size_t len)
5167{
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005168 int err = 0;
5169
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005170 if (mddev->pers) {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005171 if (mddev->pers->change_consistency_policy)
5172 err = mddev->pers->change_consistency_policy(mddev, buf);
5173 else
5174 err = -EBUSY;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005175 } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) {
5176 set_bit(MD_HAS_PPL, &mddev->flags);
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005177 } else {
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005178 err = -EINVAL;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005179 }
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01005180
5181 return err ? err : len;
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005182}
5183
5184static struct md_sysfs_entry md_consistency_policy =
5185__ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show,
5186 consistency_policy_store);
5187
Guoqing Jiang9a567842019-07-24 11:09:19 +02005188static ssize_t fail_last_dev_show(struct mddev *mddev, char *page)
5189{
5190 return sprintf(page, "%d\n", mddev->fail_last_dev);
5191}
5192
5193/*
5194 * Setting fail_last_dev to true to allow last device to be forcibly removed
5195 * from RAID1/RAID10.
5196 */
5197static ssize_t
5198fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len)
5199{
5200 int ret;
5201 bool value;
5202
5203 ret = kstrtobool(buf, &value);
5204 if (ret)
5205 return ret;
5206
5207 if (value != mddev->fail_last_dev)
5208 mddev->fail_last_dev = value;
5209
5210 return len;
5211}
5212static struct md_sysfs_entry md_fail_last_dev =
5213__ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show,
5214 fail_last_dev_store);
5215
NeilBrowneae17012005-11-08 21:39:23 -08005216static struct attribute *md_default_attrs[] = {
5217 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07005218 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005219 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08005220 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08005221 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07005222 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08005223 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08005224 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07005225 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07005226 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07005227 &md_reshape_position.attr,
NeilBrown2c810cd2012-05-21 09:27:00 +10005228 &md_reshape_direction.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11005229 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11005230 &max_corr_read_errors.attr,
Artur Paszkiewicz664aed02017-03-09 10:00:00 +01005231 &md_consistency_policy.attr,
Guoqing Jiang9a567842019-07-24 11:09:19 +02005232 &md_fail_last_dev.attr,
NeilBrown411036f2005-11-08 21:39:40 -08005233 NULL,
5234};
5235
5236static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08005237 &md_scan_mode.attr,
Jonathan Brassowc4a39552013-06-25 01:23:59 -05005238 &md_last_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08005239 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005240 &md_sync_min.attr,
5241 &md_sync_max.attr,
5242 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07005243 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08005244 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10005245 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08005246 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08005247 &md_suspend_lo.attr,
5248 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07005249 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07005250 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08005251 NULL,
5252};
NeilBrown411036f2005-11-08 21:39:40 -08005253static struct attribute_group md_redundancy_group = {
5254 .name = NULL,
5255 .attrs = md_redundancy_attrs,
5256};
5257
NeilBrowneae17012005-11-08 21:39:23 -08005258static ssize_t
5259md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
5260{
5261 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005262 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005263 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005264
5265 if (!entry->show)
5266 return -EIO;
NeilBrownaf8a2432011-12-08 15:49:46 +11005267 spin_lock(&all_mddevs_lock);
5268 if (list_empty(&mddev->all_mddevs)) {
5269 spin_unlock(&all_mddevs_lock);
5270 return -EBUSY;
5271 }
5272 mddev_get(mddev);
5273 spin_unlock(&all_mddevs_lock);
5274
NeilBrownb7b17c92014-12-15 12:56:59 +11005275 rv = entry->show(mddev, page);
NeilBrownaf8a2432011-12-08 15:49:46 +11005276 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005277 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005278}
5279
5280static ssize_t
5281md_attr_store(struct kobject *kobj, struct attribute *attr,
5282 const char *page, size_t length)
5283{
5284 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11005285 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08005286 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08005287
5288 if (!entry->store)
5289 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07005290 if (!capable(CAP_SYS_ADMIN))
5291 return -EACCES;
NeilBrownaf8a2432011-12-08 15:49:46 +11005292 spin_lock(&all_mddevs_lock);
5293 if (list_empty(&mddev->all_mddevs)) {
5294 spin_unlock(&all_mddevs_lock);
5295 return -EBUSY;
5296 }
5297 mddev_get(mddev);
5298 spin_unlock(&all_mddevs_lock);
NeilBrown67918752014-12-15 12:57:01 +11005299 rv = entry->store(mddev, page, length);
NeilBrownaf8a2432011-12-08 15:49:46 +11005300 mddev_put(mddev);
NeilBrown96de1e62005-11-08 21:39:39 -08005301 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08005302}
5303
5304static void md_free(struct kobject *ko)
5305{
NeilBrownfd01b882011-10-11 16:47:53 +11005306 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11005307
5308 if (mddev->sysfs_state)
5309 sysfs_put(mddev->sysfs_state);
5310
Bart Van Assched8115c352018-02-28 10:15:29 -08005311 if (mddev->gendisk)
5312 del_gendisk(mddev->gendisk);
NeilBrown6cd18e72015-04-27 14:12:22 +10005313 if (mddev->queue)
5314 blk_cleanup_queue(mddev->queue);
Bart Van Assched8115c352018-02-28 10:15:29 -08005315 if (mddev->gendisk)
NeilBrowna21d1502009-01-09 08:31:09 +11005316 put_disk(mddev->gendisk);
NeilBrown4ad23a972017-03-15 14:05:14 +11005317 percpu_ref_exit(&mddev->writes_pending);
NeilBrowna21d1502009-01-09 08:31:09 +11005318
Kent Overstreet28dec872018-06-07 20:52:54 -04005319 bioset_exit(&mddev->bio_set);
5320 bioset_exit(&mddev->sync_set);
NeilBrowneae17012005-11-08 21:39:23 -08005321 kfree(mddev);
5322}
5323
Emese Revfy52cf25d2010-01-19 02:58:23 +01005324static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08005325 .show = md_attr_show,
5326 .store = md_attr_store,
5327};
5328static struct kobj_type md_ktype = {
5329 .release = md_free,
5330 .sysfs_ops = &md_sysfs_ops,
5331 .default_attrs = md_default_attrs,
5332};
5333
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334int mdp_major = 0;
5335
Dan Williams5fd3a172009-03-04 00:57:25 -07005336static void mddev_delayed_delete(struct work_struct *ws)
5337{
NeilBrownfd01b882011-10-11 16:47:53 +11005338 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07005339
NeilBrown43a70502009-12-14 12:49:55 +11005340 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07005341 kobject_del(&mddev->kobj);
5342 kobject_put(&mddev->kobj);
5343}
5344
NeilBrown4ad23a972017-03-15 14:05:14 +11005345static void no_op(struct percpu_ref *r) {}
5346
NeilBrowna415c0f2017-06-05 16:05:13 +10005347int mddev_init_writes_pending(struct mddev *mddev)
5348{
5349 if (mddev->writes_pending.percpu_count_ptr)
5350 return 0;
Roman Gushchinddde2af2019-05-07 10:01:49 -07005351 if (percpu_ref_init(&mddev->writes_pending, no_op,
5352 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0)
NeilBrowna415c0f2017-06-05 16:05:13 +10005353 return -ENOMEM;
5354 /* We want to start with the refcount at zero */
5355 percpu_ref_put(&mddev->writes_pending);
5356 return 0;
5357}
5358EXPORT_SYMBOL_GPL(mddev_init_writes_pending);
5359
NeilBrownefeb53c2009-01-09 08:31:10 +11005360static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361{
NeilBrown039b7222017-04-12 16:26:13 +10005362 /*
5363 * If dev is zero, name is the name of a device to allocate with
5364 * an arbitrary minor number. It will be "md_???"
5365 * If dev is non-zero it must be a device number with a MAJOR of
5366 * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then
5367 * the device is being created by opening a node in /dev.
5368 * If "name" is not NULL, the device is being created by
5369 * writing to /sys/module/md_mod/parameters/new_array.
5370 */
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005371 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11005372 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11005374 int partitioned;
5375 int shift;
5376 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005377 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
5379 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11005380 return -ENODEV;
5381
5382 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
5383 shift = partitioned ? MdpMinorShift : 0;
5384 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
Tejun Heoe804ac72010-10-15 15:36:08 +02005386 /* wait for any previous instance of this device to be
5387 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11005388 */
Tejun Heoe804ac72010-10-15 15:36:08 +02005389 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005390
Arjan van de Ven48c9c272006-03-27 01:18:20 -08005391 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005392 error = -EEXIST;
5393 if (mddev->gendisk)
5394 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005395
NeilBrown039b7222017-04-12 16:26:13 +10005396 if (name && !dev) {
NeilBrownefeb53c2009-01-09 08:31:10 +11005397 /* Need to ensure that 'name' is not a duplicate.
5398 */
NeilBrownfd01b882011-10-11 16:47:53 +11005399 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11005400 spin_lock(&all_mddevs_lock);
5401
5402 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
5403 if (mddev2->gendisk &&
5404 strcmp(mddev2->gendisk->disk_name, name) == 0) {
5405 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10005406 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11005407 }
5408 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 }
NeilBrown039b7222017-04-12 16:26:13 +10005410 if (name && dev)
5411 /*
5412 * Creating /dev/mdNNN via "newarray", so adjust hold_active.
5413 */
5414 mddev->hold_active = UNTIL_STOP;
NeilBrown8b765392009-01-09 08:31:08 +11005415
NeilBrown0909dc42009-07-01 12:27:21 +10005416 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11005417 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10005418 if (!mddev->queue)
5419 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11005420 mddev->queue->queuedata = mddev;
5421
NeilBrown409c57f2009-03-31 14:39:39 +11005422 blk_queue_make_request(mddev->queue, md_make_request);
Martin K. Petersenb1bd0552012-01-11 16:27:11 +01005423 blk_set_stacking_limits(&mddev->queue->limits);
NeilBrown8b765392009-01-09 08:31:08 +11005424
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 disk = alloc_disk(1 << shift);
5426 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11005427 blk_cleanup_queue(mddev->queue);
5428 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10005429 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 }
NeilBrownefeb53c2009-01-09 08:31:10 +11005431 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11005433 if (name)
5434 strcpy(disk->disk_name, name);
5435 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07005437 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 disk->fops = &md_fops;
5440 disk->private_data = mddev;
5441 disk->queue = mddev->queue;
Jens Axboe56883a72016-03-30 10:16:53 -06005442 blk_queue_write_cache(mddev->queue, true, true);
NeilBrown92850bb2008-10-21 13:25:32 +11005443 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11005444 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11005445 * remove it now.
5446 */
5447 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10005449 /* As soon as we call add_disk(), another thread could get
5450 * through to md_open, so make sure it doesn't get too far
5451 */
5452 mutex_lock(&mddev->open_mutex);
5453 add_disk(disk);
5454
Kent Overstreet28dec872018-06-07 20:52:54 -04005455 error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10005456 if (error) {
5457 /* This isn't possible, but as kobject_init_and_add is marked
5458 * __must_check, we must do something with the result
5459 */
NeilBrown9d487392016-11-02 14:16:49 +11005460 pr_debug("md: cannot register %s/md - name in use\n",
5461 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10005462 error = 0;
5463 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005464 if (mddev->kobj.sd &&
5465 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown9d487392016-11-02 14:16:49 +11005466 pr_debug("pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10005467 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10005468 abort:
5469 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005470 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04005471 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005472 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11005473 }
NeilBrownd3374822009-01-09 08:31:10 +11005474 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10005475 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11005476}
5477
5478static struct kobject *md_probe(dev_t dev, int *part, void *data)
5479{
NeilBrown78b63502017-04-12 16:26:13 +10005480 if (create_on_open)
5481 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 return NULL;
5483}
5484
Kees Cooke4dca7b2017-10-17 19:04:42 -07005485static int add_named_array(const char *val, const struct kernel_param *kp)
NeilBrownefeb53c2009-01-09 08:31:10 +11005486{
NeilBrown039b7222017-04-12 16:26:13 +10005487 /*
5488 * val must be "md_*" or "mdNNN".
5489 * For "md_*" we allocate an array with a large free minor number, and
NeilBrownefeb53c2009-01-09 08:31:10 +11005490 * set the name to val. val must not already be an active name.
NeilBrown039b7222017-04-12 16:26:13 +10005491 * For "mdNNN" we allocate an array with the minor number NNN
5492 * which must not already be in use.
NeilBrownefeb53c2009-01-09 08:31:10 +11005493 */
5494 int len = strlen(val);
5495 char buf[DISK_NAME_LEN];
NeilBrown039b7222017-04-12 16:26:13 +10005496 unsigned long devnum;
NeilBrownefeb53c2009-01-09 08:31:10 +11005497
5498 while (len && val[len-1] == '\n')
5499 len--;
5500 if (len >= DISK_NAME_LEN)
5501 return -E2BIG;
5502 strlcpy(buf, val, len+1);
NeilBrown039b7222017-04-12 16:26:13 +10005503 if (strncmp(buf, "md_", 3) == 0)
5504 return md_alloc(0, buf);
5505 if (strncmp(buf, "md", 2) == 0 &&
5506 isdigit(buf[2]) &&
5507 kstrtoul(buf+2, 10, &devnum) == 0 &&
5508 devnum <= MINORMASK)
5509 return md_alloc(MKDEV(MD_MAJOR, devnum), NULL);
5510
5511 return -EINVAL;
NeilBrownefeb53c2009-01-09 08:31:10 +11005512}
5513
Kees Cook8376d3c2017-10-16 17:01:48 -07005514static void md_safemode_timeout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515{
Kees Cook8376d3c2017-10-16 17:01:48 -07005516 struct mddev *mddev = from_timer(mddev, t, safemode_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517
NeilBrown4ad23a972017-03-15 14:05:14 +11005518 mddev->safemode = 1;
5519 if (mddev->external)
5520 sysfs_notify_dirent_safe(mddev->sysfs_state);
5521
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 md_wakeup_thread(mddev->thread);
5523}
5524
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005525static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526
NeilBrownfd01b882011-10-11 16:47:53 +11005527int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528{
NeilBrown2604b702006-01-06 00:20:36 -08005529 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005530 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11005531 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532
NeilBrowna757e642005-04-16 15:26:42 -07005533 if (list_empty(&mddev->disks))
5534 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
5537 if (mddev->pers)
5538 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10005539 /* Cannot run until previous stop completes properly */
5540 if (mddev->sysfs_active)
5541 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10005542
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 /*
5544 * Analyze all RAID superblock(s)
5545 */
NeilBrown1ec4a932008-02-06 01:39:53 -08005546 if (!mddev->raid_disks) {
5547 if (!mddev->persistent)
5548 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07005549 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08005550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551
NeilBrownd9d166c2006-01-06 00:20:51 -08005552 if (mddev->level != LEVEL_NONE)
5553 request_module("md-level-%d", mddev->level);
5554 else if (mddev->clevel[0])
5555 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
5557 /*
5558 * Drop all container device buffers, from now on
5559 * the only valid external interface is through the md
5560 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 */
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005562 mddev->has_superblocks = false;
NeilBrowndafb20f2012-03-19 12:46:39 +11005563 rdev_for_each(rdev, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005564 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 continue;
5566 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07005567 invalidate_bdev(rdev->bdev);
NeilBrown97b20ef2017-04-13 08:53:48 +10005568 if (mddev->ro != 1 &&
5569 (bdev_read_only(rdev->bdev) ||
5570 bdev_read_only(rdev->meta_bdev))) {
5571 mddev->ro = 1;
5572 if (mddev->gendisk)
5573 set_disk_ro(mddev->gendisk, 1);
5574 }
NeilBrownf0d76d72007-07-17 04:06:12 -07005575
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01005576 if (rdev->sb_page)
5577 mddev->has_superblocks = true;
5578
NeilBrownf0d76d72007-07-17 04:06:12 -07005579 /* perform some consistency tests on the device.
5580 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11005581 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07005582 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11005583 if (rdev->meta_bdev) {
5584 /* Nothing to check */;
5585 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005586 if (mddev->dev_sectors &&
5587 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10005588 > rdev->sb_start) {
NeilBrown9d487392016-11-02 14:16:49 +11005589 pr_warn("md: %s: data overlaps metadata\n",
5590 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005591 return -EINVAL;
5592 }
5593 } else {
Andre Noll0f420352008-07-11 22:02:23 +10005594 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07005595 > rdev->data_offset) {
NeilBrown9d487392016-11-02 14:16:49 +11005596 pr_warn("md: %s: metadata overlaps data\n",
5597 mdname(mddev));
NeilBrownf0d76d72007-07-17 04:06:12 -07005598 return -EINVAL;
5599 }
5600 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10005601 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 }
5603
Kent Overstreetafeee512018-05-20 18:25:52 -04005604 if (!bioset_initialized(&mddev->bio_set)) {
5605 err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5606 if (err)
5607 return err;
Ming Lei10273172017-02-14 23:29:00 +08005608 }
Kent Overstreetafeee512018-05-20 18:25:52 -04005609 if (!bioset_initialized(&mddev->sync_set)) {
5610 err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
5611 if (err)
Kent Overstreet28dec872018-06-07 20:52:54 -04005612 return err;
NeilBrown5a850712017-06-21 09:12:21 +10005613 }
NeilBrowna167f662010-10-26 18:31:13 +11005614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005616 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08005617 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08005619 if (mddev->level != LEVEL_NONE)
NeilBrown9d487392016-11-02 14:16:49 +11005620 pr_warn("md: personality for level %d is not loaded!\n",
5621 mddev->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08005622 else
NeilBrown9d487392016-11-02 14:16:49 +11005623 pr_warn("md: personality for level %s is not loaded!\n",
5624 mddev->clevel);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005625 err = -EINVAL;
5626 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11005629 if (mddev->level != pers->level) {
5630 mddev->level = pers->level;
5631 mddev->new_level = pers->level;
5632 }
NeilBrownd9d166c2006-01-06 00:20:51 -08005633 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
NeilBrownf6705572006-03-27 01:18:11 -08005635 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08005636 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08005637 /* This personality cannot handle reshaping... */
NeilBrownf6705572006-03-27 01:18:11 -08005638 module_put(pers->owner);
Shaohua Libfc9dfd2018-06-13 08:39:49 -07005639 err = -EINVAL;
5640 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08005641 }
5642
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005643 if (pers->sync_request) {
5644 /* Warn if this is a potentially silly
5645 * configuration.
5646 */
5647 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005648 struct md_rdev *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005649 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005650
NeilBrowndafb20f2012-03-19 12:46:39 +11005651 rdev_for_each(rdev, mddev)
5652 rdev_for_each(rdev2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005653 if (rdev < rdev2 &&
5654 rdev->bdev->bd_contains ==
5655 rdev2->bdev->bd_contains) {
NeilBrown9d487392016-11-02 14:16:49 +11005656 pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n",
5657 mdname(mddev),
5658 bdevname(rdev->bdev,b),
5659 bdevname(rdev2->bdev,b2));
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005660 warned = 1;
5661 }
5662 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005663
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005664 if (warned)
NeilBrown9d487392016-11-02 14:16:49 +11005665 pr_warn("True protection against single-disk failure might be compromised.\n");
NeilBrown7dd5e7c32007-02-28 20:11:35 -08005666 }
5667
NeilBrown657390d2005-08-26 18:34:16 -07005668 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005669 /* may be over-ridden by personality */
5670 mddev->resync_max_sectors = mddev->dev_sectors;
5671
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005672 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
NeilBrown0f9552b52009-12-30 12:08:50 +11005674 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08005675 mddev->ro = 2; /* read-only, but switch on first write */
5676
NeilBrown36d091f2014-12-15 12:56:58 +11005677 err = pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01005678 if (err)
NeilBrown9d487392016-11-02 14:16:49 +11005679 pr_warn("md: pers->run() failed ...\n");
NeilBrown36d091f2014-12-15 12:56:58 +11005680 else if (pers->size(mddev, 0, 0) < mddev->array_sectors) {
NeilBrown9d487392016-11-02 14:16:49 +11005681 WARN_ONCE(!mddev->external_size,
5682 "%s: default size too small, but 'external_size' not in effect?\n",
5683 __func__);
5684 pr_warn("md: invalid array_size %llu > default size %llu\n",
5685 (unsigned long long)mddev->array_sectors / 2,
5686 (unsigned long long)pers->size(mddev, 0, 0) / 2);
Dan Williamsb522adc2009-03-31 15:00:31 +11005687 err = -EINVAL;
Dan Williamsb522adc2009-03-31 15:00:31 +11005688 }
NeilBrown36d091f2014-12-15 12:56:58 +11005689 if (err == 0 && pers->sync_request &&
NeilBrownef99bf42012-05-22 13:55:08 +10005690 (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005691 struct bitmap *bitmap;
5692
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005693 bitmap = md_bitmap_create(mddev, -1);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005694 if (IS_ERR(bitmap)) {
5695 err = PTR_ERR(bitmap);
NeilBrown9d487392016-11-02 14:16:49 +11005696 pr_warn("%s: failed to create bitmap (%d)\n",
5697 mdname(mddev), err);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05005698 } else
5699 mddev->bitmap = bitmap;
5700
NeilBrownb15c2e52006-01-06 00:20:16 -08005701 }
Guoqing Jiangd4945492019-06-14 17:10:39 +08005702 if (err)
5703 goto bitmap_abort;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005704
5705 if (mddev->bitmap_info.max_write_behind > 0) {
5706 bool creat_pool = false;
5707
5708 rdev_for_each(rdev, mddev) {
5709 if (test_bit(WriteMostly, &rdev->flags) &&
5710 rdev_init_wb(rdev))
5711 creat_pool = true;
5712 }
5713 if (creat_pool && mddev->wb_info_pool == NULL) {
5714 mddev->wb_info_pool =
5715 mempool_create_kmalloc_pool(NR_WB_INFOS,
5716 sizeof(struct wb_info));
5717 if (!mddev->wb_info_pool) {
5718 err = -ENOMEM;
Guoqing Jiangd4945492019-06-14 17:10:39 +08005719 goto bitmap_abort;
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005720 }
5721 }
5722 }
5723
NeilBrown5c675f82014-12-15 12:56:56 +11005724 if (mddev->queue) {
Shaohua Libb086a82016-09-30 09:45:40 -07005725 bool nonrot = true;
5726
5727 rdev_for_each(rdev, mddev) {
5728 if (rdev->raid_disk >= 0 &&
5729 !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
5730 nonrot = false;
5731 break;
5732 }
5733 }
5734 if (mddev->degraded)
5735 nonrot = false;
5736 if (nonrot)
Bart Van Assche8b904b52018-03-07 17:10:10 -08005737 blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue);
Shaohua Libb086a82016-09-30 09:45:40 -07005738 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08005739 blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue);
Jan Karadc3b17c2017-02-02 15:56:50 +01005740 mddev->queue->backing_dev_info->congested_data = mddev;
5741 mddev->queue->backing_dev_info->congested_fn = md_congested;
NeilBrown5c675f82014-12-15 12:56:56 +11005742 }
NeilBrown36d091f2014-12-15 12:56:58 +11005743 if (pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10005744 if (mddev->kobj.sd &&
5745 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown9d487392016-11-02 14:16:49 +11005746 pr_warn("md: cannot register extra attributes for %s\n",
5747 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10005748 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08005749 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08005750 mddev->ro = 0;
5751
Robert Becker1e509152009-12-14 12:49:58 +11005752 atomic_set(&mddev->max_corr_read_errors,
5753 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 mddev->safemode = 0;
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005755 if (mddev_is_clustered(mddev))
5756 mddev->safemode_delay = 0;
5757 else
5758 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11005760 smp_wmb();
NeilBrown36d091f2014-12-15 12:56:58 +11005761 spin_lock(&mddev->lock);
5762 mddev->pers = pers;
NeilBrown36d091f2014-12-15 12:56:58 +11005763 spin_unlock(&mddev->lock);
NeilBrowndafb20f2012-03-19 12:46:39 +11005764 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10005765 if (rdev->raid_disk >= 0)
Yufen Yue5b521e2019-06-14 15:41:07 -07005766 sysfs_link_rdev(mddev, rdev); /* failure here is OK */
NeilBrownf72ffdd2014-09-30 14:23:59 +10005767
NeilBrowna4a3d262015-07-17 11:57:30 +10005768 if (mddev->degraded && !mddev->ro)
5769 /* This ensures that recovering status is reported immediately
5770 * via sysfs - until a lack of spares is confirmed.
5771 */
5772 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf72ffdd2014-09-30 14:23:59 +10005774
Shaohua Li29530792016-12-08 15:48:19 -08005775 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07005776 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777
NeilBrownd7603b72006-01-06 00:20:30 -08005778 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005779 sysfs_notify_dirent_safe(mddev->sysfs_state);
5780 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10005781 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 return 0;
Xiao Nib1261942018-01-24 12:17:38 +08005783
Guoqing Jiangd4945492019-06-14 17:10:39 +08005784bitmap_abort:
5785 mddev_detach(mddev);
5786 if (mddev->private)
5787 pers->free(mddev, mddev->private);
5788 mddev->private = NULL;
5789 module_put(pers->owner);
5790 md_bitmap_destroy(mddev);
Xiao Nib1261942018-01-24 12:17:38 +08005791abort:
NeilBrown4bc034d2019-03-29 10:46:16 -07005792 bioset_exit(&mddev->bio_set);
5793 bioset_exit(&mddev->sync_set);
Xiao Nib1261942018-01-24 12:17:38 +08005794 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795}
NeilBrown390ee602010-06-01 19:37:27 +10005796EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797
NeilBrownfd01b882011-10-11 16:47:53 +11005798static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11005799{
5800 int err;
5801
5802 err = md_run(mddev);
5803 if (err)
5804 goto out;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005805 err = md_bitmap_load(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005806 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005807 md_bitmap_destroy(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005808 goto out;
5809 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005810
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005811 if (mddev_is_clustered(mddev))
5812 md_allow_write(mddev);
5813
Song Liud5d885f2017-11-19 22:17:01 -08005814 /* run start up tasks that require md_thread */
5815 md_start(mddev);
5816
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05005817 md_wakeup_thread(mddev->thread);
5818 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5819
NeilBrownfe60b012010-03-29 11:10:42 +11005820 set_capacity(mddev->gendisk, mddev->array_sectors);
5821 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005822 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11005823 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5824out:
5825 return err;
5826}
5827
Song Liud5d885f2017-11-19 22:17:01 -08005828int md_start(struct mddev *mddev)
5829{
5830 int ret = 0;
5831
5832 if (mddev->pers->start) {
5833 set_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5834 md_wakeup_thread(mddev->thread);
5835 ret = mddev->pers->start(mddev);
5836 clear_bit(MD_RECOVERY_WAIT, &mddev->recovery);
5837 md_wakeup_thread(mddev->sync_thread);
5838 }
5839 return ret;
5840}
5841EXPORT_SYMBOL_GPL(md_start);
5842
NeilBrownfd01b882011-10-11 16:47:53 +11005843static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844{
5845 struct gendisk *disk = mddev->gendisk;
NeilBrown97b20ef2017-04-13 08:53:48 +10005846 struct md_rdev *rdev;
5847 bool has_journal = false;
5848 bool has_readonly = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
Andre Noll80fab1d2008-07-11 22:02:21 +10005850 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10005852 return -ENXIO;
5853 if (!mddev->pers)
5854 return -EINVAL;
5855 if (!mddev->ro)
5856 return -EBUSY;
Song Liu339421d2015-10-08 21:54:13 -07005857
NeilBrown97b20ef2017-04-13 08:53:48 +10005858 rcu_read_lock();
5859 rdev_for_each_rcu(rdev, mddev) {
5860 if (test_bit(Journal, &rdev->flags) &&
5861 !test_bit(Faulty, &rdev->flags))
5862 has_journal = true;
5863 if (bdev_read_only(rdev->bdev))
5864 has_readonly = true;
Song Liu339421d2015-10-08 21:54:13 -07005865 }
NeilBrown97b20ef2017-04-13 08:53:48 +10005866 rcu_read_unlock();
5867 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal)
5868 /* Don't restart rw with journal missing/faulty */
5869 return -EINVAL;
5870 if (has_readonly)
5871 return -EROFS;
Song Liu339421d2015-10-08 21:54:13 -07005872
Andre Noll80fab1d2008-07-11 22:02:21 +10005873 mddev->safemode = 0;
5874 mddev->ro = 0;
5875 set_disk_ro(disk, 0);
NeilBrown9d487392016-11-02 14:16:49 +11005876 pr_debug("md: %s switched to read-write mode.\n", mdname(mddev));
Andre Noll80fab1d2008-07-11 22:02:21 +10005877 /* Kick recovery or resync if necessary */
5878 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5879 md_wakeup_thread(mddev->thread);
5880 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005881 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10005882 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883}
5884
NeilBrownfd01b882011-10-11 16:47:53 +11005885static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005886{
5887 mddev->array_sectors = 0;
5888 mddev->external_size = 0;
5889 mddev->dev_sectors = 0;
5890 mddev->raid_disks = 0;
5891 mddev->recovery_cp = 0;
5892 mddev->resync_min = 0;
5893 mddev->resync_max = MaxSector;
5894 mddev->reshape_position = MaxSector;
5895 mddev->external = 0;
5896 mddev->persistent = 0;
5897 mddev->level = LEVEL_NONE;
5898 mddev->clevel[0] = 0;
5899 mddev->flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08005900 mddev->sb_flags = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005901 mddev->ro = 0;
5902 mddev->metadata_type[0] = 0;
5903 mddev->chunk_sectors = 0;
5904 mddev->ctime = mddev->utime = 0;
5905 mddev->layout = 0;
5906 mddev->max_disks = 0;
5907 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10005908 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005909 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005910 mddev->reshape_backwards = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005911 mddev->new_level = LEVEL_NONE;
5912 mddev->new_layout = 0;
5913 mddev->new_chunk_sectors = 0;
5914 mddev->curr_resync = 0;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11005915 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown6177b472010-03-29 11:37:13 +11005916 mddev->suspend_lo = mddev->suspend_hi = 0;
5917 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5918 mddev->recovery = 0;
5919 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005920 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005921 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005922 mddev->safemode = 0;
NeilBrownbd691922015-06-25 17:01:40 +10005923 mddev->private = NULL;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005924 mddev->cluster_info = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005925 mddev->bitmap_info.offset = 0;
5926 mddev->bitmap_info.default_offset = 0;
NeilBrown6409bb02012-05-22 13:55:07 +10005927 mddev->bitmap_info.default_space = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005928 mddev->bitmap_info.chunksize = 0;
5929 mddev->bitmap_info.daemon_sleep = 0;
5930 mddev->bitmap_info.max_write_behind = 0;
Guoqing Jiangc20c33f2016-08-12 13:42:38 +08005931 mddev->bitmap_info.nodes = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005932}
5933
NeilBrownfd01b882011-10-11 16:47:53 +11005934static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005935{
NeilBrown6b6204e2013-05-09 09:48:30 +10005936 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11005937 flush_workqueue(md_misc_wq);
NeilBrowna047e122010-03-29 12:07:53 +11005938 if (mddev->sync_thread) {
NeilBrowna047e122010-03-29 12:07:53 +11005939 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10005940 md_reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005941 }
5942
5943 del_timer_sync(&mddev->safemode_timer);
5944
Shaohua Li034e33f2016-11-21 10:29:19 -08005945 if (mddev->pers && mddev->pers->quiesce) {
5946 mddev->pers->quiesce(mddev, 1);
5947 mddev->pers->quiesce(mddev, 0);
5948 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005949 md_bitmap_flush(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005950
NeilBrownb6d428c2013-04-24 11:42:42 +10005951 if (mddev->ro == 0 &&
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005952 ((!mddev->in_sync && !mddev_is_clustered(mddev)) ||
Shaohua Li29530792016-12-08 15:48:19 -08005953 mddev->sb_flags)) {
NeilBrowna047e122010-03-29 12:07:53 +11005954 /* mark array as shutdown cleanly */
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11005955 if (!mddev_is_clustered(mddev))
5956 mddev->in_sync = 1;
NeilBrowna047e122010-03-29 12:07:53 +11005957 md_update_sb(mddev, 1);
5958 }
Guoqing Jiang3e148a32019-06-19 17:30:46 +08005959 mempool_destroy(mddev->wb_info_pool);
5960 mddev->wb_info_pool = NULL;
NeilBrowna047e122010-03-29 12:07:53 +11005961}
NeilBrowndefad612011-01-14 09:14:33 +11005962
NeilBrownfd01b882011-10-11 16:47:53 +11005963void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005964{
NeilBrown29f097c2013-11-14 17:54:51 +11005965 mddev_lock_nointr(mddev);
NeilBrowndefad612011-01-14 09:14:33 +11005966 __md_stop_writes(mddev);
5967 mddev_unlock(mddev);
5968}
NeilBrown390ee602010-06-01 19:37:27 +10005969EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005970
NeilBrown5aa61f42014-12-15 12:56:57 +11005971static void mddev_detach(struct mddev *mddev)
5972{
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005973 md_bitmap_wait_behind_writes(mddev);
NeilBrown36d091f2014-12-15 12:56:58 +11005974 if (mddev->pers && mddev->pers->quiesce) {
NeilBrown5aa61f42014-12-15 12:56:57 +11005975 mddev->pers->quiesce(mddev, 1);
5976 mddev->pers->quiesce(mddev, 0);
5977 }
5978 md_unregister_thread(&mddev->thread);
5979 if (mddev->queue)
5980 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
5981}
5982
NeilBrown5eff3c42012-11-19 10:47:48 +11005983static void __md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005984{
NeilBrown36d091f2014-12-15 12:56:58 +11005985 struct md_personality *pers = mddev->pers;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005986 md_bitmap_destroy(mddev);
NeilBrown5aa61f42014-12-15 12:56:57 +11005987 mddev_detach(mddev);
NeilBrownee5d0042015-07-22 10:20:07 +10005988 /* Ensure ->event_work is done */
5989 flush_workqueue(md_misc_wq);
NeilBrown36d091f2014-12-15 12:56:58 +11005990 spin_lock(&mddev->lock);
NeilBrown6177b472010-03-29 11:37:13 +11005991 mddev->pers = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005992 spin_unlock(&mddev->lock);
5993 pers->free(mddev, mddev->private);
NeilBrownbd691922015-06-25 17:01:40 +10005994 mddev->private = NULL;
NeilBrown36d091f2014-12-15 12:56:58 +11005995 if (pers->sync_request && mddev->to_remove == NULL)
5996 mddev->to_remove = &md_redundancy_group;
5997 module_put(pers->owner);
NeilBrowncca9cf92010-04-01 12:08:16 +11005998 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Jack Wang6aaa58c2018-10-19 16:21:31 +02005999}
6000
6001void md_stop(struct mddev *mddev)
6002{
6003 /* stop the array and free an attached data structures.
6004 * This is called from dm-raid
6005 */
6006 __md_stop(mddev);
Kent Overstreetafeee512018-05-20 18:25:52 -04006007 bioset_exit(&mddev->bio_set);
6008 bioset_exit(&mddev->sync_set);
NeilBrown5eff3c42012-11-19 10:47:48 +11006009}
6010
NeilBrown390ee602010-06-01 19:37:27 +10006011EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11006012
NeilBrowna05b7ea2012-07-19 15:59:18 +10006013static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
NeilBrowna4bd82d2010-03-29 13:23:10 +11006014{
6015 int err = 0;
NeilBrown30b8feb2013-11-14 15:16:17 +11006016 int did_freeze = 0;
6017
6018 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6019 did_freeze = 1;
6020 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6021 md_wakeup_thread(mddev->thread);
6022 }
NeilBrownf851b602014-12-11 10:02:10 +11006023 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006024 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006025 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006026 /* Thread might be blocked waiting for metadata update
6027 * which will now never happen */
6028 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006029
Shaohua Li29530792016-12-08 15:48:19 -08006030 if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags))
NeilBrown88724bf2015-09-24 14:00:51 +10006031 return -EBUSY;
NeilBrown30b8feb2013-11-14 15:16:17 +11006032 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006033 wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING,
6034 &mddev->recovery));
NeilBrown88724bf2015-09-24 14:00:51 +10006035 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08006036 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown30b8feb2013-11-14 15:16:17 +11006037 mddev_lock_nointr(mddev);
6038
NeilBrowna4bd82d2010-03-29 13:23:10 +11006039 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006040 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006041 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006042 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006043 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown30b8feb2013-11-14 15:16:17 +11006044 if (did_freeze) {
6045 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006046 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006047 md_wakeup_thread(mddev->thread);
6048 }
NeilBrowna4bd82d2010-03-29 13:23:10 +11006049 err = -EBUSY;
6050 goto out;
6051 }
6052 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11006053 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11006054
6055 err = -ENXIO;
6056 if (mddev->ro==1)
6057 goto out;
6058 mddev->ro = 1;
6059 set_disk_ro(mddev->gendisk, 1);
6060 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006061 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6062 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006063 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown30b8feb2013-11-14 15:16:17 +11006064 err = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006065 }
6066out:
6067 mutex_unlock(&mddev->open_mutex);
6068 return err;
6069}
6070
NeilBrown9e653b62006-06-26 00:27:58 -07006071/* mode:
6072 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07006073 * 2 - stop but do not disassemble array
6074 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006075static int do_md_stop(struct mddev *mddev, int mode,
NeilBrowna05b7ea2012-07-19 15:59:18 +10006076 struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11006079 struct md_rdev *rdev;
NeilBrown30b8feb2013-11-14 15:16:17 +11006080 int did_freeze = 0;
6081
6082 if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
6083 did_freeze = 1;
6084 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
6085 md_wakeup_thread(mddev->thread);
6086 }
NeilBrownf851b602014-12-11 10:02:10 +11006087 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown30b8feb2013-11-14 15:16:17 +11006088 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11006089 if (mddev->sync_thread)
NeilBrown30b8feb2013-11-14 15:16:17 +11006090 /* Thread might be blocked waiting for metadata update
6091 * which will now never happen */
6092 wake_up_process(mddev->sync_thread->tsk);
NeilBrownf851b602014-12-11 10:02:10 +11006093
NeilBrown30b8feb2013-11-14 15:16:17 +11006094 mddev_unlock(mddev);
NeilBrownf851b602014-12-11 10:02:10 +11006095 wait_event(resync_wait, (mddev->sync_thread == NULL &&
6096 !test_bit(MD_RECOVERY_RUNNING,
6097 &mddev->recovery)));
NeilBrown30b8feb2013-11-14 15:16:17 +11006098 mddev_lock_nointr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
NeilBrownc8c00a62009-08-10 12:50:52 +10006100 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10006101 if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
NeilBrown30b8feb2013-11-14 15:16:17 +11006102 mddev->sysfs_active ||
6103 mddev->sync_thread ||
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08006104 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
NeilBrown9d487392016-11-02 14:16:49 +11006105 pr_warn("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10006106 mutex_unlock(&mddev->open_mutex);
NeilBrown30b8feb2013-11-14 15:16:17 +11006107 if (did_freeze) {
6108 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown45eaf452014-10-29 08:49:50 +11006109 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown30b8feb2013-11-14 15:16:17 +11006110 md_wakeup_thread(mddev->thread);
6111 }
NeilBrown260fa032013-08-27 16:44:13 +10006112 return -EBUSY;
6113 }
NeilBrown6e17b022010-08-07 21:41:19 +10006114 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11006115 if (mddev->ro)
6116 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11006117
NeilBrowndefad612011-01-14 09:14:33 +11006118 __md_stop_writes(mddev);
NeilBrown5eff3c42012-11-19 10:47:48 +11006119 __md_stop(mddev);
Jan Karadc3b17c2017-02-02 15:56:50 +01006120 mddev->queue->backing_dev_info->congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11006121
NeilBrowna4bd82d2010-03-29 13:23:10 +11006122 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10006123 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08006124
NeilBrowndafb20f2012-03-19 12:46:39 +11006125 rdev_for_each(rdev, mddev)
Namhyung Kim36fad852011-07-27 11:00:36 +10006126 if (rdev->raid_disk >= 0)
6127 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10006128
NeilBrowna4bd82d2010-03-29 13:23:10 +11006129 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10006130 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006131 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006132 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08006133
NeilBrowna4bd82d2010-03-29 13:23:10 +11006134 if (mddev->ro)
6135 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10006136 } else
6137 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 /*
6139 * Free resources if final stop
6140 */
NeilBrown9e653b62006-06-26 00:27:58 -07006141 if (mode == 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006142 pr_info("md: %s stopped.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143
NeilBrownc3d97142009-12-14 12:49:52 +11006144 if (mddev->bitmap_info.file) {
NeilBrown4af1a042014-12-15 12:57:00 +11006145 struct file *f = mddev->bitmap_info.file;
6146 spin_lock(&mddev->lock);
NeilBrownc3d97142009-12-14 12:49:52 +11006147 mddev->bitmap_info.file = NULL;
NeilBrown4af1a042014-12-15 12:57:00 +11006148 spin_unlock(&mddev->lock);
6149 fput(f);
NeilBrown978f9462006-02-02 14:28:05 -08006150 }
NeilBrownc3d97142009-12-14 12:49:52 +11006151 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08006152
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 export_array(mddev);
6154
NeilBrown6177b472010-03-29 11:37:13 +11006155 md_clean(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11006156 if (mddev->hold_active == UNTIL_STOP)
6157 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11006158 }
NeilBrownd7603b72006-01-06 00:20:30 -08006159 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006160 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10006161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162}
6163
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006164#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11006165static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166{
NeilBrown3cb03002011-10-11 16:45:26 +11006167 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 int err;
6169
NeilBrowna757e642005-04-16 15:26:42 -07006170 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172
NeilBrown9d487392016-11-02 14:16:49 +11006173 pr_info("md: running: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174
NeilBrowndafb20f2012-03-19 12:46:39 +11006175 rdev_for_each(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 char b[BDEVNAME_SIZE];
NeilBrown9d487392016-11-02 14:16:49 +11006177 pr_cont("<%s>", bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 }
NeilBrown9d487392016-11-02 14:16:49 +11006179 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180
NeilBrownd710e132008-10-13 11:55:12 +11006181 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11006183 pr_warn("md: do_md_run() returned %d\n", err);
NeilBrowna05b7ea2012-07-19 15:59:18 +10006184 do_md_stop(mddev, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 }
6186}
6187
6188/*
6189 * lets try to run arrays based on all disks that have arrived
6190 * until now. (those are in pending_raid_disks)
6191 *
6192 * the method: pick the first pending disk, collect all disks with
6193 * the same UUID, remove all from the pending list and put them into
6194 * the 'same_array' list. Then order this list based on superblock
6195 * update time (freshest comes first), kick out 'old' disks and
6196 * compare superblocks. If everything's fine then run it.
6197 *
6198 * If "unit" is allocated, then bump its reference count
6199 */
6200static void autorun_devices(int part)
6201{
NeilBrown3cb03002011-10-11 16:45:26 +11006202 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006203 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 char b[BDEVNAME_SIZE];
6205
NeilBrown9d487392016-11-02 14:16:49 +11006206 pr_info("md: autorun ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07006208 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08006210 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11006212 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213
NeilBrown9d487392016-11-02 14:16:49 +11006214 pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006216 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 if (super_90_load(rdev, rdev0, 0) >= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006218 pr_debug("md: adding %s ...\n",
6219 bdevname(rdev->bdev,b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 list_move(&rdev->same_set, &candidates);
6221 }
6222 /*
6223 * now we have a set of devices, with all of them having
6224 * mostly sane superblocks. It's time to allocate the
6225 * mddev.
6226 */
NeilBrowne8703fe2006-10-03 01:15:59 -07006227 if (part) {
6228 dev = MKDEV(mdp_major,
6229 rdev0->preferred_minor << MdpMinorShift);
6230 unit = MINOR(dev) >> MdpMinorShift;
6231 } else {
6232 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
6233 unit = MINOR(dev);
6234 }
6235 if (rdev0->preferred_minor != unit) {
NeilBrown9d487392016-11-02 14:16:49 +11006236 pr_warn("md: unit number in %s is bad: %d\n",
6237 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 break;
6239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
6241 md_probe(dev, NULL, NULL);
6242 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10006243 if (!mddev || !mddev->gendisk) {
6244 if (mddev)
6245 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 break;
6247 }
NeilBrownf72ffdd2014-09-30 14:23:59 +10006248 if (mddev_lock(mddev))
NeilBrown9d487392016-11-02 14:16:49 +11006249 pr_warn("md: %s locked, cannot run\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 else if (mddev->raid_disks || mddev->major_version
6251 || !list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11006252 pr_warn("md: %s already running, cannot run %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 mdname(mddev), bdevname(rdev0->bdev,b));
6254 mddev_unlock(mddev);
6255 } else {
NeilBrown9d487392016-11-02 14:16:49 +11006256 pr_debug("md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08006257 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006258 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 list_del_init(&rdev->same_set);
6260 if (bind_rdev_to_array(rdev, mddev))
6261 export_rdev(rdev);
6262 }
6263 autorun_array(mddev);
6264 mddev_unlock(mddev);
6265 }
6266 /* on success, candidates will be empty, on error
6267 * it won't...
6268 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006269 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10006270 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10006272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 mddev_put(mddev);
6274 }
NeilBrown9d487392016-11-02 14:16:49 +11006275 pr_info("md: ... autorun DONE.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006277#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278
NeilBrownf72ffdd2014-09-30 14:23:59 +10006279static int get_version(void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280{
6281 mdu_version_t ver;
6282
6283 ver.major = MD_MAJOR_VERSION;
6284 ver.minor = MD_MINOR_VERSION;
6285 ver.patchlevel = MD_PATCHLEVEL_VERSION;
6286
6287 if (copy_to_user(arg, &ver, sizeof(ver)))
6288 return -EFAULT;
6289
6290 return 0;
6291}
6292
NeilBrownf72ffdd2014-09-30 14:23:59 +10006293static int get_array_info(struct mddev *mddev, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294{
6295 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10006296 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11006297 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
NeilBrown1ca69c42012-10-11 13:37:33 +11006299 nr = working = insync = failed = spare = 0;
6300 rcu_read_lock();
6301 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006303 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 failed++;
6305 else {
6306 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08006307 if (test_bit(In_sync, &rdev->flags))
NeilBrownf72ffdd2014-09-30 14:23:59 +10006308 insync++;
Song Liub347af82016-08-11 17:14:45 -07006309 else if (test_bit(Journal, &rdev->flags))
6310 /* TODO: add journal count to md_u.h */
6311 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 else
6313 spare++;
6314 }
6315 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006316 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317
6318 info.major_version = mddev->major_version;
6319 info.minor_version = mddev->minor_version;
6320 info.patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006321 info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11006323 info.size = mddev->dev_sectors / 2;
6324 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08006325 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 info.nr_disks = nr;
6327 info.raid_disks = mddev->raid_disks;
6328 info.md_minor = mddev->md_minor;
6329 info.not_persistent= !mddev->persistent;
6330
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006331 info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 info.state = 0;
6333 if (mddev->in_sync)
6334 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11006335 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown9bd35922014-07-02 11:35:06 +10006336 info.state |= (1<<MD_SB_BITMAP_PRESENT);
Goldwyn Rodriguesca8895d2014-11-26 12:22:03 -06006337 if (mddev_is_clustered(mddev))
6338 info.state |= (1<<MD_SB_CLUSTERED);
NeilBrowna9f326e2009-09-23 18:06:41 +10006339 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 info.working_disks = working;
6341 info.failed_disks = failed;
6342 info.spare_disks = spare;
6343
6344 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006345 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346
6347 if (copy_to_user(arg, &info, sizeof(info)))
6348 return -EFAULT;
6349
6350 return 0;
6351}
6352
NeilBrownf72ffdd2014-09-30 14:23:59 +10006353static int get_bitmap_file(struct mddev *mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07006354{
6355 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
NeilBrownf4ad3d32014-12-15 12:57:00 +11006356 char *ptr;
NeilBrown4af1a042014-12-15 12:57:00 +11006357 int err;
NeilBrown32a76272005-06-21 17:17:14 -07006358
Benjamin Randazzob6878d92015-07-25 16:36:50 +02006359 file = kzalloc(sizeof(*file), GFP_NOIO);
NeilBrown32a76272005-06-21 17:17:14 -07006360 if (!file)
NeilBrown4af1a042014-12-15 12:57:00 +11006361 return -ENOMEM;
NeilBrown32a76272005-06-21 17:17:14 -07006362
NeilBrown32a76272005-06-21 17:17:14 -07006363 err = 0;
NeilBrown4af1a042014-12-15 12:57:00 +11006364 spin_lock(&mddev->lock);
Benjamin Randazzo25eafe12015-07-25 16:36:50 +02006365 /* bitmap enabled */
6366 if (mddev->bitmap_info.file) {
6367 ptr = file_path(mddev->bitmap_info.file, file->pathname,
6368 sizeof(file->pathname));
6369 if (IS_ERR(ptr))
6370 err = PTR_ERR(ptr);
6371 else
6372 memmove(file->pathname, ptr,
6373 sizeof(file->pathname)-(ptr-file->pathname));
6374 }
NeilBrown4af1a042014-12-15 12:57:00 +11006375 spin_unlock(&mddev->lock);
6376
6377 if (err == 0 &&
6378 copy_to_user(arg, file, sizeof(*file)))
NeilBrown32a76272005-06-21 17:17:14 -07006379 err = -EFAULT;
NeilBrown4af1a042014-12-15 12:57:00 +11006380
NeilBrown32a76272005-06-21 17:17:14 -07006381 kfree(file);
6382 return err;
6383}
6384
NeilBrownf72ffdd2014-09-30 14:23:59 +10006385static int get_disk_info(struct mddev *mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386{
6387 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11006388 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389
6390 if (copy_from_user(&info, arg, sizeof(info)))
6391 return -EFAULT;
6392
NeilBrown1ca69c42012-10-11 13:37:33 +11006393 rcu_read_lock();
Goldwyn Rodrigues57d051d2015-04-14 10:43:55 -05006394 rdev = md_find_rdev_nr_rcu(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 if (rdev) {
6396 info.major = MAJOR(rdev->bdev->bd_dev);
6397 info.minor = MINOR(rdev->bdev->bd_dev);
6398 info.raid_disk = rdev->raid_disk;
6399 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08006400 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08006402 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 info.state |= (1<<MD_DISK_ACTIVE);
6404 info.state |= (1<<MD_DISK_SYNC);
6405 }
Shaohua Li9efdca12015-10-12 16:59:50 -07006406 if (test_bit(Journal, &rdev->flags))
Song Liubac624f2015-08-13 14:31:55 -07006407 info.state |= (1<<MD_DISK_JOURNAL);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006408 if (test_bit(WriteMostly, &rdev->flags))
6409 info.state |= (1<<MD_DISK_WRITEMOSTLY);
NeilBrown688834e2016-11-18 16:16:11 +11006410 if (test_bit(FailFast, &rdev->flags))
6411 info.state |= (1<<MD_DISK_FAILFAST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 } else {
6413 info.major = info.minor = 0;
6414 info.raid_disk = -1;
6415 info.state = (1<<MD_DISK_REMOVED);
6416 }
NeilBrown1ca69c42012-10-11 13:37:33 +11006417 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418
6419 if (copy_to_user(arg, &info, sizeof(info)))
6420 return -EFAULT;
6421
6422 return 0;
6423}
6424
NeilBrownf72ffdd2014-09-30 14:23:59 +10006425static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426{
6427 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006428 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 dev_t dev = MKDEV(info->major,info->minor);
6430
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006431 if (mddev_is_clustered(mddev) &&
6432 !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) {
NeilBrown9d487392016-11-02 14:16:49 +11006433 pr_warn("%s: Cannot add to clustered mddev.\n",
6434 mdname(mddev));
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006435 return -EINVAL;
6436 }
6437
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
6439 return -EOVERFLOW;
6440
6441 if (!mddev->raid_disks) {
6442 int err;
6443 /* expecting a device which has a superblock */
6444 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
6445 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006446 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 PTR_ERR(rdev));
6448 return PTR_ERR(rdev);
6449 }
6450 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11006451 struct md_rdev *rdev0
6452 = list_entry(mddev->disks.next,
6453 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10006454 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 .load_super(rdev, rdev0, mddev->minor_version);
6456 if (err < 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006457 pr_warn("md: %s has different UUID to %s\n",
NeilBrownf72ffdd2014-09-30 14:23:59 +10006458 bdevname(rdev->bdev,b),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 bdevname(rdev0->bdev,b2));
6460 export_rdev(rdev);
6461 return -EINVAL;
6462 }
6463 }
6464 err = bind_rdev_to_array(rdev, mddev);
6465 if (err)
6466 export_rdev(rdev);
6467 return err;
6468 }
6469
6470 /*
6471 * add_new_disk can be used once the array is assembled
6472 * to add "hot spares". They must already have a superblock
6473 * written
6474 */
6475 if (mddev->pers) {
6476 int err;
6477 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006478 pr_warn("%s: personality does not support diskops!\n",
6479 mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 return -EINVAL;
6481 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07006482 if (mddev->persistent)
6483 rdev = md_import_device(dev, mddev->major_version,
6484 mddev->minor_version);
6485 else
6486 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006488 pr_warn("md: md_import_device returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 PTR_ERR(rdev));
6490 return PTR_ERR(rdev);
6491 }
NeilBrown1a855a02010-12-09 16:36:28 +11006492 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07006493 if (!mddev->persistent) {
6494 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11006495 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07006496 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11006497 set_bit(In_sync, &rdev->flags);
NeilBrown8313b8e2013-12-12 10:13:33 +11006498 clear_bit(Bitmap_sync, &rdev->flags);
NeilBrownbf572542011-01-12 09:03:35 +11006499 } else
NeilBrown41158c72005-06-21 17:17:25 -07006500 rdev->raid_disk = -1;
NeilBrownf4667222013-12-09 12:04:56 +11006501 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrown41158c72005-06-21 17:17:25 -07006502 } else
6503 super_types[mddev->major_version].
6504 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10006505 if ((info->state & (1<<MD_DISK_SYNC)) &&
NeilBrownf4563092012-07-03 15:59:06 +10006506 rdev->raid_disk != info->raid_disk) {
NeilBrownbedd86b2011-05-11 14:26:20 +10006507 /* This was a hot-add request, but events doesn't
6508 * match, so reject it.
6509 */
6510 export_rdev(rdev);
6511 return -EINVAL;
6512 }
6513
NeilBrownb2d444d2005-11-08 21:39:31 -08006514 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006515 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6516 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11006517 else
6518 clear_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006519 if (info->state & (1<<MD_DISK_FAILFAST))
6520 set_bit(FailFast, &rdev->flags);
6521 else
6522 clear_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006523
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006524 if (info->state & (1<<MD_DISK_JOURNAL)) {
6525 struct md_rdev *rdev2;
6526 bool has_journal = false;
6527
6528 /* make sure no existing journal disk */
6529 rdev_for_each(rdev2, mddev) {
6530 if (test_bit(Journal, &rdev2->flags)) {
6531 has_journal = true;
6532 break;
6533 }
6534 }
NeilBrown230b55f2017-10-17 14:24:09 +11006535 if (has_journal || mddev->bitmap) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006536 export_rdev(rdev);
6537 return -EBUSY;
6538 }
Song Liubac624f2015-08-13 14:31:55 -07006539 set_bit(Journal, &rdev->flags);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11006540 }
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006541 /*
6542 * check whether the device shows up in other nodes
6543 */
6544 if (mddev_is_clustered(mddev)) {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006545 if (info->state & (1 << MD_DISK_CANDIDATE))
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006546 set_bit(Candidate, &rdev->flags);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006547 else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006548 /* --add initiated by this node */
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006549 err = md_cluster_ops->add_new_disk(mddev, rdev);
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006550 if (err) {
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05006551 export_rdev(rdev);
6552 return err;
6553 }
6554 }
6555 }
6556
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 rdev->raid_disk = -1;
6558 err = bind_rdev_to_array(rdev, mddev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006559
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 if (err)
6561 export_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006562
6563 if (mddev_is_clustered(mddev)) {
Guoqing Jiange566aef2016-08-12 13:42:34 +08006564 if (info->state & (1 << MD_DISK_CANDIDATE)) {
6565 if (!err) {
6566 err = md_cluster_ops->new_disk_ack(mddev,
6567 err == 0);
6568 if (err)
6569 md_kick_rdev_from_array(rdev);
6570 }
6571 } else {
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006572 if (err)
6573 md_cluster_ops->add_new_disk_cancel(mddev);
6574 else
6575 err = add_bound_rdev(rdev);
6576 }
6577
6578 } else if (!err)
Goldwyn Rodriguesa6da4ef2015-04-14 10:45:22 -05006579 err = add_bound_rdev(rdev);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05006580
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 return err;
6582 }
6583
6584 /* otherwise, add_new_disk is only allowed
6585 * for major_version==0 superblocks
6586 */
6587 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006588 pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 return -EINVAL;
6590 }
6591
6592 if (!(info->state & (1<<MD_DISK_FAULTY))) {
6593 int err;
NeilBrownd710e132008-10-13 11:55:12 +11006594 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006596 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 PTR_ERR(rdev));
6598 return PTR_ERR(rdev);
6599 }
6600 rdev->desc_nr = info->number;
6601 if (info->raid_disk < mddev->raid_disks)
6602 rdev->raid_disk = info->raid_disk;
6603 else
6604 rdev->raid_disk = -1;
6605
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08006607 if (info->state & (1<<MD_DISK_SYNC))
6608 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006610 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
6611 set_bit(WriteMostly, &rdev->flags);
NeilBrown688834e2016-11-18 16:16:11 +11006612 if (info->state & (1<<MD_DISK_FAILFAST))
6613 set_bit(FailFast, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006614
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 if (!mddev->persistent) {
NeilBrown9d487392016-11-02 14:16:49 +11006616 pr_debug("md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01006617 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
6618 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006619 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10006620 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621
NeilBrown2bf071b2006-01-06 00:20:55 -08006622 err = bind_rdev_to_array(rdev, mddev);
6623 if (err) {
6624 export_rdev(rdev);
6625 return err;
6626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 }
6628
6629 return 0;
6630}
6631
NeilBrownf72ffdd2014-09-30 14:23:59 +10006632static int hot_remove_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633{
6634 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11006635 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636
Yufen Yuc42a0e22018-05-04 18:08:10 +08006637 if (!mddev->pers)
6638 return -ENODEV;
6639
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 rdev = find_rdev(mddev, dev);
6641 if (!rdev)
6642 return -ENXIO;
6643
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006644 if (rdev->raid_disk < 0)
6645 goto kick_rdev;
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006646
NeilBrown3ea8929d2013-04-24 11:42:41 +10006647 clear_bit(Blocked, &rdev->flags);
6648 remove_and_add_spares(mddev, rdev);
6649
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 if (rdev->raid_disk >= 0)
6651 goto busy;
6652
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05006653kick_rdev:
Goldwyn Rodrigues54a88392015-12-21 10:51:00 +11006654 if (mddev_is_clustered(mddev))
Goldwyn Rodrigues88bcfef2015-04-14 10:44:44 -05006655 md_cluster_ops->remove_disk(mddev, rdev);
6656
Goldwyn Rodriguesfb56dfe2015-04-14 10:43:24 -05006657 md_kick_rdev_from_array(rdev);
Shaohua Li29530792016-12-08 15:48:19 -08006658 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006659 if (mddev->thread)
6660 md_wakeup_thread(mddev->thread);
6661 else
6662 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08006663 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664
6665 return 0;
6666busy:
NeilBrown9d487392016-11-02 14:16:49 +11006667 pr_debug("md: cannot remove active disk %s from %s ...\n",
6668 bdevname(rdev->bdev,b), mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669 return -EBUSY;
6670}
6671
NeilBrownf72ffdd2014-09-30 14:23:59 +10006672static int hot_add_disk(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673{
6674 char b[BDEVNAME_SIZE];
6675 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11006676 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677
6678 if (!mddev->pers)
6679 return -ENODEV;
6680
6681 if (mddev->major_version != 0) {
NeilBrown9d487392016-11-02 14:16:49 +11006682 pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 mdname(mddev));
6684 return -EINVAL;
6685 }
6686 if (!mddev->pers->hot_add_disk) {
NeilBrown9d487392016-11-02 14:16:49 +11006687 pr_warn("%s: personality does not support diskops!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 mdname(mddev));
6689 return -EINVAL;
6690 }
6691
NeilBrownd710e132008-10-13 11:55:12 +11006692 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 if (IS_ERR(rdev)) {
NeilBrown9d487392016-11-02 14:16:49 +11006694 pr_warn("md: error, md_import_device() returned %ld\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 PTR_ERR(rdev));
6696 return -EINVAL;
6697 }
6698
6699 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11006700 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 else
Mike Snitzer77304d22010-11-08 14:39:12 +01006702 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703
NeilBrown8190e752009-06-18 08:48:58 +10006704 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705
NeilBrownb2d444d2005-11-08 21:39:31 -08006706 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown9d487392016-11-02 14:16:49 +11006707 pr_warn("md: can not hot-add faulty %s disk to %s!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 bdevname(rdev->bdev,b), mdname(mddev));
6709 err = -EINVAL;
6710 goto abort_export;
6711 }
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05006712
NeilBrownb2d444d2005-11-08 21:39:31 -08006713 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07006715 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08006716 err = bind_rdev_to_array(rdev, mddev);
6717 if (err)
Goldwyn Rodrigues2aa82192015-09-28 19:21:35 -05006718 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719
6720 /*
6721 * The rest should better be atomic, we can have disk failures
6722 * noticed in interrupt contexts ...
6723 */
6724
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 rdev->raid_disk = -1;
6726
Shaohua Li29530792016-12-08 15:48:19 -08006727 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown060b0682016-11-04 16:46:03 +11006728 if (!mddev->thread)
6729 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 /*
6731 * Kick recovery, maybe this spare has to be added to the
6732 * array immediately.
6733 */
6734 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6735 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006736 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 return 0;
6738
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739abort_export:
6740 export_rdev(rdev);
6741 return err;
6742}
6743
NeilBrownfd01b882011-10-11 16:47:53 +11006744static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07006745{
NeilBrown035328c2014-04-09 12:25:40 +10006746 int err = 0;
NeilBrown32a76272005-06-21 17:17:14 -07006747
NeilBrown36fa3062005-09-09 16:23:45 -07006748 if (mddev->pers) {
NeilBrownd66b1b32014-08-08 15:40:24 +10006749 if (!mddev->pers->quiesce || !mddev->thread)
NeilBrown36fa3062005-09-09 16:23:45 -07006750 return -EBUSY;
6751 if (mddev->recovery || mddev->sync_thread)
6752 return -EBUSY;
6753 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07006754 }
6755
NeilBrown36fa3062005-09-09 16:23:45 -07006756 if (fd >= 0) {
NeilBrown035328c2014-04-09 12:25:40 +10006757 struct inode *inode;
NeilBrown1e594bb2014-12-15 12:57:00 +11006758 struct file *f;
NeilBrown36fa3062005-09-09 16:23:45 -07006759
NeilBrown1e594bb2014-12-15 12:57:00 +11006760 if (mddev->bitmap || mddev->bitmap_info.file)
6761 return -EEXIST; /* cannot add when bitmap is present */
6762 f = fget(fd);
6763
6764 if (f == NULL) {
NeilBrown9d487392016-11-02 14:16:49 +11006765 pr_warn("%s: error: failed to get bitmap file\n",
6766 mdname(mddev));
NeilBrown36fa3062005-09-09 16:23:45 -07006767 return -EBADF;
6768 }
6769
NeilBrown1e594bb2014-12-15 12:57:00 +11006770 inode = f->f_mapping->host;
NeilBrown035328c2014-04-09 12:25:40 +10006771 if (!S_ISREG(inode->i_mode)) {
NeilBrown9d487392016-11-02 14:16:49 +11006772 pr_warn("%s: error: bitmap file must be a regular file\n",
6773 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006774 err = -EBADF;
NeilBrown1e594bb2014-12-15 12:57:00 +11006775 } else if (!(f->f_mode & FMODE_WRITE)) {
NeilBrown9d487392016-11-02 14:16:49 +11006776 pr_warn("%s: error: bitmap file must open for write\n",
6777 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006778 err = -EBADF;
6779 } else if (atomic_read(&inode->i_writecount) != 1) {
NeilBrown9d487392016-11-02 14:16:49 +11006780 pr_warn("%s: error: bitmap file is already in use\n",
6781 mdname(mddev));
NeilBrown035328c2014-04-09 12:25:40 +10006782 err = -EBUSY;
6783 }
6784 if (err) {
NeilBrown1e594bb2014-12-15 12:57:00 +11006785 fput(f);
NeilBrown36fa3062005-09-09 16:23:45 -07006786 return err;
6787 }
NeilBrown1e594bb2014-12-15 12:57:00 +11006788 mddev->bitmap_info.file = f;
NeilBrownc3d97142009-12-14 12:49:52 +11006789 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07006790 } else if (mddev->bitmap == NULL)
6791 return -ENOENT; /* cannot remove what isn't there */
6792 err = 0;
6793 if (mddev->pers) {
NeilBrown69e51b42010-06-01 19:37:35 +10006794 if (fd >= 0) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006795 struct bitmap *bitmap;
6796
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006797 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006798 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05006799 if (!IS_ERR(bitmap)) {
6800 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006801 err = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11006802 } else
6803 err = PTR_ERR(bitmap);
NeilBrown52a0d492017-10-17 13:46:43 +11006804 if (err) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006805 md_bitmap_destroy(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006806 fd = -1;
6807 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006808 mddev_resume(mddev);
NeilBrown52a0d492017-10-17 13:46:43 +11006809 } else if (fd < 0) {
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006810 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006811 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11006812 mddev_resume(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07006813 }
NeilBrownd7375ab2006-06-26 00:27:43 -07006814 }
6815 if (fd < 0) {
NeilBrown4af1a042014-12-15 12:57:00 +11006816 struct file *f = mddev->bitmap_info.file;
6817 if (f) {
6818 spin_lock(&mddev->lock);
6819 mddev->bitmap_info.file = NULL;
6820 spin_unlock(&mddev->lock);
6821 fput(f);
6822 }
NeilBrown36fa3062005-09-09 16:23:45 -07006823 }
6824
NeilBrown32a76272005-06-21 17:17:14 -07006825 return err;
6826}
6827
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828/*
6829 * set_array_info is used two different ways
6830 * The original usage is when creating a new array.
6831 * In this usage, raid_disks is > 0 and it together with
6832 * level, size, not_persistent,layout,chunksize determine the
6833 * shape of the array.
6834 * This will always create an array with a type-0.90.0 superblock.
6835 * The newer usage is when assembling an array.
6836 * In this case raid_disks will be 0, and the major_version field is
6837 * use to determine which style super-blocks are to be found on the devices.
6838 * The minor and patch _version numbers are also kept incase the
6839 * super_block handler wishes to interpret them.
6840 */
NeilBrownf72ffdd2014-09-30 14:23:59 +10006841static int set_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842{
6843
6844 if (info->raid_disks == 0) {
6845 /* just setting version number for superblock loading */
6846 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07006847 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848 super_types[info->major_version].name == NULL) {
6849 /* maybe try to auto-load a module? */
NeilBrown9d487392016-11-02 14:16:49 +11006850 pr_warn("md: superblock version %d not known\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851 info->major_version);
6852 return -EINVAL;
6853 }
6854 mddev->major_version = info->major_version;
6855 mddev->minor_version = info->minor_version;
6856 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08006857 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11006858 /* ensure mddev_put doesn't delete this now that there
6859 * is some minimal configuration.
6860 */
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006861 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 return 0;
6863 }
6864 mddev->major_version = MD_MAJOR_VERSION;
6865 mddev->minor_version = MD_MINOR_VERSION;
6866 mddev->patch_version = MD_PATCHLEVEL_VERSION;
Deepa Dinamani9ebc6ef2015-12-21 10:51:01 +11006867 mddev->ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868
6869 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08006870 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11006871 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872 mddev->raid_disks = info->raid_disks;
6873 /* don't set md_minor, it is determined by which /dev/md* was
6874 * openned
6875 */
6876 if (info->state & (1<<MD_SB_CLEAN))
6877 mddev->recovery_cp = MaxSector;
6878 else
6879 mddev->recovery_cp = 0;
6880 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08006881 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882
6883 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10006884 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885
Shaohua Li29530792016-12-08 15:48:19 -08006886 if (mddev->persistent) {
NeilBrown1b3bae42017-03-01 07:31:28 +11006887 mddev->max_disks = MD_SB_DISKS;
6888 mddev->flags = 0;
6889 mddev->sb_flags = 0;
Shaohua Li29530792016-12-08 15:48:19 -08006890 }
6891 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892
NeilBrownc3d97142009-12-14 12:49:52 +11006893 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
NeilBrown6409bb02012-05-22 13:55:07 +10006894 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
NeilBrownc3d97142009-12-14 12:49:52 +11006895 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08006896
NeilBrownf6705572006-03-27 01:18:11 -08006897 mddev->reshape_position = MaxSector;
6898
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 /*
6900 * Generate a 128 bit UUID
6901 */
6902 get_random_bytes(mddev->uuid, 16);
6903
NeilBrownf6705572006-03-27 01:18:11 -08006904 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10006905 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006906 mddev->new_layout = mddev->layout;
6907 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006908 mddev->reshape_backwards = 0;
NeilBrownf6705572006-03-27 01:18:11 -08006909
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 return 0;
6911}
6912
NeilBrownfd01b882011-10-11 16:47:53 +11006913void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11006914{
Shaohua Liefa4b772017-10-18 22:08:13 -07006915 lockdep_assert_held(&mddev->reconfig_mutex);
Dan Williamsb522adc2009-03-31 15:00:31 +11006916
6917 if (mddev->external_size)
6918 return;
6919
Dan Williams1f403622009-03-31 14:59:03 +11006920 mddev->array_sectors = array_sectors;
6921}
6922EXPORT_SYMBOL(md_set_array_sectors);
6923
NeilBrownfd01b882011-10-11 16:47:53 +11006924static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006925{
NeilBrown3cb03002011-10-11 16:45:26 +11006926 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08006927 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10006928 int fit = (num_sectors == 0);
Guoqing Jiang818da592017-03-01 16:42:40 +08006929 sector_t old_dev_sectors = mddev->dev_sectors;
Guoqing Jiangab5a98b2016-05-02 11:33:13 -04006930
NeilBrowna35b0d62006-01-06 00:20:49 -08006931 if (mddev->pers->resize == NULL)
6932 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10006933 /* The "num_sectors" is the number of sectors of each device that
6934 * is used. This can only make sense for arrays with redundancy.
6935 * linear and raid0 always use whatever space is available. We can only
6936 * consider changing this number if no resync or reconstruction is
6937 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10006938 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10006939 * of each device. If num_sectors is zero, we find the largest size
6940 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08006941 */
NeilBrownf851b602014-12-11 10:02:10 +11006942 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6943 mddev->sync_thread)
NeilBrowna35b0d62006-01-06 00:20:49 -08006944 return -EBUSY;
NeilBrownbd8839e2014-05-28 13:39:21 +10006945 if (mddev->ro)
6946 return -EROFS;
NeilBrowna4a61252012-05-22 13:55:27 +10006947
NeilBrowndafb20f2012-03-19 12:46:39 +11006948 rdev_for_each(rdev, mddev) {
Andre Nolldd8ac332009-03-31 14:33:13 +11006949 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07006950
Andre Nolld71f9f82008-07-11 22:02:22 +10006951 if (fit && (num_sectors == 0 || num_sectors > avail))
6952 num_sectors = avail;
6953 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08006954 return -ENOSPC;
6955 }
Andre Nolld71f9f82008-07-11 22:02:22 +10006956 rv = mddev->pers->resize(mddev, num_sectors);
Guoqing Jiangc9483632017-02-24 11:15:23 +08006957 if (!rv) {
Guoqing Jiang818da592017-03-01 16:42:40 +08006958 if (mddev_is_clustered(mddev))
6959 md_cluster_ops->update_size(mddev, old_dev_sectors);
6960 else if (mddev->queue) {
Guoqing Jiangc9483632017-02-24 11:15:23 +08006961 set_capacity(mddev->gendisk, mddev->array_sectors);
6962 revalidate_disk(mddev->gendisk);
6963 }
6964 }
NeilBrowna35b0d62006-01-06 00:20:49 -08006965 return rv;
6966}
6967
NeilBrownfd01b882011-10-11 16:47:53 +11006968static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b992006-01-06 00:20:54 -08006969{
6970 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10006971 struct md_rdev *rdev;
NeilBrownda943b992006-01-06 00:20:54 -08006972 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08006973 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08006974 return -EINVAL;
NeilBrownbd8839e2014-05-28 13:39:21 +10006975 if (mddev->ro)
6976 return -EROFS;
NeilBrownda943b992006-01-06 00:20:54 -08006977 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10006978 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08006979 return -EINVAL;
NeilBrownf851b602014-12-11 10:02:10 +11006980 if (mddev->sync_thread ||
6981 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6982 mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08006983 return -EBUSY;
NeilBrownc6563a82012-05-21 09:27:00 +10006984
6985 rdev_for_each(rdev, mddev) {
6986 if (mddev->raid_disks < raid_disks &&
6987 rdev->data_offset < rdev->new_data_offset)
6988 return -EINVAL;
6989 if (mddev->raid_disks > raid_disks &&
6990 rdev->data_offset > rdev->new_data_offset)
6991 return -EINVAL;
6992 }
6993
NeilBrown63c70c42006-03-27 01:18:13 -08006994 mddev->delta_disks = raid_disks - mddev->raid_disks;
NeilBrown2c810cd2012-05-21 09:27:00 +10006995 if (mddev->delta_disks < 0)
6996 mddev->reshape_backwards = 1;
6997 else if (mddev->delta_disks > 0)
6998 mddev->reshape_backwards = 0;
NeilBrown63c70c42006-03-27 01:18:13 -08006999
7000 rv = mddev->pers->check_reshape(mddev);
NeilBrown2c810cd2012-05-21 09:27:00 +10007001 if (rv < 0) {
NeilBrownde171cb2011-01-31 11:57:42 +11007002 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007003 mddev->reshape_backwards = 0;
7004 }
NeilBrownda943b992006-01-06 00:20:54 -08007005 return rv;
7006}
7007
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008/*
7009 * update_array_info is used to change the configuration of an
7010 * on-line array.
7011 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
7012 * fields in the info are checked against the array.
7013 * Any differences that cannot be handled will cause an error.
7014 * Normally, only one change can be managed at a time.
7015 */
NeilBrownfd01b882011-10-11 16:47:53 +11007016static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017{
7018 int rv = 0;
7019 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007020 int state = 0;
7021
7022 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11007023 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07007024 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025
7026 if (mddev->major_version != info->major_version ||
7027 mddev->minor_version != info->minor_version ||
7028/* mddev->patch_version != info->patch_version || */
7029 mddev->ctime != info->ctime ||
7030 mddev->level != info->level ||
7031/* mddev->layout != info->layout || */
Firo Yang4e023612015-06-11 09:41:10 +08007032 mddev->persistent != !info->not_persistent ||
Andre Noll9d8f0362009-06-18 08:45:01 +10007033 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07007034 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
7035 ((state^info->state) & 0xfffffe00)
7036 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 return -EINVAL;
7038 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11007039 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
7040 cnt++;
7041 if (mddev->raid_disks != info->raid_disks)
7042 cnt++;
7043 if (mddev->layout != info->layout)
7044 cnt++;
7045 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
7046 cnt++;
7047 if (cnt == 0)
7048 return 0;
7049 if (cnt > 1)
7050 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051
7052 if (mddev->layout != info->layout) {
7053 /* Change layout
7054 * we don't need to do anything at the md level, the
7055 * personality will take care of it all.
7056 */
NeilBrown50ac1682009-06-18 08:47:55 +10007057 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007059 else {
7060 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10007061 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10007062 if (rv)
7063 mddev->new_layout = mddev->layout;
7064 return rv;
7065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007067 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10007068 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069
NeilBrownda943b992006-01-06 00:20:54 -08007070 if (mddev->raid_disks != info->raid_disks)
7071 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072
NeilBrown36fa3062005-09-09 16:23:45 -07007073 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007074 if (mddev->pers->quiesce == NULL || mddev->thread == NULL) {
7075 rv = -EINVAL;
7076 goto err;
7077 }
7078 if (mddev->recovery || mddev->sync_thread) {
7079 rv = -EBUSY;
7080 goto err;
7081 }
NeilBrown36fa3062005-09-09 16:23:45 -07007082 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007083 struct bitmap *bitmap;
NeilBrown36fa3062005-09-09 16:23:45 -07007084 /* add the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007085 if (mddev->bitmap) {
7086 rv = -EEXIST;
7087 goto err;
7088 }
7089 if (mddev->bitmap_info.default_offset == 0) {
7090 rv = -EINVAL;
7091 goto err;
7092 }
NeilBrownc3d97142009-12-14 12:49:52 +11007093 mddev->bitmap_info.offset =
7094 mddev->bitmap_info.default_offset;
NeilBrown6409bb02012-05-22 13:55:07 +10007095 mddev->bitmap_info.space =
7096 mddev->bitmap_info.default_space;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007097 bitmap = md_bitmap_create(mddev, -1);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007098 mddev_suspend(mddev);
Goldwyn Rodriguesf9209a32014-06-06 12:43:49 -05007099 if (!IS_ERR(bitmap)) {
7100 mddev->bitmap = bitmap;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007101 rv = md_bitmap_load(mddev);
NeilBrownba599ac2015-02-25 11:44:11 +11007102 } else
7103 rv = PTR_ERR(bitmap);
NeilBrown36fa3062005-09-09 16:23:45 -07007104 if (rv)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007105 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007106 mddev_resume(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07007107 } else {
7108 /* remove the bitmap */
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007109 if (!mddev->bitmap) {
7110 rv = -ENOENT;
7111 goto err;
7112 }
7113 if (mddev->bitmap->storage.file) {
7114 rv = -EINVAL;
7115 goto err;
7116 }
Guoqing Jiangf6a2dc62015-12-21 10:51:00 +11007117 if (mddev->bitmap_info.nodes) {
7118 /* hold PW on all the bitmap lock */
7119 if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) {
NeilBrown9d487392016-11-02 14:16:49 +11007120 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 +11007121 rv = -EPERM;
7122 md_cluster_ops->unlock_all_bitmaps(mddev);
7123 goto err;
7124 }
7125
7126 mddev->bitmap_info.nodes = 0;
7127 md_cluster_ops->leave(mddev);
7128 }
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007129 mddev_suspend(mddev);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007130 md_bitmap_destroy(mddev);
NeilBrown9e1cc0a2017-10-17 13:46:43 +11007131 mddev_resume(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11007132 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07007133 }
7134 }
NeilBrown850b2b422006-10-03 01:15:46 -07007135 md_update_sb(mddev, 1);
Goldwyn Rodrigues293467a2014-06-07 01:44:51 -05007136 return rv;
7137err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 return rv;
7139}
7140
NeilBrownfd01b882011-10-11 16:47:53 +11007141static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142{
NeilBrown3cb03002011-10-11 16:45:26 +11007143 struct md_rdev *rdev;
NeilBrown1ca69c42012-10-11 13:37:33 +11007144 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145
7146 if (mddev->pers == NULL)
7147 return -ENODEV;
7148
NeilBrown1ca69c42012-10-11 13:37:33 +11007149 rcu_read_lock();
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01007150 rdev = md_find_rdev_rcu(mddev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151 if (!rdev)
NeilBrown1ca69c42012-10-11 13:37:33 +11007152 err = -ENODEV;
7153 else {
7154 md_error(mddev, rdev);
7155 if (!test_bit(Faulty, &rdev->flags))
7156 err = -EBUSY;
7157 }
7158 rcu_read_unlock();
7159 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160}
7161
Andre Noll2f9618c2008-04-25 18:57:58 +02007162/*
7163 * We have a problem here : there is no easy way to give a CHS
7164 * virtual geometry. We currently pretend that we have a 2 heads
7165 * 4 sectors (with a BIG number of cylinders...). This drives
7166 * dosfs just mad... ;-)
7167 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007168static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
7169{
NeilBrownfd01b882011-10-11 16:47:53 +11007170 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007171
7172 geo->heads = 2;
7173 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11007174 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007175 return 0;
7176}
7177
Nicolas Schichancb335f82014-01-15 16:58:52 +01007178static inline bool md_ioctl_valid(unsigned int cmd)
7179{
7180 switch (cmd) {
7181 case ADD_NEW_DISK:
7182 case BLKROSET:
7183 case GET_ARRAY_INFO:
7184 case GET_BITMAP_FILE:
7185 case GET_DISK_INFO:
7186 case HOT_ADD_DISK:
7187 case HOT_REMOVE_DISK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007188 case RAID_AUTORUN:
7189 case RAID_VERSION:
7190 case RESTART_ARRAY_RW:
7191 case RUN_ARRAY:
7192 case SET_ARRAY_INFO:
7193 case SET_BITMAP_FILE:
7194 case SET_DISK_FAULTY:
7195 case STOP_ARRAY:
7196 case STOP_ARRAY_RO:
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007197 case CLUSTERED_DISK_NACK:
Nicolas Schichancb335f82014-01-15 16:58:52 +01007198 return true;
7199 default:
7200 return false;
7201 }
7202}
7203
Al Viroa39907f2008-03-02 10:31:15 -05007204static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205 unsigned int cmd, unsigned long arg)
7206{
7207 int err = 0;
7208 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11007209 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10007210 int ro;
NeilBrown065e5192017-04-06 11:16:33 +08007211 bool did_set_md_closing = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212
Nicolas Schichancb335f82014-01-15 16:58:52 +01007213 if (!md_ioctl_valid(cmd))
7214 return -ENOTTY;
7215
NeilBrown506c9e42011-12-23 10:17:26 +11007216 switch (cmd) {
7217 case RAID_VERSION:
7218 case GET_ARRAY_INFO:
7219 case GET_DISK_INFO:
7220 break;
7221 default:
7222 if (!capable(CAP_SYS_ADMIN))
7223 return -EACCES;
7224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225
7226 /*
7227 * Commands dealing with the RAID driver but not any
7228 * particular array:
7229 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007230 switch (cmd) {
7231 case RAID_VERSION:
7232 err = get_version(argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007233 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235#ifndef MODULE
NeilBrownc02c0ae2012-12-11 13:39:21 +11007236 case RAID_AUTORUN:
7237 err = 0;
7238 autostart_arrays(arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007239 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240#endif
NeilBrownc02c0ae2012-12-11 13:39:21 +11007241 default:;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242 }
7243
7244 /*
7245 * Commands creating/starting a new array:
7246 */
7247
Al Viroa39907f2008-03-02 10:31:15 -05007248 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249
7250 if (!mddev) {
7251 BUG();
NeilBrown3adc28d2014-09-30 15:46:41 +10007252 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 }
7254
NeilBrown1ca69c42012-10-11 13:37:33 +11007255 /* Some actions do not requires the mutex */
7256 switch (cmd) {
7257 case GET_ARRAY_INFO:
7258 if (!mddev->raid_disks && !mddev->external)
7259 err = -ENODEV;
7260 else
7261 err = get_array_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007262 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007263
7264 case GET_DISK_INFO:
7265 if (!mddev->raid_disks && !mddev->external)
7266 err = -ENODEV;
7267 else
7268 err = get_disk_info(mddev, argp);
NeilBrown3adc28d2014-09-30 15:46:41 +10007269 goto out;
NeilBrown1ca69c42012-10-11 13:37:33 +11007270
7271 case SET_DISK_FAULTY:
7272 err = set_disk_faulty(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007273 goto out;
NeilBrown4af1a042014-12-15 12:57:00 +11007274
7275 case GET_BITMAP_FILE:
7276 err = get_bitmap_file(mddev, argp);
7277 goto out;
7278
NeilBrown1ca69c42012-10-11 13:37:33 +11007279 }
7280
NeilBrowna7a3f082012-12-11 13:35:54 +11007281 if (cmd == ADD_NEW_DISK)
7282 /* need to ensure md_delayed_delete() has completed */
7283 flush_workqueue(md_misc_wq);
7284
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007285 if (cmd == HOT_REMOVE_DISK)
7286 /* need to ensure recovery thread has run */
7287 wait_event_interruptible_timeout(mddev->sb_wait,
7288 !test_bit(MD_RECOVERY_NEEDED,
Shaohua Li82a301c2016-12-08 15:48:18 -08007289 &mddev->recovery),
Hannes Reinecke90f5f7a2013-04-02 08:38:55 +02007290 msecs_to_jiffies(5000));
NeilBrown260fa032013-08-27 16:44:13 +10007291 if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
7292 /* Need to flush page cache, and ensure no-one else opens
7293 * and writes
7294 */
7295 mutex_lock(&mddev->open_mutex);
NeilBrown9ba3b7f2014-09-09 14:00:15 +10007296 if (mddev->pers && atomic_read(&mddev->openers) > 1) {
NeilBrown260fa032013-08-27 16:44:13 +10007297 mutex_unlock(&mddev->open_mutex);
7298 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007299 goto out;
NeilBrown260fa032013-08-27 16:44:13 +10007300 }
NeilBrown065e5192017-04-06 11:16:33 +08007301 WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags));
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007302 set_bit(MD_CLOSING, &mddev->flags);
NeilBrown065e5192017-04-06 11:16:33 +08007303 did_set_md_closing = true;
NeilBrown260fa032013-08-27 16:44:13 +10007304 mutex_unlock(&mddev->open_mutex);
7305 sync_blockdev(bdev);
7306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 err = mddev_lock(mddev);
7308 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007309 pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n",
7310 err, cmd);
NeilBrown3adc28d2014-09-30 15:46:41 +10007311 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 }
7313
NeilBrownc02c0ae2012-12-11 13:39:21 +11007314 if (cmd == SET_ARRAY_INFO) {
7315 mdu_array_info_t info;
7316 if (!arg)
7317 memset(&info, 0, sizeof(info));
7318 else if (copy_from_user(&info, argp, sizeof(info))) {
7319 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007320 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007321 }
7322 if (mddev->pers) {
7323 err = update_array_info(mddev, &info);
7324 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007325 pr_warn("md: couldn't update array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007326 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007328 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007329 }
7330 if (!list_empty(&mddev->disks)) {
NeilBrown9d487392016-11-02 14:16:49 +11007331 pr_warn("md: array %s already has disks!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007332 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007333 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007334 }
7335 if (mddev->raid_disks) {
NeilBrown9d487392016-11-02 14:16:49 +11007336 pr_warn("md: array %s already initialised!\n", mdname(mddev));
NeilBrownc02c0ae2012-12-11 13:39:21 +11007337 err = -EBUSY;
NeilBrown3adc28d2014-09-30 15:46:41 +10007338 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007339 }
7340 err = set_array_info(mddev, &info);
7341 if (err) {
NeilBrown9d487392016-11-02 14:16:49 +11007342 pr_warn("md: couldn't set array info. %d\n", err);
NeilBrown3adc28d2014-09-30 15:46:41 +10007343 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007344 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007345 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 }
7347
7348 /*
7349 * Commands querying/configuring an existing array:
7350 */
NeilBrown32a76272005-06-21 17:17:14 -07007351 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08007352 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08007353 if ((!mddev->raid_disks && !mddev->external)
7354 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
7355 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
7356 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357 err = -ENODEV;
NeilBrown3adc28d2014-09-30 15:46:41 +10007358 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359 }
7360
7361 /*
7362 * Commands even a read-only array can execute:
7363 */
NeilBrownc02c0ae2012-12-11 13:39:21 +11007364 switch (cmd) {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007365 case RESTART_ARRAY_RW:
7366 err = restart_array(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007367 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007368
7369 case STOP_ARRAY:
7370 err = do_md_stop(mddev, 0, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007371 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007372
7373 case STOP_ARRAY_RO:
7374 err = md_set_readonly(mddev, bdev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007375 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007376
NeilBrown3ea8929d2013-04-24 11:42:41 +10007377 case HOT_REMOVE_DISK:
7378 err = hot_remove_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007379 goto unlock;
NeilBrown3ea8929d2013-04-24 11:42:41 +10007380
NeilBrown7ceb17e2013-04-24 11:42:42 +10007381 case ADD_NEW_DISK:
7382 /* We can support ADD_NEW_DISK on read-only arrays
Wei Fang466ad292016-03-21 19:19:30 +08007383 * only if we are re-adding a preexisting device.
NeilBrown7ceb17e2013-04-24 11:42:42 +10007384 * So require mddev->pers and MD_DISK_SYNC.
7385 */
7386 if (mddev->pers) {
7387 mdu_disk_info_t info;
7388 if (copy_from_user(&info, argp, sizeof(info)))
7389 err = -EFAULT;
7390 else if (!(info.state & (1<<MD_DISK_SYNC)))
7391 /* Need to clear read-only for this */
7392 break;
7393 else
7394 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007395 goto unlock;
NeilBrown7ceb17e2013-04-24 11:42:42 +10007396 }
7397 break;
7398
NeilBrownc02c0ae2012-12-11 13:39:21 +11007399 case BLKROSET:
7400 if (get_user(ro, (int __user *)(arg))) {
7401 err = -EFAULT;
NeilBrown3adc28d2014-09-30 15:46:41 +10007402 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007403 }
7404 err = -EINVAL;
7405
7406 /* if the bdev is going readonly the value of mddev->ro
7407 * does not matter, no writes are coming
7408 */
7409 if (ro)
NeilBrown3adc28d2014-09-30 15:46:41 +10007410 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007411
NeilBrownc02c0ae2012-12-11 13:39:21 +11007412 /* are we are already prepared for writes? */
7413 if (mddev->ro != 1)
NeilBrown3adc28d2014-09-30 15:46:41 +10007414 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007415
7416 /* transitioning to readauto need only happen for
7417 * arrays that call md_write_start
7418 */
7419 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420 err = restart_array(mddev);
NeilBrownc02c0ae2012-12-11 13:39:21 +11007421 if (err == 0) {
7422 mddev->ro = 2;
7423 set_disk_ro(mddev->gendisk, 0);
Dan Williamse2218352010-05-12 08:25:37 +10007424 }
NeilBrownc02c0ae2012-12-11 13:39:21 +11007425 }
NeilBrown3adc28d2014-09-30 15:46:41 +10007426 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 }
7428
7429 /*
7430 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08007431 * superblock, so we do not allow them on read-only arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432 */
NeilBrown326eb172014-09-30 15:36:28 +10007433 if (mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007434 if (mddev->ro == 2) {
7435 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007436 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10007437 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownf3378b42013-02-28 11:59:03 +11007438 /* mddev_unlock will wake thread */
7439 /* If a device failed while we were read-only, we
7440 * need to make sure the metadata is updated now.
7441 */
Shaohua Li29530792016-12-08 15:48:19 -08007442 if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) {
NeilBrownf3378b42013-02-28 11:59:03 +11007443 mddev_unlock(mddev);
7444 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08007445 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) &&
7446 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown29f097c2013-11-14 17:54:51 +11007447 mddev_lock_nointr(mddev);
NeilBrownf3378b42013-02-28 11:59:03 +11007448 }
NeilBrownf91de922005-11-08 21:39:36 -08007449 } else {
7450 err = -EROFS;
NeilBrown3adc28d2014-09-30 15:46:41 +10007451 goto unlock;
NeilBrownf91de922005-11-08 21:39:36 -08007452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 }
7454
NeilBrownc02c0ae2012-12-11 13:39:21 +11007455 switch (cmd) {
7456 case ADD_NEW_DISK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457 {
NeilBrownc02c0ae2012-12-11 13:39:21 +11007458 mdu_disk_info_t info;
7459 if (copy_from_user(&info, argp, sizeof(info)))
7460 err = -EFAULT;
7461 else
7462 err = add_new_disk(mddev, &info);
NeilBrown3adc28d2014-09-30 15:46:41 +10007463 goto unlock;
NeilBrownc02c0ae2012-12-11 13:39:21 +11007464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465
Goldwyn Rodrigues1aee41f2014-10-29 18:51:31 -05007466 case CLUSTERED_DISK_NACK:
7467 if (mddev_is_clustered(mddev))
7468 md_cluster_ops->new_disk_ack(mddev, false);
7469 else
7470 err = -EINVAL;
7471 goto unlock;
7472
NeilBrownc02c0ae2012-12-11 13:39:21 +11007473 case HOT_ADD_DISK:
7474 err = hot_add_disk(mddev, new_decode_dev(arg));
NeilBrown3adc28d2014-09-30 15:46:41 +10007475 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476
NeilBrownc02c0ae2012-12-11 13:39:21 +11007477 case RUN_ARRAY:
7478 err = do_md_run(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007479 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480
NeilBrownc02c0ae2012-12-11 13:39:21 +11007481 case SET_BITMAP_FILE:
7482 err = set_bitmap_file(mddev, (int)arg);
NeilBrown3adc28d2014-09-30 15:46:41 +10007483 goto unlock;
NeilBrown32a76272005-06-21 17:17:14 -07007484
NeilBrownc02c0ae2012-12-11 13:39:21 +11007485 default:
7486 err = -EINVAL;
NeilBrown3adc28d2014-09-30 15:46:41 +10007487 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488 }
7489
NeilBrown3adc28d2014-09-30 15:46:41 +10007490unlock:
NeilBrownd3374822009-01-09 08:31:10 +11007491 if (mddev->hold_active == UNTIL_IOCTL &&
7492 err != -EINVAL)
7493 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494 mddev_unlock(mddev);
NeilBrown3adc28d2014-09-30 15:46:41 +10007495out:
NeilBrown065e5192017-04-06 11:16:33 +08007496 if(did_set_md_closing)
7497 clear_bit(MD_CLOSING, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498 return err;
7499}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007500#ifdef CONFIG_COMPAT
7501static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
7502 unsigned int cmd, unsigned long arg)
7503{
7504 switch (cmd) {
7505 case HOT_REMOVE_DISK:
7506 case HOT_ADD_DISK:
7507 case SET_DISK_FAULTY:
7508 case SET_BITMAP_FILE:
7509 /* These take in integer arg, do not convert */
7510 break;
7511 default:
7512 arg = (unsigned long)compat_ptr(arg);
7513 break;
7514 }
7515
7516 return md_ioctl(bdev, mode, cmd, arg);
7517}
7518#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519
Al Viroa39907f2008-03-02 10:31:15 -05007520static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521{
7522 /*
7523 * Succeed if we can lock the mddev, which confirms that
7524 * it isn't being stopped right now.
7525 */
NeilBrownfd01b882011-10-11 16:47:53 +11007526 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 int err;
7528
Yuanhan Liu0c098222012-05-22 13:55:32 +10007529 if (!mddev)
7530 return -ENODEV;
7531
NeilBrownd3374822009-01-09 08:31:10 +11007532 if (mddev->gendisk != bdev->bd_disk) {
7533 /* we are racing with mddev_put which is discarding this
7534 * bd_disk.
7535 */
7536 mddev_put(mddev);
7537 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02007538 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11007539 /* Then retry the open from the top */
7540 return -ERESTARTSYS;
7541 }
7542 BUG_ON(mddev != bdev->bd_disk->private_data);
7543
NeilBrownc8c00a62009-08-10 12:50:52 +10007544 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 goto out;
7546
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007547 if (test_bit(MD_CLOSING, &mddev->flags)) {
7548 mutex_unlock(&mddev->open_mutex);
NeilBrowne2342ca2016-12-05 16:40:50 +11007549 err = -ENODEV;
7550 goto out;
Guoqing Jiangaf8d8e62016-08-12 13:42:37 +08007551 }
7552
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10007554 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10007555 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007557 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558 out:
NeilBrowne2342ca2016-12-05 16:40:50 +11007559 if (err)
7560 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561 return err;
7562}
7563
Al Virodb2a1442013-05-05 21:52:57 -04007564static void md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565{
NeilBrownf72ffdd2014-09-30 14:23:59 +10007566 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02007568 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10007569 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 mddev_put(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007572
7573static int md_media_changed(struct gendisk *disk)
7574{
NeilBrownfd01b882011-10-11 16:47:53 +11007575 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007576
7577 return mddev->changed;
7578}
7579
7580static int md_revalidate(struct gendisk *disk)
7581{
NeilBrownfd01b882011-10-11 16:47:53 +11007582 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007583
7584 mddev->changed = 0;
7585 return 0;
7586}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07007587static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588{
7589 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05007590 .open = md_open,
7591 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10007592 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11007593#ifdef CONFIG_COMPAT
7594 .compat_ioctl = md_compat_ioctl,
7595#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08007596 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11007597 .media_changed = md_media_changed,
7598 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007599};
7600
NeilBrownf72ffdd2014-09-30 14:23:59 +10007601static int md_thread(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602{
NeilBrown2b8bf342011-10-11 16:48:23 +11007603 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605 /*
7606 * md_thread is a 'system-thread', it's priority should be very
7607 * high. We avoid resource deadlocks individually in each
7608 * raid personality. (RAID5 does preallocation) We also use RR and
7609 * the very same RT priority as kswapd, thus we will never get
7610 * into a priority inversion deadlock.
7611 *
7612 * we definitely have to have equal or higher priority than
7613 * bdflush, otherwise bdflush will deadlock if there are too
7614 * many dirty RAID5 blocks.
7615 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616
NeilBrown6985c432005-10-19 21:23:47 -07007617 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07007618 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619
NeilBrown93588e22005-11-15 00:09:12 -08007620 /* We need to wait INTERRUPTIBLE so that
7621 * we don't add to the load-average.
7622 * That means we need to be sure no signals are
7623 * pending
7624 */
7625 if (signal_pending(current))
7626 flush_signals(current);
7627
7628 wait_event_interruptible_timeout
7629 (thread->wqueue,
7630 test_bit(THREAD_WAKEUP, &thread->flags)
Shaohua Lice1ccd02016-11-21 10:29:18 -08007631 || kthread_should_stop() || kthread_should_park(),
NeilBrown93588e22005-11-15 00:09:12 -08007632 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633
NeilBrown6c987912011-01-14 09:13:53 +11007634 clear_bit(THREAD_WAKEUP, &thread->flags);
Shaohua Lice1ccd02016-11-21 10:29:18 -08007635 if (kthread_should_park())
7636 kthread_parkme();
NeilBrown6c987912011-01-14 09:13:53 +11007637 if (!kthread_should_stop())
Shaohua Li4ed87312012-10-11 13:34:00 +11007638 thread->run(thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639 }
NeilBrowna6fb0932005-09-09 16:23:56 -07007640
Linus Torvalds1da177e2005-04-16 15:20:36 -07007641 return 0;
7642}
7643
NeilBrown2b8bf342011-10-11 16:48:23 +11007644void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645{
7646 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11007647 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Guoqing Jiangd1d90142017-10-09 10:32:48 +08007648 set_bit(THREAD_WAKEUP, &thread->flags);
7649 wake_up(&thread->wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650 }
7651}
NeilBrown6c144d32014-09-30 16:15:38 +10007652EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653
Shaohua Li4ed87312012-10-11 13:34:00 +11007654struct md_thread *md_register_thread(void (*run) (struct md_thread *),
7655 struct mddev *mddev, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656{
NeilBrown2b8bf342011-10-11 16:48:23 +11007657 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658
NeilBrown2b8bf342011-10-11 16:48:23 +11007659 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660 if (!thread)
7661 return NULL;
7662
Linus Torvalds1da177e2005-04-16 15:20:36 -07007663 init_waitqueue_head(&thread->wqueue);
7664
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665 thread->run = run;
7666 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07007667 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10007668 thread->tsk = kthread_run(md_thread, thread,
7669 "%s_%s",
7670 mdname(thread->mddev),
NeilBrown02326052012-07-03 15:56:52 +10007671 name);
NeilBrowna6fb0932005-09-09 16:23:56 -07007672 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673 kfree(thread);
7674 return NULL;
7675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 return thread;
7677}
NeilBrown6c144d32014-09-30 16:15:38 +10007678EXPORT_SYMBOL(md_register_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679
NeilBrown2b8bf342011-10-11 16:48:23 +11007680void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681{
NeilBrown2b8bf342011-10-11 16:48:23 +11007682 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11007683 if (!thread)
7684 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11007685 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10007686 /* Locking ensures that mddev_unlock does not wake_up a
7687 * non-existent thread
7688 */
7689 spin_lock(&pers_lock);
7690 *threadp = NULL;
7691 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07007692
7693 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 kfree(thread);
7695}
NeilBrown6c144d32014-09-30 16:15:38 +10007696EXPORT_SYMBOL(md_unregister_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697
NeilBrownfd01b882011-10-11 16:47:53 +11007698void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699{
NeilBrownb2d444d2005-11-08 21:39:31 -08007700 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007701 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07007702
NeilBrownde393cd2011-07-28 11:31:48 +10007703 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007704 return;
7705 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10007706 if (mddev->degraded)
7707 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007708 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7710 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7711 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10007712 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02007713 queue_work(md_misc_wq, &mddev->event_work);
Guoqing Jiangbb9ef712015-12-28 10:46:38 +08007714 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715}
NeilBrown6c144d32014-09-30 16:15:38 +10007716EXPORT_SYMBOL(md_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717
7718/* seq_file implementation /proc/mdstat */
7719
7720static void status_unused(struct seq_file *seq)
7721{
7722 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007723 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007724
7725 seq_printf(seq, "unused devices: ");
7726
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007727 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728 char b[BDEVNAME_SIZE];
7729 i++;
7730 seq_printf(seq, "%s ",
7731 bdevname(rdev->bdev,b));
7732 }
7733 if (!i)
7734 seq_printf(seq, "<none>");
7735
7736 seq_printf(seq, "\n");
7737}
7738
NeilBrownf7851be2015-07-02 17:12:58 +10007739static int status_resync(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740{
NeilBrowndd71cf62009-05-07 12:49:35 +10007741 sector_t max_sectors, resync, res;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02007742 unsigned long dt, db = 0;
7743 sector_t rt, curr_mark_cnt, resync_mark_cnt;
7744 int scale, recovery_active;
NeilBrown4588b422006-03-27 01:18:04 -08007745 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007746
NeilBrownc804cde2012-05-21 09:28:33 +10007747 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
7748 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10007749 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750 else
NeilBrowndd71cf62009-05-07 12:49:35 +10007751 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007752
NeilBrownf7851be2015-07-02 17:12:58 +10007753 resync = mddev->curr_resync;
7754 if (resync <= 3) {
7755 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7756 /* Still cleaning up */
7757 resync = max_sectors;
Nate Daileyd2e2ec82017-11-30 11:33:30 -05007758 } else if (resync > max_sectors)
7759 resync = max_sectors;
7760 else
NeilBrownf7851be2015-07-02 17:12:58 +10007761 resync -= atomic_read(&mddev->recovery_active);
7762
7763 if (resync == 0) {
Guoqing Jiang0357ba22018-07-02 16:26:25 +08007764 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) {
7765 struct md_rdev *rdev;
7766
7767 rdev_for_each(rdev, mddev)
7768 if (rdev->raid_disk >= 0 &&
7769 !test_bit(Faulty, &rdev->flags) &&
7770 rdev->recovery_offset != MaxSector &&
7771 rdev->recovery_offset) {
7772 seq_printf(seq, "\trecover=REMOTE");
7773 return 1;
7774 }
7775 if (mddev->reshape_position != MaxSector)
7776 seq_printf(seq, "\treshape=REMOTE");
7777 else
7778 seq_printf(seq, "\tresync=REMOTE");
7779 return 1;
7780 }
NeilBrownf7851be2015-07-02 17:12:58 +10007781 if (mddev->recovery_cp < MaxSector) {
7782 seq_printf(seq, "\tresync=PENDING");
7783 return 1;
7784 }
7785 return 0;
7786 }
7787 if (resync < 3) {
7788 seq_printf(seq, "\tresync=DELAYED");
7789 return 1;
7790 }
7791
NeilBrown403df472014-09-30 15:52:29 +10007792 WARN_ON(max_sectors == 0);
NeilBrown4588b422006-03-27 01:18:04 -08007793 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10007794 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08007795 * u32, as those are the requirements for sector_div.
7796 * Thus 'scale' must be at least 10
7797 */
7798 scale = 10;
7799 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10007800 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08007801 scale++;
7802 }
7803 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10007804 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08007805
7806 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807 {
NeilBrown4588b422006-03-27 01:18:04 -08007808 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007809 seq_printf(seq, "[");
7810 for (i = 0; i < x; i++)
7811 seq_printf(seq, "=");
7812 seq_printf(seq, ">");
7813 for (i = 0; i < y; i++)
7814 seq_printf(seq, ".");
7815 seq_printf(seq, "] ");
7816 }
NeilBrown4588b422006-03-27 01:18:04 -08007817 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08007818 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
7819 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07007820 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
7821 "check" :
7822 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
7823 "resync" : "recovery"))),
7824 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10007825 (unsigned long long) resync/2,
7826 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007827
7828 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829 * dt: time from mark until now
7830 * db: blocks written from mark until now
7831 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10007832 *
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02007833 * rt is a sector_t, which is always 64bit now. We are keeping
7834 * the original algorithm, but it is not really necessary.
7835 *
7836 * Original algorithm:
7837 * So we divide before multiply in case it is 32bit and close
7838 * to the limit.
7839 * We scale the divisor (db) by 32 to avoid losing precision
7840 * near the end of resync when the number of remaining sectors
7841 * is close to 'db'.
7842 * We then divide rt by 32 after multiplying by db to compensate.
7843 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844 */
7845 dt = ((jiffies - mddev->resync_mark) / HZ);
7846 if (!dt) dt++;
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02007847
7848 curr_mark_cnt = mddev->curr_mark_cnt;
7849 recovery_active = atomic_read(&mddev->recovery_active);
7850 resync_mark_cnt = mddev->resync_mark_cnt;
7851
7852 if (curr_mark_cnt >= (recovery_active + resync_mark_cnt))
7853 db = curr_mark_cnt - (recovery_active + resync_mark_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854
NeilBrowndd71cf62009-05-07 12:49:35 +10007855 rt = max_sectors - resync; /* number of remaining sectors */
Mariusz Tkaczyk9642fa72019-06-13 16:11:41 +02007856 rt = div64_u64(rt, db/32+1);
NeilBrowndd71cf62009-05-07 12:49:35 +10007857 rt *= dt;
7858 rt >>= 5;
7859
7860 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
7861 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007862
NeilBrownff4e8d92006-07-10 04:44:16 -07007863 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
NeilBrownf7851be2015-07-02 17:12:58 +10007864 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007865}
7866
7867static void *md_seq_start(struct seq_file *seq, loff_t *pos)
7868{
7869 struct list_head *tmp;
7870 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11007871 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872
7873 if (l >= 0x10000)
7874 return NULL;
7875 if (!l--)
7876 /* header */
7877 return (void*)1;
7878
7879 spin_lock(&all_mddevs_lock);
7880 list_for_each(tmp,&all_mddevs)
7881 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11007882 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007883 mddev_get(mddev);
7884 spin_unlock(&all_mddevs_lock);
7885 return mddev;
7886 }
7887 spin_unlock(&all_mddevs_lock);
7888 if (!l--)
7889 return (void*)2;/* tail */
7890 return NULL;
7891}
7892
7893static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
7894{
7895 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11007896 struct mddev *next_mddev, *mddev = v;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007897
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898 ++*pos;
7899 if (v == (void*)2)
7900 return NULL;
7901
7902 spin_lock(&all_mddevs_lock);
7903 if (v == (void*)1)
7904 tmp = all_mddevs.next;
7905 else
7906 tmp = mddev->all_mddevs.next;
7907 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11007908 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 else {
7910 next_mddev = (void*)2;
7911 *pos = 0x10000;
NeilBrownf72ffdd2014-09-30 14:23:59 +10007912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 spin_unlock(&all_mddevs_lock);
7914
7915 if (v != (void*)1)
7916 mddev_put(mddev);
7917 return next_mddev;
7918
7919}
7920
7921static void md_seq_stop(struct seq_file *seq, void *v)
7922{
NeilBrownfd01b882011-10-11 16:47:53 +11007923 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007924
7925 if (mddev && v != (void*)1 && v != (void*)2)
7926 mddev_put(mddev);
7927}
7928
7929static int md_seq_show(struct seq_file *seq, void *v)
7930{
NeilBrownfd01b882011-10-11 16:47:53 +11007931 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11007932 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11007933 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934
7935 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11007936 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937 seq_printf(seq, "Personalities : ");
7938 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08007939 list_for_each_entry(pers, &pers_list, list)
7940 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007941
7942 spin_unlock(&pers_lock);
7943 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02007944 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945 return 0;
7946 }
7947 if (v == (void*)2) {
7948 status_unused(seq);
7949 return 0;
7950 }
7951
NeilBrown36d091f2014-12-15 12:56:58 +11007952 spin_lock(&mddev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007953 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7954 seq_printf(seq, "%s : %sactive", mdname(mddev),
7955 mddev->pers ? "" : "in");
7956 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08007957 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007958 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08007959 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07007960 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961 seq_printf(seq, " %s", mddev->pers->name);
7962 }
7963
Andre Nolldd8ac332009-03-31 14:33:13 +11007964 sectors = 0;
NeilBrownf97fcad2014-12-15 12:56:59 +11007965 rcu_read_lock();
7966 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007967 char b[BDEVNAME_SIZE];
7968 seq_printf(seq, " %s[%d]",
7969 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07007970 if (test_bit(WriteMostly, &rdev->flags))
7971 seq_printf(seq, "(W)");
Shaohua Li9efdca12015-10-12 16:59:50 -07007972 if (test_bit(Journal, &rdev->flags))
7973 seq_printf(seq, "(J)");
NeilBrownb2d444d2005-11-08 21:39:31 -08007974 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007975 seq_printf(seq, "(F)");
7976 continue;
NeilBrown2d78f8c2011-12-23 10:17:51 +11007977 }
7978 if (rdev->raid_disk < 0)
NeilBrownb325a322005-09-09 16:24:00 -07007979 seq_printf(seq, "(S)"); /* spare */
NeilBrown2d78f8c2011-12-23 10:17:51 +11007980 if (test_bit(Replacement, &rdev->flags))
7981 seq_printf(seq, "(R)");
Andre Nolldd8ac332009-03-31 14:33:13 +11007982 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983 }
NeilBrownf97fcad2014-12-15 12:56:59 +11007984 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985
7986 if (!list_empty(&mddev->disks)) {
7987 if (mddev->pers)
7988 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10007989 (unsigned long long)
7990 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007991 else
7992 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11007993 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007994 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07007995 if (mddev->persistent) {
7996 if (mddev->major_version != 0 ||
7997 mddev->minor_version != 90) {
7998 seq_printf(seq," super %d.%d",
7999 mddev->major_version,
8000 mddev->minor_version);
8001 }
NeilBrowne6910632008-02-06 01:39:51 -08008002 } else if (mddev->external)
8003 seq_printf(seq, " super external:%s",
8004 mddev->metadata_type);
8005 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07008006 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007
8008 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11008009 mddev->pers->status(seq, mddev);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008010 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008011 if (mddev->pers->sync_request) {
NeilBrownf7851be2015-07-02 17:12:58 +10008012 if (status_resync(seq, mddev))
NeilBrown8e1b39d2005-11-08 21:39:41 -08008013 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08008014 }
NeilBrown32a76272005-06-21 17:17:14 -07008015 } else
8016 seq_printf(seq, "\n ");
8017
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008018 md_bitmap_status(seq, mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019
8020 seq_printf(seq, "\n");
8021 }
NeilBrown36d091f2014-12-15 12:56:58 +11008022 spin_unlock(&mddev->lock);
NeilBrownf72ffdd2014-09-30 14:23:59 +10008023
Linus Torvalds1da177e2005-04-16 15:20:36 -07008024 return 0;
8025}
8026
Jan Engelhardt110518b2009-05-07 12:49:37 +10008027static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008028 .start = md_seq_start,
8029 .next = md_seq_next,
8030 .stop = md_seq_stop,
8031 .show = md_seq_show,
8032};
8033
8034static int md_seq_open(struct inode *inode, struct file *file)
8035{
Kay Sieversf1514632011-07-12 20:48:39 +02008036 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008037 int error;
8038
8039 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08008040 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02008041 return error;
8042
8043 seq = file->private_data;
8044 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008045 return error;
8046}
8047
NeilBrowne2f23b62014-04-09 14:33:51 +10008048static int md_unloading;
Al Viroafc9a422017-07-03 06:39:46 -04008049static __poll_t mdstat_poll(struct file *filp, poll_table *wait)
NeilBrownd7603b72006-01-06 00:20:30 -08008050{
Kay Sieversf1514632011-07-12 20:48:39 +02008051 struct seq_file *seq = filp->private_data;
Al Viroafc9a422017-07-03 06:39:46 -04008052 __poll_t mask;
NeilBrownd7603b72006-01-06 00:20:30 -08008053
NeilBrowne2f23b62014-04-09 14:33:51 +10008054 if (md_unloading)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008055 return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008056 poll_wait(filp, &md_event_waiters, wait);
8057
8058 /* always allow read */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008059 mask = EPOLLIN | EPOLLRDNORM;
NeilBrownd7603b72006-01-06 00:20:30 -08008060
Kay Sieversf1514632011-07-12 20:48:39 +02008061 if (seq->poll_event != atomic_read(&md_event_count))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08008062 mask |= EPOLLERR | EPOLLPRI;
NeilBrownd7603b72006-01-06 00:20:30 -08008063 return mask;
8064}
8065
Arjan van de Venfa027c22007-02-12 00:55:33 -08008066static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07008067 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068 .open = md_seq_open,
8069 .read = seq_read,
8070 .llseek = seq_lseek,
Cihangir Akturk26e13042017-07-29 19:52:45 +03008071 .release = seq_release,
NeilBrownd7603b72006-01-06 00:20:30 -08008072 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008073};
8074
NeilBrown84fc4b52011-10-11 16:49:58 +11008075int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076{
NeilBrown9d487392016-11-02 14:16:49 +11008077 pr_debug("md: %s personality registered for level %d\n",
8078 p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008079 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008080 list_add_tail(&p->list, &pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081 spin_unlock(&pers_lock);
8082 return 0;
8083}
NeilBrown6c144d32014-09-30 16:15:38 +10008084EXPORT_SYMBOL(register_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008085
NeilBrown84fc4b52011-10-11 16:49:58 +11008086int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008087{
NeilBrown9d487392016-11-02 14:16:49 +11008088 pr_debug("md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008089 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08008090 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008091 spin_unlock(&pers_lock);
8092 return 0;
8093}
NeilBrown6c144d32014-09-30 16:15:38 +10008094EXPORT_SYMBOL(unregister_md_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008095
NeilBrown6022e752015-08-13 12:32:55 +10008096int register_md_cluster_operations(struct md_cluster_operations *ops,
8097 struct module *module)
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008098{
NeilBrown6022e752015-08-13 12:32:55 +10008099 int ret = 0;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008100 spin_lock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008101 if (md_cluster_ops != NULL)
8102 ret = -EALREADY;
8103 else {
8104 md_cluster_ops = ops;
8105 md_cluster_mod = module;
8106 }
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008107 spin_unlock(&pers_lock);
NeilBrown6022e752015-08-13 12:32:55 +10008108 return ret;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008109}
8110EXPORT_SYMBOL(register_md_cluster_operations);
8111
8112int unregister_md_cluster_operations(void)
8113{
8114 spin_lock(&pers_lock);
8115 md_cluster_ops = NULL;
8116 spin_unlock(&pers_lock);
8117 return 0;
8118}
8119EXPORT_SYMBOL(unregister_md_cluster_operations);
8120
8121int md_setup_cluster(struct mddev *mddev, int nodes)
8122{
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008123 if (!md_cluster_ops)
8124 request_module("md-cluster");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008125 spin_lock(&pers_lock);
Guoqing Jiang47a7b0d2016-09-04 22:17:28 -04008126 /* ensure module won't be unloaded */
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008127 if (!md_cluster_ops || !try_module_get(md_cluster_mod)) {
NeilBrown9d487392016-11-02 14:16:49 +11008128 pr_warn("can't find md-cluster module or get it's reference.\n");
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008129 spin_unlock(&pers_lock);
8130 return -ENOENT;
8131 }
8132 spin_unlock(&pers_lock);
8133
Goldwyn Rodriguescf921cc2014-03-30 00:42:49 -05008134 return md_cluster_ops->join(mddev, nodes);
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008135}
8136
8137void md_cluster_stop(struct mddev *mddev)
8138{
Goldwyn Rodriguesc4ce8672014-03-29 10:20:02 -05008139 if (!md_cluster_ops)
8140 return;
Goldwyn Rodriguesedb39c92014-03-29 10:01:53 -05008141 md_cluster_ops->leave(mddev);
8142 module_put(md_cluster_mod);
8143}
8144
NeilBrownfd01b882011-10-11 16:47:53 +11008145static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008146{
NeilBrownf72ffdd2014-09-30 14:23:59 +10008147 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008148 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11008149 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150
8151 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10008152 rcu_read_lock();
8153 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008154 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Michael Callahan59767fb2018-07-18 04:47:37 -07008155 curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
NeilBrowneea1bf32009-03-31 14:27:02 +11008156 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07008157 /* sync IO will cause sync_io to increase before the disk_stats
8158 * as sync_io is counted when a request starts, and
8159 * disk_stats is counted when it completes.
8160 * So resync activity will cause curr_events to be smaller than
8161 * when there was no such activity.
8162 * non-sync IO will cause disk_stat to increase without
8163 * increasing sync_io so curr_events will (eventually)
8164 * be larger than it was before. Once it becomes
8165 * substantially larger, the test below will cause
8166 * the array to appear non-idle, and resync will slow
8167 * down.
8168 * If there is a lot of outstanding resync activity when
8169 * we set last_event to curr_events, then all that activity
8170 * completing might cause the array to appear non-idle
8171 * and resync will be slowed down even though there might
8172 * not have been non-resync activity. This will only
8173 * happen once though. 'last_events' will soon reflect
8174 * the state where there is little or no outstanding
8175 * resync requests, and further resync activity will
8176 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08008177 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178 */
NeilBrowneea1bf32009-03-31 14:27:02 +11008179 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008180 rdev->last_events = curr_events;
8181 idle = 0;
8182 }
8183 }
NeilBrown4b809912008-07-21 17:05:25 +10008184 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008185 return idle;
8186}
8187
NeilBrownfd01b882011-10-11 16:47:53 +11008188void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189{
8190 /* another "blocks" (512byte) blocks have been synced */
8191 atomic_sub(blocks, &mddev->recovery_active);
8192 wake_up(&mddev->recovery_wait);
8193 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07008194 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
majianpeng0a19caa2012-11-19 19:57:34 +08008195 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196 md_wakeup_thread(mddev->thread);
8197 // stop recovery, signal do_sync ....
8198 }
8199}
NeilBrown6c144d32014-09-30 16:15:38 +10008200EXPORT_SYMBOL(md_done_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201
NeilBrown06d91a52005-06-21 17:17:12 -07008202/* md_write_start(mddev, bi)
8203 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07008204 * in superblock) before writing, schedule a superblock update
8205 * and wait for it to complete.
NeilBrowncc27b0c2017-06-05 16:49:39 +10008206 * A return value of 'false' means that the write wasn't recorded
8207 * and cannot proceed as the array is being suspend.
NeilBrown06d91a52005-06-21 17:17:12 -07008208 */
NeilBrowncc27b0c2017-06-05 16:49:39 +10008209bool md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008210{
Neil Brown0fd62b82008-06-28 08:31:36 +10008211 int did_change = 0;
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008212
NeilBrown06d91a52005-06-21 17:17:12 -07008213 if (bio_data_dir(bi) != WRITE)
NeilBrowncc27b0c2017-06-05 16:49:39 +10008214 return true;
NeilBrown06d91a52005-06-21 17:17:12 -07008215
NeilBrownf91de922005-11-08 21:39:36 -08008216 BUG_ON(mddev->ro == 1);
8217 if (mddev->ro == 2) {
8218 /* need to switch to read/write */
8219 mddev->ro = 0;
8220 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8221 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08008222 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008223 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08008224 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008225 rcu_read_lock();
8226 percpu_ref_get(&mddev->writes_pending);
NeilBrown55cc39f2017-03-15 14:05:14 +11008227 smp_mb(); /* Match smp_mb in set_in_sync() */
NeilBrown31a59e32008-04-30 00:52:30 -07008228 if (mddev->safemode == 1)
8229 mddev->safemode = 0;
NeilBrown4ad23a972017-03-15 14:05:14 +11008230 /* sync_checkers is always 0 when writes_pending is in per-cpu mode */
NeilBrown81fe48e2017-08-08 16:56:36 +10008231 if (mddev->in_sync || mddev->sync_checkers) {
NeilBrown85572d72014-12-15 12:56:56 +11008232 spin_lock(&mddev->lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07008233 if (mddev->in_sync) {
8234 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008235 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8236 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07008237 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10008238 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07008239 }
NeilBrown85572d72014-12-15 12:56:56 +11008240 spin_unlock(&mddev->lock);
NeilBrown06d91a52005-06-21 17:17:12 -07008241 }
NeilBrown4ad23a972017-03-15 14:05:14 +11008242 rcu_read_unlock();
Neil Brown0fd62b82008-06-28 08:31:36 +10008243 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10008244 sysfs_notify_dirent_safe(mddev->sysfs_state);
Heinz Mauelshagen4b6c1062018-02-02 23:13:19 +01008245 if (!mddev->has_superblocks)
8246 return true;
NeilBrown09a44cc2008-05-23 13:04:36 -07008247 wait_event(mddev->sb_wait,
NeilBrownd47c8ad2017-10-05 16:23:16 +11008248 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) ||
8249 mddev->suspended);
NeilBrowncc27b0c2017-06-05 16:49:39 +10008250 if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
8251 percpu_ref_put(&mddev->writes_pending);
8252 return false;
8253 }
8254 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255}
NeilBrown6c144d32014-09-30 16:15:38 +10008256EXPORT_SYMBOL(md_write_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008257
NeilBrown49728052017-03-15 14:05:12 +11008258/* md_write_inc can only be called when md_write_start() has
8259 * already been called at least once of the current request.
8260 * It increments the counter and is useful when a single request
8261 * is split into several parts. Each part causes an increment and
8262 * so needs a matching md_write_end().
8263 * Unlike md_write_start(), it is safe to call md_write_inc() inside
8264 * a spinlocked region.
8265 */
8266void md_write_inc(struct mddev *mddev, struct bio *bi)
8267{
8268 if (bio_data_dir(bi) != WRITE)
8269 return;
8270 WARN_ON_ONCE(mddev->in_sync || mddev->ro);
NeilBrown4ad23a972017-03-15 14:05:14 +11008271 percpu_ref_get(&mddev->writes_pending);
NeilBrown49728052017-03-15 14:05:12 +11008272}
8273EXPORT_SYMBOL(md_write_inc);
8274
NeilBrownfd01b882011-10-11 16:47:53 +11008275void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276{
NeilBrown4ad23a972017-03-15 14:05:14 +11008277 percpu_ref_put(&mddev->writes_pending);
8278
8279 if (mddev->safemode == 2)
8280 md_wakeup_thread(mddev->thread);
8281 else if (mddev->safemode_delay)
8282 /* The roundup() ensures this only performs locking once
8283 * every ->safemode_delay jiffies
8284 */
8285 mod_timer(&mddev->safemode_timer,
8286 roundup(jiffies, mddev->safemode_delay) +
8287 mddev->safemode_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008288}
NeilBrown4ad23a972017-03-15 14:05:14 +11008289
NeilBrown6c144d32014-09-30 16:15:38 +10008290EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291
NeilBrown2a2275d2007-01-26 00:57:11 -08008292/* md_allow_write(mddev)
8293 * Calling this ensures that the array is marked 'active' so that writes
8294 * may proceed without blocking. It is important to call this before
8295 * attempting a GFP_KERNEL allocation while holding the mddev lock.
8296 * Must be called with mddev_lock held.
8297 */
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008298void md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08008299{
8300 if (!mddev->pers)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008301 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008302 if (mddev->ro)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008303 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10008304 if (!mddev->pers->sync_request)
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008305 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08008306
NeilBrown85572d72014-12-15 12:56:56 +11008307 spin_lock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008308 if (mddev->in_sync) {
8309 mddev->in_sync = 0;
Shaohua Li29530792016-12-08 15:48:19 -08008310 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
8311 set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08008312 if (mddev->safemode_delay &&
8313 mddev->safemode == 0)
8314 mddev->safemode = 1;
NeilBrown85572d72014-12-15 12:56:56 +11008315 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008316 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10008317 sysfs_notify_dirent_safe(mddev->sysfs_state);
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008318 /* wait for the dirty state to be recorded in the metadata */
8319 wait_event(mddev->sb_wait,
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02008320 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08008321 } else
NeilBrown85572d72014-12-15 12:56:56 +11008322 spin_unlock(&mddev->lock);
NeilBrown2a2275d2007-01-26 00:57:11 -08008323}
8324EXPORT_SYMBOL_GPL(md_allow_write);
8325
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326#define SYNC_MARKS 10
8327#define SYNC_MARK_STEP (3*HZ)
majianpeng54f89342012-10-31 11:59:10 +11008328#define UPDATE_FREQUENCY (5*60*HZ)
Shaohua Li4ed87312012-10-11 13:34:00 +11008329void md_do_sync(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008330{
Shaohua Li4ed87312012-10-11 13:34:00 +11008331 struct mddev *mddev = thread->mddev;
NeilBrownfd01b882011-10-11 16:47:53 +11008332 struct mddev *mddev2;
Yufen Yue5b521e2019-06-14 15:41:07 -07008333 unsigned int currspeed = 0, window;
Xiao Niac7e50a2014-08-07 09:37:41 -04008334 sector_t max_sectors,j, io_sectors, recovery_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008335 unsigned long mark[SYNC_MARKS];
majianpeng54f89342012-10-31 11:59:10 +11008336 unsigned long update_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008337 sector_t mark_cnt[SYNC_MARKS];
8338 int last_mark,m;
8339 struct list_head *tmp;
8340 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07008341 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11008342 struct md_rdev *rdev;
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008343 char *desc, *action = NULL;
majianpeng7c2c57c2012-07-03 12:12:26 +10008344 struct blk_plug plug;
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008345 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008346
8347 /* just incase thread restarts... */
Song Liud5d885f2017-11-19 22:17:01 -08008348 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
8349 test_bit(MD_RECOVERY_WAIT, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008350 return;
NeilBrown3991b312014-05-28 13:39:23 +10008351 if (mddev->ro) {/* never try to sync a read-only array */
8352 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008353 return;
NeilBrown3991b312014-05-28 13:39:23 +10008354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008356 if (mddev_is_clustered(mddev)) {
8357 ret = md_cluster_ops->resync_start(mddev);
8358 if (ret)
8359 goto skip;
8360
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008361 set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags);
Guoqing Jiang41a9a0d2016-05-02 11:33:08 -04008362 if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
8363 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) ||
8364 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
8365 && ((unsigned long long)mddev->curr_resync_completed
8366 < (unsigned long long)mddev->resync_max_sectors))
8367 goto skip;
8368 }
8369
NeilBrown61df9d92006-10-03 01:15:57 -07008370 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008371 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008372 desc = "data-check";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008373 action = "check";
8374 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
NeilBrown61df9d92006-10-03 01:15:57 -07008375 desc = "requested-resync";
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008376 action = "repair";
8377 } else
NeilBrown61df9d92006-10-03 01:15:57 -07008378 desc = "resync";
8379 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
8380 desc = "reshape";
8381 else
8382 desc = "recovery";
8383
Jonathan Brassowc4a39552013-06-25 01:23:59 -05008384 mddev->last_sync_action = action ?: desc;
8385
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386 /* we overload curr_resync somewhat here.
8387 * 0 == not engaged in resync at all
8388 * 2 == checking that there is no conflict with another sync
8389 * 1 == like 2, but have yielded to allow conflicting resync to
Yufen Yue5b521e2019-06-14 15:41:07 -07008390 * commence
Linus Torvalds1da177e2005-04-16 15:20:36 -07008391 * other == active in resync - this many blocks
8392 *
8393 * Before starting a resync we must have set curr_resync to
8394 * 2, and then checked that every "conflicting" array has curr_resync
8395 * less than ours. When we find one that is the same or higher
8396 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
8397 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
8398 * This will mean we have to start checking from the beginning again.
8399 *
8400 */
8401
8402 do {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008403 int mddev2_minor = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404 mddev->curr_resync = 2;
8405
8406 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11008407 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08008409 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410 if (mddev2 == mddev)
8411 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07008412 if (!mddev->parallel_resync
8413 && mddev2->curr_resync
8414 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 DEFINE_WAIT(wq);
8416 if (mddev < mddev2 && mddev->curr_resync == 2) {
8417 /* arbitrarily yield */
8418 mddev->curr_resync = 1;
8419 wake_up(&resync_wait);
8420 }
8421 if (mddev > mddev2 && mddev->curr_resync == 1)
8422 /* no need to wait here, we can wait the next
8423 * time 'round when curr_resync == 2
8424 */
8425 continue;
NeilBrown97441972008-09-19 11:49:54 +10008426 /* We need to wait 'interruptible' so as not to
8427 * contribute to the load average, and not to
8428 * be caught by 'softlockup'
8429 */
8430 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrownc91abf52013-11-19 12:02:01 +11008431 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown8712e552005-10-26 01:58:58 -07008432 mddev2->curr_resync >= mddev->curr_resync) {
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008433 if (mddev2_minor != mddev2->md_minor) {
8434 mddev2_minor = mddev2->md_minor;
NeilBrown9d487392016-11-02 14:16:49 +11008435 pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n",
8436 desc, mdname(mddev),
8437 mdname(mddev2));
Artur Paszkiewiczc622ca52016-08-16 14:26:08 +02008438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10008440 if (signal_pending(current))
8441 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442 schedule();
8443 finish_wait(&resync_wait, &wq);
8444 goto try_again;
8445 }
8446 finish_wait(&resync_wait, &wq);
8447 }
8448 }
8449 } while (mddev->curr_resync < 2);
8450
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008451 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08008452 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008453 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07008454 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07008455 */
8456 max_sectors = mddev->resync_max_sectors;
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11008457 atomic64_set(&mddev->resync_mismatches, 0);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008458 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10008459 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8460 j = mddev->resync_min;
8461 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008462 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10008463
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008464 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
NeilBrownc804cde2012-05-21 09:28:33 +10008465 max_sectors = mddev->resync_max_sectors;
Guoqing Jiangcb9ee152018-10-18 16:37:47 +08008466 /*
8467 * If the original node aborts reshaping then we continue the
8468 * reshaping, so set j again to avoid restart reshape from the
8469 * first beginning
8470 */
8471 if (mddev_is_clustered(mddev) &&
8472 mddev->reshape_position != MaxSector)
8473 j = mddev->reshape_position;
8474 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008475 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11008476 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008477 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07008478 rcu_read_lock();
NeilBrowndafb20f2012-03-19 12:46:39 +11008479 rdev_for_each_rcu(rdev, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008480 if (rdev->raid_disk >= 0 &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008481 !test_bit(Journal, &rdev->flags) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008482 !test_bit(Faulty, &rdev->flags) &&
8483 !test_bit(In_sync, &rdev->flags) &&
8484 rdev->recovery_offset < j)
8485 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07008486 rcu_read_unlock();
NeilBrown133d4522014-07-02 12:04:14 +10008487
8488 /* If there is a bitmap, we need to make sure all
8489 * writes that started before we added a spare
8490 * complete before we start doing a recovery.
8491 * Otherwise the write might complete and (via
8492 * bitmap_endwrite) set a bit in the bitmap after the
8493 * recovery has checked that bit and skipped that
8494 * region.
8495 */
8496 if (mddev->bitmap) {
8497 mddev->pers->quiesce(mddev, 1);
8498 mddev->pers->quiesce(mddev, 0);
8499 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501
NeilBrown9d487392016-11-02 14:16:49 +11008502 pr_info("md: %s of RAID array %s\n", desc, mdname(mddev));
8503 pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev));
8504 pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n",
8505 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008506
NeilBrowneea1bf32009-03-31 14:27:02 +11008507 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008508
NeilBrown57afd892005-06-21 17:17:13 -07008509 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510 for (m = 0; m < SYNC_MARKS; m++) {
8511 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008512 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513 }
8514 last_mark = 0;
8515 mddev->resync_mark = mark[last_mark];
8516 mddev->resync_mark_cnt = mark_cnt[last_mark];
8517
8518 /*
8519 * Tune reconstruction:
8520 */
Yufen Yue5b521e2019-06-14 15:41:07 -07008521 window = 32 * (PAGE_SIZE / 512);
NeilBrown9d487392016-11-02 14:16:49 +11008522 pr_debug("md: using %dk window, over a total of %lluk.\n",
8523 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008524
8525 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008526 last_check = 0;
8527
8528 if (j>2) {
NeilBrown9d487392016-11-02 14:16:49 +11008529 pr_debug("md: resuming %s of %s from checkpoint.\n",
8530 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531 mddev->curr_resync = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008532 } else
8533 mddev->curr_resync = 3; /* no longer delayed */
NeilBrown75d3da42011-01-14 09:14:34 +11008534 mddev->curr_resync_completed = j;
NeilBrown72f36d52012-10-11 14:25:57 +11008535 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8536 md_new_event(mddev);
majianpeng54f89342012-10-31 11:59:10 +11008537 update_time = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538
majianpeng7c2c57c2012-07-03 12:12:26 +10008539 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07008541 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542
NeilBrown57afd892005-06-21 17:17:13 -07008543 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11008544
NeilBrown7a91ee12009-05-26 12:57:21 +10008545 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8546 ((mddev->curr_resync > mddev->curr_resync_completed &&
8547 (mddev->curr_resync - mddev->curr_resync_completed)
8548 > (max_sectors >> 4)) ||
majianpeng54f89342012-10-31 11:59:10 +11008549 time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
NeilBrown7a91ee12009-05-26 12:57:21 +10008550 (j - mddev->curr_resync_completed)*2
NeilBrownc5e19d92015-07-17 12:06:02 +10008551 >= mddev->resync_max - mddev->curr_resync_completed ||
8552 mddev->curr_resync_completed > mddev->resync_max
NeilBrown7a91ee12009-05-26 12:57:21 +10008553 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11008554 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11008555 wait_event(mddev->recovery_wait,
8556 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11008557 mddev->curr_resync_completed = j;
kernelmail35d78c62012-10-31 11:59:10 +11008558 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
8559 j > mddev->recovery_cp)
8560 mddev->recovery_cp = j;
majianpeng54f89342012-10-31 11:59:10 +11008561 update_time = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08008562 set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
NeilBrownacb180b2009-04-14 16:28:34 +10008563 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11008564 }
NeilBrownacb180b2009-04-14 16:28:34 +10008565
NeilBrownc91abf52013-11-19 12:02:01 +11008566 while (j >= mddev->resync_max &&
8567 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
NeilBrowne62e58a2009-07-01 13:15:35 +10008568 /* As this condition is controlled by user-space,
8569 * we can block indefinitely, so use '_interruptible'
8570 * to avoid triggering warnings.
8571 */
8572 flush_signals(current); /* just in case */
8573 wait_event_interruptible(mddev->recovery_wait,
8574 mddev->resync_max > j
NeilBrownc91abf52013-11-19 12:02:01 +11008575 || test_bit(MD_RECOVERY_INTR,
8576 &mddev->recovery));
NeilBrowne62e58a2009-07-01 13:15:35 +10008577 }
NeilBrownacb180b2009-04-14 16:28:34 +10008578
NeilBrownc91abf52013-11-19 12:02:01 +11008579 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8580 break;
NeilBrownacb180b2009-04-14 16:28:34 +10008581
NeilBrown09314792015-02-19 16:04:40 +11008582 sectors = mddev->pers->sync_request(mddev, j, &skipped);
NeilBrown57afd892005-06-21 17:17:13 -07008583 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07008584 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrownc91abf52013-11-19 12:02:01 +11008585 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008586 }
NeilBrown57afd892005-06-21 17:17:13 -07008587
8588 if (!skipped) { /* actual IO requested */
8589 io_sectors += sectors;
8590 atomic_add(sectors, &mddev->recovery_active);
8591 }
8592
NeilBrowne875ece2011-07-28 11:39:24 +10008593 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8594 break;
8595
Linus Torvalds1da177e2005-04-16 15:20:36 -07008596 j += sectors;
NeilBrown5ed1df22015-07-24 13:27:08 +10008597 if (j > max_sectors)
8598 /* when skipping, extra large numbers can be returned. */
8599 j = max_sectors;
NeilBrown72f36d52012-10-11 14:25:57 +11008600 if (j > 2)
8601 mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07008602 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08008603 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10008604 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08008605 * visible in /proc/mdstat
8606 */
8607 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07008608
8609 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008610 continue;
8611
NeilBrown57afd892005-06-21 17:17:13 -07008612 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613 repeat:
8614 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
8615 /* step marks */
8616 int next = (last_mark+1) % SYNC_MARKS;
8617
8618 mddev->resync_mark = mark[next];
8619 mddev->resync_mark_cnt = mark_cnt[next];
8620 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07008621 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008622 last_mark = next;
8623 }
8624
NeilBrownc91abf52013-11-19 12:02:01 +11008625 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8626 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008627
8628 /*
8629 * this loop exits only if either when we are slower than
8630 * the 'hard' speed limit, or the system was IO-idle for
8631 * a jiffy.
8632 * the system might be non-idle CPU-wise, but we only care
8633 * about not overloading the IO subsystem. (things like an
8634 * e2fsck being done on the RAID array should execute fast)
8635 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008636 cond_resched();
8637
Xiao Niac7e50a2014-08-07 09:37:41 -04008638 recovery_done = io_sectors - atomic_read(&mddev->recovery_active);
8639 currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2
NeilBrown57afd892005-06-21 17:17:13 -07008640 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641
NeilBrown88202a02006-01-06 00:21:36 -08008642 if (currspeed > speed_min(mddev)) {
NeilBrownac8fa412015-02-19 16:55:00 +11008643 if (currspeed > speed_max(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08008644 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008645 goto repeat;
8646 }
NeilBrownac8fa412015-02-19 16:55:00 +11008647 if (!is_mddev_idle(mddev, 0)) {
8648 /*
8649 * Give other IO more of a chance.
8650 * The faster the devices, the less we wait.
8651 */
8652 wait_event(mddev->recovery_wait,
8653 !atomic_read(&mddev->recovery_active));
8654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008655 }
8656 }
NeilBrown9d487392016-11-02 14:16:49 +11008657 pr_info("md: %s: %s %s.\n",mdname(mddev), desc,
8658 test_bit(MD_RECOVERY_INTR, &mddev->recovery)
8659 ? "interrupted" : "done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008660 /*
8661 * this also signals 'finished resyncing' to md_stop
8662 */
majianpeng7c2c57c2012-07-03 12:12:26 +10008663 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008664 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
8665
NeilBrown5ed1df22015-07-24 13:27:08 +10008666 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8667 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008668 mddev->curr_resync > 3) {
NeilBrown5ed1df22015-07-24 13:27:08 +10008669 mddev->curr_resync_completed = mddev->curr_resync;
8670 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
8671 }
NeilBrown09314792015-02-19 16:04:40 +11008672 mddev->pers->sync_request(mddev, max_sectors, &skipped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008673
NeilBrowndfc70642008-05-23 13:04:39 -07008674 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown1217e1d2016-10-28 15:59:41 +11008675 mddev->curr_resync > 3) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008676 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
8677 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8678 if (mddev->curr_resync >= mddev->recovery_cp) {
NeilBrown9d487392016-11-02 14:16:49 +11008679 pr_debug("md: checkpointing %s of %s.\n",
8680 desc, mdname(mddev));
majianpeng0a19caa2012-11-19 19:57:34 +08008681 if (test_bit(MD_RECOVERY_ERROR,
8682 &mddev->recovery))
8683 mddev->recovery_cp =
8684 mddev->curr_resync_completed;
8685 else
8686 mddev->recovery_cp =
8687 mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008688 }
8689 } else
8690 mddev->recovery_cp = MaxSector;
8691 } else {
8692 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
8693 mddev->curr_resync = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008694 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8695 test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) {
8696 rcu_read_lock();
8697 rdev_for_each_rcu(rdev, mddev)
8698 if (rdev->raid_disk >= 0 &&
8699 mddev->delta_disks >= 0 &&
8700 !test_bit(Journal, &rdev->flags) &&
8701 !test_bit(Faulty, &rdev->flags) &&
8702 !test_bit(In_sync, &rdev->flags) &&
8703 rdev->recovery_offset < mddev->curr_resync)
8704 rdev->recovery_offset = mddev->curr_resync;
8705 rcu_read_unlock();
8706 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07008707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008708 }
NeilBrowndb91ff52012-02-07 12:01:51 +11008709 skip:
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04008710 /* set CHANGE_PENDING here since maybe another update is needed,
8711 * so other nodes are informed. It should be harmless for normal
8712 * raid */
Shaohua Li29530792016-12-08 15:48:19 -08008713 set_mask_bits(&mddev->sb_flags, 0,
8714 BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS));
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008715
BingJing Chang88763912018-02-22 13:34:46 +08008716 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
8717 !test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
8718 mddev->delta_disks > 0 &&
8719 mddev->pers->finish_reshape &&
8720 mddev->pers->size &&
8721 mddev->queue) {
8722 mddev_lock_nointr(mddev);
8723 md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0));
8724 mddev_unlock(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08008725 if (!mddev_is_clustered(mddev)) {
8726 set_capacity(mddev->gendisk, mddev->array_sectors);
8727 revalidate_disk(mddev->gendisk);
8728 }
BingJing Chang88763912018-02-22 13:34:46 +08008729 }
8730
NeilBrown23da4222014-12-15 12:57:01 +11008731 spin_lock(&mddev->lock);
NeilBrownc07b70a2009-12-14 12:49:48 +11008732 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8733 /* We completed so min/max setting can be forgotten if used. */
8734 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8735 mddev->resync_min = 0;
8736 mddev->resync_max = MaxSector;
8737 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
8738 mddev->resync_min = mddev->curr_resync_completed;
NeilBrownf7851be2015-07-02 17:12:58 +10008739 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 mddev->curr_resync = 0;
NeilBrown23da4222014-12-15 12:57:01 +11008741 spin_unlock(&mddev->lock);
8742
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743 wake_up(&resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08008745 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008746}
NeilBrown29269552006-03-27 01:18:10 -08008747EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008748
NeilBrown746d3202013-04-24 11:42:41 +10008749static int remove_and_add_spares(struct mddev *mddev,
8750 struct md_rdev *this)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008751{
NeilBrown3cb03002011-10-11 16:45:26 +11008752 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008753 int spares = 0;
NeilBrownf2a371c2012-01-09 00:46:41 +11008754 int removed = 0;
NeilBrownd787be42016-06-02 16:19:53 +10008755 bool remove_some = false;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008756
NeilBrown39772f02018-02-03 09:19:30 +11008757 if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
8758 /* Mustn't remove devices when resync thread is running */
8759 return 0;
8760
NeilBrownd787be42016-06-02 16:19:53 +10008761 rdev_for_each(rdev, mddev) {
NeilBrown746d3202013-04-24 11:42:41 +10008762 if ((this == NULL || rdev == this) &&
8763 rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07008764 !test_bit(Blocked, &rdev->flags) &&
NeilBrownd787be42016-06-02 16:19:53 +10008765 test_bit(Faulty, &rdev->flags) &&
8766 atomic_read(&rdev->nr_pending)==0) {
8767 /* Faulty non-Blocked devices with nr_pending == 0
8768 * never get nr_pending incremented,
8769 * never get Faulty cleared, and never get Blocked set.
8770 * So we can synchronize_rcu now rather than once per device
8771 */
8772 remove_some = true;
8773 set_bit(RemoveSynchronized, &rdev->flags);
8774 }
8775 }
8776
8777 if (remove_some)
8778 synchronize_rcu();
8779 rdev_for_each(rdev, mddev) {
8780 if ((this == NULL || rdev == this) &&
8781 rdev->raid_disk >= 0 &&
8782 !test_bit(Blocked, &rdev->flags) &&
8783 ((test_bit(RemoveSynchronized, &rdev->flags) ||
Shaohua Lif2076e72015-10-08 21:54:12 -07008784 (!test_bit(In_sync, &rdev->flags) &&
8785 !test_bit(Journal, &rdev->flags))) &&
NeilBrownd787be42016-06-02 16:19:53 +10008786 atomic_read(&rdev->nr_pending)==0)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008787 if (mddev->pers->hot_remove_disk(
NeilBrownb8321b62011-12-23 10:17:51 +11008788 mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10008789 sysfs_unlink_rdev(mddev, rdev);
NeilBrown011abdc2018-04-26 14:46:29 +10008790 rdev->saved_raid_disk = rdev->raid_disk;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008791 rdev->raid_disk = -1;
NeilBrownf2a371c2012-01-09 00:46:41 +11008792 removed++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008793 }
8794 }
NeilBrownd787be42016-06-02 16:19:53 +10008795 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
8796 clear_bit(RemoveSynchronized, &rdev->flags);
8797 }
8798
Jonathan Brassow90584fc2013-03-07 16:24:26 -06008799 if (removed && mddev->kobj.sd)
8800 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008801
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008802 if (this && removed)
NeilBrown746d3202013-04-24 11:42:41 +10008803 goto no_add;
8804
NeilBrowndafb20f2012-03-19 12:46:39 +11008805 rdev_for_each(rdev, mddev) {
Goldwyn Rodrigues2910ff12015-09-28 10:27:26 -05008806 if (this && this != rdev)
8807 continue;
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05008808 if (test_bit(Candidate, &rdev->flags))
8809 continue;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008810 if (rdev->raid_disk >= 0 &&
8811 !test_bit(In_sync, &rdev->flags) &&
Shaohua Lif2076e72015-10-08 21:54:12 -07008812 !test_bit(Journal, &rdev->flags) &&
NeilBrown7bfec5f2011-12-23 10:17:53 +11008813 !test_bit(Faulty, &rdev->flags))
8814 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008815 if (rdev->raid_disk >= 0)
8816 continue;
8817 if (test_bit(Faulty, &rdev->flags))
8818 continue;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008819 if (!test_bit(Journal, &rdev->flags)) {
8820 if (mddev->ro &&
8821 ! (rdev->saved_raid_disk >= 0 &&
8822 !test_bit(Bitmap_sync, &rdev->flags)))
8823 continue;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008824
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008825 rdev->recovery_offset = 0;
8826 }
NeilBrown7ceb17e2013-04-24 11:42:42 +10008827 if (mddev->pers->
8828 hot_add_disk(mddev, rdev) == 0) {
8829 if (sysfs_link_rdev(mddev, rdev))
8830 /* failure here is OK */;
Shaohua Lif6b6ec52015-12-21 10:51:02 +11008831 if (!test_bit(Journal, &rdev->flags))
8832 spares++;
NeilBrown7ceb17e2013-04-24 11:42:42 +10008833 md_new_event(mddev);
Shaohua Li29530792016-12-08 15:48:19 -08008834 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrowndfc70642008-05-23 13:04:39 -07008835 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008836 }
NeilBrown746d3202013-04-24 11:42:41 +10008837no_add:
NeilBrown6dafab62012-09-19 12:54:22 +10008838 if (removed)
Shaohua Li29530792016-12-08 15:48:19 -08008839 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008840 return spares;
8841}
NeilBrown7ebc0be2011-01-14 09:14:33 +11008842
NeilBrownac05f252014-09-30 08:10:42 +10008843static void md_start_sync(struct work_struct *ws)
8844{
8845 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Goldwyn Rodriguesc186b122015-09-30 13:20:35 -05008846
NeilBrownac05f252014-09-30 08:10:42 +10008847 mddev->sync_thread = md_register_thread(md_do_sync,
8848 mddev,
8849 "resync");
8850 if (!mddev->sync_thread) {
NeilBrown9d487392016-11-02 14:16:49 +11008851 pr_warn("%s: could not start resync thread...\n",
8852 mdname(mddev));
NeilBrownac05f252014-09-30 08:10:42 +10008853 /* leave the spares where they are, it shouldn't hurt */
8854 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8855 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8856 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
8857 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
8858 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11008859 wake_up(&resync_wait);
NeilBrownac05f252014-09-30 08:10:42 +10008860 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
8861 &mddev->recovery))
8862 if (mddev->sysfs_action)
8863 sysfs_notify_dirent_safe(mddev->sysfs_action);
8864 } else
8865 md_wakeup_thread(mddev->sync_thread);
8866 sysfs_notify_dirent_safe(mddev->sysfs_action);
8867 md_new_event(mddev);
8868}
8869
Linus Torvalds1da177e2005-04-16 15:20:36 -07008870/*
8871 * This routine is regularly called by all per-raid-array threads to
8872 * deal with generic issues like resync and super-block update.
8873 * Raid personalities that don't have a thread (linear/raid0) do not
8874 * need this as they never do any recovery or update the superblock.
8875 *
8876 * It does not do any resync itself, but rather "forks" off other threads
8877 * to do that as needed.
8878 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
8879 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07008880 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07008881 * and wakeups up this thread which will reap the thread and finish up.
8882 * This thread also removes any faulty devices (with nr_pending == 0).
8883 *
8884 * The overall approach is:
8885 * 1/ if the superblock needs updating, update it.
8886 * 2/ If a recovery thread is running, don't do anything else.
8887 * 3/ If recovery has finished, clean up, possibly marking spares active.
8888 * 4/ If there are any faulty devices, remove them.
8889 * 5/ If array is degraded, try to add spares devices
8890 * 6/ If array has spares or is not in-sync, start a resync thread.
8891 */
NeilBrownfd01b882011-10-11 16:47:53 +11008892void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008893{
NeilBrown059421e2018-10-03 15:04:41 +10008894 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) {
8895 /* Write superblock - thread that called mddev_suspend()
8896 * holds reconfig_mutex for us.
8897 */
8898 set_bit(MD_UPDATING_SB, &mddev->flags);
8899 smp_mb__after_atomic();
8900 if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags))
8901 md_update_sb(mddev, 0);
8902 clear_bit_unlock(MD_UPDATING_SB, &mddev->flags);
8903 wake_up(&mddev->sb_wait);
8904 }
8905
Jonathan Brassow68866e42011-06-08 15:10:08 +10008906 if (mddev->suspended)
8907 return;
8908
NeilBrown5f404022005-06-21 17:17:16 -07008909 if (mddev->bitmap)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008910 md_bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008911
NeilBrownfca4d842005-06-21 17:17:11 -07008912 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07008913 if (mddev->pers->sync_request && !mddev->external) {
NeilBrown9d487392016-11-02 14:16:49 +11008914 pr_debug("md: %s in immediate safe mode\n",
8915 mdname(mddev));
NeilBrownfca4d842005-06-21 17:17:11 -07008916 mddev->safemode = 2;
8917 }
8918 flush_signals(current);
8919 }
8920
NeilBrownc89a8ee2008-08-05 15:54:13 +10008921 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
8922 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008923 if ( ! (
Shaohua Li29530792016-12-08 15:48:19 -08008924 (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07008926 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07008927 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrown4ad23a972017-03-15 14:05:14 +11008928 (mddev->safemode == 2
NeilBrownfca4d842005-06-21 17:17:11 -07008929 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008930 ))
8931 return;
NeilBrownfca4d842005-06-21 17:17:11 -07008932
NeilBrowndf5b89b2006-03-27 01:18:20 -08008933 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08008934 int spares = 0;
NeilBrown480523f2019-08-20 10:21:09 +10008935 bool try_set_sync = mddev->safemode != 0;
NeilBrownfca4d842005-06-21 17:17:11 -07008936
Shaohua Liafc1f552017-08-11 20:34:45 -07008937 if (!mddev->external && mddev->safemode == 1)
NeilBrown33182d12017-08-08 16:56:36 +10008938 mddev->safemode = 0;
8939
NeilBrownc89a8ee2008-08-05 15:54:13 +10008940 if (mddev->ro) {
Neil Brownab16bfc2015-06-17 12:31:46 +10008941 struct md_rdev *rdev;
8942 if (!mddev->external && mddev->in_sync)
8943 /* 'Blocked' flag not needed as failed devices
8944 * will be recorded if array switched to read/write.
8945 * Leaving it set will prevent the device
8946 * from being removed.
8947 */
8948 rdev_for_each(rdev, mddev)
8949 clear_bit(Blocked, &rdev->flags);
NeilBrown7ceb17e2013-04-24 11:42:42 +10008950 /* On a read-only array we can:
8951 * - remove failed devices
8952 * - add already-in_sync devices if the array itself
8953 * is in-sync.
8954 * As we only add devices that are already in-sync,
8955 * we can activate the spares immediately.
NeilBrownc89a8ee2008-08-05 15:54:13 +10008956 */
NeilBrown7ceb17e2013-04-24 11:42:42 +10008957 remove_and_add_spares(mddev, NULL);
NeilBrown8313b8e2013-12-12 10:13:33 +11008958 /* There is no thread, but we need to call
8959 * ->spare_active and clear saved_raid_disk
8960 */
NeilBrown2ac295a2014-05-29 11:40:03 +10008961 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008962 md_reap_sync_thread(mddev);
NeilBrowna4a3d262015-07-17 11:57:30 +10008963 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown8313b8e2013-12-12 10:13:33 +11008964 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Shaohua Li29530792016-12-08 15:48:19 -08008965 clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
NeilBrownc89a8ee2008-08-05 15:54:13 +10008966 goto unlock;
8967 }
8968
Guoqing Jiang659b2542015-12-21 10:50:59 +11008969 if (mddev_is_clustered(mddev)) {
8970 struct md_rdev *rdev;
8971 /* kick the device if another node issued a
8972 * remove disk.
8973 */
8974 rdev_for_each(rdev, mddev) {
8975 if (test_and_clear_bit(ClusterRemove, &rdev->flags) &&
8976 rdev->raid_disk < 0)
8977 md_kick_rdev_from_array(rdev);
8978 }
8979 }
8980
NeilBrown480523f2019-08-20 10:21:09 +10008981 if (try_set_sync && !mddev->external && !mddev->in_sync) {
NeilBrown85572d72014-12-15 12:56:56 +11008982 spin_lock(&mddev->lock);
NeilBrown6497709b2017-03-15 14:05:14 +11008983 set_in_sync(mddev);
NeilBrown85572d72014-12-15 12:56:56 +11008984 spin_unlock(&mddev->lock);
NeilBrownfca4d842005-06-21 17:17:11 -07008985 }
NeilBrownfca4d842005-06-21 17:17:11 -07008986
Shaohua Li29530792016-12-08 15:48:19 -08008987 if (mddev->sb_flags)
NeilBrown850b2b422006-10-03 01:15:46 -07008988 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07008989
Linus Torvalds1da177e2005-04-16 15:20:36 -07008990 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
8991 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
8992 /* resync/recovery still happening */
8993 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8994 goto unlock;
8995 }
8996 if (mddev->sync_thread) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10008997 md_reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008998 goto unlock;
8999 }
Neil Brown72a23c22008-06-28 08:31:41 +10009000 /* Set RUNNING before clearing NEEDED to avoid
9001 * any transients in the value of "sync_action".
9002 */
NeilBrown72f36d52012-10-11 14:25:57 +11009003 mddev->curr_resync_completed = 0;
NeilBrown23da4222014-12-15 12:57:01 +11009004 spin_lock(&mddev->lock);
Neil Brown72a23c22008-06-28 08:31:41 +10009005 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrown23da4222014-12-15 12:57:01 +11009006 spin_unlock(&mddev->lock);
NeilBrown24dd4692005-11-08 21:39:26 -08009007 /* Clear some bits that don't mean anything, but
9008 * might be left set
9009 */
NeilBrown24dd4692005-11-08 21:39:26 -08009010 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
9011 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009012
NeilBrowned209582012-04-24 10:23:14 +10009013 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
9014 test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrownac05f252014-09-30 08:10:42 +10009015 goto not_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009016 /* no recovery is running.
9017 * remove any failed drives, then
9018 * add spares if possible.
NeilBrown72f36d52012-10-11 14:25:57 +11009019 * Spares are also removed and re-added, to allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07009020 * the personality to fail the re-add.
9021 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009022
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009023 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10009024 if (mddev->pers->check_reshape == NULL ||
9025 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009026 /* Cannot proceed */
NeilBrownac05f252014-09-30 08:10:42 +10009027 goto not_running;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08009028 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009029 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown746d3202013-04-24 11:42:41 +10009030 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
NeilBrown24dd4692005-11-08 21:39:26 -08009031 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9032 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07009033 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009034 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009035 } else if (mddev->recovery_cp < MaxSector) {
9036 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10009037 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08009038 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
9039 /* nothing to be done ... */
NeilBrownac05f252014-09-30 08:10:42 +10009040 goto not_running;
NeilBrown24dd4692005-11-08 21:39:26 -08009041
Linus Torvalds1da177e2005-04-16 15:20:36 -07009042 if (mddev->pers->sync_request) {
NeilBrownef99bf42012-05-22 13:55:08 +10009043 if (spares) {
NeilBrowna654b9d82005-06-21 17:17:27 -07009044 /* We are adding a device or devices to an array
9045 * which has the bitmap stored on all devices.
9046 * So make sure all bitmap pages get written
9047 */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009048 md_bitmap_write_all(mddev->bitmap);
NeilBrowna654b9d82005-06-21 17:17:27 -07009049 }
NeilBrownac05f252014-09-30 08:10:42 +10009050 INIT_WORK(&mddev->del_work, md_start_sync);
9051 queue_work(md_misc_wq, &mddev->del_work);
9052 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009053 }
NeilBrownac05f252014-09-30 08:10:42 +10009054 not_running:
Neil Brown72a23c22008-06-28 08:31:41 +10009055 if (!mddev->sync_thread) {
9056 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownf851b602014-12-11 10:02:10 +11009057 wake_up(&resync_wait);
Neil Brown72a23c22008-06-28 08:31:41 +10009058 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
9059 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11009060 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10009061 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10009062 }
NeilBrownac05f252014-09-30 08:10:42 +10009063 unlock:
9064 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009065 mddev_unlock(mddev);
9066 }
9067}
NeilBrown6c144d32014-09-30 16:15:38 +10009068EXPORT_SYMBOL(md_check_recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009069
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009070void md_reap_sync_thread(struct mddev *mddev)
9071{
9072 struct md_rdev *rdev;
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009073 sector_t old_dev_sectors = mddev->dev_sectors;
9074 bool is_reshaped = false;
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009075
9076 /* resync has finished, collect result */
9077 md_unregister_thread(&mddev->sync_thread);
9078 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
Guoqing Jiang0d8ed0e92019-07-24 11:09:21 +02009079 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
9080 mddev->degraded != mddev->raid_disks) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009081 /* success...*/
9082 /* activate any spares */
9083 if (mddev->pers->spare_active(mddev)) {
9084 sysfs_notify(&mddev->kobj, NULL,
9085 "degraded");
Shaohua Li29530792016-12-08 15:48:19 -08009086 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009087 }
9088 }
9089 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009090 mddev->pers->finish_reshape) {
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009091 mddev->pers->finish_reshape(mddev);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009092 if (mddev_is_clustered(mddev))
9093 is_reshaped = true;
9094 }
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009095
9096 /* If array is no-longer degraded, then any saved_raid_disk
NeilBrownf4667222013-12-09 12:04:56 +11009097 * information must be scrapped.
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009098 */
NeilBrownf4667222013-12-09 12:04:56 +11009099 if (!mddev->degraded)
9100 rdev_for_each(rdev, mddev)
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009101 rdev->saved_raid_disk = -1;
9102
9103 md_update_sb(mddev, 1);
Shaohua Li29530792016-12-08 15:48:19 -08009104 /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can
Guoqing Jiangbb8bf152016-06-02 23:32:04 -04009105 * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by
9106 * clustered raid */
9107 if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags))
9108 md_cluster_ops->resync_finish(mddev);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009109 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10009110 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009111 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
9112 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
9113 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
9114 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Guoqing Jiangaefb2e52018-10-18 16:37:44 +08009115 /*
9116 * We call md_cluster_ops->update_size here because sync_size could
9117 * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared,
9118 * so it is time to update size across cluster.
9119 */
9120 if (mddev_is_clustered(mddev) && is_reshaped
9121 && !test_bit(MD_CLOSING, &mddev->flags))
9122 md_cluster_ops->update_size(mddev, old_dev_sectors);
NeilBrownf851b602014-12-11 10:02:10 +11009123 wake_up(&resync_wait);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009124 /* flag recovery needed just to double check */
9125 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9126 sysfs_notify_dirent_safe(mddev->sysfs_action);
9127 md_new_event(mddev);
9128 if (mddev->event_work.func)
9129 queue_work(md_misc_wq, &mddev->event_work);
9130}
NeilBrown6c144d32014-09-30 16:15:38 +10009131EXPORT_SYMBOL(md_reap_sync_thread);
Jonathan Brassowa91d5ac2013-04-24 11:42:43 +10009132
NeilBrownfd01b882011-10-11 16:47:53 +11009133void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07009134{
NeilBrown00bcb4a2010-06-01 19:37:23 +10009135 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07009136 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10009137 !test_bit(Blocked, &rdev->flags) &&
9138 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07009139 msecs_to_jiffies(5000));
9140 rdev_dec_pending(rdev, mddev);
9141}
9142EXPORT_SYMBOL(md_wait_for_blocked_rdev);
9143
NeilBrownc6563a82012-05-21 09:27:00 +10009144void md_finish_reshape(struct mddev *mddev)
9145{
9146 /* called be personality module when reshape completes. */
9147 struct md_rdev *rdev;
9148
9149 rdev_for_each(rdev, mddev) {
9150 if (rdev->data_offset > rdev->new_data_offset)
9151 rdev->sectors += rdev->data_offset - rdev->new_data_offset;
9152 else
9153 rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
9154 rdev->data_offset = rdev->new_data_offset;
9155 }
9156}
9157EXPORT_SYMBOL(md_finish_reshape);
NeilBrown2230dfe2011-07-28 11:31:46 +10009158
Vishal Vermafc974ee2015-12-24 19:20:34 -07009159/* Bad block management */
NeilBrown2230dfe2011-07-28 11:31:46 +10009160
Vishal Vermafc974ee2015-12-24 19:20:34 -07009161/* Returns 1 on success, 0 on failure */
NeilBrown3cb03002011-10-11 16:45:26 +11009162int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrownc6563a82012-05-21 09:27:00 +10009163 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009164{
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04009165 struct mddev *mddev = rdev->mddev;
NeilBrownc6563a82012-05-21 09:27:00 +10009166 int rv;
9167 if (is_new)
9168 s += rdev->new_data_offset;
9169 else
9170 s += rdev->data_offset;
Vishal Vermafc974ee2015-12-24 19:20:34 -07009171 rv = badblocks_set(&rdev->badblocks, s, sectors, 0);
9172 if (rv == 0) {
NeilBrown2230dfe2011-07-28 11:31:46 +10009173 /* Make sure they get written out promptly */
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009174 if (test_bit(ExternalBbl, &rdev->flags))
9175 sysfs_notify(&rdev->kobj, NULL,
9176 "unacknowledged_bad_blocks");
NeilBrown8bd2f0a2011-12-08 16:26:08 +11009177 sysfs_notify_dirent_safe(rdev->sysfs_state);
Shaohua Li29530792016-12-08 15:48:19 -08009178 set_mask_bits(&mddev->sb_flags, 0,
9179 BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING));
NeilBrown2230dfe2011-07-28 11:31:46 +10009180 md_wakeup_thread(rdev->mddev->thread);
Vishal Vermafc974ee2015-12-24 19:20:34 -07009181 return 1;
9182 } else
9183 return 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10009184}
9185EXPORT_SYMBOL_GPL(rdev_set_badblocks);
9186
NeilBrownc6563a82012-05-21 09:27:00 +10009187int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
9188 int is_new)
NeilBrown2230dfe2011-07-28 11:31:46 +10009189{
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009190 int rv;
NeilBrownc6563a82012-05-21 09:27:00 +10009191 if (is_new)
9192 s += rdev->new_data_offset;
9193 else
9194 s += rdev->data_offset;
Tomasz Majchrzak35b785f2016-10-21 16:26:57 +02009195 rv = badblocks_clear(&rdev->badblocks, s, sectors);
9196 if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags))
9197 sysfs_notify(&rdev->kobj, NULL, "bad_blocks");
9198 return rv;
NeilBrown2230dfe2011-07-28 11:31:46 +10009199}
9200EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
9201
Adrian Bunk75c96f82005-05-05 16:16:09 -07009202static int md_notify_reboot(struct notifier_block *this,
9203 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009204{
9205 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11009206 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009207 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009208
NeilBrownc744a652012-03-19 12:46:37 +11009209 for_each_mddev(mddev, tmp) {
9210 if (mddev_trylock(mddev)) {
NeilBrown30b8aa92012-04-24 10:23:16 +10009211 if (mddev->pers)
9212 __md_stop_writes(mddev);
NeilBrown0f62fb22014-05-06 09:36:08 +10009213 if (mddev->persistent)
9214 mddev->safemode = 2;
NeilBrownc744a652012-03-19 12:46:37 +11009215 mddev_unlock(mddev);
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01009216 }
NeilBrownc744a652012-03-19 12:46:37 +11009217 need_delay = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009218 }
NeilBrownc744a652012-03-19 12:46:37 +11009219 /*
9220 * certain more exotic SCSI devices are known to be
9221 * volatile wrt too early system reboots. While the
9222 * right place to handle this issue is the given
9223 * driver, we do want to have a safe RAID driver ...
9224 */
9225 if (need_delay)
9226 mdelay(1000*1);
9227
Linus Torvalds1da177e2005-04-16 15:20:36 -07009228 return NOTIFY_DONE;
9229}
9230
Adrian Bunk75c96f82005-05-05 16:16:09 -07009231static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009232 .notifier_call = md_notify_reboot,
9233 .next = NULL,
9234 .priority = INT_MAX, /* before any real devices */
9235};
9236
9237static void md_geninit(void)
9238{
NeilBrown36a4e1f2011-10-07 14:23:17 +11009239 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07009240
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07009241 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009242}
9243
Adrian Bunk75c96f82005-05-05 16:16:09 -07009244static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009245{
Tejun Heoe804ac72010-10-15 15:36:08 +02009246 int ret = -ENOMEM;
9247
Tejun Heoada609e2011-01-25 14:35:54 +01009248 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02009249 if (!md_wq)
9250 goto err_wq;
9251
9252 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
9253 if (!md_misc_wq)
9254 goto err_misc_wq;
9255
9256 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
9257 goto err_md;
9258
9259 if ((ret = register_blkdev(0, "mdp")) < 0)
9260 goto err_mdp;
9261 mdp_major = ret;
9262
NeilBrownaf5628f2014-07-31 13:54:54 +10009263 blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07009264 md_probe, NULL, NULL);
9265 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009266 md_probe, NULL, NULL);
9267
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08009269 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009270
9271 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11009272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009273
Tejun Heoe804ac72010-10-15 15:36:08 +02009274err_mdp:
9275 unregister_blkdev(MD_MAJOR, "md");
9276err_md:
9277 destroy_workqueue(md_misc_wq);
9278err_misc_wq:
9279 destroy_workqueue(md_wq);
9280err_wq:
9281 return ret;
9282}
Linus Torvalds1da177e2005-04-16 15:20:36 -07009283
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009284static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev)
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009285{
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009286 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
9287 struct md_rdev *rdev2;
9288 int role, ret;
9289 char b[BDEVNAME_SIZE];
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009290
Guoqing Jiang818da592017-03-01 16:42:40 +08009291 /*
9292 * If size is changed in another node then we need to
9293 * do resize as well.
9294 */
9295 if (mddev->dev_sectors != le64_to_cpu(sb->size)) {
9296 ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size));
9297 if (ret)
9298 pr_info("md-cluster: resize failed\n");
9299 else
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07009300 md_bitmap_update_sb(mddev->bitmap);
Guoqing Jiang818da592017-03-01 16:42:40 +08009301 }
9302
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009303 /* Check for change of roles in the active devices */
9304 rdev_for_each(rdev2, mddev) {
9305 if (test_bit(Faulty, &rdev2->flags))
9306 continue;
9307
9308 /* Check if the roles changed */
9309 role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]);
Goldwyn Rodriguesdbb64f82015-10-01 13:20:27 -05009310
9311 if (test_bit(Candidate, &rdev2->flags)) {
9312 if (role == 0xfffe) {
9313 pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b));
9314 md_kick_rdev_from_array(rdev2);
9315 continue;
9316 }
9317 else
9318 clear_bit(Candidate, &rdev2->flags);
9319 }
9320
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009321 if (role != rdev2->raid_disk) {
Guoqing Jiangca1e98e2018-10-18 16:37:45 +08009322 /*
9323 * got activated except reshape is happening.
9324 */
9325 if (rdev2->raid_disk == -1 && role != 0xffff &&
9326 !(le32_to_cpu(sb->feature_map) &
9327 MD_FEATURE_RESHAPE_ACTIVE)) {
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009328 rdev2->saved_raid_disk = role;
9329 ret = remove_and_add_spares(mddev, rdev2);
9330 pr_info("Activated spare: %s\n",
NeilBrown9d487392016-11-02 14:16:49 +11009331 bdevname(rdev2->bdev,b));
Guoqing Jianga5781832016-05-02 11:33:14 -04009332 /* wakeup mddev->thread here, so array could
9333 * perform resync with the new activated disk */
9334 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
9335 md_wakeup_thread(mddev->thread);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009336 }
9337 /* device faulty
9338 * We just want to do the minimum to mark the disk
9339 * as faulty. The recovery is performed by the
9340 * one who initiated the error.
9341 */
9342 if ((role == 0xfffe) || (role == 0xfffd)) {
9343 md_error(mddev, rdev2);
9344 clear_bit(Blocked, &rdev2->flags);
9345 }
9346 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009347 }
9348
Goldwyn Rodrigues28c1b9f2015-10-22 16:01:25 +11009349 if (mddev->raid_disks != le32_to_cpu(sb->raid_disks))
9350 update_raid_disks(mddev, le32_to_cpu(sb->raid_disks));
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009351
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009352 /*
9353 * Since mddev->delta_disks has already updated in update_raid_disks,
9354 * so it is time to check reshape.
9355 */
9356 if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9357 (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9358 /*
9359 * reshape is happening in the remote node, we need to
9360 * update reshape_position and call start_reshape.
9361 */
Christoph Hellwiged4d0a4e2019-04-04 18:56:10 +02009362 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
Guoqing Jiang7564bed2018-10-18 16:37:42 +08009363 if (mddev->pers->update_reshape_pos)
9364 mddev->pers->update_reshape_pos(mddev);
9365 if (mddev->pers->start_reshape)
9366 mddev->pers->start_reshape(mddev);
9367 } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
9368 mddev->reshape_position != MaxSector &&
9369 !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
9370 /* reshape is just done in another node. */
9371 mddev->reshape_position = MaxSector;
9372 if (mddev->pers->update_reshape_pos)
9373 mddev->pers->update_reshape_pos(mddev);
9374 }
9375
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009376 /* Finally set the event to be up to date */
9377 mddev->events = le64_to_cpu(sb->events);
9378}
9379
9380static int read_rdev(struct mddev *mddev, struct md_rdev *rdev)
9381{
9382 int err;
9383 struct page *swapout = rdev->sb_page;
9384 struct mdp_superblock_1 *sb;
9385
9386 /* Store the sb page of the rdev in the swapout temporary
9387 * variable in case we err in the future
9388 */
9389 rdev->sb_page = NULL;
NeilBrown7f0f0d82016-11-02 14:16:49 +11009390 err = alloc_disk_sb(rdev);
9391 if (err == 0) {
9392 ClearPageUptodate(rdev->sb_page);
9393 rdev->sb_loaded = 0;
9394 err = super_types[mddev->major_version].
9395 load_super(rdev, NULL, mddev->minor_version);
9396 }
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009397 if (err < 0) {
9398 pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n",
9399 __func__, __LINE__, rdev->desc_nr, err);
NeilBrown7f0f0d82016-11-02 14:16:49 +11009400 if (rdev->sb_page)
9401 put_page(rdev->sb_page);
Goldwyn Rodrigues70bcecd2015-08-21 10:33:39 -05009402 rdev->sb_page = swapout;
9403 rdev->sb_loaded = 1;
9404 return err;
9405 }
9406
9407 sb = page_address(rdev->sb_page);
9408 /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET
9409 * is not set
9410 */
9411
9412 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET))
9413 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
9414
9415 /* The other node finished recovery, call spare_active to set
9416 * device In_sync and mddev->degraded
9417 */
9418 if (rdev->recovery_offset == MaxSector &&
9419 !test_bit(In_sync, &rdev->flags) &&
9420 mddev->pers->spare_active(mddev))
9421 sysfs_notify(&mddev->kobj, NULL, "degraded");
9422
9423 put_page(swapout);
9424 return 0;
9425}
9426
9427void md_reload_sb(struct mddev *mddev, int nr)
9428{
9429 struct md_rdev *rdev;
9430 int err;
9431
9432 /* Find the rdev */
9433 rdev_for_each_rcu(rdev, mddev) {
9434 if (rdev->desc_nr == nr)
9435 break;
9436 }
9437
9438 if (!rdev || rdev->desc_nr != nr) {
9439 pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr);
9440 return;
9441 }
9442
9443 err = read_rdev(mddev, rdev);
9444 if (err < 0)
9445 return;
9446
9447 check_sb_changes(mddev, rdev);
9448
9449 /* Read all rdev's to update recovery_offset */
Guoqing Jiang0ea99242018-04-09 17:01:21 +08009450 rdev_for_each_rcu(rdev, mddev) {
9451 if (!test_bit(Faulty, &rdev->flags))
9452 read_rdev(mddev, rdev);
9453 }
Goldwyn Rodrigues1d7e3e92014-06-07 01:53:00 -05009454}
9455EXPORT_SYMBOL(md_reload_sb);
9456
Linus Torvalds1da177e2005-04-16 15:20:36 -07009457#ifndef MODULE
9458
9459/*
9460 * Searches all registered partitions for autorun RAID arrays
9461 * at boot time.
9462 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009463
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009464static DEFINE_MUTEX(detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009465static LIST_HEAD(all_detected_devices);
9466struct detected_devices_node {
9467 struct list_head list;
9468 dev_t dev;
9469};
Linus Torvalds1da177e2005-04-16 15:20:36 -07009470
9471void md_autodetect_dev(dev_t dev)
9472{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009473 struct detected_devices_node *node_detected_dev;
9474
9475 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
9476 if (node_detected_dev) {
9477 node_detected_dev->dev = dev;
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009478 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009479 list_add_tail(&node_detected_dev->list, &all_detected_devices);
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009480 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009482}
9483
Linus Torvalds1da177e2005-04-16 15:20:36 -07009484static void autostart_arrays(int part)
9485{
NeilBrown3cb03002011-10-11 16:45:26 +11009486 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009487 struct detected_devices_node *node_detected_dev;
9488 dev_t dev;
9489 int i_scanned, i_passed;
9490
9491 i_scanned = 0;
9492 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009493
NeilBrown9d487392016-11-02 14:16:49 +11009494 pr_info("md: Autodetecting RAID arrays.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009495
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009496 mutex_lock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009497 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
9498 i_scanned++;
9499 node_detected_dev = list_entry(all_detected_devices.next,
9500 struct detected_devices_node, list);
9501 list_del(&node_detected_dev->list);
9502 dev = node_detected_dev->dev;
9503 kfree(node_detected_dev);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009504 mutex_unlock(&detected_devices_mutex);
NeilBrowndf968c42007-07-17 04:06:11 -07009505 rdev = md_import_device(dev,0, 90);
Shaohua Li90bcf1332016-09-14 14:26:54 -07009506 mutex_lock(&detected_devices_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009507 if (IS_ERR(rdev))
9508 continue;
9509
NeilBrown403df472014-09-30 15:52:29 +10009510 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009511 continue;
NeilBrown403df472014-09-30 15:52:29 +10009512
NeilBrownd0fae182008-03-04 14:29:31 -08009513 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009514 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009515 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009516 }
Cong Wang5b1f5bc32016-06-08 09:20:16 -07009517 mutex_unlock(&detected_devices_mutex);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07009518
NeilBrown9d487392016-11-02 14:16:49 +11009519 pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009520
9521 autorun_devices(part);
9522}
9523
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08009524#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009525
9526static __exit void md_exit(void)
9527{
NeilBrownfd01b882011-10-11 16:47:53 +11009528 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009529 struct list_head *tmp;
NeilBrowne2f23b62014-04-09 14:33:51 +10009530 int delay = 1;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07009531
NeilBrownaf5628f2014-07-31 13:54:54 +10009532 blk_unregister_region(MKDEV(MD_MAJOR,0), 512);
NeilBrowne8703fe2006-10-03 01:15:59 -07009533 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009534
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11009535 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07009536 unregister_blkdev(mdp_major, "mdp");
9537 unregister_reboot_notifier(&md_notifier);
9538 unregister_sysctl_table(raid_table_header);
NeilBrowne2f23b62014-04-09 14:33:51 +10009539
9540 /* We cannot unload the modules while some process is
9541 * waiting for us in select() or poll() - wake them up
9542 */
9543 md_unloading = 1;
9544 while (waitqueue_active(&md_event_waiters)) {
9545 /* not safe to leave yet */
9546 wake_up(&md_event_waiters);
9547 msleep(delay);
9548 delay += delay;
9549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009550 remove_proc_entry("mdstat", NULL);
NeilBrowne2f23b62014-04-09 14:33:51 +10009551
NeilBrown29ac4aa2008-02-06 01:39:58 -08009552 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009553 export_array(mddev);
NeilBrown93568632017-02-06 13:41:39 +11009554 mddev->ctime = 0;
NeilBrownd3374822009-01-09 08:31:10 +11009555 mddev->hold_active = 0;
NeilBrown93568632017-02-06 13:41:39 +11009556 /*
9557 * for_each_mddev() will call mddev_put() at the end of each
9558 * iteration. As the mddev is now fully clear, this will
9559 * schedule the mddev for destruction by a workqueue, and the
9560 * destroy_workqueue() below will wait for that to complete.
9561 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009562 }
Tejun Heoe804ac72010-10-15 15:36:08 +02009563 destroy_workqueue(md_misc_wq);
9564 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009565}
9566
Dan Williams685784a2007-07-09 11:56:42 -07009567subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009568module_exit(md_exit)
9569
Kees Cooke4dca7b2017-10-17 19:04:42 -07009570static int get_ro(char *buffer, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009571{
9572 return sprintf(buffer, "%d", start_readonly);
9573}
Kees Cooke4dca7b2017-10-17 19:04:42 -07009574static int set_ro(const char *val, const struct kernel_param *kp)
NeilBrownf91de922005-11-08 21:39:36 -08009575{
Alexey Dobriyan4c9309c2015-05-16 14:02:38 +03009576 return kstrtouint(val, 10, (unsigned int *)&start_readonly);
NeilBrownf91de922005-11-08 21:39:36 -08009577}
9578
NeilBrown80ca3a42006-07-10 04:44:18 -07009579module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
9580module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrownefeb53c2009-01-09 08:31:10 +11009581module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrown78b63502017-04-12 16:26:13 +10009582module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08009583
Linus Torvalds1da177e2005-04-16 15:20:36 -07009584MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11009585MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07009586MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07009587MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);