Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 33 | |
| 34 | Errors, Warnings, etc. |
| 35 | Please use: |
| 36 | pr_crit() for error conditions that risk data loss |
| 37 | pr_err() for error conditions that are unexpected, like an IO error |
| 38 | or internal inconsistency |
| 39 | pr_warn() for error conditions that could have been predicated, like |
| 40 | adding a device to an array when it has incompatible metadata |
| 41 | pr_info() for every interesting, very rare events, like an array starting |
| 42 | or stopping, or resync starting or stopping |
| 43 | pr_debug() for everything else. |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | */ |
| 46 | |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 47 | #include <linux/sched/signal.h> |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 48 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 49 | #include <linux/blkdev.h> |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 50 | #include <linux/badblocks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 52 | #include <linux/seq_file.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 53 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 54 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 55 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 56 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 57 | #include <linux/hdreg.h> |
| 58 | #include <linux/proc_fs.h> |
| 59 | #include <linux/random.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 60 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 61 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 62 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 63 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 64 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 65 | #include <linux/raid/md_p.h> |
| 66 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 67 | #include <linux/slab.h> |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 68 | #include <linux/percpu-refcount.h> |
| 69 | |
Shaohua Li | 504634f | 2016-11-18 09:44:08 -0800 | [diff] [blame] | 70 | #include <trace/events/block.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 71 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 72 | #include "bitmap.h" |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 73 | #include "md-cluster.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 76 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | #endif |
| 78 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 79 | /* pers_list is a list of registered personalities protected |
| 80 | * by pers_lock. |
| 81 | * pers_lock does extra service to protect accesses to |
| 82 | * mddev->thread when the mutex cannot be held. |
| 83 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 84 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | static DEFINE_SPINLOCK(pers_lock); |
| 86 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 87 | struct md_cluster_operations *md_cluster_ops; |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame] | 88 | EXPORT_SYMBOL(md_cluster_ops); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 89 | struct module *md_cluster_mod; |
| 90 | EXPORT_SYMBOL(md_cluster_mod); |
| 91 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 92 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 93 | static struct workqueue_struct *md_wq; |
| 94 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 95 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 96 | static int remove_and_add_spares(struct mddev *mddev, |
| 97 | struct md_rdev *this); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 98 | static void mddev_detach(struct mddev *mddev); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 101 | * Default number of read corrections we'll attempt on an rdev |
| 102 | * before ejecting it from the array. We divide the read error |
| 103 | * count by 2 for every hour elapsed between read errors. |
| 104 | */ |
| 105 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 106 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 108 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 109 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 110 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 112 | * speed limit - in case reconstruction slows down your system despite |
| 113 | * idle IO detection. |
| 114 | * |
| 115 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 116 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | */ |
| 118 | |
| 119 | static int sysctl_speed_limit_min = 1000; |
| 120 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 121 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 122 | { |
| 123 | return mddev->sync_speed_min ? |
| 124 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 125 | } |
| 126 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 127 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 128 | { |
| 129 | return mddev->sync_speed_max ? |
| 130 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 131 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | static struct ctl_table_header *raid_table_header; |
| 134 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 135 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | .procname = "speed_limit_min", |
| 138 | .data = &sysctl_speed_limit_min, |
| 139 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 140 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 141 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | }, |
| 143 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | .procname = "speed_limit_max", |
| 145 | .data = &sysctl_speed_limit_max, |
| 146 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 147 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 148 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 150 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 153 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | .procname = "raid", |
| 156 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 157 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | .child = raid_table, |
| 159 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 160 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | }; |
| 162 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 163 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | .procname = "dev", |
| 166 | .maxlen = 0, |
| 167 | .mode = 0555, |
| 168 | .child = raid_dir_table, |
| 169 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 170 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 173 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 175 | static int start_readonly; |
| 176 | |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 177 | /* |
| 178 | * The original mechanism for creating an md device is to create |
| 179 | * a device node in /dev and to open it. This causes races with device-close. |
| 180 | * The preferred method is to write to the "new_array" module parameter. |
| 181 | * This can avoid races. |
| 182 | * Setting create_on_open to false disables the original mechanism |
| 183 | * so all the races disappear. |
| 184 | */ |
| 185 | static bool create_on_open = true; |
| 186 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 187 | /* bio_clone_mddev |
| 188 | * like bio_clone, but with a local bio set |
| 189 | */ |
| 190 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 191 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 192 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 193 | { |
| 194 | struct bio *b; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 195 | |
| 196 | if (!mddev || !mddev->bio_set) |
| 197 | return bio_alloc(gfp_mask, nr_iovecs); |
| 198 | |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 199 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 200 | if (!b) |
| 201 | return NULL; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 202 | return b; |
| 203 | } |
| 204 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 207 | * We have a system wide 'event count' that is incremented |
| 208 | * on any 'interesting' event, and readers of /proc/mdstat |
| 209 | * can use 'poll' or 'select' to find out when the event |
| 210 | * count increases. |
| 211 | * |
| 212 | * Events are: |
| 213 | * start array, stop array, error, add device, remove device, |
| 214 | * start build, activate spare |
| 215 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 216 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 217 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 218 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 219 | { |
| 220 | atomic_inc(&md_event_count); |
| 221 | wake_up(&md_event_waiters); |
| 222 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 223 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 224 | |
| 225 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | * Enables to iterate over all existing md arrays |
| 227 | * all_mddevs_lock protects this list. |
| 228 | */ |
| 229 | static LIST_HEAD(all_mddevs); |
| 230 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | /* |
| 233 | * iterates through all used mddevs in the system. |
| 234 | * We take care to grab the all_mddevs_lock whenever navigating |
| 235 | * the list, and to always hold a refcount when unlocked. |
| 236 | * Any code which breaks out of this loop while own |
| 237 | * a reference to the current mddev and must mddev_put it. |
| 238 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 239 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | \ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 241 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 242 | _tmp = all_mddevs.next; \ |
| 243 | _mddev = NULL;}); \ |
| 244 | ({ if (_tmp != &all_mddevs) \ |
| 245 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 247 | if (_mddev) mddev_put(_mddev); \ |
| 248 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 249 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 251 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | ) |
| 253 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 254 | /* Rather than calling directly into the personality make_request function, |
| 255 | * IO requests come here first so that we can check if the device is |
| 256 | * being suspended pending a reconfiguration. |
| 257 | * We hold a refcount over the call to ->make_request. By the time that |
| 258 | * call has finished, the bio has been linked into some internal structure |
| 259 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 260 | */ |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 261 | static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 263 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 264 | struct mddev *mddev = q->queuedata; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 265 | unsigned int sectors; |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 266 | int cpu; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 267 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 268 | blk_queue_split(q, &bio, q->bio_split); |
| 269 | |
NeilBrown | 274d8cb | 2016-01-04 16:16:58 +1100 | [diff] [blame] | 270 | if (mddev == NULL || mddev->pers == NULL) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 271 | bio_io_error(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 272 | return BLK_QC_T_NONE; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 273 | } |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 274 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 275 | if (bio_sectors(bio) != 0) |
| 276 | bio->bi_error = -EROFS; |
| 277 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 278 | return BLK_QC_T_NONE; |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 279 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 280 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 281 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 282 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 283 | DEFINE_WAIT(__wait); |
| 284 | for (;;) { |
| 285 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 286 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 287 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 288 | break; |
| 289 | rcu_read_unlock(); |
| 290 | schedule(); |
| 291 | rcu_read_lock(); |
| 292 | } |
| 293 | finish_wait(&mddev->sb_wait, &__wait); |
| 294 | } |
| 295 | atomic_inc(&mddev->active_io); |
| 296 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 297 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 298 | /* |
| 299 | * save the sectors now since our bio can |
| 300 | * go away inside make_request |
| 301 | */ |
| 302 | sectors = bio_sectors(bio); |
Shaohua Li | 9c573de | 2016-04-25 16:52:38 -0700 | [diff] [blame] | 303 | /* bio could be mergeable after passing to underlayer */ |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 304 | bio->bi_opf &= ~REQ_NOMERGE; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 305 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 306 | |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 307 | cpu = part_stat_lock(); |
| 308 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 309 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
| 310 | part_stat_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 311 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 312 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 313 | wake_up(&mddev->sb_wait); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 314 | |
| 315 | return BLK_QC_T_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | } |
| 317 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 318 | /* mddev_suspend makes sure no new requests are submitted |
| 319 | * to the device, and that any requests that have been submitted |
| 320 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 321 | * Once mddev_detach() is called and completes, the module will be |
| 322 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 323 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 324 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 325 | { |
Heinz Mauelshagen | 092398d | 2016-05-03 19:43:57 +0200 | [diff] [blame] | 326 | WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk); |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 327 | if (mddev->suspended++) |
| 328 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 329 | synchronize_rcu(); |
| 330 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 331 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 332 | |
| 333 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 334 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 335 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 336 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 337 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 338 | { |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 339 | if (--mddev->suspended) |
| 340 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 341 | wake_up(&mddev->sb_wait); |
| 342 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 343 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 344 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 345 | md_wakeup_thread(mddev->thread); |
| 346 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 347 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 348 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 349 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 350 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 351 | { |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 352 | struct md_personality *pers = mddev->pers; |
| 353 | int ret = 0; |
| 354 | |
| 355 | rcu_read_lock(); |
| 356 | if (mddev->suspended) |
| 357 | ret = 1; |
| 358 | else if (pers && pers->congested) |
| 359 | ret = pers->congested(mddev, bits); |
| 360 | rcu_read_unlock(); |
| 361 | return ret; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 362 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 363 | EXPORT_SYMBOL_GPL(mddev_congested); |
| 364 | static int md_congested(void *data, int bits) |
| 365 | { |
| 366 | struct mddev *mddev = data; |
| 367 | return mddev_congested(mddev, bits); |
| 368 | } |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 369 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 370 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 371 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 372 | */ |
| 373 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 374 | static void md_end_flush(struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 375 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 376 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 377 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 378 | |
| 379 | rdev_dec_pending(rdev, mddev); |
| 380 | |
| 381 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 382 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 383 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 384 | } |
| 385 | bio_put(bio); |
| 386 | } |
| 387 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 388 | static void md_submit_flush_data(struct work_struct *ws); |
| 389 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 390 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 391 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 392 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 393 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 394 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 395 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 396 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 397 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 398 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 399 | if (rdev->raid_disk >= 0 && |
| 400 | !test_bit(Faulty, &rdev->flags)) { |
| 401 | /* Take two references, one is dropped |
| 402 | * when request finishes, one after |
| 403 | * we reclaim rcu_read_lock |
| 404 | */ |
| 405 | struct bio *bi; |
| 406 | atomic_inc(&rdev->nr_pending); |
| 407 | atomic_inc(&rdev->nr_pending); |
| 408 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 409 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 410 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 411 | bi->bi_private = rdev; |
| 412 | bi->bi_bdev = rdev->bdev; |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 413 | bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 414 | atomic_inc(&mddev->flush_pending); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 415 | submit_bio(bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 416 | rcu_read_lock(); |
| 417 | rdev_dec_pending(rdev, mddev); |
| 418 | } |
| 419 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 420 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 421 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 422 | } |
| 423 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 424 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 425 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 426 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 427 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 428 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 429 | if (bio->bi_iter.bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 430 | /* an empty barrier - all done */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 431 | bio_endio(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 432 | else { |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 433 | bio->bi_opf &= ~REQ_PREFLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 434 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 435 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 436 | |
| 437 | mddev->flush_bio = NULL; |
| 438 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 439 | } |
| 440 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 441 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 442 | { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 443 | spin_lock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 444 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 445 | !mddev->flush_bio, |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 446 | mddev->lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 447 | mddev->flush_bio = bio; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 448 | spin_unlock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 449 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 450 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 451 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 452 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 453 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 454 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 455 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | { |
| 457 | atomic_inc(&mddev->active); |
| 458 | return mddev; |
| 459 | } |
| 460 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 461 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 462 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 463 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 465 | struct bio_set *bs = NULL; |
| 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 468 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 469 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 470 | mddev->ctime == 0 && !mddev->hold_active) { |
| 471 | /* Array is not configured at all, and not held active, |
| 472 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 473 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 474 | bs = mddev->bio_set; |
| 475 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 476 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 477 | /* We did a probe so need to clean up. Call |
| 478 | * queue_work inside the spinlock so that |
| 479 | * flush_workqueue() after mddev_find will |
| 480 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 481 | */ |
| 482 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 483 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 484 | } else |
| 485 | kfree(mddev); |
| 486 | } |
| 487 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 488 | if (bs) |
| 489 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 492 | static void md_safemode_timeout(unsigned long data); |
| 493 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 494 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 495 | { |
| 496 | mutex_init(&mddev->open_mutex); |
| 497 | mutex_init(&mddev->reconfig_mutex); |
| 498 | mutex_init(&mddev->bitmap_info.mutex); |
| 499 | INIT_LIST_HEAD(&mddev->disks); |
| 500 | INIT_LIST_HEAD(&mddev->all_mddevs); |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 501 | setup_timer(&mddev->safemode_timer, md_safemode_timeout, |
| 502 | (unsigned long) mddev); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 503 | atomic_set(&mddev->active, 1); |
| 504 | atomic_set(&mddev->openers, 0); |
| 505 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 506 | spin_lock_init(&mddev->lock); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 507 | atomic_set(&mddev->flush_pending, 0); |
| 508 | init_waitqueue_head(&mddev->sb_wait); |
| 509 | init_waitqueue_head(&mddev->recovery_wait); |
| 510 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 511 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 512 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 513 | mddev->resync_min = 0; |
| 514 | mddev->resync_max = MaxSector; |
| 515 | mddev->level = LEVEL_NONE; |
| 516 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 517 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 518 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 519 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 521 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 523 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 524 | unit &= ~((1<<MdpMinorShift)-1); |
| 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | retry: |
| 527 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 529 | if (unit) { |
| 530 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 531 | if (mddev->unit == unit) { |
| 532 | mddev_get(mddev); |
| 533 | spin_unlock(&all_mddevs_lock); |
| 534 | kfree(new); |
| 535 | return mddev; |
| 536 | } |
| 537 | |
| 538 | if (new) { |
| 539 | list_add(&new->all_mddevs, &all_mddevs); |
| 540 | spin_unlock(&all_mddevs_lock); |
| 541 | new->hold_active = UNTIL_IOCTL; |
| 542 | return new; |
| 543 | } |
| 544 | } else if (new) { |
| 545 | /* find an unused unit number */ |
| 546 | static int next_minor = 512; |
| 547 | int start = next_minor; |
| 548 | int is_free = 0; |
| 549 | int dev = 0; |
| 550 | while (!is_free) { |
| 551 | dev = MKDEV(MD_MAJOR, next_minor); |
| 552 | next_minor++; |
| 553 | if (next_minor > MINORMASK) |
| 554 | next_minor = 0; |
| 555 | if (next_minor == start) { |
| 556 | /* Oh dear, all in use. */ |
| 557 | spin_unlock(&all_mddevs_lock); |
| 558 | kfree(new); |
| 559 | return NULL; |
| 560 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 561 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 562 | is_free = 1; |
| 563 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 564 | if (mddev->unit == dev) { |
| 565 | is_free = 0; |
| 566 | break; |
| 567 | } |
| 568 | } |
| 569 | new->unit = dev; |
| 570 | new->md_minor = MINOR(dev); |
| 571 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | list_add(&new->all_mddevs, &all_mddevs); |
| 573 | spin_unlock(&all_mddevs_lock); |
| 574 | return new; |
| 575 | } |
| 576 | spin_unlock(&all_mddevs_lock); |
| 577 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 578 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | if (!new) |
| 580 | return NULL; |
| 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | new->unit = unit; |
| 583 | if (MAJOR(unit) == MD_MAJOR) |
| 584 | new->md_minor = MINOR(unit); |
| 585 | else |
| 586 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 587 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 588 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | goto retry; |
| 591 | } |
| 592 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 593 | static struct attribute_group md_redundancy_group; |
| 594 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 595 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 597 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 598 | /* These cannot be removed under reconfig_mutex as |
| 599 | * an access to the files will try to take reconfig_mutex |
| 600 | * while holding the file unremovable, which leads to |
| 601 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 602 | * So hold set sysfs_active while the remove in happeing, |
| 603 | * and anything else which might set ->to_remove or my |
| 604 | * otherwise change the sysfs namespace will fail with |
| 605 | * -EBUSY if sysfs_active is still set. |
| 606 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 607 | * test it under the same mutex to ensure its correct value |
| 608 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 609 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 610 | struct attribute_group *to_remove = mddev->to_remove; |
| 611 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 612 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 613 | mutex_unlock(&mddev->reconfig_mutex); |
| 614 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 615 | if (mddev->kobj.sd) { |
| 616 | if (to_remove != &md_redundancy_group) |
| 617 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 618 | if (mddev->pers == NULL || |
| 619 | mddev->pers->sync_request == NULL) { |
| 620 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 621 | if (mddev->sysfs_action) |
| 622 | sysfs_put(mddev->sysfs_action); |
| 623 | mddev->sysfs_action = NULL; |
| 624 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 625 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 626 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 627 | } else |
| 628 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 630 | /* As we've dropped the mutex we need a spinlock to |
| 631 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 632 | */ |
| 633 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 634 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 635 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 637 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 639 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 640 | { |
| 641 | struct md_rdev *rdev; |
| 642 | |
| 643 | rdev_for_each_rcu(rdev, mddev) |
| 644 | if (rdev->desc_nr == nr) |
| 645 | return rdev; |
| 646 | |
| 647 | return NULL; |
| 648 | } |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 649 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 650 | |
| 651 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 653 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 655 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | if (rdev->bdev->bd_dev == dev) |
| 657 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | return NULL; |
| 660 | } |
| 661 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 662 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 663 | { |
| 664 | struct md_rdev *rdev; |
| 665 | |
| 666 | rdev_for_each_rcu(rdev, mddev) |
| 667 | if (rdev->bdev->bd_dev == dev) |
| 668 | return rdev; |
| 669 | |
| 670 | return NULL; |
| 671 | } |
| 672 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 673 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 674 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 675 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 676 | list_for_each_entry(pers, &pers_list, list) { |
| 677 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 678 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 679 | if (strcmp(pers->name, clevel)==0) |
| 680 | return pers; |
| 681 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 682 | return NULL; |
| 683 | } |
| 684 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 685 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 686 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 688 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 689 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | } |
| 691 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 692 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | rdev->sb_page = alloc_page(GFP_KERNEL); |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 695 | if (!rdev->sb_page) |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 696 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | return 0; |
| 698 | } |
| 699 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 700 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | { |
| 702 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 703 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | rdev->sb_loaded = 0; |
| 705 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 706 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 707 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 709 | if (rdev->bb_page) { |
| 710 | put_page(rdev->bb_page); |
| 711 | rdev->bb_page = NULL; |
| 712 | } |
Dan Williams | d3b407fb | 2016-01-06 12:19:22 -0800 | [diff] [blame] | 713 | badblocks_exit(&rdev->badblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 715 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 717 | static void super_written(struct bio *bio) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 718 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 719 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 720 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 721 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 722 | if (bio->bi_error) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 723 | pr_err("md: super_written gets error=%d\n", bio->bi_error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 724 | md_error(mddev, rdev); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 725 | if (!test_bit(Faulty, &rdev->flags) |
| 726 | && (bio->bi_opf & MD_FAILFAST)) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 727 | set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 728 | set_bit(LastDev, &rdev->flags); |
| 729 | } |
| 730 | } else |
| 731 | clear_bit(LastDev, &rdev->flags); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 732 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 733 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 734 | wake_up(&mddev->sb_wait); |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 735 | rdev_dec_pending(rdev, mddev); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 736 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 737 | } |
| 738 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 739 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 740 | sector_t sector, int size, struct page *page) |
| 741 | { |
| 742 | /* write first size bytes of page to sector of rdev |
| 743 | * Increment mddev->pending_writes before returning |
| 744 | * and decrement it on completion, waking up sb_wait |
| 745 | * if zero is reached. |
| 746 | * If an error occurred, call md_error |
| 747 | */ |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 748 | struct bio *bio; |
| 749 | int ff = 0; |
| 750 | |
| 751 | if (test_bit(Faulty, &rdev->flags)) |
| 752 | return; |
| 753 | |
| 754 | bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 755 | |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 756 | atomic_inc(&rdev->nr_pending); |
| 757 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 758 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 759 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 760 | bio_add_page(bio, page, size, 0); |
| 761 | bio->bi_private = rdev; |
| 762 | bio->bi_end_io = super_written; |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 763 | |
| 764 | if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) && |
| 765 | test_bit(FailFast, &rdev->flags) && |
| 766 | !test_bit(LastDev, &rdev->flags)) |
| 767 | ff = MD_FAILFAST; |
Jan Kara | 5a8948f | 2017-05-31 09:44:33 +0200 | [diff] [blame] | 768 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 769 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 770 | atomic_inc(&mddev->pending_writes); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 771 | submit_bio(bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 772 | } |
| 773 | |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 774 | int md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 775 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 776 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 777 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 778 | if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags)) |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 779 | return -EAGAIN; |
| 780 | return 0; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 781 | } |
| 782 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 783 | int sync_page_io(struct md_rdev *rdev, sector_t sector, int size, |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 784 | struct page *page, int op, int op_flags, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 786 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | int ret; |
| 788 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 789 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 790 | rdev->meta_bdev : rdev->bdev; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 791 | bio_set_op_attrs(bio, op, op_flags); |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 792 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 793 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 794 | else if (rdev->mddev->reshape_position != MaxSector && |
| 795 | (rdev->mddev->reshape_backwards == |
| 796 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 797 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 798 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 799 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | bio_add_page(bio, page, size, 0); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 801 | |
| 802 | submit_bio_wait(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 804 | ret = !bio->bi_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | bio_put(bio); |
| 806 | return ret; |
| 807 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 808 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 810 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | { |
| 812 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | if (rdev->sb_loaded) |
| 815 | return 0; |
| 816 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 817 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | goto fail; |
| 819 | rdev->sb_loaded = 1; |
| 820 | return 0; |
| 821 | |
| 822 | fail: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 823 | pr_err("md: disabled device %s, could not read superblock.\n", |
| 824 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | return -EINVAL; |
| 826 | } |
| 827 | |
| 828 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 829 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 830 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 831 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 832 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 833 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 837 | { |
| 838 | int ret; |
| 839 | mdp_super_t *tmp1, *tmp2; |
| 840 | |
| 841 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 842 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 843 | |
| 844 | if (!tmp1 || !tmp2) { |
| 845 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | goto abort; |
| 847 | } |
| 848 | |
| 849 | *tmp1 = *sb1; |
| 850 | *tmp2 = *sb2; |
| 851 | |
| 852 | /* |
| 853 | * nr_disks is not constant |
| 854 | */ |
| 855 | tmp1->nr_disks = 0; |
| 856 | tmp2->nr_disks = 0; |
| 857 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 858 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 860 | kfree(tmp1); |
| 861 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | return ret; |
| 863 | } |
| 864 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 865 | static u32 md_csum_fold(u32 csum) |
| 866 | { |
| 867 | csum = (csum & 0xffff) + (csum >> 16); |
| 868 | return (csum & 0xffff) + (csum >> 16); |
| 869 | } |
| 870 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 871 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 873 | u64 newcsum = 0; |
| 874 | u32 *sb32 = (u32*)sb; |
| 875 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | unsigned int disk_csum, csum; |
| 877 | |
| 878 | disk_csum = sb->sb_csum; |
| 879 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 880 | |
| 881 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 882 | newcsum += sb32[i]; |
| 883 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 884 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 885 | #ifdef CONFIG_ALPHA |
| 886 | /* This used to use csum_partial, which was wrong for several |
| 887 | * reasons including that different results are returned on |
| 888 | * different architectures. It isn't critical that we get exactly |
| 889 | * the same return value as before (we always csum_fold before |
| 890 | * testing, and that removes any differences). However as we |
| 891 | * know that csum_partial always returned a 16bit value on |
| 892 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 893 | */ |
| 894 | sb->sb_csum = md_csum_fold(disk_csum); |
| 895 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 897 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | return csum; |
| 899 | } |
| 900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | /* |
| 902 | * Handle superblock details. |
| 903 | * We want to be able to handle multiple superblock formats |
| 904 | * so we have a common interface to them all, and an array of |
| 905 | * different handlers. |
| 906 | * We rely on user-space to write the initial superblock, and support |
| 907 | * reading and updating of superblocks. |
| 908 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 909 | * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | * loads and validates a superblock on dev. |
| 911 | * if refdev != NULL, compare superblocks on both devices |
| 912 | * Return: |
| 913 | * 0 - dev has a superblock that is compatible with refdev |
| 914 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 915 | * so dev should be used as the refdev in future |
| 916 | * -EINVAL superblock incompatible or invalid |
| 917 | * -othererror e.g. -EIO |
| 918 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 919 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | * Verify that dev is acceptable into mddev. |
| 921 | * The first time, mddev->raid_disks will be 0, and data from |
| 922 | * dev should be merged in. Subsequent calls check that dev |
| 923 | * is new enough. Return 0 or -EINVAL |
| 924 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 925 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | * Update the superblock for rdev with data in mddev |
| 927 | * This does not write to disc. |
| 928 | * |
| 929 | */ |
| 930 | |
| 931 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 932 | char *name; |
| 933 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 934 | int (*load_super)(struct md_rdev *rdev, |
| 935 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 936 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 937 | int (*validate_super)(struct mddev *mddev, |
| 938 | struct md_rdev *rdev); |
| 939 | void (*sync_super)(struct mddev *mddev, |
| 940 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 941 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 942 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 943 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 944 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | }; |
| 946 | |
| 947 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 948 | * Check that the given mddev has no bitmap. |
| 949 | * |
| 950 | * This function is called from the run method of all personalities that do not |
| 951 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 952 | * has a bitmap. Otherwise, it returns 0. |
| 953 | * |
| 954 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 955 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 956 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 957 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 958 | return 0; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 959 | pr_warn("%s: bitmaps are not supported for %s\n", |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 960 | mdname(mddev), mddev->pers->name); |
| 961 | return 1; |
| 962 | } |
| 963 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 964 | |
| 965 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 966 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 968 | static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | { |
| 970 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 971 | mdp_super_t *sb; |
| 972 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | |
| 974 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 975 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | * it's at the end of the disk. |
| 977 | * |
| 978 | * It also happens to be a multiple of 4Kb. |
| 979 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 980 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 982 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 983 | if (ret) |
| 984 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
| 986 | ret = -EINVAL; |
| 987 | |
| 988 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 989 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
| 991 | if (sb->md_magic != MD_SB_MAGIC) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 992 | pr_warn("md: invalid raid superblock magic on %s\n", b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | goto abort; |
| 994 | } |
| 995 | |
| 996 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 997 | sb->minor_version < 90 || |
| 998 | sb->minor_version > 91) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 999 | pr_warn("Bad version number %d.%d on %s\n", |
| 1000 | sb->major_version, sb->minor_version, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | goto abort; |
| 1002 | } |
| 1003 | |
| 1004 | if (sb->raid_disks <= 0) |
| 1005 | goto abort; |
| 1006 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1007 | if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1008 | pr_warn("md: invalid superblock checksum on %s\n", b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | goto abort; |
| 1010 | } |
| 1011 | |
| 1012 | rdev->preferred_minor = sb->md_minor; |
| 1013 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1014 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1015 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1016 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
| 1018 | if (sb->level == LEVEL_MULTIPATH) |
| 1019 | rdev->desc_nr = -1; |
| 1020 | else |
| 1021 | rdev->desc_nr = sb->this_disk.number; |
| 1022 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1023 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1025 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1027 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | if (!uuid_equal(refsb, sb)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1029 | pr_warn("md: %s has different UUID to %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | b, bdevname(refdev->bdev,b2)); |
| 1031 | goto abort; |
| 1032 | } |
| 1033 | if (!sb_equal(refsb, sb)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1034 | pr_warn("md: %s has same UUID but different superblock to %s\n", |
| 1035 | b, bdevname(refdev->bdev, b2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | goto abort; |
| 1037 | } |
| 1038 | ev1 = md_event(sb); |
| 1039 | ev2 = md_event(refsb); |
| 1040 | if (ev1 > ev2) |
| 1041 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1042 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | ret = 0; |
| 1044 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1045 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1046 | /* Limit to 4TB as metadata cannot record more than that. |
| 1047 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1048 | * record this size) |
| 1049 | */ |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1050 | if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) && |
| 1051 | sb->level >= 1) |
| 1052 | rdev->sectors = (sector_t)(2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1054 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1055 | /* "this cannot possibly happen" ... */ |
| 1056 | ret = -EINVAL; |
| 1057 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | abort: |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | * validate_super for 0.90.0 |
| 1064 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1065 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | { |
| 1067 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1068 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1069 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1071 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1072 | clear_bit(Faulty, &rdev->flags); |
| 1073 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1074 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1075 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | if (mddev->raid_disks == 0) { |
| 1078 | mddev->major_version = 0; |
| 1079 | mddev->minor_version = sb->minor_version; |
| 1080 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1081 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1082 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | mddev->ctime = sb->ctime; |
| 1084 | mddev->utime = sb->utime; |
| 1085 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1086 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | mddev->layout = sb->layout; |
| 1088 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1089 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1090 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1091 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1092 | mddev->bitmap_info.space = 0; |
| 1093 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1094 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1095 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1096 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1098 | if (mddev->minor_version >= 91) { |
| 1099 | mddev->reshape_position = sb->reshape_position; |
| 1100 | mddev->delta_disks = sb->delta_disks; |
| 1101 | mddev->new_level = sb->new_level; |
| 1102 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1103 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1104 | if (mddev->delta_disks < 0) |
| 1105 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1106 | } else { |
| 1107 | mddev->reshape_position = MaxSector; |
| 1108 | mddev->delta_disks = 0; |
| 1109 | mddev->new_level = mddev->level; |
| 1110 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1111 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1112 | } |
| 1113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1115 | mddev->recovery_cp = MaxSector; |
| 1116 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1117 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | sb->events_lo == sb->cp_events_lo) { |
| 1119 | mddev->recovery_cp = sb->recovery_cp; |
| 1120 | } else |
| 1121 | mddev->recovery_cp = 0; |
| 1122 | } |
| 1123 | |
| 1124 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1125 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1126 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1127 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1128 | |
| 1129 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1130 | |
| 1131 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1132 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1133 | mddev->bitmap_info.offset = |
| 1134 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1135 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1136 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1137 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1138 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1139 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1140 | /* Insist on good event counter while assembling, except |
| 1141 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1143 | if (sb->disks[rdev->desc_nr].state & ( |
| 1144 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1145 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1146 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1147 | } else if (mddev->bitmap) { |
| 1148 | /* if adding to array with a bitmap, then we can accept an |
| 1149 | * older device ... but not too old. |
| 1150 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1151 | if (ev1 < mddev->bitmap->events_cleared) |
| 1152 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1153 | if (ev1 < mddev->events) |
| 1154 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1155 | } else { |
| 1156 | if (ev1 < mddev->events) |
| 1157 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1158 | return 0; |
| 1159 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | desc = sb->disks + rdev->desc_nr; |
| 1163 | |
| 1164 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1165 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1166 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1167 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1168 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1170 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1171 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1172 | /* active but not in sync implies recovery up to |
| 1173 | * reshape position. We don't know exactly where |
| 1174 | * that is, so set to zero for now */ |
| 1175 | if (mddev->minor_version >= 91) { |
| 1176 | rdev->recovery_offset = 0; |
| 1177 | rdev->raid_disk = desc->raid_disk; |
| 1178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1180 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1181 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1182 | if (desc->state & (1<<MD_DISK_FAILFAST)) |
| 1183 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1184 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1185 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | /* |
| 1190 | * sync_super for 0.90.0 |
| 1191 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1192 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | { |
| 1194 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1195 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | /* make rdev->sb match mddev data.. |
| 1199 | * |
| 1200 | * 1/ zero out disks |
| 1201 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1202 | * 3/ any empty disks < next_spare become removed |
| 1203 | * |
| 1204 | * disks[0] gets initialised to REMOVED because |
| 1205 | * we cannot be sure from other fields if it has |
| 1206 | * been initialised or not. |
| 1207 | */ |
| 1208 | int i; |
| 1209 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1210 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1211 | rdev->sb_size = MD_SB_BYTES; |
| 1212 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1213 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | |
| 1215 | memset(sb, 0, sizeof(*sb)); |
| 1216 | |
| 1217 | sb->md_magic = MD_SB_MAGIC; |
| 1218 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | sb->patch_version = mddev->patch_version; |
| 1220 | sb->gvalid_words = 0; /* ignored */ |
| 1221 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1222 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1223 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1224 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1225 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1226 | sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1228 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | sb->raid_disks = mddev->raid_disks; |
| 1230 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1231 | sb->not_persistent = 0; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1232 | sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | sb->state = 0; |
| 1234 | sb->events_hi = (mddev->events>>32); |
| 1235 | sb->events_lo = (u32)mddev->events; |
| 1236 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1237 | if (mddev->reshape_position == MaxSector) |
| 1238 | sb->minor_version = 90; |
| 1239 | else { |
| 1240 | sb->minor_version = 91; |
| 1241 | sb->reshape_position = mddev->reshape_position; |
| 1242 | sb->new_level = mddev->new_level; |
| 1243 | sb->delta_disks = mddev->delta_disks; |
| 1244 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1245 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1246 | } |
| 1247 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | if (mddev->in_sync) |
| 1249 | { |
| 1250 | sb->recovery_cp = mddev->recovery_cp; |
| 1251 | sb->cp_events_hi = (mddev->events>>32); |
| 1252 | sb->cp_events_lo = (u32)mddev->events; |
| 1253 | if (mddev->recovery_cp == MaxSector) |
| 1254 | sb->state = (1<< MD_SB_CLEAN); |
| 1255 | } else |
| 1256 | sb->recovery_cp = 0; |
| 1257 | |
| 1258 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1259 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1261 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1262 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1265 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1267 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1268 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1269 | |
| 1270 | if (rdev2->raid_disk >= 0 && |
| 1271 | sb->minor_version >= 91) |
| 1272 | /* we have nowhere to store the recovery_offset, |
| 1273 | * but if it is not below the reshape_position, |
| 1274 | * we can piggy-back on that. |
| 1275 | */ |
| 1276 | is_active = 1; |
| 1277 | if (rdev2->raid_disk < 0 || |
| 1278 | test_bit(Faulty, &rdev2->flags)) |
| 1279 | is_active = 0; |
| 1280 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1281 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1283 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1284 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | d = &sb->disks[rdev2->desc_nr]; |
| 1286 | nr_disks++; |
| 1287 | d->number = rdev2->desc_nr; |
| 1288 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1289 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1290 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | d->raid_disk = rdev2->raid_disk; |
| 1292 | else |
| 1293 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1294 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1296 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1298 | if (test_bit(In_sync, &rdev2->flags)) |
| 1299 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | active++; |
| 1301 | working++; |
| 1302 | } else { |
| 1303 | d->state = 0; |
| 1304 | spare++; |
| 1305 | working++; |
| 1306 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1307 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1308 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1309 | if (test_bit(FailFast, &rdev2->flags)) |
| 1310 | d->state |= (1<<MD_DISK_FAILFAST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1313 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1314 | mdp_disk_t *d = &sb->disks[i]; |
| 1315 | if (d->state == 0 && d->number == 0) { |
| 1316 | d->number = i; |
| 1317 | d->raid_disk = i; |
| 1318 | d->state = (1<<MD_DISK_REMOVED); |
| 1319 | d->state |= (1<<MD_DISK_FAULTY); |
| 1320 | failed++; |
| 1321 | } |
| 1322 | } |
| 1323 | sb->nr_disks = nr_disks; |
| 1324 | sb->active_disks = active; |
| 1325 | sb->working_disks = working; |
| 1326 | sb->failed_disks = failed; |
| 1327 | sb->spare_disks = spare; |
| 1328 | |
| 1329 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1330 | sb->sb_csum = calc_sb_csum(sb); |
| 1331 | } |
| 1332 | |
| 1333 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1334 | * rdev_size_change for 0.90.0 |
| 1335 | */ |
| 1336 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1337 | super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1338 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1339 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1340 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1341 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1342 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1343 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1344 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1345 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1346 | /* Limit to 4TB as metadata cannot record more than that. |
| 1347 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1348 | */ |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1349 | if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) && |
| 1350 | rdev->mddev->level >= 1) |
| 1351 | num_sectors = (sector_t)(2ULL << 32) - 2; |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1352 | do { |
| 1353 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1354 | rdev->sb_page); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1355 | } while (md_super_wait(rdev->mddev) < 0); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1356 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1357 | } |
| 1358 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1359 | static int |
| 1360 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1361 | { |
| 1362 | /* non-zero offset changes not possible with v0.90 */ |
| 1363 | return new_offset == 0; |
| 1364 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1365 | |
| 1366 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | * version 1 superblock |
| 1368 | */ |
| 1369 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1370 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1372 | __le32 disk_csum; |
| 1373 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | unsigned long long newcsum; |
| 1375 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1376 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | |
| 1378 | disk_csum = sb->sb_csum; |
| 1379 | sb->sb_csum = 0; |
| 1380 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1381 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | newcsum += le32_to_cpu(*isuper++); |
| 1383 | |
| 1384 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1385 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | |
| 1387 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1388 | sb->sb_csum = disk_csum; |
| 1389 | return cpu_to_le32(csum); |
| 1390 | } |
| 1391 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1392 | static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | { |
| 1394 | struct mdp_superblock_1 *sb; |
| 1395 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1396 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1397 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1399 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | |
| 1401 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1402 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | * It is always aligned to a 4K boundary and |
| 1404 | * depeding on minor_version, it can be: |
| 1405 | * 0: At least 8K, but less than 12K, from end of device |
| 1406 | * 1: At start of device |
| 1407 | * 2: 4K from start of device. |
| 1408 | */ |
| 1409 | switch(minor_version) { |
| 1410 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1411 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1412 | sb_start -= 8*2; |
| 1413 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | break; |
| 1415 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1416 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | break; |
| 1418 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1419 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | break; |
| 1421 | default: |
| 1422 | return -EINVAL; |
| 1423 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1424 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1426 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1427 | * and it is safe to read 4k, so we do that |
| 1428 | */ |
| 1429 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | if (ret) return ret; |
| 1431 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1432 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | |
| 1434 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1435 | sb->major_version != cpu_to_le32(1) || |
| 1436 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1437 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1438 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | return -EINVAL; |
| 1440 | |
| 1441 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1442 | pr_warn("md: invalid superblock checksum on %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | bdevname(rdev->bdev,b)); |
| 1444 | return -EINVAL; |
| 1445 | } |
| 1446 | if (le64_to_cpu(sb->data_size) < 10) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1447 | pr_warn("md: data_size too small on %s\n", |
| 1448 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | return -EINVAL; |
| 1450 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1451 | if (sb->pad0 || |
| 1452 | sb->pad3[0] || |
| 1453 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1454 | /* Some padding is non-zero, might be a new feature */ |
| 1455 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | rdev->preferred_minor = 0xffff; |
| 1458 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1459 | rdev->new_data_offset = rdev->data_offset; |
| 1460 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1461 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1462 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1463 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1465 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1466 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1467 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1468 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1469 | |
| 1470 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1471 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1472 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1473 | if (minor_version |
| 1474 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1475 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1476 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1477 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1478 | rdev->desc_nr = -1; |
| 1479 | else |
| 1480 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1481 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1482 | if (!rdev->bb_page) { |
| 1483 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1484 | if (!rdev->bb_page) |
| 1485 | return -ENOMEM; |
| 1486 | } |
| 1487 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1488 | rdev->badblocks.count == 0) { |
| 1489 | /* need to load the bad block list. |
| 1490 | * Currently we limit it to one page. |
| 1491 | */ |
| 1492 | s32 offset; |
| 1493 | sector_t bb_sector; |
| 1494 | u64 *bbp; |
| 1495 | int i; |
| 1496 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1497 | if (sectors > (PAGE_SIZE / 512)) |
| 1498 | return -EINVAL; |
| 1499 | offset = le32_to_cpu(sb->bblog_offset); |
| 1500 | if (offset == 0) |
| 1501 | return -EINVAL; |
| 1502 | bb_sector = (long long)offset; |
| 1503 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1504 | rdev->bb_page, REQ_OP_READ, 0, true)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1505 | return -EIO; |
| 1506 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1507 | rdev->badblocks.shift = sb->bblog_shift; |
| 1508 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1509 | u64 bb = le64_to_cpu(*bbp); |
| 1510 | int count = bb & (0x3ff); |
| 1511 | u64 sector = bb >> 10; |
| 1512 | sector <<= sb->bblog_shift; |
| 1513 | count <<= sb->bblog_shift; |
| 1514 | if (bb + 1 == 0) |
| 1515 | break; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 1516 | if (badblocks_set(&rdev->badblocks, sector, count, 1)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1517 | return -EINVAL; |
| 1518 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1519 | } else if (sb->bblog_offset != 0) |
| 1520 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1521 | |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1522 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) { |
| 1523 | rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset); |
| 1524 | rdev->ppl.size = le16_to_cpu(sb->ppl.size); |
| 1525 | rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset; |
| 1526 | } |
| 1527 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1528 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1529 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1530 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1532 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | |
| 1534 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1535 | sb->level != refsb->level || |
| 1536 | sb->layout != refsb->layout || |
| 1537 | sb->chunksize != refsb->chunksize) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1538 | pr_warn("md: %s has strangely different superblock to %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | bdevname(rdev->bdev,b), |
| 1540 | bdevname(refdev->bdev,b2)); |
| 1541 | return -EINVAL; |
| 1542 | } |
| 1543 | ev1 = le64_to_cpu(sb->events); |
| 1544 | ev2 = le64_to_cpu(refsb->events); |
| 1545 | |
| 1546 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1547 | ret = 1; |
| 1548 | else |
| 1549 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1551 | if (minor_version) { |
| 1552 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1553 | sectors -= rdev->data_offset; |
| 1554 | } else |
| 1555 | sectors = rdev->sb_start; |
| 1556 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1558 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1559 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1562 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1564 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1565 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1567 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1568 | clear_bit(Faulty, &rdev->flags); |
| 1569 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1570 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1571 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | if (mddev->raid_disks == 0) { |
| 1574 | mddev->major_version = 1; |
| 1575 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1576 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1577 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1578 | mddev->ctime = le64_to_cpu(sb->ctime); |
| 1579 | mddev->utime = le64_to_cpu(sb->utime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1581 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | mddev->layout = le32_to_cpu(sb->layout); |
| 1583 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1584 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1585 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1586 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1587 | mddev->bitmap_info.space = 0; |
| 1588 | /* Default location for bitmap is 1K after superblock |
| 1589 | * using 3K - total of 4K |
| 1590 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1591 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1592 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1593 | mddev->reshape_backwards = 0; |
| 1594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1596 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1597 | |
| 1598 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1599 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1600 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1601 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1602 | mddev->bitmap_info.offset = |
| 1603 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1604 | /* Metadata doesn't record how much space is available. |
| 1605 | * For 1.0, we assume we can use up to the superblock |
| 1606 | * if before, else to 4K beyond superblock. |
| 1607 | * For others, assume no change is possible. |
| 1608 | */ |
| 1609 | if (mddev->minor_version > 0) |
| 1610 | mddev->bitmap_info.space = 0; |
| 1611 | else if (mddev->bitmap_info.offset > 0) |
| 1612 | mddev->bitmap_info.space = |
| 1613 | 8 - mddev->bitmap_info.offset; |
| 1614 | else |
| 1615 | mddev->bitmap_info.space = |
| 1616 | -mddev->bitmap_info.offset; |
| 1617 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1618 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1619 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1620 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1621 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1622 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1623 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1624 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1625 | if (mddev->delta_disks < 0 || |
| 1626 | (mddev->delta_disks == 0 && |
| 1627 | (le32_to_cpu(sb->feature_map) |
| 1628 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1629 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1630 | } else { |
| 1631 | mddev->reshape_position = MaxSector; |
| 1632 | mddev->delta_disks = 0; |
| 1633 | mddev->new_level = mddev->level; |
| 1634 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1635 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1636 | } |
| 1637 | |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 1638 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) |
Shaohua Li | a62ab49 | 2016-01-06 14:37:13 -0800 | [diff] [blame] | 1639 | set_bit(MD_HAS_JOURNAL, &mddev->flags); |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1640 | |
| 1641 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) { |
| 1642 | if (le32_to_cpu(sb->feature_map) & |
| 1643 | (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL)) |
| 1644 | return -EINVAL; |
| 1645 | set_bit(MD_HAS_PPL, &mddev->flags); |
| 1646 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1647 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1648 | /* Insist of good event counter while assembling, except for |
| 1649 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1651 | if (rdev->desc_nr >= 0 && |
| 1652 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
Song Liu | a3dfbda | 2015-10-08 21:54:11 -0700 | [diff] [blame] | 1653 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1654 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1655 | if (ev1 < mddev->events) |
| 1656 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1657 | } else if (mddev->bitmap) { |
| 1658 | /* If adding to array with a bitmap, then we can accept an |
| 1659 | * older device, but not too old. |
| 1660 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1661 | if (ev1 < mddev->bitmap->events_cleared) |
| 1662 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1663 | if (ev1 < mddev->events) |
| 1664 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1665 | } else { |
| 1666 | if (ev1 < mddev->events) |
| 1667 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1668 | return 0; |
| 1669 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1671 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1672 | if (rdev->desc_nr < 0 || |
| 1673 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1674 | role = MD_DISK_ROLE_SPARE; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1675 | rdev->desc_nr = -1; |
| 1676 | } else |
| 1677 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | switch(role) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1679 | case MD_DISK_ROLE_SPARE: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | break; |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1681 | case MD_DISK_ROLE_FAULTY: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1682 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | break; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1684 | case MD_DISK_ROLE_JOURNAL: /* journal device */ |
| 1685 | if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) { |
| 1686 | /* journal device without journal feature */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1687 | pr_warn("md: journal device provided without journal feature, ignoring the device\n"); |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1688 | return -EINVAL; |
| 1689 | } |
| 1690 | set_bit(Journal, &rdev->flags); |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1691 | rdev->journal_tail = le64_to_cpu(sb->journal_tail); |
Shaohua Li | 9b15603 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 1692 | rdev->raid_disk = 0; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1693 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1695 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1696 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1697 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1698 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1699 | if (!(le32_to_cpu(sb->feature_map) & |
| 1700 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1701 | rdev->saved_raid_disk = -1; |
| 1702 | } else |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1703 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | rdev->raid_disk = role; |
| 1705 | break; |
| 1706 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1707 | if (sb->devflags & WriteMostly1) |
| 1708 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1709 | if (sb->devflags & FailFast1) |
| 1710 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1711 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1712 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1713 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1714 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | return 0; |
| 1717 | } |
| 1718 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1719 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | { |
| 1721 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1722 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | int max_dev, i; |
| 1724 | /* make rdev->sb match mddev and rdev data. */ |
| 1725 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1726 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | |
| 1728 | sb->feature_map = 0; |
| 1729 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1730 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1732 | |
| 1733 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1734 | sb->events = cpu_to_le64(mddev->events); |
| 1735 | if (mddev->in_sync) |
| 1736 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
Shaohua Li | bd18f64 | 2015-09-02 13:49:50 -0700 | [diff] [blame] | 1737 | else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags)) |
| 1738 | sb->resync_offset = cpu_to_le64(MaxSector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | else |
| 1740 | sb->resync_offset = cpu_to_le64(0); |
| 1741 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1742 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1743 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1744 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1745 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1746 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1747 | sb->level = cpu_to_le32(mddev->level); |
| 1748 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1749 | if (test_bit(FailFast, &rdev->flags)) |
| 1750 | sb->devflags |= FailFast1; |
| 1751 | else |
| 1752 | sb->devflags &= ~FailFast1; |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1753 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1754 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1755 | sb->devflags |= WriteMostly1; |
| 1756 | else |
| 1757 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1758 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1759 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1760 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1761 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1762 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1763 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1764 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1765 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 1766 | if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1767 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1768 | sb->feature_map |= |
| 1769 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1770 | sb->recovery_offset = |
| 1771 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1772 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 1773 | sb->feature_map |= |
| 1774 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1775 | } |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1776 | /* Note: recovery_offset and journal_tail share space */ |
| 1777 | if (test_bit(Journal, &rdev->flags)) |
| 1778 | sb->journal_tail = cpu_to_le64(rdev->journal_tail); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1779 | if (test_bit(Replacement, &rdev->flags)) |
| 1780 | sb->feature_map |= |
| 1781 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1782 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1783 | if (mddev->reshape_position != MaxSector) { |
| 1784 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1785 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1786 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1787 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1788 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1789 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1790 | if (mddev->delta_disks == 0 && |
| 1791 | mddev->reshape_backwards) |
| 1792 | sb->feature_map |
| 1793 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1794 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1795 | sb->feature_map |
| 1796 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1797 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1798 | - rdev->data_offset)); |
| 1799 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1800 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1801 | |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 1802 | if (mddev_is_clustered(mddev)) |
| 1803 | sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED); |
| 1804 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1805 | if (rdev->badblocks.count == 0) |
| 1806 | /* Nothing to do for bad blocks*/ ; |
| 1807 | else if (sb->bblog_offset == 0) |
| 1808 | /* Cannot record bad blocks on this device */ |
| 1809 | md_error(mddev, rdev); |
| 1810 | else { |
| 1811 | struct badblocks *bb = &rdev->badblocks; |
| 1812 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1813 | u64 *p = bb->page; |
| 1814 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1815 | if (bb->changed) { |
| 1816 | unsigned seq; |
| 1817 | |
| 1818 | retry: |
| 1819 | seq = read_seqbegin(&bb->lock); |
| 1820 | |
| 1821 | memset(bbp, 0xff, PAGE_SIZE); |
| 1822 | |
| 1823 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1824 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1825 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1826 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1827 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1828 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1829 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1830 | if (read_seqretry(&bb->lock, seq)) |
| 1831 | goto retry; |
| 1832 | |
| 1833 | bb->sector = (rdev->sb_start + |
| 1834 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1835 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1836 | } |
| 1837 | } |
| 1838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1840 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | if (rdev2->desc_nr+1 > max_dev) |
| 1842 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1843 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1844 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1845 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1846 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1847 | rdev->sb_size = max_dev * 2 + 256; |
| 1848 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1849 | if (rdev->sb_size & bmask) |
| 1850 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1851 | } else |
| 1852 | max_dev = le32_to_cpu(sb->max_dev); |
| 1853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | for (i=0; i<max_dev;i++) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1855 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1856 | |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1857 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) |
| 1858 | sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1860 | if (test_bit(MD_HAS_PPL, &mddev->flags)) { |
| 1861 | sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL); |
| 1862 | sb->ppl.offset = cpu_to_le16(rdev->ppl.offset); |
| 1863 | sb->ppl.size = cpu_to_le16(rdev->ppl.size); |
| 1864 | } |
| 1865 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1866 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1868 | if (test_bit(Faulty, &rdev2->flags)) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1869 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1870 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1872 | else if (test_bit(Journal, &rdev2->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1873 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1874 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1875 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | else |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1877 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | } |
| 1879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1881 | } |
| 1882 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1883 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1884 | super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1885 | { |
| 1886 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1887 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1888 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1889 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1890 | if (rdev->data_offset != rdev->new_data_offset) |
| 1891 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1892 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1893 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1894 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1895 | max_sectors -= rdev->data_offset; |
| 1896 | if (!num_sectors || num_sectors > max_sectors) |
| 1897 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1898 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1899 | /* minor version 0 with bitmap we can't move */ |
| 1900 | return 0; |
| 1901 | } else { |
| 1902 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1903 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1904 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1905 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1906 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1907 | if (!num_sectors || num_sectors > max_sectors) |
| 1908 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1909 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1910 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1911 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1912 | sb->data_size = cpu_to_le64(num_sectors); |
Jason Yan | 3fb632e | 2017-03-10 11:27:23 +0800 | [diff] [blame] | 1913 | sb->super_offset = cpu_to_le64(rdev->sb_start); |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1914 | sb->sb_csum = calc_sb_1_csum(sb); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1915 | do { |
| 1916 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
| 1917 | rdev->sb_page); |
| 1918 | } while (md_super_wait(rdev->mddev) < 0); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1919 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1920 | |
| 1921 | } |
| 1922 | |
| 1923 | static int |
| 1924 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1925 | unsigned long long new_offset) |
| 1926 | { |
| 1927 | /* All necessary checks on new >= old have been done */ |
| 1928 | struct bitmap *bitmap; |
| 1929 | if (new_offset >= rdev->data_offset) |
| 1930 | return 1; |
| 1931 | |
| 1932 | /* with 1.0 metadata, there is no metadata to tread on |
| 1933 | * so we can always move back */ |
| 1934 | if (rdev->mddev->minor_version == 0) |
| 1935 | return 1; |
| 1936 | |
| 1937 | /* otherwise we must be sure not to step on |
| 1938 | * any metadata, so stay: |
| 1939 | * 36K beyond start of superblock |
| 1940 | * beyond end of badblocks |
| 1941 | * beyond write-intent bitmap |
| 1942 | */ |
| 1943 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1944 | return 0; |
| 1945 | bitmap = rdev->mddev->bitmap; |
| 1946 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1947 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1948 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1949 | return 0; |
| 1950 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1951 | return 0; |
| 1952 | |
| 1953 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1954 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1956 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | [0] = { |
| 1958 | .name = "0.90.0", |
| 1959 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1960 | .load_super = super_90_load, |
| 1961 | .validate_super = super_90_validate, |
| 1962 | .sync_super = super_90_sync, |
| 1963 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1964 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | }, |
| 1966 | [1] = { |
| 1967 | .name = "md-1", |
| 1968 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1969 | .load_super = super_1_load, |
| 1970 | .validate_super = super_1_validate, |
| 1971 | .sync_super = super_1_sync, |
| 1972 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1973 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | }, |
| 1975 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1977 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1978 | { |
| 1979 | if (mddev->sync_super) { |
| 1980 | mddev->sync_super(mddev, rdev); |
| 1981 | return; |
| 1982 | } |
| 1983 | |
| 1984 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1985 | |
| 1986 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1987 | } |
| 1988 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1989 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1991 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1993 | rcu_read_lock(); |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 1994 | rdev_for_each_rcu(rdev, mddev1) { |
| 1995 | if (test_bit(Faulty, &rdev->flags) || |
| 1996 | test_bit(Journal, &rdev->flags) || |
| 1997 | rdev->raid_disk == -1) |
| 1998 | continue; |
| 1999 | rdev_for_each_rcu(rdev2, mddev2) { |
| 2000 | if (test_bit(Faulty, &rdev2->flags) || |
| 2001 | test_bit(Journal, &rdev2->flags) || |
| 2002 | rdev2->raid_disk == -1) |
| 2003 | continue; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2004 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2005 | rdev2->bdev->bd_contains) { |
| 2006 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2007 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2008 | } |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 2009 | } |
| 2010 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2011 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | return 0; |
| 2013 | } |
| 2014 | |
| 2015 | static LIST_HEAD(pending_raid_disks); |
| 2016 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2017 | /* |
| 2018 | * Try to register data integrity profile for an mddev |
| 2019 | * |
| 2020 | * This is called when an array is started and after a disk has been kicked |
| 2021 | * from the array. It only succeeds if all working and active component devices |
| 2022 | * are integrity capable with matching profiles. |
| 2023 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2024 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2025 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2026 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2027 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2028 | if (list_empty(&mddev->disks)) |
| 2029 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 2030 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 2031 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2032 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2033 | /* skip spares and non-functional disks */ |
| 2034 | if (test_bit(Faulty, &rdev->flags)) |
| 2035 | continue; |
| 2036 | if (rdev->raid_disk < 0) |
| 2037 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2038 | if (!reference) { |
| 2039 | /* Use the first rdev as the reference */ |
| 2040 | reference = rdev; |
| 2041 | continue; |
| 2042 | } |
| 2043 | /* does this rdev's profile match the reference profile? */ |
| 2044 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 2045 | rdev->bdev->bd_disk) < 0) |
| 2046 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2047 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2048 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2049 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2050 | /* |
| 2051 | * All component devices are integrity capable and have matching |
| 2052 | * profiles, register the common profile for the md device. |
| 2053 | */ |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 2054 | blk_integrity_register(mddev->gendisk, |
| 2055 | bdev_get_integrity(reference->bdev)); |
| 2056 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2057 | pr_debug("md: data integrity enabled on %s\n", mdname(mddev)); |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2058 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2059 | pr_err("md: failed to create integrity pool for %s\n", |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2060 | mdname(mddev)); |
| 2061 | return -EINVAL; |
| 2062 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2063 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2064 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2065 | EXPORT_SYMBOL(md_integrity_register); |
| 2066 | |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2067 | /* |
| 2068 | * Attempt to add an rdev, but only if it is consistent with the current |
| 2069 | * integrity profile |
| 2070 | */ |
| 2071 | int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2072 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2073 | struct blk_integrity *bi_rdev; |
| 2074 | struct blk_integrity *bi_mddev; |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2075 | char name[BDEVNAME_SIZE]; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2076 | |
| 2077 | if (!mddev->gendisk) |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2078 | return 0; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2079 | |
| 2080 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2081 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2082 | |
| 2083 | if (!bi_mddev) /* nothing to do */ |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2084 | return 0; |
| 2085 | |
| 2086 | if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2087 | pr_err("%s: incompatible integrity profile for %s\n", |
| 2088 | mdname(mddev), bdevname(rdev->bdev, name)); |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2089 | return -ENXIO; |
| 2090 | } |
| 2091 | |
| 2092 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2093 | } |
| 2094 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2095 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2096 | static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2098 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2099 | struct kobject *ko; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2100 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2102 | /* prevent duplicates */ |
| 2103 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2104 | return -EEXIST; |
| 2105 | |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 2106 | if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) && |
| 2107 | mddev->pers) |
| 2108 | return -EROFS; |
| 2109 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2110 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2111 | if (!test_bit(Journal, &rdev->flags) && |
| 2112 | rdev->sectors && |
| 2113 | (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2114 | if (mddev->pers) { |
| 2115 | /* Cannot change size, so fail |
| 2116 | * If mddev->level <= 0, then we don't care |
| 2117 | * about aligning sizes (e.g. linear) |
| 2118 | */ |
| 2119 | if (mddev->level > 0) |
| 2120 | return -ENOSPC; |
| 2121 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2122 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2123 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | |
| 2125 | /* Verify rdev->desc_nr is unique. |
| 2126 | * If it is -1, assign a free number, else |
| 2127 | * check number is not in use |
| 2128 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2129 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | if (rdev->desc_nr < 0) { |
| 2131 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2132 | if (mddev->pers) |
| 2133 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2134 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | choice++; |
| 2136 | rdev->desc_nr = choice; |
| 2137 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2138 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2139 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2143 | rcu_read_unlock(); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2144 | if (!test_bit(Journal, &rdev->flags) && |
| 2145 | mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2146 | pr_warn("md: %s: array is limited to %d devices\n", |
| 2147 | mdname(mddev), mddev->max_disks); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2148 | return -EBUSY; |
| 2149 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2150 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2151 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | rdev->mddev = mddev; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2154 | pr_debug("md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2155 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2156 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2157 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2158 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2159 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2160 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2161 | /* failure here is OK */; |
| 2162 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2163 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2164 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2165 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2166 | |
| 2167 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2168 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2171 | |
| 2172 | fail: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2173 | pr_warn("md: failed to register dev-%s for %s\n", |
| 2174 | b, mdname(mddev)); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2175 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | } |
| 2177 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2178 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2179 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2180 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2181 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2182 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2183 | } |
| 2184 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2185 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | { |
| 2187 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2188 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2189 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2190 | list_del_rcu(&rdev->same_set); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2191 | pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2193 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2194 | sysfs_put(rdev->sysfs_state); |
| 2195 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2196 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2197 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2198 | * writing to 'remove' to "dev/state". We also need |
| 2199 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2200 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2201 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2202 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2203 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2204 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | /* |
| 2208 | * prevent the device from being mounted, repartitioned or |
| 2209 | * otherwise reused by a RAID array (or any other kernel |
| 2210 | * subsystem), by bd_claiming the device. |
| 2211 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2212 | static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | { |
| 2214 | int err = 0; |
| 2215 | struct block_device *bdev; |
| 2216 | char b[BDEVNAME_SIZE]; |
| 2217 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2218 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2219 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | if (IS_ERR(bdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2221 | pr_warn("md: could not open %s.\n", __bdevname(dev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | return PTR_ERR(bdev); |
| 2223 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | rdev->bdev = bdev; |
| 2225 | return err; |
| 2226 | } |
| 2227 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2228 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | { |
| 2230 | struct block_device *bdev = rdev->bdev; |
| 2231 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2232 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | void md_autodetect_dev(dev_t dev); |
| 2236 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2237 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | { |
| 2239 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2240 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2241 | pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2242 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2244 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2245 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | #endif |
| 2247 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2248 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | } |
| 2250 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2251 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | { |
| 2253 | unbind_rdev_from_array(rdev); |
| 2254 | export_rdev(rdev); |
| 2255 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2256 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2258 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2260 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2262 | while (!list_empty(&mddev->disks)) { |
| 2263 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2264 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2265 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | mddev->raid_disks = 0; |
| 2268 | mddev->major_version = 0; |
| 2269 | } |
| 2270 | |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2271 | static bool set_in_sync(struct mddev *mddev) |
| 2272 | { |
| 2273 | WARN_ON_ONCE(!spin_is_locked(&mddev->lock)); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2274 | if (!mddev->in_sync) { |
| 2275 | mddev->sync_checkers++; |
| 2276 | spin_unlock(&mddev->lock); |
| 2277 | percpu_ref_switch_to_atomic_sync(&mddev->writes_pending); |
| 2278 | spin_lock(&mddev->lock); |
| 2279 | if (!mddev->in_sync && |
| 2280 | percpu_ref_is_zero(&mddev->writes_pending)) { |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2281 | mddev->in_sync = 1; |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2282 | /* |
| 2283 | * Ensure ->in_sync is visible before we clear |
| 2284 | * ->sync_checkers. |
| 2285 | */ |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2286 | smp_mb(); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2287 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 2288 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 2289 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2290 | if (--mddev->sync_checkers == 0) |
| 2291 | percpu_ref_switch_to_percpu(&mddev->writes_pending); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2292 | } |
| 2293 | if (mddev->safemode == 1) |
| 2294 | mddev->safemode = 0; |
| 2295 | return mddev->in_sync; |
| 2296 | } |
| 2297 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2298 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2300 | /* Update each superblock (in-memory image), but |
| 2301 | * if we are allowed to, skip spares which already |
| 2302 | * have the right event counter, or have one earlier |
| 2303 | * (which would mean they aren't being marked as dirty |
| 2304 | * with the rest of the array) |
| 2305 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2306 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2307 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2308 | if (rdev->sb_events == mddev->events || |
| 2309 | (nospares && |
| 2310 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2311 | rdev->sb_events+1 == mddev->events)) { |
| 2312 | /* Don't update this superblock */ |
| 2313 | rdev->sb_loaded = 2; |
| 2314 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2315 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2316 | rdev->sb_loaded = 1; |
| 2317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | } |
| 2319 | } |
| 2320 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2321 | static bool does_sb_need_changing(struct mddev *mddev) |
| 2322 | { |
| 2323 | struct md_rdev *rdev; |
| 2324 | struct mdp_superblock_1 *sb; |
| 2325 | int role; |
| 2326 | |
| 2327 | /* Find a good rdev */ |
| 2328 | rdev_for_each(rdev, mddev) |
| 2329 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) |
| 2330 | break; |
| 2331 | |
| 2332 | /* No good device found. */ |
| 2333 | if (!rdev) |
| 2334 | return false; |
| 2335 | |
| 2336 | sb = page_address(rdev->sb_page); |
| 2337 | /* Check if a device has become faulty or a spare become active */ |
| 2338 | rdev_for_each(rdev, mddev) { |
| 2339 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 2340 | /* Device activated? */ |
| 2341 | if (role == 0xffff && rdev->raid_disk >=0 && |
| 2342 | !test_bit(Faulty, &rdev->flags)) |
| 2343 | return true; |
| 2344 | /* Device turned faulty? */ |
| 2345 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) |
| 2346 | return true; |
| 2347 | } |
| 2348 | |
| 2349 | /* Check if any mddev parameters have changed */ |
| 2350 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || |
| 2351 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || |
Jason Yan | 1345921 | 2017-03-10 11:49:12 +0800 | [diff] [blame] | 2352 | (mddev->layout != le32_to_cpu(sb->layout)) || |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2353 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || |
| 2354 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) |
| 2355 | return true; |
| 2356 | |
| 2357 | return false; |
| 2358 | } |
| 2359 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2360 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2362 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2363 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2364 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2365 | int any_badblocks_changed = 0; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2366 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2368 | if (mddev->ro) { |
| 2369 | if (force_change) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2370 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2371 | return; |
| 2372 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2373 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2374 | repeat: |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2375 | if (mddev_is_clustered(mddev)) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2376 | if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2377 | force_change = 1; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2378 | if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags)) |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 2379 | nospares = 1; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2380 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2381 | /* Has someone else has updated the sb */ |
| 2382 | if (!does_sb_need_changing(mddev)) { |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2383 | if (ret == 0) |
| 2384 | md_cluster_ops->metadata_update_cancel(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2385 | bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING), |
| 2386 | BIT(MD_SB_CHANGE_DEVS) | |
| 2387 | BIT(MD_SB_CHANGE_CLEAN)); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2388 | return; |
| 2389 | } |
| 2390 | } |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2391 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2392 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2393 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2394 | if (rdev->raid_disk >= 0 && |
| 2395 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2396 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2397 | !test_bit(In_sync, &rdev->flags) && |
| 2398 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2399 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2400 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2401 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2402 | if (!mddev->persistent) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2403 | clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 2404 | clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2405 | if (!mddev->external) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2406 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2407 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2408 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2409 | rdev->badblocks.changed = 0; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2410 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2411 | md_error(mddev, rdev); |
| 2412 | } |
| 2413 | clear_bit(Blocked, &rdev->flags); |
| 2414 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2415 | wake_up(&rdev->blocked_wait); |
| 2416 | } |
| 2417 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2418 | wake_up(&mddev->sb_wait); |
| 2419 | return; |
| 2420 | } |
| 2421 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2422 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2423 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 2424 | mddev->utime = ktime_get_real_seconds(); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2425 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2426 | if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2427 | force_change = 1; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2428 | if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags)) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2429 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2430 | * though if events isn't the right even/odd, we will have to do |
| 2431 | * spares after all |
| 2432 | */ |
| 2433 | nospares = 1; |
| 2434 | if (force_change) |
| 2435 | nospares = 0; |
| 2436 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2437 | /* If the array is degraded, then skipping spares is both |
| 2438 | * dangerous and fairly pointless. |
| 2439 | * Dangerous because a device that was removed from the array |
| 2440 | * might have a event_count that still looks up-to-date, |
| 2441 | * so it can be re-added without a resync. |
| 2442 | * Pointless because if there are any spares to skip, |
| 2443 | * then a recovery will happen and soon that array won't |
| 2444 | * be degraded any more and the spare can go back to sleep then. |
| 2445 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2446 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2447 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2448 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2449 | |
| 2450 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2451 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2452 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2453 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2454 | && mddev->can_decrease_events |
| 2455 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2456 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2457 | mddev->can_decrease_events = 0; |
| 2458 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2459 | /* otherwise we have to go forward and ... */ |
| 2460 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2461 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2464 | /* |
| 2465 | * This 64-bit counter should never wrap. |
| 2466 | * Either we are in around ~1 trillion A.C., assuming |
| 2467 | * 1 reboot per second, or we have a bug... |
| 2468 | */ |
| 2469 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2470 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2471 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2472 | if (rdev->badblocks.changed) |
| 2473 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2474 | if (test_bit(Faulty, &rdev->flags)) |
| 2475 | set_bit(FaultRecorded, &rdev->flags); |
| 2476 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2477 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2478 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2479 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2481 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2482 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | |
Shaohua Li | 504634f | 2016-11-18 09:44:08 -0800 | [diff] [blame] | 2484 | if (mddev->queue) |
| 2485 | blk_add_trace_msg(mddev->queue, "md md_update_sb"); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2486 | rewrite: |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2487 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2488 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2490 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2491 | if (rdev->sb_loaded != 1) |
| 2492 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2494 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2495 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2496 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2497 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2498 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2499 | bdevname(rdev->bdev, b), |
| 2500 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2501 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2502 | if (rdev->badblocks.size) { |
| 2503 | md_super_write(mddev, rdev, |
| 2504 | rdev->badblocks.sector, |
| 2505 | rdev->badblocks.size << 9, |
| 2506 | rdev->bb_page); |
| 2507 | rdev->badblocks.size = 0; |
| 2508 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2509 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2510 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2511 | pr_debug("md: %s (skipping faulty)\n", |
| 2512 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2513 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2514 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | /* only need to write one superblock... */ |
| 2516 | break; |
| 2517 | } |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2518 | if (md_super_wait(mddev) < 0) |
| 2519 | goto rewrite; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2520 | /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */ |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2521 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2522 | if (mddev_is_clustered(mddev) && ret == 0) |
| 2523 | md_cluster_ops->metadata_update_finish(mddev); |
| 2524 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2525 | if (mddev->in_sync != sync_req || |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2526 | !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING), |
| 2527 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN))) |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2528 | /* have to write it out again */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2529 | goto repeat; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2530 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2531 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2532 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2533 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2534 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2535 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2536 | clear_bit(Blocked, &rdev->flags); |
| 2537 | |
| 2538 | if (any_badblocks_changed) |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2539 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2540 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2541 | wake_up(&rdev->blocked_wait); |
| 2542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2544 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2546 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2547 | { |
| 2548 | struct mddev *mddev = rdev->mddev; |
| 2549 | int err = 0; |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2550 | bool add_journal = test_bit(Journal, &rdev->flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2551 | |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2552 | if (!mddev->pers->hot_remove_disk || add_journal) { |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2553 | /* If there is hot_add_disk but no hot_remove_disk |
| 2554 | * then added disks for geometry changes, |
| 2555 | * and should be added immediately. |
| 2556 | */ |
| 2557 | super_types[mddev->major_version]. |
| 2558 | validate_super(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2559 | if (add_journal) |
| 2560 | mddev_suspend(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2561 | err = mddev->pers->hot_add_disk(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2562 | if (add_journal) |
| 2563 | mddev_resume(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2564 | if (err) { |
Guoqing Jiang | db76767 | 2016-06-02 23:32:05 -0400 | [diff] [blame] | 2565 | md_kick_rdev_from_array(rdev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2566 | return err; |
| 2567 | } |
| 2568 | } |
| 2569 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2570 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2571 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2572 | if (mddev->degraded) |
| 2573 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2574 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2575 | md_new_event(mddev); |
| 2576 | md_wakeup_thread(mddev->thread); |
| 2577 | return 0; |
| 2578 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2580 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2581 | * We want to accept with case. For this we use cmd_match. |
| 2582 | */ |
| 2583 | static int cmd_match(const char *cmd, const char *str) |
| 2584 | { |
| 2585 | /* See if cmd, written into a sysfs file, matches |
| 2586 | * str. They must either be the same, or cmd can |
| 2587 | * have a trailing newline |
| 2588 | */ |
| 2589 | while (*cmd && *str && *cmd == *str) { |
| 2590 | cmd++; |
| 2591 | str++; |
| 2592 | } |
| 2593 | if (*cmd == '\n') |
| 2594 | cmd++; |
| 2595 | if (*str || *cmd) |
| 2596 | return 0; |
| 2597 | return 1; |
| 2598 | } |
| 2599 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2600 | struct rdev_sysfs_entry { |
| 2601 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2602 | ssize_t (*show)(struct md_rdev *, char *); |
| 2603 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2604 | }; |
| 2605 | |
| 2606 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2607 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2608 | { |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2609 | char *sep = ","; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2610 | size_t len = 0; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2611 | unsigned long flags = ACCESS_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2612 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2613 | if (test_bit(Faulty, &flags) || |
Tomasz Majchrzak | dcbcb48 | 2016-10-21 16:27:08 +0200 | [diff] [blame] | 2614 | (!test_bit(ExternalBbl, &flags) && |
| 2615 | rdev->badblocks.unacked_exist)) |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2616 | len += sprintf(page+len, "faulty%s", sep); |
| 2617 | if (test_bit(In_sync, &flags)) |
| 2618 | len += sprintf(page+len, "in_sync%s", sep); |
| 2619 | if (test_bit(Journal, &flags)) |
| 2620 | len += sprintf(page+len, "journal%s", sep); |
| 2621 | if (test_bit(WriteMostly, &flags)) |
| 2622 | len += sprintf(page+len, "write_mostly%s", sep); |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2623 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2624 | (rdev->badblocks.unacked_exist |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2625 | && !test_bit(Faulty, &flags))) |
| 2626 | len += sprintf(page+len, "blocked%s", sep); |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2627 | if (!test_bit(Faulty, &flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2628 | !test_bit(Journal, &flags) && |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2629 | !test_bit(In_sync, &flags)) |
| 2630 | len += sprintf(page+len, "spare%s", sep); |
| 2631 | if (test_bit(WriteErrorSeen, &flags)) |
| 2632 | len += sprintf(page+len, "write_error%s", sep); |
| 2633 | if (test_bit(WantReplacement, &flags)) |
| 2634 | len += sprintf(page+len, "want_replacement%s", sep); |
| 2635 | if (test_bit(Replacement, &flags)) |
| 2636 | len += sprintf(page+len, "replacement%s", sep); |
| 2637 | if (test_bit(ExternalBbl, &flags)) |
| 2638 | len += sprintf(page+len, "external_bbl%s", sep); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2639 | if (test_bit(FailFast, &flags)) |
| 2640 | len += sprintf(page+len, "failfast%s", sep); |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2641 | |
| 2642 | if (len) |
| 2643 | len -= strlen(sep); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2644 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2645 | return len+sprintf(page+len, "\n"); |
| 2646 | } |
| 2647 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2648 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2649 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2650 | { |
| 2651 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2652 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2653 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2654 | * writemostly - sets write_mostly |
| 2655 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2656 | * blocked - sets the Blocked flags |
| 2657 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2658 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2659 | * -insync - clear Insync for a device with a slot assigned, |
| 2660 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2661 | * write_error - sets WriteErrorSeen |
| 2662 | * -write_error - clears WriteErrorSeen |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2663 | * {,-}failfast - set/clear FailFast |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2664 | */ |
| 2665 | int err = -EINVAL; |
| 2666 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2667 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2668 | if (test_bit(Faulty, &rdev->flags)) |
| 2669 | err = 0; |
| 2670 | else |
| 2671 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2672 | } else if (cmd_match(buf, "remove")) { |
Shaohua Li | 5d88178 | 2016-07-28 09:06:34 -0700 | [diff] [blame] | 2673 | if (rdev->mddev->pers) { |
| 2674 | clear_bit(Blocked, &rdev->flags); |
| 2675 | remove_and_add_spares(rdev->mddev, rdev); |
| 2676 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2677 | if (rdev->raid_disk >= 0) |
| 2678 | err = -EBUSY; |
| 2679 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2680 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2681 | err = 0; |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 2682 | if (mddev_is_clustered(mddev)) |
| 2683 | err = md_cluster_ops->remove_disk(mddev, rdev); |
| 2684 | |
| 2685 | if (err == 0) { |
| 2686 | md_kick_rdev_from_array(rdev); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 2687 | if (mddev->pers) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2688 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 2689 | md_wakeup_thread(mddev->thread); |
| 2690 | } |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 2691 | md_new_event(mddev); |
| 2692 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2693 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2694 | } else if (cmd_match(buf, "writemostly")) { |
| 2695 | set_bit(WriteMostly, &rdev->flags); |
| 2696 | err = 0; |
| 2697 | } else if (cmd_match(buf, "-writemostly")) { |
| 2698 | clear_bit(WriteMostly, &rdev->flags); |
| 2699 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2700 | } else if (cmd_match(buf, "blocked")) { |
| 2701 | set_bit(Blocked, &rdev->flags); |
| 2702 | err = 0; |
| 2703 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2704 | if (!test_bit(Faulty, &rdev->flags) && |
Tomasz Majchrzak | dcbcb48 | 2016-10-21 16:27:08 +0200 | [diff] [blame] | 2705 | !test_bit(ExternalBbl, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2706 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2707 | /* metadata handler doesn't understand badblocks, |
| 2708 | * so we need to fail the device |
| 2709 | */ |
| 2710 | md_error(rdev->mddev, rdev); |
| 2711 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2712 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2713 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2714 | wake_up(&rdev->blocked_wait); |
| 2715 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2716 | md_wakeup_thread(rdev->mddev->thread); |
| 2717 | |
| 2718 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2719 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2720 | set_bit(In_sync, &rdev->flags); |
| 2721 | err = 0; |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2722 | } else if (cmd_match(buf, "failfast")) { |
| 2723 | set_bit(FailFast, &rdev->flags); |
| 2724 | err = 0; |
| 2725 | } else if (cmd_match(buf, "-failfast")) { |
| 2726 | clear_bit(FailFast, &rdev->flags); |
| 2727 | err = 0; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2728 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 && |
| 2729 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 2730 | if (rdev->mddev->pers == NULL) { |
| 2731 | clear_bit(In_sync, &rdev->flags); |
| 2732 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2733 | rdev->raid_disk = -1; |
| 2734 | err = 0; |
| 2735 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2736 | } else if (cmd_match(buf, "write_error")) { |
| 2737 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2738 | err = 0; |
| 2739 | } else if (cmd_match(buf, "-write_error")) { |
| 2740 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2741 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2742 | } else if (cmd_match(buf, "want_replacement")) { |
| 2743 | /* Any non-spare device that is not a replacement can |
| 2744 | * become want_replacement at any time, but we then need to |
| 2745 | * check if recovery is needed. |
| 2746 | */ |
| 2747 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2748 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2749 | !test_bit(Replacement, &rdev->flags)) |
| 2750 | set_bit(WantReplacement, &rdev->flags); |
| 2751 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2752 | md_wakeup_thread(rdev->mddev->thread); |
| 2753 | err = 0; |
| 2754 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2755 | /* Clearing 'want_replacement' is always allowed. |
| 2756 | * Once replacements starts it is too late though. |
| 2757 | */ |
| 2758 | err = 0; |
| 2759 | clear_bit(WantReplacement, &rdev->flags); |
| 2760 | } else if (cmd_match(buf, "replacement")) { |
| 2761 | /* Can only set a device as a replacement when array has not |
| 2762 | * yet been started. Once running, replacement is automatic |
| 2763 | * from spares, or by assigning 'slot'. |
| 2764 | */ |
| 2765 | if (rdev->mddev->pers) |
| 2766 | err = -EBUSY; |
| 2767 | else { |
| 2768 | set_bit(Replacement, &rdev->flags); |
| 2769 | err = 0; |
| 2770 | } |
| 2771 | } else if (cmd_match(buf, "-replacement")) { |
| 2772 | /* Similarly, can only clear Replacement before start */ |
| 2773 | if (rdev->mddev->pers) |
| 2774 | err = -EBUSY; |
| 2775 | else { |
| 2776 | clear_bit(Replacement, &rdev->flags); |
| 2777 | err = 0; |
| 2778 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2779 | } else if (cmd_match(buf, "re-add")) { |
| 2780 | if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 2781 | /* clear_bit is performed _after_ all the devices |
| 2782 | * have their local Faulty bit cleared. If any writes |
| 2783 | * happen in the meantime in the local node, they |
| 2784 | * will land in the local bitmap, which will be synced |
| 2785 | * by this node eventually |
| 2786 | */ |
| 2787 | if (!mddev_is_clustered(rdev->mddev) || |
| 2788 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 2789 | clear_bit(Faulty, &rdev->flags); |
| 2790 | err = add_bound_rdev(rdev); |
| 2791 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2792 | } else |
| 2793 | err = -EBUSY; |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2794 | } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) { |
| 2795 | set_bit(ExternalBbl, &rdev->flags); |
| 2796 | rdev->badblocks.shift = 0; |
| 2797 | err = 0; |
| 2798 | } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) { |
| 2799 | clear_bit(ExternalBbl, &rdev->flags); |
| 2800 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2801 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2802 | if (!err) |
| 2803 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2804 | return err ? err : len; |
| 2805 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2806 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 2807 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2808 | |
| 2809 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2810 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2811 | { |
| 2812 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2813 | } |
| 2814 | |
| 2815 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2816 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2817 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2818 | unsigned int n; |
| 2819 | int rv; |
| 2820 | |
| 2821 | rv = kstrtouint(buf, 10, &n); |
| 2822 | if (rv < 0) |
| 2823 | return rv; |
| 2824 | atomic_set(&rdev->corrected_errors, n); |
| 2825 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2826 | } |
| 2827 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2828 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2829 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2830 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2831 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2832 | { |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2833 | if (test_bit(Journal, &rdev->flags)) |
| 2834 | return sprintf(page, "journal\n"); |
| 2835 | else if (rdev->raid_disk < 0) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2836 | return sprintf(page, "none\n"); |
| 2837 | else |
| 2838 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2839 | } |
| 2840 | |
| 2841 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2842 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2843 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2844 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2845 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2846 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2847 | if (test_bit(Journal, &rdev->flags)) |
| 2848 | return -EBUSY; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2849 | if (strncmp(buf, "none", 4)==0) |
| 2850 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2851 | else { |
| 2852 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 2853 | if (err < 0) |
| 2854 | return err; |
| 2855 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2856 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2857 | /* Setting 'slot' on an active array requires also |
| 2858 | * updating the 'rd%d' link, and communicating |
| 2859 | * with the personality with ->hot_*_disk. |
| 2860 | * For now we only support removing |
| 2861 | * failed/spare devices. This normally happens automatically, |
| 2862 | * but not when the metadata is externally managed. |
| 2863 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2864 | if (rdev->raid_disk == -1) |
| 2865 | return -EEXIST; |
| 2866 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2867 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2868 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2869 | clear_bit(Blocked, &rdev->flags); |
| 2870 | remove_and_add_spares(rdev->mddev, rdev); |
| 2871 | if (rdev->raid_disk >= 0) |
| 2872 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2873 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2874 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2875 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2876 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2877 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2878 | */ |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2879 | int err; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2880 | |
| 2881 | if (rdev->raid_disk != -1) |
| 2882 | return -EBUSY; |
| 2883 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2884 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2885 | return -EBUSY; |
| 2886 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2887 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2888 | return -EINVAL; |
| 2889 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2890 | if (slot >= rdev->mddev->raid_disks && |
| 2891 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2892 | return -ENOSPC; |
| 2893 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2894 | rdev->raid_disk = slot; |
| 2895 | if (test_bit(In_sync, &rdev->flags)) |
| 2896 | rdev->saved_raid_disk = slot; |
| 2897 | else |
| 2898 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2899 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 2900 | clear_bit(Bitmap_sync, &rdev->flags); |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2901 | err = rdev->mddev->pers-> |
| 2902 | hot_add_disk(rdev->mddev, rdev); |
| 2903 | if (err) { |
| 2904 | rdev->raid_disk = -1; |
| 2905 | return err; |
| 2906 | } else |
| 2907 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2908 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
| 2909 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2910 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2911 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2912 | if (slot >= rdev->mddev->raid_disks && |
| 2913 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2914 | return -ENOSPC; |
| 2915 | rdev->raid_disk = slot; |
| 2916 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2917 | clear_bit(Faulty, &rdev->flags); |
| 2918 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2919 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2920 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2921 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2922 | return len; |
| 2923 | } |
| 2924 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2925 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2926 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2927 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2928 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2929 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2930 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2931 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2932 | } |
| 2933 | |
| 2934 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2935 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2936 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2937 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2938 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2939 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2940 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2941 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2942 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2943 | /* Must set offset before size, so overlap checks |
| 2944 | * can be sane */ |
| 2945 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2946 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2947 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2948 | return len; |
| 2949 | } |
| 2950 | |
| 2951 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2952 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2953 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2954 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2955 | { |
| 2956 | return sprintf(page, "%llu\n", |
| 2957 | (unsigned long long)rdev->new_data_offset); |
| 2958 | } |
| 2959 | |
| 2960 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2961 | const char *buf, size_t len) |
| 2962 | { |
| 2963 | unsigned long long new_offset; |
| 2964 | struct mddev *mddev = rdev->mddev; |
| 2965 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2966 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2967 | return -EINVAL; |
| 2968 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 2969 | if (mddev->sync_thread || |
| 2970 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2971 | return -EBUSY; |
| 2972 | if (new_offset == rdev->data_offset) |
| 2973 | /* reset is always permitted */ |
| 2974 | ; |
| 2975 | else if (new_offset > rdev->data_offset) { |
| 2976 | /* must not push array size beyond rdev_sectors */ |
| 2977 | if (new_offset - rdev->data_offset |
| 2978 | + mddev->dev_sectors > rdev->sectors) |
| 2979 | return -E2BIG; |
| 2980 | } |
| 2981 | /* Metadata worries about other space details. */ |
| 2982 | |
| 2983 | /* decreasing the offset is inconsistent with a backwards |
| 2984 | * reshape. |
| 2985 | */ |
| 2986 | if (new_offset < rdev->data_offset && |
| 2987 | mddev->reshape_backwards) |
| 2988 | return -EINVAL; |
| 2989 | /* Increasing offset is inconsistent with forwards |
| 2990 | * reshape. reshape_direction should be set to |
| 2991 | * 'backwards' first. |
| 2992 | */ |
| 2993 | if (new_offset > rdev->data_offset && |
| 2994 | !mddev->reshape_backwards) |
| 2995 | return -EINVAL; |
| 2996 | |
| 2997 | if (mddev->pers && mddev->persistent && |
| 2998 | !super_types[mddev->major_version] |
| 2999 | .allow_new_offset(rdev, new_offset)) |
| 3000 | return -E2BIG; |
| 3001 | rdev->new_data_offset = new_offset; |
| 3002 | if (new_offset > rdev->data_offset) |
| 3003 | mddev->reshape_backwards = 1; |
| 3004 | else if (new_offset < rdev->data_offset) |
| 3005 | mddev->reshape_backwards = 0; |
| 3006 | |
| 3007 | return len; |
| 3008 | } |
| 3009 | static struct rdev_sysfs_entry rdev_new_offset = |
| 3010 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 3011 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3012 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3013 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3014 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3015 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3016 | } |
| 3017 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3018 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 3019 | { |
| 3020 | /* check if two start/length pairs overlap */ |
| 3021 | if (s1+l1 <= s2) |
| 3022 | return 0; |
| 3023 | if (s2+l2 <= s1) |
| 3024 | return 0; |
| 3025 | return 1; |
| 3026 | } |
| 3027 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3028 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 3029 | { |
| 3030 | unsigned long long blocks; |
| 3031 | sector_t new; |
| 3032 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3033 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3034 | return -EINVAL; |
| 3035 | |
| 3036 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 3037 | return -EINVAL; /* sector conversion overflow */ |
| 3038 | |
| 3039 | new = blocks * 2; |
| 3040 | if (new != blocks * 2) |
| 3041 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 3042 | |
| 3043 | *sectors = new; |
| 3044 | return 0; |
| 3045 | } |
| 3046 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3047 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3048 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3049 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3050 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3051 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3052 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3053 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3054 | if (test_bit(Journal, &rdev->flags)) |
| 3055 | return -EBUSY; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3056 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3057 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3058 | if (rdev->data_offset != rdev->new_data_offset) |
| 3059 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3060 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3061 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3062 | sectors = super_types[my_mddev->major_version]. |
| 3063 | rdev_size_change(rdev, sectors); |
| 3064 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3065 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3066 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3067 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3068 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 3069 | if (!my_mddev->pers->resize) |
| 3070 | /* Cannot change size for RAID0 or Linear etc */ |
| 3071 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3072 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3073 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3074 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3075 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3076 | rdev->sectors = sectors; |
| 3077 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3078 | /* Need to check that all other rdevs with the same |
| 3079 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 3080 | * the rdev lists safely. |
| 3081 | * This check does not provide a hard guarantee, it |
| 3082 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3083 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3084 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3085 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3086 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3087 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3088 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 3089 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3090 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3091 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3092 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 3093 | if (rdev->bdev == rdev2->bdev && |
| 3094 | rdev != rdev2 && |
| 3095 | overlaps(rdev->data_offset, rdev->sectors, |
| 3096 | rdev2->data_offset, |
| 3097 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3098 | overlap = 1; |
| 3099 | break; |
| 3100 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3101 | if (overlap) { |
| 3102 | mddev_put(mddev); |
| 3103 | break; |
| 3104 | } |
| 3105 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3106 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3107 | if (overlap) { |
| 3108 | /* Someone else could have slipped in a size |
| 3109 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3110 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3111 | * safe, and trust userspace not to race with |
| 3112 | * itself |
| 3113 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3114 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3115 | return -EBUSY; |
| 3116 | } |
| 3117 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3118 | return len; |
| 3119 | } |
| 3120 | |
| 3121 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3122 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3123 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3124 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3125 | { |
| 3126 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3127 | |
| 3128 | if (test_bit(In_sync, &rdev->flags) || |
| 3129 | recovery_start == MaxSector) |
| 3130 | return sprintf(page, "none\n"); |
| 3131 | |
| 3132 | return sprintf(page, "%llu\n", recovery_start); |
| 3133 | } |
| 3134 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3135 | static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3136 | { |
| 3137 | unsigned long long recovery_start; |
| 3138 | |
| 3139 | if (cmd_match(buf, "none")) |
| 3140 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3141 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3142 | return -EINVAL; |
| 3143 | |
| 3144 | if (rdev->mddev->pers && |
| 3145 | rdev->raid_disk >= 0) |
| 3146 | return -EBUSY; |
| 3147 | |
| 3148 | rdev->recovery_offset = recovery_start; |
| 3149 | if (recovery_start == MaxSector) |
| 3150 | set_bit(In_sync, &rdev->flags); |
| 3151 | else |
| 3152 | clear_bit(In_sync, &rdev->flags); |
| 3153 | return len; |
| 3154 | } |
| 3155 | |
| 3156 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3157 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3158 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3159 | /* sysfs access to bad-blocks list. |
| 3160 | * We present two files. |
| 3161 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 3162 | * are recorded as bad. The list is truncated to fit within |
| 3163 | * the one-page limit of sysfs. |
| 3164 | * Writing "sector length" to this file adds an acknowledged |
| 3165 | * bad block list. |
| 3166 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 3167 | * been acknowledged. Writing to this file adds bad blocks |
| 3168 | * without acknowledging them. This is largely for testing. |
| 3169 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3170 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3171 | { |
| 3172 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3173 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3174 | static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3175 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3176 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3177 | /* Maybe that ack was all we needed */ |
| 3178 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3179 | wake_up(&rdev->blocked_wait); |
| 3180 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3181 | } |
| 3182 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3183 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3184 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3185 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3186 | { |
| 3187 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3188 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3189 | static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3190 | { |
| 3191 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3192 | } |
| 3193 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3194 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3195 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 3196 | static ssize_t |
| 3197 | ppl_sector_show(struct md_rdev *rdev, char *page) |
| 3198 | { |
| 3199 | return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector); |
| 3200 | } |
| 3201 | |
| 3202 | static ssize_t |
| 3203 | ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len) |
| 3204 | { |
| 3205 | unsigned long long sector; |
| 3206 | |
| 3207 | if (kstrtoull(buf, 10, §or) < 0) |
| 3208 | return -EINVAL; |
| 3209 | if (sector != (sector_t)sector) |
| 3210 | return -EINVAL; |
| 3211 | |
| 3212 | if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) && |
| 3213 | rdev->raid_disk >= 0) |
| 3214 | return -EBUSY; |
| 3215 | |
| 3216 | if (rdev->mddev->persistent) { |
| 3217 | if (rdev->mddev->major_version == 0) |
| 3218 | return -EINVAL; |
| 3219 | if ((sector > rdev->sb_start && |
| 3220 | sector - rdev->sb_start > S16_MAX) || |
| 3221 | (sector < rdev->sb_start && |
| 3222 | rdev->sb_start - sector > -S16_MIN)) |
| 3223 | return -EINVAL; |
| 3224 | rdev->ppl.offset = sector - rdev->sb_start; |
| 3225 | } else if (!rdev->mddev->external) { |
| 3226 | return -EBUSY; |
| 3227 | } |
| 3228 | rdev->ppl.sector = sector; |
| 3229 | return len; |
| 3230 | } |
| 3231 | |
| 3232 | static struct rdev_sysfs_entry rdev_ppl_sector = |
| 3233 | __ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store); |
| 3234 | |
| 3235 | static ssize_t |
| 3236 | ppl_size_show(struct md_rdev *rdev, char *page) |
| 3237 | { |
| 3238 | return sprintf(page, "%u\n", rdev->ppl.size); |
| 3239 | } |
| 3240 | |
| 3241 | static ssize_t |
| 3242 | ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
| 3243 | { |
| 3244 | unsigned int size; |
| 3245 | |
| 3246 | if (kstrtouint(buf, 10, &size) < 0) |
| 3247 | return -EINVAL; |
| 3248 | |
| 3249 | if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) && |
| 3250 | rdev->raid_disk >= 0) |
| 3251 | return -EBUSY; |
| 3252 | |
| 3253 | if (rdev->mddev->persistent) { |
| 3254 | if (rdev->mddev->major_version == 0) |
| 3255 | return -EINVAL; |
| 3256 | if (size > U16_MAX) |
| 3257 | return -EINVAL; |
| 3258 | } else if (!rdev->mddev->external) { |
| 3259 | return -EBUSY; |
| 3260 | } |
| 3261 | rdev->ppl.size = size; |
| 3262 | return len; |
| 3263 | } |
| 3264 | |
| 3265 | static struct rdev_sysfs_entry rdev_ppl_size = |
| 3266 | __ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store); |
| 3267 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3268 | static struct attribute *rdev_default_attrs[] = { |
| 3269 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3270 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3271 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3272 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3273 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3274 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3275 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3276 | &rdev_bad_blocks.attr, |
| 3277 | &rdev_unack_bad_blocks.attr, |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 3278 | &rdev_ppl_sector.attr, |
| 3279 | &rdev_ppl_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3280 | NULL, |
| 3281 | }; |
| 3282 | static ssize_t |
| 3283 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3284 | { |
| 3285 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3286 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3287 | |
| 3288 | if (!entry->show) |
| 3289 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3290 | if (!rdev->mddev) |
| 3291 | return -EBUSY; |
| 3292 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | static ssize_t |
| 3296 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3297 | const char *page, size_t length) |
| 3298 | { |
| 3299 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3300 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3301 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3302 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3303 | |
| 3304 | if (!entry->store) |
| 3305 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3306 | if (!capable(CAP_SYS_ADMIN)) |
| 3307 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3308 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3309 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3310 | if (rdev->mddev == NULL) |
| 3311 | rv = -EBUSY; |
| 3312 | else |
| 3313 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3314 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3315 | } |
| 3316 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | static void rdev_free(struct kobject *ko) |
| 3320 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3321 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3322 | kfree(rdev); |
| 3323 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3324 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3325 | .show = rdev_attr_show, |
| 3326 | .store = rdev_attr_store, |
| 3327 | }; |
| 3328 | static struct kobj_type rdev_ktype = { |
| 3329 | .release = rdev_free, |
| 3330 | .sysfs_ops = &rdev_sysfs_ops, |
| 3331 | .default_attrs = rdev_default_attrs, |
| 3332 | }; |
| 3333 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3334 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3335 | { |
| 3336 | rdev->desc_nr = -1; |
| 3337 | rdev->saved_raid_disk = -1; |
| 3338 | rdev->raid_disk = -1; |
| 3339 | rdev->flags = 0; |
| 3340 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3341 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3342 | rdev->sb_events = 0; |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 3343 | rdev->last_read_error = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3344 | rdev->sb_loaded = 0; |
| 3345 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3346 | atomic_set(&rdev->nr_pending, 0); |
| 3347 | atomic_set(&rdev->read_errors, 0); |
| 3348 | atomic_set(&rdev->corrected_errors, 0); |
| 3349 | |
| 3350 | INIT_LIST_HEAD(&rdev->same_set); |
| 3351 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3352 | |
| 3353 | /* Add space to store bad block list. |
| 3354 | * This reserves the space even on arrays where it cannot |
| 3355 | * be used - I wonder if that matters |
| 3356 | */ |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3357 | return badblocks_init(&rdev->badblocks, 0); |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3358 | } |
| 3359 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | /* |
| 3361 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3362 | * |
| 3363 | * mark the device faulty if: |
| 3364 | * |
| 3365 | * - the device is nonexistent (zero size) |
| 3366 | * - the device has no valid superblock |
| 3367 | * |
| 3368 | * a faulty rdev _never_ has rdev->sb set. |
| 3369 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3370 | static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | { |
| 3372 | char b[BDEVNAME_SIZE]; |
| 3373 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3374 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | sector_t size; |
| 3376 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3377 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3378 | if (!rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3380 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3381 | err = md_rdev_init(rdev); |
| 3382 | if (err) |
| 3383 | goto abort_free; |
| 3384 | err = alloc_disk_sb(rdev); |
| 3385 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3386 | goto abort_free; |
| 3387 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3388 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | if (err) |
| 3390 | goto abort_free; |
| 3391 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3392 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3393 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3394 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | if (!size) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3396 | pr_warn("md: %s has zero or unknown size, marking faulty!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3397 | bdevname(rdev->bdev,b)); |
| 3398 | err = -EINVAL; |
| 3399 | goto abort_free; |
| 3400 | } |
| 3401 | |
| 3402 | if (super_format >= 0) { |
| 3403 | err = super_types[super_format]. |
| 3404 | load_super(rdev, NULL, super_minor); |
| 3405 | if (err == -EINVAL) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3406 | pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n", |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3407 | bdevname(rdev->bdev,b), |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3408 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | goto abort_free; |
| 3410 | } |
| 3411 | if (err < 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3412 | pr_warn("md: could not read %s's sb, not importing!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | bdevname(rdev->bdev,b)); |
| 3414 | goto abort_free; |
| 3415 | } |
| 3416 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | return rdev; |
| 3419 | |
| 3420 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3421 | if (rdev->bdev) |
| 3422 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3423 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | kfree(rdev); |
| 3425 | return ERR_PTR(err); |
| 3426 | } |
| 3427 | |
| 3428 | /* |
| 3429 | * Check a full RAID array for plausibility |
| 3430 | */ |
| 3431 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3432 | static void analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | { |
| 3434 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3435 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | char b[BDEVNAME_SIZE]; |
| 3437 | |
| 3438 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3439 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3440 | switch (super_types[mddev->major_version]. |
| 3441 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3442 | case 1: |
| 3443 | freshest = rdev; |
| 3444 | break; |
| 3445 | case 0: |
| 3446 | break; |
| 3447 | default: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3448 | pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3450 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | } |
| 3452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | super_types[mddev->major_version]. |
| 3454 | validate_super(mddev, freshest); |
| 3455 | |
| 3456 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3457 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3458 | if (mddev->max_disks && |
| 3459 | (rdev->desc_nr >= mddev->max_disks || |
| 3460 | i > mddev->max_disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3461 | pr_warn("md: %s: %s: only %d devices permitted\n", |
| 3462 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3463 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3464 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3465 | continue; |
| 3466 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3467 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | if (super_types[mddev->major_version]. |
| 3469 | validate_super(mddev, rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3470 | pr_warn("md: kicking non-fresh %s from array!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3472 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | continue; |
| 3474 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3475 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3476 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3477 | rdev->desc_nr = i++; |
| 3478 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3479 | set_bit(In_sync, &rdev->flags); |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3480 | } else if (rdev->raid_disk >= |
| 3481 | (mddev->raid_disks - min(0, mddev->delta_disks)) && |
| 3482 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3483 | rdev->raid_disk = -1; |
| 3484 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | } |
| 3486 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | } |
| 3488 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3489 | /* Read a fixed-point number. |
| 3490 | * Numbers in sysfs attributes should be in "standard" units where |
| 3491 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3492 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3493 | * milliseconds or jiffies. |
| 3494 | * This function takes a decimal number with a possible fractional |
| 3495 | * component, and produces an integer which is the result of |
| 3496 | * multiplying that number by 10^'scale'. |
| 3497 | * all without any floating-point arithmetic. |
| 3498 | */ |
| 3499 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3500 | { |
| 3501 | unsigned long result = 0; |
| 3502 | long decimals = -1; |
| 3503 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3504 | if (*cp == '.') |
| 3505 | decimals = 0; |
| 3506 | else if (decimals < scale) { |
| 3507 | unsigned int value; |
| 3508 | value = *cp - '0'; |
| 3509 | result = result * 10 + value; |
| 3510 | if (decimals >= 0) |
| 3511 | decimals++; |
| 3512 | } |
| 3513 | cp++; |
| 3514 | } |
| 3515 | if (*cp == '\n') |
| 3516 | cp++; |
| 3517 | if (*cp) |
| 3518 | return -EINVAL; |
| 3519 | if (decimals < 0) |
| 3520 | decimals = 0; |
| 3521 | while (decimals < scale) { |
| 3522 | result *= 10; |
| 3523 | decimals ++; |
| 3524 | } |
| 3525 | *res = result; |
| 3526 | return 0; |
| 3527 | } |
| 3528 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3529 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3530 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3531 | { |
| 3532 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3533 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3534 | } |
| 3535 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3536 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3537 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3538 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3539 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3540 | if (mddev_is_clustered(mddev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3541 | pr_warn("md: Safemode is disabled for clustered mode\n"); |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3542 | return -EINVAL; |
| 3543 | } |
| 3544 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3545 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3546 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3547 | if (msec == 0) |
| 3548 | mddev->safemode_delay = 0; |
| 3549 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3550 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3551 | unsigned long new_delay = (msec*HZ)/1000; |
| 3552 | |
| 3553 | if (new_delay == 0) |
| 3554 | new_delay = 1; |
| 3555 | mddev->safemode_delay = new_delay; |
| 3556 | if (new_delay < old_delay || old_delay == 0) |
| 3557 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3558 | } |
| 3559 | return len; |
| 3560 | } |
| 3561 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3562 | __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3563 | |
| 3564 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3565 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3566 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3567 | struct md_personality *p; |
| 3568 | int ret; |
| 3569 | spin_lock(&mddev->lock); |
| 3570 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3571 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3572 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3573 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3574 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3575 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3576 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3577 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3578 | ret = 0; |
| 3579 | spin_unlock(&mddev->lock); |
| 3580 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3581 | } |
| 3582 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3583 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3584 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3585 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3586 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3587 | ssize_t rv; |
| 3588 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3589 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3590 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3591 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3592 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3593 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3594 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3595 | return -EINVAL; |
| 3596 | |
| 3597 | rv = mddev_lock(mddev); |
| 3598 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3599 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3600 | |
| 3601 | if (mddev->pers == NULL) { |
| 3602 | strncpy(mddev->clevel, buf, slen); |
| 3603 | if (mddev->clevel[slen-1] == '\n') |
| 3604 | slen--; |
| 3605 | mddev->clevel[slen] = 0; |
| 3606 | mddev->level = LEVEL_NONE; |
| 3607 | rv = len; |
| 3608 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3609 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3610 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3611 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3612 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3613 | |
| 3614 | /* request to change the personality. Need to ensure: |
| 3615 | * - array is not engaged in resync/recovery/reshape |
| 3616 | * - old personality can be suspended |
| 3617 | * - new personality will access other array. |
| 3618 | */ |
| 3619 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3620 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3621 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3622 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3623 | mddev->reshape_position != MaxSector || |
| 3624 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3625 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3626 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3627 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3628 | if (!mddev->pers->quiesce) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3629 | pr_warn("md: %s: %s does not support online personality change\n", |
| 3630 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3631 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3635 | strncpy(clevel, buf, slen); |
| 3636 | if (clevel[slen-1] == '\n') |
| 3637 | slen--; |
| 3638 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3639 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3640 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3641 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3642 | if (request_module("md-%s", clevel) != 0) |
| 3643 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3644 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3645 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3646 | if (!pers || !try_module_get(pers->owner)) { |
| 3647 | spin_unlock(&pers_lock); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3648 | pr_warn("md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3649 | rv = -EINVAL; |
| 3650 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3651 | } |
| 3652 | spin_unlock(&pers_lock); |
| 3653 | |
| 3654 | if (pers == mddev->pers) { |
| 3655 | /* Nothing to do! */ |
| 3656 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3657 | rv = len; |
| 3658 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3659 | } |
| 3660 | if (!pers->takeover) { |
| 3661 | module_put(pers->owner); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3662 | pr_warn("md: %s: %s does not support personality takeover\n", |
| 3663 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3664 | rv = -EINVAL; |
| 3665 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3666 | } |
| 3667 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3668 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3669 | rdev->new_raid_disk = rdev->raid_disk; |
| 3670 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3671 | /* ->takeover must set new_* and/or delta_disks |
| 3672 | * if it succeeds, and may set them when it fails. |
| 3673 | */ |
| 3674 | priv = pers->takeover(mddev); |
| 3675 | if (IS_ERR(priv)) { |
| 3676 | mddev->new_level = mddev->level; |
| 3677 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3678 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3679 | mddev->raid_disks -= mddev->delta_disks; |
| 3680 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3681 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3682 | module_put(pers->owner); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3683 | pr_warn("md: %s: %s would not accept array\n", |
| 3684 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3685 | rv = PTR_ERR(priv); |
| 3686 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | /* Looks like we have a winner */ |
| 3690 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 3691 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3692 | |
| 3693 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3694 | oldpers = mddev->pers; |
| 3695 | oldpriv = mddev->private; |
| 3696 | mddev->pers = pers; |
| 3697 | mddev->private = priv; |
| 3698 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3699 | mddev->level = mddev->new_level; |
| 3700 | mddev->layout = mddev->new_layout; |
| 3701 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 3702 | mddev->delta_disks = 0; |
| 3703 | mddev->reshape_backwards = 0; |
| 3704 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3705 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3706 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3707 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3708 | mddev->external) { |
| 3709 | /* We are converting from a no-redundancy array |
| 3710 | * to a redundancy array and metadata is managed |
| 3711 | * externally so we need to be sure that writes |
| 3712 | * won't block due to a need to transition |
| 3713 | * clean->dirty |
| 3714 | * until external management is started. |
| 3715 | */ |
| 3716 | mddev->in_sync = 0; |
| 3717 | mddev->safemode_delay = 0; |
| 3718 | mddev->safemode = 0; |
| 3719 | } |
| 3720 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3721 | oldpers->free(mddev, oldpriv); |
| 3722 | |
| 3723 | if (oldpers->sync_request == NULL && |
| 3724 | pers->sync_request != NULL) { |
| 3725 | /* need to add the md_redundancy_group */ |
| 3726 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3727 | pr_warn("md: cannot register extra attributes for %s\n", |
| 3728 | mdname(mddev)); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3729 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3730 | } |
| 3731 | if (oldpers->sync_request != NULL && |
| 3732 | pers->sync_request == NULL) { |
| 3733 | /* need to remove the md_redundancy_group */ |
| 3734 | if (mddev->to_remove == NULL) |
| 3735 | mddev->to_remove = &md_redundancy_group; |
| 3736 | } |
| 3737 | |
Alexey Obitotskiy | 4cb9da7 | 2016-06-23 12:11:01 +0200 | [diff] [blame] | 3738 | module_put(oldpers->owner); |
| 3739 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3740 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3741 | if (rdev->raid_disk < 0) |
| 3742 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3743 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3744 | rdev->new_raid_disk = -1; |
| 3745 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3746 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3747 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3748 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3749 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3750 | if (rdev->raid_disk < 0) |
| 3751 | continue; |
| 3752 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3753 | continue; |
| 3754 | rdev->raid_disk = rdev->new_raid_disk; |
| 3755 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3756 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3757 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3758 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3759 | pr_warn("md: cannot register rd%d for %s after level change\n", |
| 3760 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3761 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3762 | } |
| 3763 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3764 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3765 | /* this is now an array without redundancy, so |
| 3766 | * it must always be in_sync |
| 3767 | */ |
| 3768 | mddev->in_sync = 1; |
| 3769 | del_timer_sync(&mddev->safemode_timer); |
| 3770 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 3771 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3772 | pers->run(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 3773 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3774 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 3775 | if (!mddev->thread) |
| 3776 | md_update_sb(mddev, 1); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3777 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3778 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3779 | rv = len; |
| 3780 | out_unlock: |
| 3781 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3782 | return rv; |
| 3783 | } |
| 3784 | |
| 3785 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3786 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3787 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3788 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3789 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3790 | { |
| 3791 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3792 | if (mddev->reshape_position != MaxSector && |
| 3793 | mddev->layout != mddev->new_layout) |
| 3794 | return sprintf(page, "%d (%d)\n", |
| 3795 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3796 | return sprintf(page, "%d\n", mddev->layout); |
| 3797 | } |
| 3798 | |
| 3799 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3800 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3801 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3802 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3803 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3804 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3805 | err = kstrtouint(buf, 10, &n); |
| 3806 | if (err < 0) |
| 3807 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3808 | err = mddev_lock(mddev); |
| 3809 | if (err) |
| 3810 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3811 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3812 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3813 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3814 | err = -EBUSY; |
| 3815 | else if (mddev->ro) |
| 3816 | err = -EROFS; |
| 3817 | else { |
| 3818 | mddev->new_layout = n; |
| 3819 | err = mddev->pers->check_reshape(mddev); |
| 3820 | if (err) |
| 3821 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3822 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3823 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3824 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3825 | if (mddev->reshape_position == MaxSector) |
| 3826 | mddev->layout = n; |
| 3827 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3828 | mddev_unlock(mddev); |
| 3829 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3830 | } |
| 3831 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3832 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3833 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3834 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3835 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3836 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3837 | if (mddev->raid_disks == 0) |
| 3838 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3839 | if (mddev->reshape_position != MaxSector && |
| 3840 | mddev->delta_disks != 0) |
| 3841 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3842 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3843 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3844 | } |
| 3845 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3846 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3847 | |
| 3848 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3849 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3850 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3851 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3852 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3853 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3854 | err = kstrtouint(buf, 10, &n); |
| 3855 | if (err < 0) |
| 3856 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3857 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3858 | err = mddev_lock(mddev); |
| 3859 | if (err) |
| 3860 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3861 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3862 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3863 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3864 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3865 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3866 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3867 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3868 | rdev_for_each(rdev, mddev) { |
| 3869 | if (olddisks < n && |
| 3870 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3871 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3872 | if (olddisks > n && |
| 3873 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3874 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3875 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3876 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3877 | mddev->delta_disks = n - olddisks; |
| 3878 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3879 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3880 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3881 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3882 | out_unlock: |
| 3883 | mddev_unlock(mddev); |
| 3884 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3885 | } |
| 3886 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3887 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3888 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3889 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3890 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3891 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3892 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3893 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3894 | return sprintf(page, "%d (%d)\n", |
| 3895 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3896 | mddev->chunk_sectors << 9); |
| 3897 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3901 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3902 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3903 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3904 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3905 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3906 | err = kstrtoul(buf, 10, &n); |
| 3907 | if (err < 0) |
| 3908 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3909 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3910 | err = mddev_lock(mddev); |
| 3911 | if (err) |
| 3912 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3913 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3914 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3915 | err = -EBUSY; |
| 3916 | else if (mddev->ro) |
| 3917 | err = -EROFS; |
| 3918 | else { |
| 3919 | mddev->new_chunk_sectors = n >> 9; |
| 3920 | err = mddev->pers->check_reshape(mddev); |
| 3921 | if (err) |
| 3922 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3923 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3924 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3925 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3926 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3927 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3928 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3929 | mddev_unlock(mddev); |
| 3930 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3931 | } |
| 3932 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3933 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3934 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3935 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3936 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3937 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3938 | if (mddev->recovery_cp == MaxSector) |
| 3939 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3940 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3941 | } |
| 3942 | |
| 3943 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3944 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3945 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3946 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3947 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3948 | |
| 3949 | if (cmd_match(buf, "none")) |
| 3950 | n = MaxSector; |
| 3951 | else { |
| 3952 | err = kstrtoull(buf, 10, &n); |
| 3953 | if (err < 0) |
| 3954 | return err; |
| 3955 | if (n != (sector_t)n) |
| 3956 | return -EINVAL; |
| 3957 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3958 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3959 | err = mddev_lock(mddev); |
| 3960 | if (err) |
| 3961 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3962 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3963 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3964 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3965 | if (!err) { |
| 3966 | mddev->recovery_cp = n; |
| 3967 | if (mddev->pers) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 3968 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3969 | } |
| 3970 | mddev_unlock(mddev); |
| 3971 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3972 | } |
| 3973 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3974 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 3975 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3976 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3977 | /* |
| 3978 | * The array state can be: |
| 3979 | * |
| 3980 | * clear |
| 3981 | * No devices, no size, no level |
| 3982 | * Equivalent to STOP_ARRAY ioctl |
| 3983 | * inactive |
| 3984 | * May have some settings, but array is not active |
| 3985 | * all IO results in error |
| 3986 | * When written, doesn't tear down array, but just stops it |
| 3987 | * suspended (not supported yet) |
| 3988 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3989 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3990 | * readonly |
| 3991 | * no resync can happen. no superblocks get written. |
| 3992 | * write requests fail |
| 3993 | * read-auto |
| 3994 | * like readonly, but behaves like 'clean' on a write request. |
| 3995 | * |
| 3996 | * clean - no pending writes, but otherwise active. |
| 3997 | * When written to inactive array, starts without resync |
| 3998 | * If a write request arrives then |
| 3999 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 4000 | * if not known, block and switch to write-pending |
| 4001 | * If written to an active array that has pending writes, then fails. |
| 4002 | * active |
| 4003 | * fully active: IO and resync can be happening. |
| 4004 | * When written to inactive array, starts with resync |
| 4005 | * |
| 4006 | * write-pending |
| 4007 | * clean, but writes are blocked waiting for 'active' to be written. |
| 4008 | * |
| 4009 | * active-idle |
| 4010 | * like active, but no writes have been seen for a while (100msec). |
| 4011 | * |
| 4012 | */ |
| 4013 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 4014 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 4015 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4016 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 4017 | "write-pending", "active-idle", NULL }; |
| 4018 | |
| 4019 | static int match_word(const char *word, char **list) |
| 4020 | { |
| 4021 | int n; |
| 4022 | for (n=0; list[n]; n++) |
| 4023 | if (cmd_match(word, list[n])) |
| 4024 | break; |
| 4025 | return n; |
| 4026 | } |
| 4027 | |
| 4028 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4029 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4030 | { |
| 4031 | enum array_state st = inactive; |
| 4032 | |
| 4033 | if (mddev->pers) |
| 4034 | switch(mddev->ro) { |
| 4035 | case 1: |
| 4036 | st = readonly; |
| 4037 | break; |
| 4038 | case 2: |
| 4039 | st = read_auto; |
| 4040 | break; |
| 4041 | case 0: |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4042 | spin_lock(&mddev->lock); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4043 | if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4044 | st = write_pending; |
Tomasz Majchrzak | 16f8894 | 2016-10-24 12:47:28 +0200 | [diff] [blame] | 4045 | else if (mddev->in_sync) |
| 4046 | st = clean; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4047 | else if (mddev->safemode) |
| 4048 | st = active_idle; |
| 4049 | else |
| 4050 | st = active; |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4051 | spin_unlock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4052 | } |
| 4053 | else { |
| 4054 | if (list_empty(&mddev->disks) && |
| 4055 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4056 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4057 | st = clear; |
| 4058 | else |
| 4059 | st = inactive; |
| 4060 | } |
| 4061 | return sprintf(page, "%s\n", array_states[st]); |
| 4062 | } |
| 4063 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4064 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 4065 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 4066 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4067 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4068 | |
| 4069 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4070 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4071 | { |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4072 | int err = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4073 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4074 | |
| 4075 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 4076 | /* don't take reconfig_mutex when toggling between |
| 4077 | * clean and active |
| 4078 | */ |
| 4079 | spin_lock(&mddev->lock); |
| 4080 | if (st == active) { |
| 4081 | restart_array(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4082 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
Tomasz Majchrzak | 91a6c4a | 2016-10-25 17:07:08 +0200 | [diff] [blame] | 4083 | md_wakeup_thread(mddev->thread); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4084 | wake_up(&mddev->sb_wait); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4085 | } else /* st == clean */ { |
| 4086 | restart_array(mddev); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4087 | if (!set_in_sync(mddev)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4088 | err = -EBUSY; |
| 4089 | } |
Tomasz Majchrzak | 573275b | 2016-06-30 10:47:09 +0200 | [diff] [blame] | 4090 | if (!err) |
| 4091 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4092 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 4093 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4094 | } |
| 4095 | err = mddev_lock(mddev); |
| 4096 | if (err) |
| 4097 | return err; |
| 4098 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4099 | switch(st) { |
| 4100 | case bad_word: |
| 4101 | break; |
| 4102 | case clear: |
| 4103 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4104 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4105 | break; |
| 4106 | case inactive: |
| 4107 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 4108 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4109 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 4110 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4111 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4112 | break; |
| 4113 | case suspended: |
| 4114 | break; /* not supported yet */ |
| 4115 | case readonly: |
| 4116 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4117 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4118 | else { |
| 4119 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4120 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4121 | err = do_md_run(mddev); |
| 4122 | } |
| 4123 | break; |
| 4124 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4125 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4126 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4127 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4128 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4129 | err = restart_array(mddev); |
| 4130 | if (err == 0) { |
| 4131 | mddev->ro = 2; |
| 4132 | set_disk_ro(mddev->gendisk, 0); |
| 4133 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4134 | } else { |
| 4135 | mddev->ro = 2; |
| 4136 | err = do_md_run(mddev); |
| 4137 | } |
| 4138 | break; |
| 4139 | case clean: |
| 4140 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4141 | err = restart_array(mddev); |
| 4142 | if (err) |
| 4143 | break; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4144 | spin_lock(&mddev->lock); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4145 | if (!set_in_sync(mddev)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4146 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4147 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 4148 | } else |
| 4149 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4150 | break; |
| 4151 | case active: |
| 4152 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4153 | err = restart_array(mddev); |
| 4154 | if (err) |
| 4155 | break; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4156 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4157 | wake_up(&mddev->sb_wait); |
| 4158 | err = 0; |
| 4159 | } else { |
| 4160 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4161 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4162 | err = do_md_run(mddev); |
| 4163 | } |
| 4164 | break; |
| 4165 | case write_pending: |
| 4166 | case active_idle: |
| 4167 | /* these cannot be set */ |
| 4168 | break; |
| 4169 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4170 | |
| 4171 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 4172 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4173 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4174 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4175 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4176 | mddev_unlock(mddev); |
| 4177 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4178 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4179 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4180 | __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4181 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4182 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4183 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4184 | return sprintf(page, "%d\n", |
| 4185 | atomic_read(&mddev->max_corr_read_errors)); |
| 4186 | } |
| 4187 | |
| 4188 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4189 | max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len) |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4190 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4191 | unsigned int n; |
| 4192 | int rv; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4193 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4194 | rv = kstrtouint(buf, 10, &n); |
| 4195 | if (rv < 0) |
| 4196 | return rv; |
| 4197 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4198 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | static struct md_sysfs_entry max_corr_read_errors = |
| 4202 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4203 | max_corrected_read_errors_store); |
| 4204 | |
| 4205 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4206 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4207 | { |
| 4208 | return -EINVAL; |
| 4209 | } |
| 4210 | |
| 4211 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4212 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4213 | { |
| 4214 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4215 | /* The new device is added to the array. |
| 4216 | * If the array has a persistent superblock, we read the |
| 4217 | * superblock to initialise info and check validity. |
| 4218 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4219 | * which mainly checks size. |
| 4220 | */ |
| 4221 | char *e; |
| 4222 | int major = simple_strtoul(buf, &e, 10); |
| 4223 | int minor; |
| 4224 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4225 | struct md_rdev *rdev; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4226 | int err; |
| 4227 | |
| 4228 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4229 | return -EINVAL; |
| 4230 | minor = simple_strtoul(e+1, &e, 10); |
| 4231 | if (*e && *e != '\n') |
| 4232 | return -EINVAL; |
| 4233 | dev = MKDEV(major, minor); |
| 4234 | if (major != MAJOR(dev) || |
| 4235 | minor != MINOR(dev)) |
| 4236 | return -EOVERFLOW; |
| 4237 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4238 | flush_workqueue(md_misc_wq); |
| 4239 | |
| 4240 | err = mddev_lock(mddev); |
| 4241 | if (err) |
| 4242 | return err; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4243 | if (mddev->persistent) { |
| 4244 | rdev = md_import_device(dev, mddev->major_version, |
| 4245 | mddev->minor_version); |
| 4246 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4247 | struct md_rdev *rdev0 |
| 4248 | = list_entry(mddev->disks.next, |
| 4249 | struct md_rdev, same_set); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4250 | err = super_types[mddev->major_version] |
| 4251 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4252 | if (err < 0) |
| 4253 | goto out; |
| 4254 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4255 | } else if (mddev->external) |
| 4256 | rdev = md_import_device(dev, -2, -1); |
| 4257 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4258 | rdev = md_import_device(dev, -1, -1); |
| 4259 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4260 | if (IS_ERR(rdev)) { |
| 4261 | mddev_unlock(mddev); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4262 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4263 | } |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4264 | err = bind_rdev_to_array(rdev, mddev); |
| 4265 | out: |
| 4266 | if (err) |
| 4267 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4268 | mddev_unlock(mddev); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4269 | return err ? err : len; |
| 4270 | } |
| 4271 | |
| 4272 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4273 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4274 | |
| 4275 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4276 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4277 | { |
| 4278 | char *end; |
| 4279 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4280 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4281 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4282 | err = mddev_lock(mddev); |
| 4283 | if (err) |
| 4284 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4285 | if (!mddev->bitmap) |
| 4286 | goto out; |
| 4287 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4288 | while (*buf) { |
| 4289 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4290 | if (buf == end) break; |
| 4291 | if (*end == '-') { /* range */ |
| 4292 | buf = end + 1; |
| 4293 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4294 | if (buf == end) break; |
| 4295 | } |
| 4296 | if (*end && !isspace(*end)) break; |
| 4297 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4298 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4299 | } |
| 4300 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4301 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4302 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4303 | return len; |
| 4304 | } |
| 4305 | |
| 4306 | static struct md_sysfs_entry md_bitmap = |
| 4307 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4308 | |
| 4309 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4310 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4311 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4312 | return sprintf(page, "%llu\n", |
| 4313 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4314 | } |
| 4315 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4316 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4317 | |
| 4318 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4319 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4320 | { |
| 4321 | /* If array is inactive, we can reduce the component size, but |
| 4322 | * not increase it (except from 0). |
| 4323 | * If array is active, we can try an on-line resize |
| 4324 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4325 | sector_t sectors; |
| 4326 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4327 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4328 | if (err < 0) |
| 4329 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4330 | err = mddev_lock(mddev); |
| 4331 | if (err) |
| 4332 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4333 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4334 | err = update_size(mddev, sectors); |
Xiao Ni | 4ba1e78 | 2016-06-12 17:18:00 +0800 | [diff] [blame] | 4335 | if (err == 0) |
| 4336 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4337 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4338 | if (mddev->dev_sectors == 0 || |
| 4339 | mddev->dev_sectors > sectors) |
| 4340 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4341 | else |
| 4342 | err = -ENOSPC; |
| 4343 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4344 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4345 | return err ? err : len; |
| 4346 | } |
| 4347 | |
| 4348 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4349 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4350 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4351 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4352 | * This is one of |
| 4353 | * 'none' for arrays with no metadata (good luck...) |
| 4354 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4355 | * or N.M for internally known formats |
| 4356 | */ |
| 4357 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4358 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4359 | { |
| 4360 | if (mddev->persistent) |
| 4361 | return sprintf(page, "%d.%d\n", |
| 4362 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4363 | else if (mddev->external) |
| 4364 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4365 | else |
| 4366 | return sprintf(page, "none\n"); |
| 4367 | } |
| 4368 | |
| 4369 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4370 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4371 | { |
| 4372 | int major, minor; |
| 4373 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4374 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4375 | /* Changing the details of 'external' metadata is |
| 4376 | * always permitted. Otherwise there must be |
| 4377 | * no devices attached to the array. |
| 4378 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4379 | |
| 4380 | err = mddev_lock(mddev); |
| 4381 | if (err) |
| 4382 | return err; |
| 4383 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4384 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4385 | ; |
| 4386 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4387 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4388 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4389 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4390 | if (cmd_match(buf, "none")) { |
| 4391 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4392 | mddev->external = 0; |
| 4393 | mddev->major_version = 0; |
| 4394 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4395 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4396 | } |
| 4397 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4398 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4399 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4400 | namelen = sizeof(mddev->metadata_type)-1; |
| 4401 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4402 | mddev->metadata_type[namelen] = 0; |
| 4403 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4404 | mddev->metadata_type[--namelen] = 0; |
| 4405 | mddev->persistent = 0; |
| 4406 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4407 | mddev->major_version = 0; |
| 4408 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4409 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4410 | } |
| 4411 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4412 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4413 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4414 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4415 | buf = e+1; |
| 4416 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4417 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4418 | goto out_unlock; |
| 4419 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4420 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4421 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4422 | mddev->major_version = major; |
| 4423 | mddev->minor_version = minor; |
| 4424 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4425 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4426 | err = 0; |
| 4427 | out_unlock: |
| 4428 | mddev_unlock(mddev); |
| 4429 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4430 | } |
| 4431 | |
| 4432 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4433 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4434 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4435 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4436 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4437 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4438 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4439 | unsigned long recovery = mddev->recovery; |
| 4440 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4441 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4442 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4443 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4444 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4445 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4446 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4447 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4448 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4449 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4450 | type = "check"; |
| 4451 | else |
| 4452 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4453 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4454 | type = "recover"; |
NeilBrown | 985ca97 | 2015-07-06 12:26:57 +1000 | [diff] [blame] | 4455 | else if (mddev->reshape_position != MaxSector) |
| 4456 | type = "reshape"; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4457 | } |
| 4458 | return sprintf(page, "%s\n", type); |
| 4459 | } |
| 4460 | |
| 4461 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4462 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4463 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4464 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4465 | return -EINVAL; |
| 4466 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4467 | |
| 4468 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4469 | if (cmd_match(page, "frozen")) |
| 4470 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4471 | else |
| 4472 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4473 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4474 | mddev_lock(mddev) == 0) { |
| 4475 | flush_workqueue(md_misc_wq); |
| 4476 | if (mddev->sync_thread) { |
| 4477 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4478 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4479 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4480 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4481 | } |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4482 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4483 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4484 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4485 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4486 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4487 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4488 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4489 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4490 | int err; |
| 4491 | if (mddev->pers->start_reshape == NULL) |
| 4492 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4493 | err = mddev_lock(mddev); |
| 4494 | if (!err) { |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4495 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4496 | err = -EBUSY; |
| 4497 | else { |
| 4498 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4499 | err = mddev->pers->start_reshape(mddev); |
| 4500 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4501 | mddev_unlock(mddev); |
| 4502 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4503 | if (err) |
| 4504 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4505 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4506 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4507 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4508 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4509 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4510 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4511 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4512 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4513 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4514 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4515 | if (mddev->ro == 2) { |
| 4516 | /* A write to sync_action is enough to justify |
| 4517 | * canceling read-auto mode |
| 4518 | */ |
| 4519 | mddev->ro = 0; |
| 4520 | md_wakeup_thread(mddev->sync_thread); |
| 4521 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4522 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4523 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4524 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4525 | return len; |
| 4526 | } |
| 4527 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4528 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4529 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4530 | |
| 4531 | static ssize_t |
| 4532 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4533 | { |
| 4534 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4535 | } |
| 4536 | |
| 4537 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4538 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4539 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4540 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4541 | { |
| 4542 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4543 | (unsigned long long) |
| 4544 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4545 | } |
| 4546 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4547 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4548 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4549 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4550 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4551 | { |
| 4552 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4553 | mddev->sync_speed_min ? "local": "system"); |
| 4554 | } |
| 4555 | |
| 4556 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4557 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4558 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4559 | unsigned int min; |
| 4560 | int rv; |
| 4561 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4562 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4563 | min = 0; |
| 4564 | } else { |
| 4565 | rv = kstrtouint(buf, 10, &min); |
| 4566 | if (rv < 0) |
| 4567 | return rv; |
| 4568 | if (min == 0) |
| 4569 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4570 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4571 | mddev->sync_speed_min = min; |
| 4572 | return len; |
| 4573 | } |
| 4574 | |
| 4575 | static struct md_sysfs_entry md_sync_min = |
| 4576 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4577 | |
| 4578 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4579 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4580 | { |
| 4581 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4582 | mddev->sync_speed_max ? "local": "system"); |
| 4583 | } |
| 4584 | |
| 4585 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4586 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4587 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4588 | unsigned int max; |
| 4589 | int rv; |
| 4590 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4591 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4592 | max = 0; |
| 4593 | } else { |
| 4594 | rv = kstrtouint(buf, 10, &max); |
| 4595 | if (rv < 0) |
| 4596 | return rv; |
| 4597 | if (max == 0) |
| 4598 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4599 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4600 | mddev->sync_speed_max = max; |
| 4601 | return len; |
| 4602 | } |
| 4603 | |
| 4604 | static struct md_sysfs_entry md_sync_max = |
| 4605 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4606 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4607 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4608 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4609 | { |
| 4610 | return sprintf(page, "%d\n", mddev->degraded); |
| 4611 | } |
| 4612 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4613 | |
| 4614 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4615 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4616 | { |
| 4617 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4618 | } |
| 4619 | |
| 4620 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4621 | sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4622 | { |
| 4623 | long n; |
| 4624 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4625 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4626 | return -EINVAL; |
| 4627 | |
| 4628 | if (n != 0 && n != 1) |
| 4629 | return -EINVAL; |
| 4630 | |
| 4631 | mddev->parallel_resync = n; |
| 4632 | |
| 4633 | if (mddev->sync_thread) |
| 4634 | wake_up(&resync_wait); |
| 4635 | |
| 4636 | return len; |
| 4637 | } |
| 4638 | |
| 4639 | /* force parallel resync, even with shared block devices */ |
| 4640 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4641 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4642 | sync_force_parallel_show, sync_force_parallel_store); |
| 4643 | |
| 4644 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4645 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4646 | { |
| 4647 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4648 | if (mddev->curr_resync == 0) |
| 4649 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4650 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4651 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4652 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4653 | db = resync - mddev->resync_mark_cnt; |
| 4654 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4655 | } |
| 4656 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4657 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4658 | |
| 4659 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4660 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4661 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4662 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4663 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4664 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4665 | return sprintf(page, "none\n"); |
| 4666 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4667 | if (mddev->curr_resync == 1 || |
| 4668 | mddev->curr_resync == 2) |
| 4669 | return sprintf(page, "delayed\n"); |
| 4670 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4671 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4672 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4673 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4674 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4675 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4676 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4677 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4678 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4679 | } |
| 4680 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4681 | static struct md_sysfs_entry md_sync_completed = |
| 4682 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4683 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4684 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4685 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4686 | { |
| 4687 | return sprintf(page, "%llu\n", |
| 4688 | (unsigned long long)mddev->resync_min); |
| 4689 | } |
| 4690 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4691 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4692 | { |
| 4693 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4694 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4695 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4696 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4697 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4698 | |
| 4699 | spin_lock(&mddev->lock); |
| 4700 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4701 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4702 | goto out_unlock; |
| 4703 | |
| 4704 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4705 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4706 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4707 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 4708 | /* Round down to multiple of 4K for safety */ |
| 4709 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4710 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4711 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4712 | out_unlock: |
| 4713 | spin_unlock(&mddev->lock); |
| 4714 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4715 | } |
| 4716 | |
| 4717 | static struct md_sysfs_entry md_min_sync = |
| 4718 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4719 | |
| 4720 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4721 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4722 | { |
| 4723 | if (mddev->resync_max == MaxSector) |
| 4724 | return sprintf(page, "max\n"); |
| 4725 | else |
| 4726 | return sprintf(page, "%llu\n", |
| 4727 | (unsigned long long)mddev->resync_max); |
| 4728 | } |
| 4729 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4730 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4731 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4732 | int err; |
| 4733 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4734 | if (strncmp(buf, "max", 3) == 0) |
| 4735 | mddev->resync_max = MaxSector; |
| 4736 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4737 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4738 | int chunk; |
| 4739 | |
| 4740 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4741 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4742 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4743 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4744 | goto out_unlock; |
| 4745 | |
| 4746 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4747 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4748 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4749 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4750 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4751 | |
| 4752 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4753 | chunk = mddev->chunk_sectors; |
| 4754 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4755 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4756 | |
| 4757 | err = -EINVAL; |
| 4758 | if (sector_div(temp, chunk)) |
| 4759 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4760 | } |
| 4761 | mddev->resync_max = max; |
| 4762 | } |
| 4763 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4764 | err = 0; |
| 4765 | out_unlock: |
| 4766 | spin_unlock(&mddev->lock); |
| 4767 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4768 | } |
| 4769 | |
| 4770 | static struct md_sysfs_entry md_max_sync = |
| 4771 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4772 | |
| 4773 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4774 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4775 | { |
| 4776 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4777 | } |
| 4778 | |
| 4779 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4780 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4781 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4782 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4783 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4784 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4785 | err = kstrtoull(buf, 10, &new); |
| 4786 | if (err < 0) |
| 4787 | return err; |
| 4788 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4789 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4790 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4791 | err = mddev_lock(mddev); |
| 4792 | if (err) |
| 4793 | return err; |
| 4794 | err = -EINVAL; |
| 4795 | if (mddev->pers == NULL || |
| 4796 | mddev->pers->quiesce == NULL) |
| 4797 | goto unlock; |
| 4798 | old = mddev->suspend_lo; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4799 | mddev->suspend_lo = new; |
| 4800 | if (new >= old) |
| 4801 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4802 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4803 | else { |
| 4804 | /* Expanding suspended region - need to wait */ |
| 4805 | mddev->pers->quiesce(mddev, 1); |
| 4806 | mddev->pers->quiesce(mddev, 0); |
| 4807 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4808 | err = 0; |
| 4809 | unlock: |
| 4810 | mddev_unlock(mddev); |
| 4811 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4812 | } |
| 4813 | static struct md_sysfs_entry md_suspend_lo = |
| 4814 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4815 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4816 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4817 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4818 | { |
| 4819 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4820 | } |
| 4821 | |
| 4822 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4823 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4824 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4825 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4826 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4827 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4828 | err = kstrtoull(buf, 10, &new); |
| 4829 | if (err < 0) |
| 4830 | return err; |
| 4831 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4832 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4833 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4834 | err = mddev_lock(mddev); |
| 4835 | if (err) |
| 4836 | return err; |
| 4837 | err = -EINVAL; |
| 4838 | if (mddev->pers == NULL || |
| 4839 | mddev->pers->quiesce == NULL) |
| 4840 | goto unlock; |
| 4841 | old = mddev->suspend_hi; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4842 | mddev->suspend_hi = new; |
| 4843 | if (new <= old) |
| 4844 | /* Shrinking suspended region */ |
| 4845 | mddev->pers->quiesce(mddev, 2); |
| 4846 | else { |
| 4847 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4848 | mddev->pers->quiesce(mddev, 1); |
| 4849 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4850 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4851 | err = 0; |
| 4852 | unlock: |
| 4853 | mddev_unlock(mddev); |
| 4854 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4855 | } |
| 4856 | static struct md_sysfs_entry md_suspend_hi = |
| 4857 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4858 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4859 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4860 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4861 | { |
| 4862 | if (mddev->reshape_position != MaxSector) |
| 4863 | return sprintf(page, "%llu\n", |
| 4864 | (unsigned long long)mddev->reshape_position); |
| 4865 | strcpy(page, "none\n"); |
| 4866 | return 5; |
| 4867 | } |
| 4868 | |
| 4869 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4870 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4871 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4872 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4873 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4874 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4875 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4876 | err = kstrtoull(buf, 10, &new); |
| 4877 | if (err < 0) |
| 4878 | return err; |
| 4879 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4880 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4881 | err = mddev_lock(mddev); |
| 4882 | if (err) |
| 4883 | return err; |
| 4884 | err = -EBUSY; |
| 4885 | if (mddev->pers) |
| 4886 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4887 | mddev->reshape_position = new; |
| 4888 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4889 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4890 | mddev->new_level = mddev->level; |
| 4891 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4892 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4893 | rdev_for_each(rdev, mddev) |
| 4894 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4895 | err = 0; |
| 4896 | unlock: |
| 4897 | mddev_unlock(mddev); |
| 4898 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4899 | } |
| 4900 | |
| 4901 | static struct md_sysfs_entry md_reshape_position = |
| 4902 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4903 | reshape_position_store); |
| 4904 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4905 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4906 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4907 | { |
| 4908 | return sprintf(page, "%s\n", |
| 4909 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4910 | } |
| 4911 | |
| 4912 | static ssize_t |
| 4913 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4914 | { |
| 4915 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4916 | int err; |
| 4917 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4918 | if (cmd_match(buf, "forwards")) |
| 4919 | backwards = 0; |
| 4920 | else if (cmd_match(buf, "backwards")) |
| 4921 | backwards = 1; |
| 4922 | else |
| 4923 | return -EINVAL; |
| 4924 | if (mddev->reshape_backwards == backwards) |
| 4925 | return len; |
| 4926 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4927 | err = mddev_lock(mddev); |
| 4928 | if (err) |
| 4929 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4930 | /* check if we are allowed to change */ |
| 4931 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4932 | err = -EBUSY; |
| 4933 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4934 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4935 | err = -EINVAL; |
| 4936 | else |
| 4937 | mddev->reshape_backwards = backwards; |
| 4938 | mddev_unlock(mddev); |
| 4939 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4940 | } |
| 4941 | |
| 4942 | static struct md_sysfs_entry md_reshape_direction = |
| 4943 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4944 | reshape_direction_store); |
| 4945 | |
| 4946 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4947 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4948 | { |
| 4949 | if (mddev->external_size) |
| 4950 | return sprintf(page, "%llu\n", |
| 4951 | (unsigned long long)mddev->array_sectors/2); |
| 4952 | else |
| 4953 | return sprintf(page, "default\n"); |
| 4954 | } |
| 4955 | |
| 4956 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4957 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4958 | { |
| 4959 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4960 | int err; |
| 4961 | |
| 4962 | err = mddev_lock(mddev); |
| 4963 | if (err) |
| 4964 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4965 | |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 4966 | /* cluster raid doesn't support change array_sectors */ |
Zhilong Liu | b670883 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 4967 | if (mddev_is_clustered(mddev)) { |
| 4968 | mddev_unlock(mddev); |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 4969 | return -EINVAL; |
Zhilong Liu | b670883 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 4970 | } |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 4971 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4972 | if (strncmp(buf, "default", 7) == 0) { |
| 4973 | if (mddev->pers) |
| 4974 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4975 | else |
| 4976 | sectors = mddev->array_sectors; |
| 4977 | |
| 4978 | mddev->external_size = 0; |
| 4979 | } else { |
| 4980 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4981 | err = -EINVAL; |
| 4982 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 4983 | err = -E2BIG; |
| 4984 | else |
| 4985 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4986 | } |
| 4987 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4988 | if (!err) { |
| 4989 | mddev->array_sectors = sectors; |
| 4990 | if (mddev->pers) { |
| 4991 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4992 | revalidate_disk(mddev->gendisk); |
| 4993 | } |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4994 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4995 | mddev_unlock(mddev); |
| 4996 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4997 | } |
| 4998 | |
| 4999 | static struct md_sysfs_entry md_array_size = |
| 5000 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 5001 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5002 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5003 | static ssize_t |
| 5004 | consistency_policy_show(struct mddev *mddev, char *page) |
| 5005 | { |
| 5006 | int ret; |
| 5007 | |
| 5008 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 5009 | ret = sprintf(page, "journal\n"); |
| 5010 | } else if (test_bit(MD_HAS_PPL, &mddev->flags)) { |
| 5011 | ret = sprintf(page, "ppl\n"); |
| 5012 | } else if (mddev->bitmap) { |
| 5013 | ret = sprintf(page, "bitmap\n"); |
| 5014 | } else if (mddev->pers) { |
| 5015 | if (mddev->pers->sync_request) |
| 5016 | ret = sprintf(page, "resync\n"); |
| 5017 | else |
| 5018 | ret = sprintf(page, "none\n"); |
| 5019 | } else { |
| 5020 | ret = sprintf(page, "unknown\n"); |
| 5021 | } |
| 5022 | |
| 5023 | return ret; |
| 5024 | } |
| 5025 | |
| 5026 | static ssize_t |
| 5027 | consistency_policy_store(struct mddev *mddev, const char *buf, size_t len) |
| 5028 | { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5029 | int err = 0; |
| 5030 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5031 | if (mddev->pers) { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5032 | if (mddev->pers->change_consistency_policy) |
| 5033 | err = mddev->pers->change_consistency_policy(mddev, buf); |
| 5034 | else |
| 5035 | err = -EBUSY; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5036 | } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) { |
| 5037 | set_bit(MD_HAS_PPL, &mddev->flags); |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5038 | } else { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5039 | err = -EINVAL; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5040 | } |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5041 | |
| 5042 | return err ? err : len; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5043 | } |
| 5044 | |
| 5045 | static struct md_sysfs_entry md_consistency_policy = |
| 5046 | __ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show, |
| 5047 | consistency_policy_store); |
| 5048 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5049 | static struct attribute *md_default_attrs[] = { |
| 5050 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 5051 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5052 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 5053 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5054 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 5055 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 5056 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 5057 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5058 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5059 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5060 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5061 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5062 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5063 | &max_corr_read_errors.attr, |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5064 | &md_consistency_policy.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5065 | NULL, |
| 5066 | }; |
| 5067 | |
| 5068 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5069 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 5070 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5071 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5072 | &md_sync_min.attr, |
| 5073 | &md_sync_max.attr, |
| 5074 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5075 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5076 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5077 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5078 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5079 | &md_suspend_lo.attr, |
| 5080 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 5081 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 5082 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5083 | NULL, |
| 5084 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5085 | static struct attribute_group md_redundancy_group = { |
| 5086 | .name = NULL, |
| 5087 | .attrs = md_redundancy_attrs, |
| 5088 | }; |
| 5089 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5090 | static ssize_t |
| 5091 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 5092 | { |
| 5093 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5094 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5095 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5096 | |
| 5097 | if (!entry->show) |
| 5098 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5099 | spin_lock(&all_mddevs_lock); |
| 5100 | if (list_empty(&mddev->all_mddevs)) { |
| 5101 | spin_unlock(&all_mddevs_lock); |
| 5102 | return -EBUSY; |
| 5103 | } |
| 5104 | mddev_get(mddev); |
| 5105 | spin_unlock(&all_mddevs_lock); |
| 5106 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 5107 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5108 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5109 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5110 | } |
| 5111 | |
| 5112 | static ssize_t |
| 5113 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 5114 | const char *page, size_t length) |
| 5115 | { |
| 5116 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5117 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5118 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5119 | |
| 5120 | if (!entry->store) |
| 5121 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 5122 | if (!capable(CAP_SYS_ADMIN)) |
| 5123 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5124 | spin_lock(&all_mddevs_lock); |
| 5125 | if (list_empty(&mddev->all_mddevs)) { |
| 5126 | spin_unlock(&all_mddevs_lock); |
| 5127 | return -EBUSY; |
| 5128 | } |
| 5129 | mddev_get(mddev); |
| 5130 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5131 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5132 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5133 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5134 | } |
| 5135 | |
| 5136 | static void md_free(struct kobject *ko) |
| 5137 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5138 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5139 | |
| 5140 | if (mddev->sysfs_state) |
| 5141 | sysfs_put(mddev->sysfs_state); |
| 5142 | |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 5143 | if (mddev->queue) |
| 5144 | blk_cleanup_queue(mddev->queue); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5145 | if (mddev->gendisk) { |
| 5146 | del_gendisk(mddev->gendisk); |
| 5147 | put_disk(mddev->gendisk); |
| 5148 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5149 | percpu_ref_exit(&mddev->writes_pending); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5150 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5151 | kfree(mddev); |
| 5152 | } |
| 5153 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 5154 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5155 | .show = md_attr_show, |
| 5156 | .store = md_attr_store, |
| 5157 | }; |
| 5158 | static struct kobj_type md_ktype = { |
| 5159 | .release = md_free, |
| 5160 | .sysfs_ops = &md_sysfs_ops, |
| 5161 | .default_attrs = md_default_attrs, |
| 5162 | }; |
| 5163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | int mdp_major = 0; |
| 5165 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5166 | static void mddev_delayed_delete(struct work_struct *ws) |
| 5167 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5168 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5169 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5170 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5171 | kobject_del(&mddev->kobj); |
| 5172 | kobject_put(&mddev->kobj); |
| 5173 | } |
| 5174 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5175 | static void no_op(struct percpu_ref *r) {} |
| 5176 | |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 5177 | int mddev_init_writes_pending(struct mddev *mddev) |
| 5178 | { |
| 5179 | if (mddev->writes_pending.percpu_count_ptr) |
| 5180 | return 0; |
| 5181 | if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0) |
| 5182 | return -ENOMEM; |
| 5183 | /* We want to start with the refcount at zero */ |
| 5184 | percpu_ref_put(&mddev->writes_pending); |
| 5185 | return 0; |
| 5186 | } |
| 5187 | EXPORT_SYMBOL_GPL(mddev_init_writes_pending); |
| 5188 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5189 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5190 | { |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5191 | /* |
| 5192 | * If dev is zero, name is the name of a device to allocate with |
| 5193 | * an arbitrary minor number. It will be "md_???" |
| 5194 | * If dev is non-zero it must be a device number with a MAJOR of |
| 5195 | * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then |
| 5196 | * the device is being created by opening a node in /dev. |
| 5197 | * If "name" is not NULL, the device is being created by |
| 5198 | * writing to /sys/module/md_mod/parameters/new_array. |
| 5199 | */ |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5200 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5201 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5202 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5203 | int partitioned; |
| 5204 | int shift; |
| 5205 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5206 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | |
| 5208 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5209 | return -ENODEV; |
| 5210 | |
| 5211 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 5212 | shift = partitioned ? MdpMinorShift : 0; |
| 5213 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5215 | /* wait for any previous instance of this device to be |
| 5216 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5217 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5218 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5219 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5220 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5221 | error = -EEXIST; |
| 5222 | if (mddev->gendisk) |
| 5223 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5224 | |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5225 | if (name && !dev) { |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5226 | /* Need to ensure that 'name' is not a duplicate. |
| 5227 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5228 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5229 | spin_lock(&all_mddevs_lock); |
| 5230 | |
| 5231 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 5232 | if (mddev2->gendisk && |
| 5233 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 5234 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5235 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5236 | } |
| 5237 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | } |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5239 | if (name && dev) |
| 5240 | /* |
| 5241 | * Creating /dev/mdNNN via "newarray", so adjust hold_active. |
| 5242 | */ |
| 5243 | mddev->hold_active = UNTIL_STOP; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5244 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5245 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5246 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5247 | if (!mddev->queue) |
| 5248 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5249 | mddev->queue->queuedata = mddev; |
| 5250 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5251 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 5252 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5254 | disk = alloc_disk(1 << shift); |
| 5255 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5256 | blk_cleanup_queue(mddev->queue); |
| 5257 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5258 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5260 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5262 | if (name) |
| 5263 | strcpy(disk->disk_name, name); |
| 5264 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5266 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | disk->fops = &md_fops; |
| 5269 | disk->private_data = mddev; |
| 5270 | disk->queue = mddev->queue; |
Jens Axboe | 56883a7 | 2016-03-30 10:16:53 -0600 | [diff] [blame] | 5271 | blk_queue_write_cache(mddev->queue, true, true); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5272 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5273 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5274 | * remove it now. |
| 5275 | */ |
| 5276 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5277 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5278 | /* As soon as we call add_disk(), another thread could get |
| 5279 | * through to md_open, so make sure it doesn't get too far |
| 5280 | */ |
| 5281 | mutex_lock(&mddev->open_mutex); |
| 5282 | add_disk(disk); |
| 5283 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 5284 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 5285 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5286 | if (error) { |
| 5287 | /* This isn't possible, but as kobject_init_and_add is marked |
| 5288 | * __must_check, we must do something with the result |
| 5289 | */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5290 | pr_debug("md: cannot register %s/md - name in use\n", |
| 5291 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5292 | error = 0; |
| 5293 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5294 | if (mddev->kobj.sd && |
| 5295 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5296 | pr_debug("pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5297 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5298 | abort: |
| 5299 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5300 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5301 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5302 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5303 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5304 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5305 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5306 | } |
| 5307 | |
| 5308 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 5309 | { |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5310 | if (create_on_open) |
| 5311 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | return NULL; |
| 5313 | } |
| 5314 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5315 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 5316 | { |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5317 | /* |
| 5318 | * val must be "md_*" or "mdNNN". |
| 5319 | * For "md_*" we allocate an array with a large free minor number, and |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5320 | * set the name to val. val must not already be an active name. |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5321 | * For "mdNNN" we allocate an array with the minor number NNN |
| 5322 | * which must not already be in use. |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5323 | */ |
| 5324 | int len = strlen(val); |
| 5325 | char buf[DISK_NAME_LEN]; |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5326 | unsigned long devnum; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5327 | |
| 5328 | while (len && val[len-1] == '\n') |
| 5329 | len--; |
| 5330 | if (len >= DISK_NAME_LEN) |
| 5331 | return -E2BIG; |
| 5332 | strlcpy(buf, val, len+1); |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5333 | if (strncmp(buf, "md_", 3) == 0) |
| 5334 | return md_alloc(0, buf); |
| 5335 | if (strncmp(buf, "md", 2) == 0 && |
| 5336 | isdigit(buf[2]) && |
| 5337 | kstrtoul(buf+2, 10, &devnum) == 0 && |
| 5338 | devnum <= MINORMASK) |
| 5339 | return md_alloc(MKDEV(MD_MAJOR, devnum), NULL); |
| 5340 | |
| 5341 | return -EINVAL; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5342 | } |
| 5343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | static void md_safemode_timeout(unsigned long data) |
| 5345 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5346 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5347 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5348 | mddev->safemode = 1; |
| 5349 | if (mddev->external) |
| 5350 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | md_wakeup_thread(mddev->thread); |
| 5353 | } |
| 5354 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5355 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5356 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5357 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5358 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5359 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5360 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5361 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5362 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5363 | if (list_empty(&mddev->disks)) |
| 5364 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5365 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | |
| 5367 | if (mddev->pers) |
| 5368 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5369 | /* Cannot run until previous stop completes properly */ |
| 5370 | if (mddev->sysfs_active) |
| 5371 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | /* |
| 5374 | * Analyze all RAID superblock(s) |
| 5375 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5376 | if (!mddev->raid_disks) { |
| 5377 | if (!mddev->persistent) |
| 5378 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5379 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5381 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5382 | if (mddev->level != LEVEL_NONE) |
| 5383 | request_module("md-level-%d", mddev->level); |
| 5384 | else if (mddev->clevel[0]) |
| 5385 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5386 | |
| 5387 | /* |
| 5388 | * Drop all container device buffers, from now on |
| 5389 | * the only valid external interface is through the md |
| 5390 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5391 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5392 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5393 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5394 | continue; |
| 5395 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5396 | invalidate_bdev(rdev->bdev); |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 5397 | if (mddev->ro != 1 && |
| 5398 | (bdev_read_only(rdev->bdev) || |
| 5399 | bdev_read_only(rdev->meta_bdev))) { |
| 5400 | mddev->ro = 1; |
| 5401 | if (mddev->gendisk) |
| 5402 | set_disk_ro(mddev->gendisk, 1); |
| 5403 | } |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5404 | |
| 5405 | /* perform some consistency tests on the device. |
| 5406 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5407 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5408 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5409 | if (rdev->meta_bdev) { |
| 5410 | /* Nothing to check */; |
| 5411 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5412 | if (mddev->dev_sectors && |
| 5413 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5414 | > rdev->sb_start) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5415 | pr_warn("md: %s: data overlaps metadata\n", |
| 5416 | mdname(mddev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5417 | return -EINVAL; |
| 5418 | } |
| 5419 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5420 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5421 | > rdev->data_offset) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5422 | pr_warn("md: %s: metadata overlaps data\n", |
| 5423 | mdname(mddev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5424 | return -EINVAL; |
| 5425 | } |
| 5426 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5427 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5428 | } |
| 5429 | |
Ming Lei | 1027317 | 2017-02-14 23:29:00 +0800 | [diff] [blame] | 5430 | if (mddev->bio_set == NULL) { |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 5431 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
Ming Lei | 1027317 | 2017-02-14 23:29:00 +0800 | [diff] [blame] | 5432 | if (!mddev->bio_set) |
| 5433 | return -ENOMEM; |
| 5434 | } |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5437 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5438 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5440 | if (mddev->level != LEVEL_NONE) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5441 | pr_warn("md: personality for level %d is not loaded!\n", |
| 5442 | mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5443 | else |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5444 | pr_warn("md: personality for level %s is not loaded!\n", |
| 5445 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5446 | return -EINVAL; |
| 5447 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5449 | if (mddev->level != pers->level) { |
| 5450 | mddev->level = pers->level; |
| 5451 | mddev->new_level = pers->level; |
| 5452 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5453 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5455 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5456 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5457 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5458 | module_put(pers->owner); |
| 5459 | return -EINVAL; |
| 5460 | } |
| 5461 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5462 | if (pers->sync_request) { |
| 5463 | /* Warn if this is a potentially silly |
| 5464 | * configuration. |
| 5465 | */ |
| 5466 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5467 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5468 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5469 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5470 | rdev_for_each(rdev, mddev) |
| 5471 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5472 | if (rdev < rdev2 && |
| 5473 | rdev->bdev->bd_contains == |
| 5474 | rdev2->bdev->bd_contains) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5475 | pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n", |
| 5476 | mdname(mddev), |
| 5477 | bdevname(rdev->bdev,b), |
| 5478 | bdevname(rdev2->bdev,b2)); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5479 | warned = 1; |
| 5480 | } |
| 5481 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5482 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5483 | if (warned) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5484 | pr_warn("True protection against single-disk failure might be compromised.\n"); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5485 | } |
| 5486 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5487 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5488 | /* may be over-ridden by personality */ |
| 5489 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5490 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5491 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5493 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5494 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5495 | |
Song Liu | a85dd7b | 2017-01-23 17:12:57 -0800 | [diff] [blame] | 5496 | /* |
| 5497 | * NOTE: some pers->run(), for example r5l_recovery_log(), wakes |
| 5498 | * up mddev->thread. It is important to initialize critical |
| 5499 | * resources for mddev->thread BEFORE calling pers->run(). |
| 5500 | */ |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5501 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5502 | if (err) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5503 | pr_warn("md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5504 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5505 | WARN_ONCE(!mddev->external_size, |
| 5506 | "%s: default size too small, but 'external_size' not in effect?\n", |
| 5507 | __func__); |
| 5508 | pr_warn("md: invalid array_size %llu > default size %llu\n", |
| 5509 | (unsigned long long)mddev->array_sectors / 2, |
| 5510 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5511 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5512 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5513 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5514 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5515 | struct bitmap *bitmap; |
| 5516 | |
| 5517 | bitmap = bitmap_create(mddev, -1); |
| 5518 | if (IS_ERR(bitmap)) { |
| 5519 | err = PTR_ERR(bitmap); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5520 | pr_warn("%s: failed to create bitmap (%d)\n", |
| 5521 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5522 | } else |
| 5523 | mddev->bitmap = bitmap; |
| 5524 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5525 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | if (err) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5527 | mddev_detach(mddev); |
NeilBrown | 0c35bd4 | 2015-03-13 11:51:18 +1100 | [diff] [blame] | 5528 | if (mddev->private) |
| 5529 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5530 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5531 | module_put(pers->owner); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5532 | bitmap_destroy(mddev); |
| 5533 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5535 | if (mddev->queue) { |
Shaohua Li | bb086a8 | 2016-09-30 09:45:40 -0700 | [diff] [blame] | 5536 | bool nonrot = true; |
| 5537 | |
| 5538 | rdev_for_each(rdev, mddev) { |
| 5539 | if (rdev->raid_disk >= 0 && |
| 5540 | !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) { |
| 5541 | nonrot = false; |
| 5542 | break; |
| 5543 | } |
| 5544 | } |
| 5545 | if (mddev->degraded) |
| 5546 | nonrot = false; |
| 5547 | if (nonrot) |
| 5548 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue); |
| 5549 | else |
| 5550 | queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue); |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 5551 | mddev->queue->backing_dev_info->congested_data = mddev; |
| 5552 | mddev->queue->backing_dev_info->congested_fn = md_congested; |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5553 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5554 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5555 | if (mddev->kobj.sd && |
| 5556 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5557 | pr_warn("md: cannot register extra attributes for %s\n", |
| 5558 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5559 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5560 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5561 | mddev->ro = 0; |
| 5562 | |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5563 | atomic_set(&mddev->max_corr_read_errors, |
| 5564 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5565 | mddev->safemode = 0; |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5566 | if (mddev_is_clustered(mddev)) |
| 5567 | mddev->safemode_delay = 0; |
| 5568 | else |
| 5569 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5570 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5571 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5572 | spin_lock(&mddev->lock); |
| 5573 | mddev->pers = pers; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5574 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5575 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5576 | if (rdev->raid_disk >= 0) |
| 5577 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5578 | /* failure here is OK */; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5579 | |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 5580 | if (mddev->degraded && !mddev->ro) |
| 5581 | /* This ensures that recovering status is reported immediately |
| 5582 | * via sysfs - until a lack of spares is confirmed. |
| 5583 | */ |
| 5584 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5585 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5586 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5587 | if (mddev->sb_flags) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5588 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5589 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5590 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5591 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5592 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5593 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5594 | return 0; |
| 5595 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5596 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5597 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5598 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5599 | { |
| 5600 | int err; |
| 5601 | |
| 5602 | err = md_run(mddev); |
| 5603 | if (err) |
| 5604 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5605 | err = bitmap_load(mddev); |
| 5606 | if (err) { |
| 5607 | bitmap_destroy(mddev); |
| 5608 | goto out; |
| 5609 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5610 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5611 | if (mddev_is_clustered(mddev)) |
| 5612 | md_allow_write(mddev); |
| 5613 | |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5614 | md_wakeup_thread(mddev->thread); |
| 5615 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5616 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5617 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5618 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5619 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5620 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5621 | out: |
| 5622 | return err; |
| 5623 | } |
| 5624 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5625 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5626 | { |
| 5627 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 5628 | struct md_rdev *rdev; |
| 5629 | bool has_journal = false; |
| 5630 | bool has_readonly = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5632 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5633 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5634 | return -ENXIO; |
| 5635 | if (!mddev->pers) |
| 5636 | return -EINVAL; |
| 5637 | if (!mddev->ro) |
| 5638 | return -EBUSY; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 5639 | |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 5640 | rcu_read_lock(); |
| 5641 | rdev_for_each_rcu(rdev, mddev) { |
| 5642 | if (test_bit(Journal, &rdev->flags) && |
| 5643 | !test_bit(Faulty, &rdev->flags)) |
| 5644 | has_journal = true; |
| 5645 | if (bdev_read_only(rdev->bdev)) |
| 5646 | has_readonly = true; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 5647 | } |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 5648 | rcu_read_unlock(); |
| 5649 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal) |
| 5650 | /* Don't restart rw with journal missing/faulty */ |
| 5651 | return -EINVAL; |
| 5652 | if (has_readonly) |
| 5653 | return -EROFS; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 5654 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5655 | mddev->safemode = 0; |
| 5656 | mddev->ro = 0; |
| 5657 | set_disk_ro(disk, 0); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5658 | pr_debug("md: %s switched to read-write mode.\n", mdname(mddev)); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5659 | /* Kick recovery or resync if necessary */ |
| 5660 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5661 | md_wakeup_thread(mddev->thread); |
| 5662 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5663 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5664 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5665 | } |
| 5666 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5667 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5668 | { |
| 5669 | mddev->array_sectors = 0; |
| 5670 | mddev->external_size = 0; |
| 5671 | mddev->dev_sectors = 0; |
| 5672 | mddev->raid_disks = 0; |
| 5673 | mddev->recovery_cp = 0; |
| 5674 | mddev->resync_min = 0; |
| 5675 | mddev->resync_max = MaxSector; |
| 5676 | mddev->reshape_position = MaxSector; |
| 5677 | mddev->external = 0; |
| 5678 | mddev->persistent = 0; |
| 5679 | mddev->level = LEVEL_NONE; |
| 5680 | mddev->clevel[0] = 0; |
| 5681 | mddev->flags = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5682 | mddev->sb_flags = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5683 | mddev->ro = 0; |
| 5684 | mddev->metadata_type[0] = 0; |
| 5685 | mddev->chunk_sectors = 0; |
| 5686 | mddev->ctime = mddev->utime = 0; |
| 5687 | mddev->layout = 0; |
| 5688 | mddev->max_disks = 0; |
| 5689 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5690 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5691 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5692 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5693 | mddev->new_level = LEVEL_NONE; |
| 5694 | mddev->new_layout = 0; |
| 5695 | mddev->new_chunk_sectors = 0; |
| 5696 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5697 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5698 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5699 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5700 | mddev->recovery = 0; |
| 5701 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5702 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5703 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5704 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5705 | mddev->private = NULL; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 5706 | mddev->cluster_info = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5707 | mddev->bitmap_info.offset = 0; |
| 5708 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5709 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5710 | mddev->bitmap_info.chunksize = 0; |
| 5711 | mddev->bitmap_info.daemon_sleep = 0; |
| 5712 | mddev->bitmap_info.max_write_behind = 0; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 5713 | mddev->bitmap_info.nodes = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5714 | } |
| 5715 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5716 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5717 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 5718 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5719 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5720 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5721 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5722 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5723 | } |
| 5724 | |
| 5725 | del_timer_sync(&mddev->safemode_timer); |
| 5726 | |
Shaohua Li | 034e33f | 2016-11-21 10:29:19 -0800 | [diff] [blame] | 5727 | if (mddev->pers && mddev->pers->quiesce) { |
| 5728 | mddev->pers->quiesce(mddev, 1); |
| 5729 | mddev->pers->quiesce(mddev, 0); |
| 5730 | } |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5731 | bitmap_flush(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5732 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5733 | if (mddev->ro == 0 && |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5734 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5735 | mddev->sb_flags)) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5736 | /* mark array as shutdown cleanly */ |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5737 | if (!mddev_is_clustered(mddev)) |
| 5738 | mddev->in_sync = 1; |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5739 | md_update_sb(mddev, 1); |
| 5740 | } |
| 5741 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5742 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5743 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5744 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 5745 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5746 | __md_stop_writes(mddev); |
| 5747 | mddev_unlock(mddev); |
| 5748 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5749 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5750 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5751 | static void mddev_detach(struct mddev *mddev) |
| 5752 | { |
Guoqing Jiang | 48df498 | 2017-03-14 09:40:20 +0800 | [diff] [blame] | 5753 | bitmap_wait_behind_writes(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5754 | if (mddev->pers && mddev->pers->quiesce) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5755 | mddev->pers->quiesce(mddev, 1); |
| 5756 | mddev->pers->quiesce(mddev, 0); |
| 5757 | } |
| 5758 | md_unregister_thread(&mddev->thread); |
| 5759 | if (mddev->queue) |
| 5760 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 5761 | } |
| 5762 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5763 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5764 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5765 | struct md_personality *pers = mddev->pers; |
Guoqing Jiang | 48df498 | 2017-03-14 09:40:20 +0800 | [diff] [blame] | 5766 | bitmap_destroy(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5767 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 5768 | /* Ensure ->event_work is done */ |
| 5769 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5770 | spin_lock(&mddev->lock); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5771 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5772 | spin_unlock(&mddev->lock); |
| 5773 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5774 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5775 | if (pers->sync_request && mddev->to_remove == NULL) |
| 5776 | mddev->to_remove = &md_redundancy_group; |
| 5777 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5778 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5779 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5780 | |
| 5781 | void md_stop(struct mddev *mddev) |
| 5782 | { |
| 5783 | /* stop the array and free an attached data structures. |
| 5784 | * This is called from dm-raid |
| 5785 | */ |
| 5786 | __md_stop(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5787 | if (mddev->bio_set) |
| 5788 | bioset_free(mddev->bio_set); |
| 5789 | } |
| 5790 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5791 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5792 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5793 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5794 | { |
| 5795 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5796 | int did_freeze = 0; |
| 5797 | |
| 5798 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5799 | did_freeze = 1; |
| 5800 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5801 | md_wakeup_thread(mddev->thread); |
| 5802 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5803 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5804 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5805 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5806 | /* Thread might be blocked waiting for metadata update |
| 5807 | * which will now never happen */ |
| 5808 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5809 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5810 | if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5811 | return -EBUSY; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5812 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5813 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 5814 | &mddev->recovery)); |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5815 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5816 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5817 | mddev_lock_nointr(mddev); |
| 5818 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5819 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5820 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5821 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 5822 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5823 | pr_warn("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5824 | if (did_freeze) { |
| 5825 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5826 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5827 | md_wakeup_thread(mddev->thread); |
| 5828 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5829 | err = -EBUSY; |
| 5830 | goto out; |
| 5831 | } |
| 5832 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5833 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5834 | |
| 5835 | err = -ENXIO; |
| 5836 | if (mddev->ro==1) |
| 5837 | goto out; |
| 5838 | mddev->ro = 1; |
| 5839 | set_disk_ro(mddev->gendisk, 1); |
| 5840 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5841 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5842 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5843 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5844 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5845 | } |
| 5846 | out: |
| 5847 | mutex_unlock(&mddev->open_mutex); |
| 5848 | return err; |
| 5849 | } |
| 5850 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5851 | /* mode: |
| 5852 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5853 | * 2 - stop but do not disassemble array |
| 5854 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5855 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5856 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5859 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5860 | int did_freeze = 0; |
| 5861 | |
| 5862 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5863 | did_freeze = 1; |
| 5864 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5865 | md_wakeup_thread(mddev->thread); |
| 5866 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5867 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5868 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5869 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5870 | /* Thread might be blocked waiting for metadata update |
| 5871 | * which will now never happen */ |
| 5872 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5873 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5874 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5875 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 5876 | !test_bit(MD_RECOVERY_RUNNING, |
| 5877 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5878 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5879 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5880 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5881 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5882 | mddev->sysfs_active || |
| 5883 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 5884 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5885 | pr_warn("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5886 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5887 | if (did_freeze) { |
| 5888 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5889 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5890 | md_wakeup_thread(mddev->thread); |
| 5891 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 5892 | return -EBUSY; |
| 5893 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5894 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5895 | if (mddev->ro) |
| 5896 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5897 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5898 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5899 | __md_stop(mddev); |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 5900 | mddev->queue->backing_dev_info->congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5901 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5902 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5903 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5904 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5905 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5906 | if (rdev->raid_disk >= 0) |
| 5907 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5908 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5909 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5910 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5911 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5912 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5913 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5914 | if (mddev->ro) |
| 5915 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5916 | } else |
| 5917 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | /* |
| 5919 | * Free resources if final stop |
| 5920 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5921 | if (mode == 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5922 | pr_info("md: %s stopped.\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5924 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5925 | struct file *f = mddev->bitmap_info.file; |
| 5926 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5927 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5928 | spin_unlock(&mddev->lock); |
| 5929 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5930 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5931 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | export_array(mddev); |
| 5934 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5935 | md_clean(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5936 | if (mddev->hold_active == UNTIL_STOP) |
| 5937 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5938 | } |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5939 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5940 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5941 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5942 | } |
| 5943 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5944 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5945 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5947 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | int err; |
| 5949 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5950 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5953 | pr_info("md: running: "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5954 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5955 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | char b[BDEVNAME_SIZE]; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5957 | pr_cont("<%s>", bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5958 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5959 | pr_cont("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5961 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5962 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5963 | pr_warn("md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5964 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5965 | } |
| 5966 | } |
| 5967 | |
| 5968 | /* |
| 5969 | * lets try to run arrays based on all disks that have arrived |
| 5970 | * until now. (those are in pending_raid_disks) |
| 5971 | * |
| 5972 | * the method: pick the first pending disk, collect all disks with |
| 5973 | * the same UUID, remove all from the pending list and put them into |
| 5974 | * the 'same_array' list. Then order this list based on superblock |
| 5975 | * update time (freshest comes first), kick out 'old' disks and |
| 5976 | * compare superblocks. If everything's fine then run it. |
| 5977 | * |
| 5978 | * If "unit" is allocated, then bump its reference count |
| 5979 | */ |
| 5980 | static void autorun_devices(int part) |
| 5981 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5982 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5983 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5984 | char b[BDEVNAME_SIZE]; |
| 5985 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5986 | pr_info("md: autorun ...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5987 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5988 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5989 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5990 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5992 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5993 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5994 | pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5995 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5996 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5997 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5998 | pr_debug("md: adding %s ...\n", |
| 5999 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | list_move(&rdev->same_set, &candidates); |
| 6001 | } |
| 6002 | /* |
| 6003 | * now we have a set of devices, with all of them having |
| 6004 | * mostly sane superblocks. It's time to allocate the |
| 6005 | * mddev. |
| 6006 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6007 | if (part) { |
| 6008 | dev = MKDEV(mdp_major, |
| 6009 | rdev0->preferred_minor << MdpMinorShift); |
| 6010 | unit = MINOR(dev) >> MdpMinorShift; |
| 6011 | } else { |
| 6012 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 6013 | unit = MINOR(dev); |
| 6014 | } |
| 6015 | if (rdev0->preferred_minor != unit) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6016 | pr_warn("md: unit number in %s is bad: %d\n", |
| 6017 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6018 | break; |
| 6019 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | |
| 6021 | md_probe(dev, NULL, NULL); |
| 6022 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 6023 | if (!mddev || !mddev->gendisk) { |
| 6024 | if (mddev) |
| 6025 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6026 | break; |
| 6027 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6028 | if (mddev_lock(mddev)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6029 | pr_warn("md: %s locked, cannot run\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6030 | else if (mddev->raid_disks || mddev->major_version |
| 6031 | || !list_empty(&mddev->disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6032 | pr_warn("md: %s already running, cannot run %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6033 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 6034 | mddev_unlock(mddev); |
| 6035 | } else { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6036 | pr_debug("md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 6037 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6038 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6039 | list_del_init(&rdev->same_set); |
| 6040 | if (bind_rdev_to_array(rdev, mddev)) |
| 6041 | export_rdev(rdev); |
| 6042 | } |
| 6043 | autorun_array(mddev); |
| 6044 | mddev_unlock(mddev); |
| 6045 | } |
| 6046 | /* on success, candidates will be empty, on error |
| 6047 | * it won't... |
| 6048 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6049 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6050 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6052 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6053 | mddev_put(mddev); |
| 6054 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6055 | pr_info("md: ... autorun DONE.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6056 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6057 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6058 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6059 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6060 | { |
| 6061 | mdu_version_t ver; |
| 6062 | |
| 6063 | ver.major = MD_MAJOR_VERSION; |
| 6064 | ver.minor = MD_MINOR_VERSION; |
| 6065 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 6066 | |
| 6067 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 6068 | return -EFAULT; |
| 6069 | |
| 6070 | return 0; |
| 6071 | } |
| 6072 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6073 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6074 | { |
| 6075 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6076 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6077 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6079 | nr = working = insync = failed = spare = 0; |
| 6080 | rcu_read_lock(); |
| 6081 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6083 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | failed++; |
| 6085 | else { |
| 6086 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6087 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6088 | insync++; |
Song Liu | b347af8 | 2016-08-11 17:14:45 -0700 | [diff] [blame] | 6089 | else if (test_bit(Journal, &rdev->flags)) |
| 6090 | /* TODO: add journal count to md_u.h */ |
| 6091 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6092 | else |
| 6093 | spare++; |
| 6094 | } |
| 6095 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6096 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | |
| 6098 | info.major_version = mddev->major_version; |
| 6099 | info.minor_version = mddev->minor_version; |
| 6100 | info.patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6101 | info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6102 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6103 | info.size = mddev->dev_sectors / 2; |
| 6104 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 6105 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6106 | info.nr_disks = nr; |
| 6107 | info.raid_disks = mddev->raid_disks; |
| 6108 | info.md_minor = mddev->md_minor; |
| 6109 | info.not_persistent= !mddev->persistent; |
| 6110 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6111 | info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6112 | info.state = 0; |
| 6113 | if (mddev->in_sync) |
| 6114 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6115 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 6116 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 6117 | if (mddev_is_clustered(mddev)) |
| 6118 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6119 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | info.working_disks = working; |
| 6121 | info.failed_disks = failed; |
| 6122 | info.spare_disks = spare; |
| 6123 | |
| 6124 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6125 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6126 | |
| 6127 | if (copy_to_user(arg, &info, sizeof(info))) |
| 6128 | return -EFAULT; |
| 6129 | |
| 6130 | return 0; |
| 6131 | } |
| 6132 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6133 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6134 | { |
| 6135 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6136 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6137 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6138 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 6139 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6140 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6141 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6142 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6143 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6144 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 6145 | /* bitmap enabled */ |
| 6146 | if (mddev->bitmap_info.file) { |
| 6147 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 6148 | sizeof(file->pathname)); |
| 6149 | if (IS_ERR(ptr)) |
| 6150 | err = PTR_ERR(ptr); |
| 6151 | else |
| 6152 | memmove(file->pathname, ptr, |
| 6153 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 6154 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6155 | spin_unlock(&mddev->lock); |
| 6156 | |
| 6157 | if (err == 0 && |
| 6158 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6159 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6160 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6161 | kfree(file); |
| 6162 | return err; |
| 6163 | } |
| 6164 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6165 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6166 | { |
| 6167 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6168 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6169 | |
| 6170 | if (copy_from_user(&info, arg, sizeof(info))) |
| 6171 | return -EFAULT; |
| 6172 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6173 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 6174 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6175 | if (rdev) { |
| 6176 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 6177 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 6178 | info.raid_disk = rdev->raid_disk; |
| 6179 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6180 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6181 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6182 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6183 | info.state |= (1<<MD_DISK_ACTIVE); |
| 6184 | info.state |= (1<<MD_DISK_SYNC); |
| 6185 | } |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 6186 | if (test_bit(Journal, &rdev->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6187 | info.state |= (1<<MD_DISK_JOURNAL); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6188 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6189 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6190 | if (test_bit(FailFast, &rdev->flags)) |
| 6191 | info.state |= (1<<MD_DISK_FAILFAST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6192 | } else { |
| 6193 | info.major = info.minor = 0; |
| 6194 | info.raid_disk = -1; |
| 6195 | info.state = (1<<MD_DISK_REMOVED); |
| 6196 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6197 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6198 | |
| 6199 | if (copy_to_user(arg, &info, sizeof(info))) |
| 6200 | return -EFAULT; |
| 6201 | |
| 6202 | return 0; |
| 6203 | } |
| 6204 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6205 | static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6206 | { |
| 6207 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6208 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6209 | dev_t dev = MKDEV(info->major,info->minor); |
| 6210 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6211 | if (mddev_is_clustered(mddev) && |
| 6212 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6213 | pr_warn("%s: Cannot add to clustered mddev.\n", |
| 6214 | mdname(mddev)); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6215 | return -EINVAL; |
| 6216 | } |
| 6217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6218 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 6219 | return -EOVERFLOW; |
| 6220 | |
| 6221 | if (!mddev->raid_disks) { |
| 6222 | int err; |
| 6223 | /* expecting a device which has a superblock */ |
| 6224 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 6225 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6226 | pr_warn("md: md_import_device returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6227 | PTR_ERR(rdev)); |
| 6228 | return PTR_ERR(rdev); |
| 6229 | } |
| 6230 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6231 | struct md_rdev *rdev0 |
| 6232 | = list_entry(mddev->disks.next, |
| 6233 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6234 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6235 | .load_super(rdev, rdev0, mddev->minor_version); |
| 6236 | if (err < 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6237 | pr_warn("md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6238 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6239 | bdevname(rdev0->bdev,b2)); |
| 6240 | export_rdev(rdev); |
| 6241 | return -EINVAL; |
| 6242 | } |
| 6243 | } |
| 6244 | err = bind_rdev_to_array(rdev, mddev); |
| 6245 | if (err) |
| 6246 | export_rdev(rdev); |
| 6247 | return err; |
| 6248 | } |
| 6249 | |
| 6250 | /* |
| 6251 | * add_new_disk can be used once the array is assembled |
| 6252 | * to add "hot spares". They must already have a superblock |
| 6253 | * written |
| 6254 | */ |
| 6255 | if (mddev->pers) { |
| 6256 | int err; |
| 6257 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6258 | pr_warn("%s: personality does not support diskops!\n", |
| 6259 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6260 | return -EINVAL; |
| 6261 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 6262 | if (mddev->persistent) |
| 6263 | rdev = md_import_device(dev, mddev->major_version, |
| 6264 | mddev->minor_version); |
| 6265 | else |
| 6266 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6267 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6268 | pr_warn("md: md_import_device returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6269 | PTR_ERR(rdev)); |
| 6270 | return PTR_ERR(rdev); |
| 6271 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 6272 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6273 | if (!mddev->persistent) { |
| 6274 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6275 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6276 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6277 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 6278 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6279 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6280 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 6281 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6282 | } else |
| 6283 | super_types[mddev->major_version]. |
| 6284 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6285 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 6286 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6287 | /* This was a hot-add request, but events doesn't |
| 6288 | * match, so reject it. |
| 6289 | */ |
| 6290 | export_rdev(rdev); |
| 6291 | return -EINVAL; |
| 6292 | } |
| 6293 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6294 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6295 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6296 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6297 | else |
| 6298 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6299 | if (info->state & (1<<MD_DISK_FAILFAST)) |
| 6300 | set_bit(FailFast, &rdev->flags); |
| 6301 | else |
| 6302 | clear_bit(FailFast, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6303 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6304 | if (info->state & (1<<MD_DISK_JOURNAL)) { |
| 6305 | struct md_rdev *rdev2; |
| 6306 | bool has_journal = false; |
| 6307 | |
| 6308 | /* make sure no existing journal disk */ |
| 6309 | rdev_for_each(rdev2, mddev) { |
| 6310 | if (test_bit(Journal, &rdev2->flags)) { |
| 6311 | has_journal = true; |
| 6312 | break; |
| 6313 | } |
| 6314 | } |
| 6315 | if (has_journal) { |
| 6316 | export_rdev(rdev); |
| 6317 | return -EBUSY; |
| 6318 | } |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6319 | set_bit(Journal, &rdev->flags); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6320 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6321 | /* |
| 6322 | * check whether the device shows up in other nodes |
| 6323 | */ |
| 6324 | if (mddev_is_clustered(mddev)) { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6325 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6326 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6327 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6328 | /* --add initiated by this node */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6329 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6330 | if (err) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6331 | export_rdev(rdev); |
| 6332 | return err; |
| 6333 | } |
| 6334 | } |
| 6335 | } |
| 6336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6337 | rdev->raid_disk = -1; |
| 6338 | err = bind_rdev_to_array(rdev, mddev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6340 | if (err) |
| 6341 | export_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6342 | |
| 6343 | if (mddev_is_clustered(mddev)) { |
Guoqing Jiang | e566aef | 2016-08-12 13:42:34 +0800 | [diff] [blame] | 6344 | if (info->state & (1 << MD_DISK_CANDIDATE)) { |
| 6345 | if (!err) { |
| 6346 | err = md_cluster_ops->new_disk_ack(mddev, |
| 6347 | err == 0); |
| 6348 | if (err) |
| 6349 | md_kick_rdev_from_array(rdev); |
| 6350 | } |
| 6351 | } else { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6352 | if (err) |
| 6353 | md_cluster_ops->add_new_disk_cancel(mddev); |
| 6354 | else |
| 6355 | err = add_bound_rdev(rdev); |
| 6356 | } |
| 6357 | |
| 6358 | } else if (!err) |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 6359 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6361 | return err; |
| 6362 | } |
| 6363 | |
| 6364 | /* otherwise, add_new_disk is only allowed |
| 6365 | * for major_version==0 superblocks |
| 6366 | */ |
| 6367 | if (mddev->major_version != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6368 | pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6369 | return -EINVAL; |
| 6370 | } |
| 6371 | |
| 6372 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 6373 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6374 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6376 | pr_warn("md: error, md_import_device() returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6377 | PTR_ERR(rdev)); |
| 6378 | return PTR_ERR(rdev); |
| 6379 | } |
| 6380 | rdev->desc_nr = info->number; |
| 6381 | if (info->raid_disk < mddev->raid_disks) |
| 6382 | rdev->raid_disk = info->raid_disk; |
| 6383 | else |
| 6384 | rdev->raid_disk = -1; |
| 6385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6386 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6387 | if (info->state & (1<<MD_DISK_SYNC)) |
| 6388 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6389 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6390 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6391 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6392 | if (info->state & (1<<MD_DISK_FAILFAST)) |
| 6393 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6395 | if (!mddev->persistent) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6396 | pr_debug("md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6397 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 6398 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6399 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6400 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6401 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6402 | err = bind_rdev_to_array(rdev, mddev); |
| 6403 | if (err) { |
| 6404 | export_rdev(rdev); |
| 6405 | return err; |
| 6406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6407 | } |
| 6408 | |
| 6409 | return 0; |
| 6410 | } |
| 6411 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6412 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6413 | { |
| 6414 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6415 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6417 | rdev = find_rdev(mddev, dev); |
| 6418 | if (!rdev) |
| 6419 | return -ENXIO; |
| 6420 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6421 | if (rdev->raid_disk < 0) |
| 6422 | goto kick_rdev; |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6423 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6424 | clear_bit(Blocked, &rdev->flags); |
| 6425 | remove_and_add_spares(mddev, rdev); |
| 6426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6427 | if (rdev->raid_disk >= 0) |
| 6428 | goto busy; |
| 6429 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6430 | kick_rdev: |
Goldwyn Rodrigues | 54a8839 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6431 | if (mddev_is_clustered(mddev)) |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6432 | md_cluster_ops->remove_disk(mddev, rdev); |
| 6433 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6434 | md_kick_rdev_from_array(rdev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6435 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 6436 | if (mddev->thread) |
| 6437 | md_wakeup_thread(mddev->thread); |
| 6438 | else |
| 6439 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6440 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6441 | |
| 6442 | return 0; |
| 6443 | busy: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6444 | pr_debug("md: cannot remove active disk %s from %s ...\n", |
| 6445 | bdevname(rdev->bdev,b), mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6446 | return -EBUSY; |
| 6447 | } |
| 6448 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6449 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | { |
| 6451 | char b[BDEVNAME_SIZE]; |
| 6452 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6453 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6454 | |
| 6455 | if (!mddev->pers) |
| 6456 | return -ENODEV; |
| 6457 | |
| 6458 | if (mddev->major_version != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6459 | pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6460 | mdname(mddev)); |
| 6461 | return -EINVAL; |
| 6462 | } |
| 6463 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6464 | pr_warn("%s: personality does not support diskops!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6465 | mdname(mddev)); |
| 6466 | return -EINVAL; |
| 6467 | } |
| 6468 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6469 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6470 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6471 | pr_warn("md: error, md_import_device() returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | PTR_ERR(rdev)); |
| 6473 | return -EINVAL; |
| 6474 | } |
| 6475 | |
| 6476 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6477 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6478 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6479 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6480 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6481 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6482 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6483 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6484 | pr_warn("md: can not hot-add faulty %s disk to %s!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6485 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6486 | err = -EINVAL; |
| 6487 | goto abort_export; |
| 6488 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6489 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6490 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6491 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 6492 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6493 | err = bind_rdev_to_array(rdev, mddev); |
| 6494 | if (err) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 6495 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6496 | |
| 6497 | /* |
| 6498 | * The rest should better be atomic, we can have disk failures |
| 6499 | * noticed in interrupt contexts ... |
| 6500 | */ |
| 6501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6502 | rdev->raid_disk = -1; |
| 6503 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6504 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 6505 | if (!mddev->thread) |
| 6506 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6507 | /* |
| 6508 | * Kick recovery, maybe this spare has to be added to the |
| 6509 | * array immediately. |
| 6510 | */ |
| 6511 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6512 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6513 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6514 | return 0; |
| 6515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6516 | abort_export: |
| 6517 | export_rdev(rdev); |
| 6518 | return err; |
| 6519 | } |
| 6520 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6521 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6522 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6523 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6524 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6525 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 6526 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6527 | return -EBUSY; |
| 6528 | if (mddev->recovery || mddev->sync_thread) |
| 6529 | return -EBUSY; |
| 6530 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6531 | } |
| 6532 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6533 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6534 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6535 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6536 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6537 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 6538 | return -EEXIST; /* cannot add when bitmap is present */ |
| 6539 | f = fget(fd); |
| 6540 | |
| 6541 | if (f == NULL) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6542 | pr_warn("%s: error: failed to get bitmap file\n", |
| 6543 | mdname(mddev)); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6544 | return -EBADF; |
| 6545 | } |
| 6546 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6547 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6548 | if (!S_ISREG(inode->i_mode)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6549 | pr_warn("%s: error: bitmap file must be a regular file\n", |
| 6550 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6551 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6552 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6553 | pr_warn("%s: error: bitmap file must open for write\n", |
| 6554 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6555 | err = -EBADF; |
| 6556 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6557 | pr_warn("%s: error: bitmap file is already in use\n", |
| 6558 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6559 | err = -EBUSY; |
| 6560 | } |
| 6561 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6562 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6563 | return err; |
| 6564 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6565 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6566 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6567 | } else if (mddev->bitmap == NULL) |
| 6568 | return -ENOENT; /* cannot remove what isn't there */ |
| 6569 | err = 0; |
| 6570 | if (mddev->pers) { |
| 6571 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6572 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6573 | struct bitmap *bitmap; |
| 6574 | |
| 6575 | bitmap = bitmap_create(mddev, -1); |
| 6576 | if (!IS_ERR(bitmap)) { |
| 6577 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6578 | err = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6579 | } else |
| 6580 | err = PTR_ERR(bitmap); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6581 | } |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6582 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6583 | bitmap_destroy(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6584 | fd = -1; /* make sure to put the file */ |
| 6585 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6586 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6587 | } |
| 6588 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6589 | struct file *f = mddev->bitmap_info.file; |
| 6590 | if (f) { |
| 6591 | spin_lock(&mddev->lock); |
| 6592 | mddev->bitmap_info.file = NULL; |
| 6593 | spin_unlock(&mddev->lock); |
| 6594 | fput(f); |
| 6595 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6596 | } |
| 6597 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6598 | return err; |
| 6599 | } |
| 6600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6601 | /* |
| 6602 | * set_array_info is used two different ways |
| 6603 | * The original usage is when creating a new array. |
| 6604 | * In this usage, raid_disks is > 0 and it together with |
| 6605 | * level, size, not_persistent,layout,chunksize determine the |
| 6606 | * shape of the array. |
| 6607 | * This will always create an array with a type-0.90.0 superblock. |
| 6608 | * The newer usage is when assembling an array. |
| 6609 | * In this case raid_disks will be 0, and the major_version field is |
| 6610 | * use to determine which style super-blocks are to be found on the devices. |
| 6611 | * The minor and patch _version numbers are also kept incase the |
| 6612 | * super_block handler wishes to interpret them. |
| 6613 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6614 | static int set_array_info(struct mddev *mddev, mdu_array_info_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6615 | { |
| 6616 | |
| 6617 | if (info->raid_disks == 0) { |
| 6618 | /* just setting version number for superblock loading */ |
| 6619 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6620 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6621 | super_types[info->major_version].name == NULL) { |
| 6622 | /* maybe try to auto-load a module? */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6623 | pr_warn("md: superblock version %d not known\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6624 | info->major_version); |
| 6625 | return -EINVAL; |
| 6626 | } |
| 6627 | mddev->major_version = info->major_version; |
| 6628 | mddev->minor_version = info->minor_version; |
| 6629 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6630 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6631 | /* ensure mddev_put doesn't delete this now that there |
| 6632 | * is some minimal configuration. |
| 6633 | */ |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6634 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6635 | return 0; |
| 6636 | } |
| 6637 | mddev->major_version = MD_MAJOR_VERSION; |
| 6638 | mddev->minor_version = MD_MINOR_VERSION; |
| 6639 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6640 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6641 | |
| 6642 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6643 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6644 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6645 | mddev->raid_disks = info->raid_disks; |
| 6646 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6647 | * openned |
| 6648 | */ |
| 6649 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6650 | mddev->recovery_cp = MaxSector; |
| 6651 | else |
| 6652 | mddev->recovery_cp = 0; |
| 6653 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6654 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6655 | |
| 6656 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6657 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6658 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6659 | if (mddev->persistent) { |
NeilBrown | 1b3bae4 | 2017-03-01 07:31:28 +1100 | [diff] [blame] | 6660 | mddev->max_disks = MD_SB_DISKS; |
| 6661 | mddev->flags = 0; |
| 6662 | mddev->sb_flags = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6663 | } |
| 6664 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6665 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6666 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6667 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6668 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6669 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6670 | mddev->reshape_position = MaxSector; |
| 6671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6672 | /* |
| 6673 | * Generate a 128 bit UUID |
| 6674 | */ |
| 6675 | get_random_bytes(mddev->uuid, 16); |
| 6676 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6677 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6678 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6679 | mddev->new_layout = mddev->layout; |
| 6680 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6681 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6683 | return 0; |
| 6684 | } |
| 6685 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6686 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6687 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6688 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6689 | |
| 6690 | if (mddev->external_size) |
| 6691 | return; |
| 6692 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6693 | mddev->array_sectors = array_sectors; |
| 6694 | } |
| 6695 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6696 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6697 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6698 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6699 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6700 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6701 | int fit = (num_sectors == 0); |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 6702 | sector_t old_dev_sectors = mddev->dev_sectors; |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 6703 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6704 | if (mddev->pers->resize == NULL) |
| 6705 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6706 | /* The "num_sectors" is the number of sectors of each device that |
| 6707 | * is used. This can only make sense for arrays with redundancy. |
| 6708 | * linear and raid0 always use whatever space is available. We can only |
| 6709 | * consider changing this number if no resync or reconstruction is |
| 6710 | * happening, and if the new size is acceptable. It must fit before the |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 6711 | * sb_start or, if that is <data_offset, it must fit before the size |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6712 | * of each device. If num_sectors is zero, we find the largest size |
| 6713 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6714 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6715 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6716 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6717 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6718 | if (mddev->ro) |
| 6719 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6720 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6721 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6722 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6723 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6724 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6725 | num_sectors = avail; |
| 6726 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6727 | return -ENOSPC; |
| 6728 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6729 | rv = mddev->pers->resize(mddev, num_sectors); |
Guoqing Jiang | c948363 | 2017-02-24 11:15:23 +0800 | [diff] [blame] | 6730 | if (!rv) { |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 6731 | if (mddev_is_clustered(mddev)) |
| 6732 | md_cluster_ops->update_size(mddev, old_dev_sectors); |
| 6733 | else if (mddev->queue) { |
Guoqing Jiang | c948363 | 2017-02-24 11:15:23 +0800 | [diff] [blame] | 6734 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 6735 | revalidate_disk(mddev->gendisk); |
| 6736 | } |
| 6737 | } |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6738 | return rv; |
| 6739 | } |
| 6740 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6741 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6742 | { |
| 6743 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6744 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6745 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6746 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6747 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6748 | if (mddev->ro) |
| 6749 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6750 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6751 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6752 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6753 | if (mddev->sync_thread || |
| 6754 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6755 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6756 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6757 | |
| 6758 | rdev_for_each(rdev, mddev) { |
| 6759 | if (mddev->raid_disks < raid_disks && |
| 6760 | rdev->data_offset < rdev->new_data_offset) |
| 6761 | return -EINVAL; |
| 6762 | if (mddev->raid_disks > raid_disks && |
| 6763 | rdev->data_offset > rdev->new_data_offset) |
| 6764 | return -EINVAL; |
| 6765 | } |
| 6766 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6767 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6768 | if (mddev->delta_disks < 0) |
| 6769 | mddev->reshape_backwards = 1; |
| 6770 | else if (mddev->delta_disks > 0) |
| 6771 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6772 | |
| 6773 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6774 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6775 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6776 | mddev->reshape_backwards = 0; |
| 6777 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6778 | return rv; |
| 6779 | } |
| 6780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6781 | /* |
| 6782 | * update_array_info is used to change the configuration of an |
| 6783 | * on-line array. |
| 6784 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6785 | * fields in the info are checked against the array. |
| 6786 | * Any differences that cannot be handled will cause an error. |
| 6787 | * Normally, only one change can be managed at a time. |
| 6788 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6789 | static int update_array_info(struct mddev *mddev, mdu_array_info_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6790 | { |
| 6791 | int rv = 0; |
| 6792 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6793 | int state = 0; |
| 6794 | |
| 6795 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6796 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6797 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6798 | |
| 6799 | if (mddev->major_version != info->major_version || |
| 6800 | mddev->minor_version != info->minor_version || |
| 6801 | /* mddev->patch_version != info->patch_version || */ |
| 6802 | mddev->ctime != info->ctime || |
| 6803 | mddev->level != info->level || |
| 6804 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 6805 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6806 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6807 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6808 | ((state^info->state) & 0xfffffe00) |
| 6809 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6810 | return -EINVAL; |
| 6811 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6812 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6813 | cnt++; |
| 6814 | if (mddev->raid_disks != info->raid_disks) |
| 6815 | cnt++; |
| 6816 | if (mddev->layout != info->layout) |
| 6817 | cnt++; |
| 6818 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6819 | cnt++; |
| 6820 | if (cnt == 0) |
| 6821 | return 0; |
| 6822 | if (cnt > 1) |
| 6823 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6824 | |
| 6825 | if (mddev->layout != info->layout) { |
| 6826 | /* Change layout |
| 6827 | * we don't need to do anything at the md level, the |
| 6828 | * personality will take care of it all. |
| 6829 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6830 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6831 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6832 | else { |
| 6833 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6834 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6835 | if (rv) |
| 6836 | mddev->new_layout = mddev->layout; |
| 6837 | return rv; |
| 6838 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6839 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6840 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6841 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6842 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6843 | if (mddev->raid_disks != info->raid_disks) |
| 6844 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6845 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6846 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6847 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 6848 | rv = -EINVAL; |
| 6849 | goto err; |
| 6850 | } |
| 6851 | if (mddev->recovery || mddev->sync_thread) { |
| 6852 | rv = -EBUSY; |
| 6853 | goto err; |
| 6854 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6855 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6856 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6857 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6858 | if (mddev->bitmap) { |
| 6859 | rv = -EEXIST; |
| 6860 | goto err; |
| 6861 | } |
| 6862 | if (mddev->bitmap_info.default_offset == 0) { |
| 6863 | rv = -EINVAL; |
| 6864 | goto err; |
| 6865 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6866 | mddev->bitmap_info.offset = |
| 6867 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6868 | mddev->bitmap_info.space = |
| 6869 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6870 | mddev->pers->quiesce(mddev, 1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6871 | bitmap = bitmap_create(mddev, -1); |
| 6872 | if (!IS_ERR(bitmap)) { |
| 6873 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6874 | rv = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6875 | } else |
| 6876 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6877 | if (rv) |
| 6878 | bitmap_destroy(mddev); |
| 6879 | mddev->pers->quiesce(mddev, 0); |
| 6880 | } else { |
| 6881 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6882 | if (!mddev->bitmap) { |
| 6883 | rv = -ENOENT; |
| 6884 | goto err; |
| 6885 | } |
| 6886 | if (mddev->bitmap->storage.file) { |
| 6887 | rv = -EINVAL; |
| 6888 | goto err; |
| 6889 | } |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6890 | if (mddev->bitmap_info.nodes) { |
| 6891 | /* hold PW on all the bitmap lock */ |
| 6892 | if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6893 | pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n"); |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6894 | rv = -EPERM; |
| 6895 | md_cluster_ops->unlock_all_bitmaps(mddev); |
| 6896 | goto err; |
| 6897 | } |
| 6898 | |
| 6899 | mddev->bitmap_info.nodes = 0; |
| 6900 | md_cluster_ops->leave(mddev); |
| 6901 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6902 | mddev->pers->quiesce(mddev, 1); |
| 6903 | bitmap_destroy(mddev); |
| 6904 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6905 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6906 | } |
| 6907 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6908 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6909 | return rv; |
| 6910 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6911 | return rv; |
| 6912 | } |
| 6913 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6914 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6915 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6916 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6917 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6918 | |
| 6919 | if (mddev->pers == NULL) |
| 6920 | return -ENODEV; |
| 6921 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6922 | rcu_read_lock(); |
| 6923 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6924 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6925 | err = -ENODEV; |
| 6926 | else { |
| 6927 | md_error(mddev, rdev); |
| 6928 | if (!test_bit(Faulty, &rdev->flags)) |
| 6929 | err = -EBUSY; |
| 6930 | } |
| 6931 | rcu_read_unlock(); |
| 6932 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6933 | } |
| 6934 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6935 | /* |
| 6936 | * We have a problem here : there is no easy way to give a CHS |
| 6937 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6938 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6939 | * dosfs just mad... ;-) |
| 6940 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6941 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6942 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6943 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6944 | |
| 6945 | geo->heads = 2; |
| 6946 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6947 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6948 | return 0; |
| 6949 | } |
| 6950 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6951 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 6952 | { |
| 6953 | switch (cmd) { |
| 6954 | case ADD_NEW_DISK: |
| 6955 | case BLKROSET: |
| 6956 | case GET_ARRAY_INFO: |
| 6957 | case GET_BITMAP_FILE: |
| 6958 | case GET_DISK_INFO: |
| 6959 | case HOT_ADD_DISK: |
| 6960 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6961 | case RAID_AUTORUN: |
| 6962 | case RAID_VERSION: |
| 6963 | case RESTART_ARRAY_RW: |
| 6964 | case RUN_ARRAY: |
| 6965 | case SET_ARRAY_INFO: |
| 6966 | case SET_BITMAP_FILE: |
| 6967 | case SET_DISK_FAULTY: |
| 6968 | case STOP_ARRAY: |
| 6969 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6970 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6971 | return true; |
| 6972 | default: |
| 6973 | return false; |
| 6974 | } |
| 6975 | } |
| 6976 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6977 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | unsigned int cmd, unsigned long arg) |
| 6979 | { |
| 6980 | int err = 0; |
| 6981 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6982 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6983 | int ro; |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 6984 | bool did_set_md_closing = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6985 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6986 | if (!md_ioctl_valid(cmd)) |
| 6987 | return -ENOTTY; |
| 6988 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6989 | switch (cmd) { |
| 6990 | case RAID_VERSION: |
| 6991 | case GET_ARRAY_INFO: |
| 6992 | case GET_DISK_INFO: |
| 6993 | break; |
| 6994 | default: |
| 6995 | if (!capable(CAP_SYS_ADMIN)) |
| 6996 | return -EACCES; |
| 6997 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6998 | |
| 6999 | /* |
| 7000 | * Commands dealing with the RAID driver but not any |
| 7001 | * particular array: |
| 7002 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7003 | switch (cmd) { |
| 7004 | case RAID_VERSION: |
| 7005 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7006 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7008 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7009 | case RAID_AUTORUN: |
| 7010 | err = 0; |
| 7011 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7012 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7013 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7014 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7015 | } |
| 7016 | |
| 7017 | /* |
| 7018 | * Commands creating/starting a new array: |
| 7019 | */ |
| 7020 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7021 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7022 | |
| 7023 | if (!mddev) { |
| 7024 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7025 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7026 | } |
| 7027 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7028 | /* Some actions do not requires the mutex */ |
| 7029 | switch (cmd) { |
| 7030 | case GET_ARRAY_INFO: |
| 7031 | if (!mddev->raid_disks && !mddev->external) |
| 7032 | err = -ENODEV; |
| 7033 | else |
| 7034 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7035 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7036 | |
| 7037 | case GET_DISK_INFO: |
| 7038 | if (!mddev->raid_disks && !mddev->external) |
| 7039 | err = -ENODEV; |
| 7040 | else |
| 7041 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7042 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7043 | |
| 7044 | case SET_DISK_FAULTY: |
| 7045 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7046 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7047 | |
| 7048 | case GET_BITMAP_FILE: |
| 7049 | err = get_bitmap_file(mddev, argp); |
| 7050 | goto out; |
| 7051 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7052 | } |
| 7053 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 7054 | if (cmd == ADD_NEW_DISK) |
| 7055 | /* need to ensure md_delayed_delete() has completed */ |
| 7056 | flush_workqueue(md_misc_wq); |
| 7057 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7058 | if (cmd == HOT_REMOVE_DISK) |
| 7059 | /* need to ensure recovery thread has run */ |
| 7060 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 7061 | !test_bit(MD_RECOVERY_NEEDED, |
Shaohua Li | 82a301c | 2016-12-08 15:48:18 -0800 | [diff] [blame] | 7062 | &mddev->recovery), |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7063 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7064 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 7065 | /* Need to flush page cache, and ensure no-one else opens |
| 7066 | * and writes |
| 7067 | */ |
| 7068 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 7069 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7070 | mutex_unlock(&mddev->open_mutex); |
| 7071 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7072 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7073 | } |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7074 | WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags)); |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7075 | set_bit(MD_CLOSING, &mddev->flags); |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7076 | did_set_md_closing = true; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7077 | mutex_unlock(&mddev->open_mutex); |
| 7078 | sync_blockdev(bdev); |
| 7079 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7080 | err = mddev_lock(mddev); |
| 7081 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7082 | pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 7083 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7084 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7085 | } |
| 7086 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7087 | if (cmd == SET_ARRAY_INFO) { |
| 7088 | mdu_array_info_t info; |
| 7089 | if (!arg) |
| 7090 | memset(&info, 0, sizeof(info)); |
| 7091 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 7092 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7093 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7094 | } |
| 7095 | if (mddev->pers) { |
| 7096 | err = update_array_info(mddev, &info); |
| 7097 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7098 | pr_warn("md: couldn't update array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7099 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7100 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7101 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7102 | } |
| 7103 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7104 | pr_warn("md: array %s already has disks!\n", mdname(mddev)); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7105 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7106 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7107 | } |
| 7108 | if (mddev->raid_disks) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7109 | pr_warn("md: array %s already initialised!\n", mdname(mddev)); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7110 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7111 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7112 | } |
| 7113 | err = set_array_info(mddev, &info); |
| 7114 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7115 | pr_warn("md: couldn't set array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7116 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7117 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7118 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7119 | } |
| 7120 | |
| 7121 | /* |
| 7122 | * Commands querying/configuring an existing array: |
| 7123 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7124 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 7125 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 7126 | if ((!mddev->raid_disks && !mddev->external) |
| 7127 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 7128 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 7129 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7130 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7131 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7132 | } |
| 7133 | |
| 7134 | /* |
| 7135 | * Commands even a read-only array can execute: |
| 7136 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7137 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7138 | case RESTART_ARRAY_RW: |
| 7139 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7140 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7141 | |
| 7142 | case STOP_ARRAY: |
| 7143 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7144 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7145 | |
| 7146 | case STOP_ARRAY_RO: |
| 7147 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7148 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7149 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7150 | case HOT_REMOVE_DISK: |
| 7151 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7152 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7153 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7154 | case ADD_NEW_DISK: |
| 7155 | /* We can support ADD_NEW_DISK on read-only arrays |
Wei Fang | 466ad29 | 2016-03-21 19:19:30 +0800 | [diff] [blame] | 7156 | * only if we are re-adding a preexisting device. |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7157 | * So require mddev->pers and MD_DISK_SYNC. |
| 7158 | */ |
| 7159 | if (mddev->pers) { |
| 7160 | mdu_disk_info_t info; |
| 7161 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7162 | err = -EFAULT; |
| 7163 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 7164 | /* Need to clear read-only for this */ |
| 7165 | break; |
| 7166 | else |
| 7167 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7168 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7169 | } |
| 7170 | break; |
| 7171 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7172 | case BLKROSET: |
| 7173 | if (get_user(ro, (int __user *)(arg))) { |
| 7174 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7175 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7176 | } |
| 7177 | err = -EINVAL; |
| 7178 | |
| 7179 | /* if the bdev is going readonly the value of mddev->ro |
| 7180 | * does not matter, no writes are coming |
| 7181 | */ |
| 7182 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7183 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7184 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7185 | /* are we are already prepared for writes? */ |
| 7186 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7187 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7188 | |
| 7189 | /* transitioning to readauto need only happen for |
| 7190 | * arrays that call md_write_start |
| 7191 | */ |
| 7192 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7193 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7194 | if (err == 0) { |
| 7195 | mddev->ro = 2; |
| 7196 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 7197 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7198 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7199 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7200 | } |
| 7201 | |
| 7202 | /* |
| 7203 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7204 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7205 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 7206 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7207 | if (mddev->ro == 2) { |
| 7208 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7209 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7210 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7211 | /* mddev_unlock will wake thread */ |
| 7212 | /* If a device failed while we were read-only, we |
| 7213 | * need to make sure the metadata is updated now. |
| 7214 | */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7215 | if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) { |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7216 | mddev_unlock(mddev); |
| 7217 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7218 | !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) && |
| 7219 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 7220 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7221 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7222 | } else { |
| 7223 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7224 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7226 | } |
| 7227 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7228 | switch (cmd) { |
| 7229 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7230 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7231 | mdu_disk_info_t info; |
| 7232 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7233 | err = -EFAULT; |
| 7234 | else |
| 7235 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7236 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7238 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 7239 | case CLUSTERED_DISK_NACK: |
| 7240 | if (mddev_is_clustered(mddev)) |
| 7241 | md_cluster_ops->new_disk_ack(mddev, false); |
| 7242 | else |
| 7243 | err = -EINVAL; |
| 7244 | goto unlock; |
| 7245 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7246 | case HOT_ADD_DISK: |
| 7247 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7248 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7249 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7250 | case RUN_ARRAY: |
| 7251 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7252 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7253 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7254 | case SET_BITMAP_FILE: |
| 7255 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7256 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7257 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7258 | default: |
| 7259 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7260 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7261 | } |
| 7262 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7263 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7264 | if (mddev->hold_active == UNTIL_IOCTL && |
| 7265 | err != -EINVAL) |
| 7266 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7267 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7268 | out: |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7269 | if(did_set_md_closing) |
| 7270 | clear_bit(MD_CLOSING, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7271 | return err; |
| 7272 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7273 | #ifdef CONFIG_COMPAT |
| 7274 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 7275 | unsigned int cmd, unsigned long arg) |
| 7276 | { |
| 7277 | switch (cmd) { |
| 7278 | case HOT_REMOVE_DISK: |
| 7279 | case HOT_ADD_DISK: |
| 7280 | case SET_DISK_FAULTY: |
| 7281 | case SET_BITMAP_FILE: |
| 7282 | /* These take in integer arg, do not convert */ |
| 7283 | break; |
| 7284 | default: |
| 7285 | arg = (unsigned long)compat_ptr(arg); |
| 7286 | break; |
| 7287 | } |
| 7288 | |
| 7289 | return md_ioctl(bdev, mode, cmd, arg); |
| 7290 | } |
| 7291 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7292 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7293 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7294 | { |
| 7295 | /* |
| 7296 | * Succeed if we can lock the mddev, which confirms that |
| 7297 | * it isn't being stopped right now. |
| 7298 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7299 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7300 | int err; |
| 7301 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 7302 | if (!mddev) |
| 7303 | return -ENODEV; |
| 7304 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7305 | if (mddev->gendisk != bdev->bd_disk) { |
| 7306 | /* we are racing with mddev_put which is discarding this |
| 7307 | * bd_disk. |
| 7308 | */ |
| 7309 | mddev_put(mddev); |
| 7310 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7311 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7312 | /* Then retry the open from the top */ |
| 7313 | return -ERESTARTSYS; |
| 7314 | } |
| 7315 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 7316 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7317 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7318 | goto out; |
| 7319 | |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7320 | if (test_bit(MD_CLOSING, &mddev->flags)) { |
| 7321 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | e2342ca | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7322 | err = -ENODEV; |
| 7323 | goto out; |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7324 | } |
| 7325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7326 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7327 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7328 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7329 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7330 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7331 | out: |
NeilBrown | e2342ca | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7332 | if (err) |
| 7333 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7334 | return err; |
| 7335 | } |
| 7336 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 7337 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7338 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7339 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7340 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 7341 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7342 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7343 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7344 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7345 | |
| 7346 | static int md_media_changed(struct gendisk *disk) |
| 7347 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7348 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7349 | |
| 7350 | return mddev->changed; |
| 7351 | } |
| 7352 | |
| 7353 | static int md_revalidate(struct gendisk *disk) |
| 7354 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7355 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7356 | |
| 7357 | mddev->changed = 0; |
| 7358 | return 0; |
| 7359 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 7360 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7361 | { |
| 7362 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7363 | .open = md_open, |
| 7364 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 7365 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7366 | #ifdef CONFIG_COMPAT |
| 7367 | .compat_ioctl = md_compat_ioctl, |
| 7368 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7369 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7370 | .media_changed = md_media_changed, |
| 7371 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7372 | }; |
| 7373 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7374 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7375 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7376 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7378 | /* |
| 7379 | * md_thread is a 'system-thread', it's priority should be very |
| 7380 | * high. We avoid resource deadlocks individually in each |
| 7381 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 7382 | * the very same RT priority as kswapd, thus we will never get |
| 7383 | * into a priority inversion deadlock. |
| 7384 | * |
| 7385 | * we definitely have to have equal or higher priority than |
| 7386 | * bdflush, otherwise bdflush will deadlock if there are too |
| 7387 | * many dirty RAID5 blocks. |
| 7388 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7389 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7390 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7391 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7392 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7393 | /* We need to wait INTERRUPTIBLE so that |
| 7394 | * we don't add to the load-average. |
| 7395 | * That means we need to be sure no signals are |
| 7396 | * pending |
| 7397 | */ |
| 7398 | if (signal_pending(current)) |
| 7399 | flush_signals(current); |
| 7400 | |
| 7401 | wait_event_interruptible_timeout |
| 7402 | (thread->wqueue, |
| 7403 | test_bit(THREAD_WAKEUP, &thread->flags) |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 7404 | || kthread_should_stop() || kthread_should_park(), |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7405 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7406 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7407 | clear_bit(THREAD_WAKEUP, &thread->flags); |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 7408 | if (kthread_should_park()) |
| 7409 | kthread_parkme(); |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7410 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7411 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7412 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7414 | return 0; |
| 7415 | } |
| 7416 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7417 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7418 | { |
| 7419 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7420 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7421 | if (!test_and_set_bit(THREAD_WAKEUP, &thread->flags)) |
| 7422 | wake_up(&thread->wqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7423 | } |
| 7424 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7425 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7426 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7427 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7428 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7429 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7430 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7431 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7432 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7433 | if (!thread) |
| 7434 | return NULL; |
| 7435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7436 | init_waitqueue_head(&thread->wqueue); |
| 7437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7438 | thread->run = run; |
| 7439 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7440 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7441 | thread->tsk = kthread_run(md_thread, thread, |
| 7442 | "%s_%s", |
| 7443 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7444 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7445 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7446 | kfree(thread); |
| 7447 | return NULL; |
| 7448 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7449 | return thread; |
| 7450 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7451 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7452 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7453 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7454 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7455 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7456 | if (!thread) |
| 7457 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7458 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7459 | /* Locking ensures that mddev_unlock does not wake_up a |
| 7460 | * non-existent thread |
| 7461 | */ |
| 7462 | spin_lock(&pers_lock); |
| 7463 | *threadp = NULL; |
| 7464 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7465 | |
| 7466 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7467 | kfree(thread); |
| 7468 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7469 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7470 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7471 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7472 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7473 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7474 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7475 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7476 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7477 | return; |
| 7478 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7479 | if (mddev->degraded) |
| 7480 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7481 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7482 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7483 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7484 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 7485 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7486 | queue_work(md_misc_wq, &mddev->event_work); |
Guoqing Jiang | bb9ef71 | 2015-12-28 10:46:38 +0800 | [diff] [blame] | 7487 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7488 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7489 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7490 | |
| 7491 | /* seq_file implementation /proc/mdstat */ |
| 7492 | |
| 7493 | static void status_unused(struct seq_file *seq) |
| 7494 | { |
| 7495 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7496 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7497 | |
| 7498 | seq_printf(seq, "unused devices: "); |
| 7499 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7500 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7501 | char b[BDEVNAME_SIZE]; |
| 7502 | i++; |
| 7503 | seq_printf(seq, "%s ", |
| 7504 | bdevname(rdev->bdev,b)); |
| 7505 | } |
| 7506 | if (!i) |
| 7507 | seq_printf(seq, "<none>"); |
| 7508 | |
| 7509 | seq_printf(seq, "\n"); |
| 7510 | } |
| 7511 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7512 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7513 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7514 | sector_t max_sectors, resync, res; |
| 7515 | unsigned long dt, db; |
| 7516 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7517 | int scale; |
| 7518 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7519 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7520 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 7521 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7522 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7523 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7524 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7525 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7526 | resync = mddev->curr_resync; |
| 7527 | if (resync <= 3) { |
| 7528 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7529 | /* Still cleaning up */ |
| 7530 | resync = max_sectors; |
| 7531 | } else |
| 7532 | resync -= atomic_read(&mddev->recovery_active); |
| 7533 | |
| 7534 | if (resync == 0) { |
| 7535 | if (mddev->recovery_cp < MaxSector) { |
| 7536 | seq_printf(seq, "\tresync=PENDING"); |
| 7537 | return 1; |
| 7538 | } |
| 7539 | return 0; |
| 7540 | } |
| 7541 | if (resync < 3) { |
| 7542 | seq_printf(seq, "\tresync=DELAYED"); |
| 7543 | return 1; |
| 7544 | } |
| 7545 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 7546 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7547 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7548 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7549 | * u32, as those are the requirements for sector_div. |
| 7550 | * Thus 'scale' must be at least 10 |
| 7551 | */ |
| 7552 | scale = 10; |
| 7553 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7554 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7555 | scale++; |
| 7556 | } |
| 7557 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7558 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7559 | |
| 7560 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7561 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7562 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7563 | seq_printf(seq, "["); |
| 7564 | for (i = 0; i < x; i++) |
| 7565 | seq_printf(seq, "="); |
| 7566 | seq_printf(seq, ">"); |
| 7567 | for (i = 0; i < y; i++) |
| 7568 | seq_printf(seq, "."); |
| 7569 | seq_printf(seq, "] "); |
| 7570 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7571 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7572 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 7573 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7574 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 7575 | "check" : |
| 7576 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 7577 | "resync" : "recovery"))), |
| 7578 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7579 | (unsigned long long) resync/2, |
| 7580 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7581 | |
| 7582 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7583 | * dt: time from mark until now |
| 7584 | * db: blocks written from mark until now |
| 7585 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7586 | * |
| 7587 | * rt is a sector_t, so could be 32bit or 64bit. |
| 7588 | * So we divide before multiply in case it is 32bit and close |
| 7589 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 7590 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7591 | * near the end of resync when the number of remaining sectors |
| 7592 | * is close to 'db'. |
| 7593 | * We then divide rt by 32 after multiplying by db to compensate. |
| 7594 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7595 | */ |
| 7596 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 7597 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7598 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 7599 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7600 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7601 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 7602 | sector_div(rt, db/32+1); |
| 7603 | rt *= dt; |
| 7604 | rt >>= 5; |
| 7605 | |
| 7606 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 7607 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7608 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7609 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7610 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7611 | } |
| 7612 | |
| 7613 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 7614 | { |
| 7615 | struct list_head *tmp; |
| 7616 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7617 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7618 | |
| 7619 | if (l >= 0x10000) |
| 7620 | return NULL; |
| 7621 | if (!l--) |
| 7622 | /* header */ |
| 7623 | return (void*)1; |
| 7624 | |
| 7625 | spin_lock(&all_mddevs_lock); |
| 7626 | list_for_each(tmp,&all_mddevs) |
| 7627 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7628 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7629 | mddev_get(mddev); |
| 7630 | spin_unlock(&all_mddevs_lock); |
| 7631 | return mddev; |
| 7632 | } |
| 7633 | spin_unlock(&all_mddevs_lock); |
| 7634 | if (!l--) |
| 7635 | return (void*)2;/* tail */ |
| 7636 | return NULL; |
| 7637 | } |
| 7638 | |
| 7639 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 7640 | { |
| 7641 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7642 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7644 | ++*pos; |
| 7645 | if (v == (void*)2) |
| 7646 | return NULL; |
| 7647 | |
| 7648 | spin_lock(&all_mddevs_lock); |
| 7649 | if (v == (void*)1) |
| 7650 | tmp = all_mddevs.next; |
| 7651 | else |
| 7652 | tmp = mddev->all_mddevs.next; |
| 7653 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7654 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7655 | else { |
| 7656 | next_mddev = (void*)2; |
| 7657 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7658 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7659 | spin_unlock(&all_mddevs_lock); |
| 7660 | |
| 7661 | if (v != (void*)1) |
| 7662 | mddev_put(mddev); |
| 7663 | return next_mddev; |
| 7664 | |
| 7665 | } |
| 7666 | |
| 7667 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 7668 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7669 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7670 | |
| 7671 | if (mddev && v != (void*)1 && v != (void*)2) |
| 7672 | mddev_put(mddev); |
| 7673 | } |
| 7674 | |
| 7675 | static int md_seq_show(struct seq_file *seq, void *v) |
| 7676 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7677 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7678 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7679 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7680 | |
| 7681 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7682 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7683 | seq_printf(seq, "Personalities : "); |
| 7684 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7685 | list_for_each_entry(pers, &pers_list, list) |
| 7686 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7687 | |
| 7688 | spin_unlock(&pers_lock); |
| 7689 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7690 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7691 | return 0; |
| 7692 | } |
| 7693 | if (v == (void*)2) { |
| 7694 | status_unused(seq); |
| 7695 | return 0; |
| 7696 | } |
| 7697 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7698 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7699 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7700 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7701 | mddev->pers ? "" : "in"); |
| 7702 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7703 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7704 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7705 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7706 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7707 | seq_printf(seq, " %s", mddev->pers->name); |
| 7708 | } |
| 7709 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7710 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7711 | rcu_read_lock(); |
| 7712 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7713 | char b[BDEVNAME_SIZE]; |
| 7714 | seq_printf(seq, " %s[%d]", |
| 7715 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7716 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7717 | seq_printf(seq, "(W)"); |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 7718 | if (test_bit(Journal, &rdev->flags)) |
| 7719 | seq_printf(seq, "(J)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7720 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7721 | seq_printf(seq, "(F)"); |
| 7722 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7723 | } |
| 7724 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7725 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7726 | if (test_bit(Replacement, &rdev->flags)) |
| 7727 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7728 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7729 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7730 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7731 | |
| 7732 | if (!list_empty(&mddev->disks)) { |
| 7733 | if (mddev->pers) |
| 7734 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7735 | (unsigned long long) |
| 7736 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7737 | else |
| 7738 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7739 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7740 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7741 | if (mddev->persistent) { |
| 7742 | if (mddev->major_version != 0 || |
| 7743 | mddev->minor_version != 90) { |
| 7744 | seq_printf(seq," super %d.%d", |
| 7745 | mddev->major_version, |
| 7746 | mddev->minor_version); |
| 7747 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7748 | } else if (mddev->external) |
| 7749 | seq_printf(seq, " super external:%s", |
| 7750 | mddev->metadata_type); |
| 7751 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7752 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7753 | |
| 7754 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7755 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7756 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7757 | if (mddev->pers->sync_request) { |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7758 | if (status_resync(seq, mddev)) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7759 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7760 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7761 | } else |
| 7762 | seq_printf(seq, "\n "); |
| 7763 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7764 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7765 | |
| 7766 | seq_printf(seq, "\n"); |
| 7767 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7768 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7770 | return 0; |
| 7771 | } |
| 7772 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7773 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7774 | .start = md_seq_start, |
| 7775 | .next = md_seq_next, |
| 7776 | .stop = md_seq_stop, |
| 7777 | .show = md_seq_show, |
| 7778 | }; |
| 7779 | |
| 7780 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7781 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7782 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7783 | int error; |
| 7784 | |
| 7785 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7786 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7787 | return error; |
| 7788 | |
| 7789 | seq = file->private_data; |
| 7790 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7791 | return error; |
| 7792 | } |
| 7793 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7794 | static int md_unloading; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7795 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7796 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7797 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7798 | int mask; |
| 7799 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7800 | if (md_unloading) |
kbuild test robot | 7d7e64f | 2014-12-03 16:07:59 +1100 | [diff] [blame] | 7801 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7802 | poll_wait(filp, &md_event_waiters, wait); |
| 7803 | |
| 7804 | /* always allow read */ |
| 7805 | mask = POLLIN | POLLRDNORM; |
| 7806 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7807 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7808 | mask |= POLLERR | POLLPRI; |
| 7809 | return mask; |
| 7810 | } |
| 7811 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7812 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7813 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7814 | .open = md_seq_open, |
| 7815 | .read = seq_read, |
| 7816 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7817 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7818 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7819 | }; |
| 7820 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7821 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7822 | { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7823 | pr_debug("md: %s personality registered for level %d\n", |
| 7824 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7825 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7826 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7827 | spin_unlock(&pers_lock); |
| 7828 | return 0; |
| 7829 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7830 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7831 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7832 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7833 | { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7834 | pr_debug("md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7835 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7836 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7837 | spin_unlock(&pers_lock); |
| 7838 | return 0; |
| 7839 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7840 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7841 | |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7842 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
| 7843 | struct module *module) |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7844 | { |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7845 | int ret = 0; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7846 | spin_lock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7847 | if (md_cluster_ops != NULL) |
| 7848 | ret = -EALREADY; |
| 7849 | else { |
| 7850 | md_cluster_ops = ops; |
| 7851 | md_cluster_mod = module; |
| 7852 | } |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7853 | spin_unlock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7854 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7855 | } |
| 7856 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 7857 | |
| 7858 | int unregister_md_cluster_operations(void) |
| 7859 | { |
| 7860 | spin_lock(&pers_lock); |
| 7861 | md_cluster_ops = NULL; |
| 7862 | spin_unlock(&pers_lock); |
| 7863 | return 0; |
| 7864 | } |
| 7865 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 7866 | |
| 7867 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 7868 | { |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 7869 | if (!md_cluster_ops) |
| 7870 | request_module("md-cluster"); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7871 | spin_lock(&pers_lock); |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 7872 | /* ensure module won't be unloaded */ |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7873 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7874 | pr_warn("can't find md-cluster module or get it's reference.\n"); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7875 | spin_unlock(&pers_lock); |
| 7876 | return -ENOENT; |
| 7877 | } |
| 7878 | spin_unlock(&pers_lock); |
| 7879 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 7880 | return md_cluster_ops->join(mddev, nodes); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7881 | } |
| 7882 | |
| 7883 | void md_cluster_stop(struct mddev *mddev) |
| 7884 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 7885 | if (!md_cluster_ops) |
| 7886 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7887 | md_cluster_ops->leave(mddev); |
| 7888 | module_put(md_cluster_mod); |
| 7889 | } |
| 7890 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7891 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7892 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7893 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7894 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7895 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7896 | |
| 7897 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7898 | rcu_read_lock(); |
| 7899 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7900 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7901 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7902 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7903 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7904 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7905 | * as sync_io is counted when a request starts, and |
| 7906 | * disk_stats is counted when it completes. |
| 7907 | * So resync activity will cause curr_events to be smaller than |
| 7908 | * when there was no such activity. |
| 7909 | * non-sync IO will cause disk_stat to increase without |
| 7910 | * increasing sync_io so curr_events will (eventually) |
| 7911 | * be larger than it was before. Once it becomes |
| 7912 | * substantially larger, the test below will cause |
| 7913 | * the array to appear non-idle, and resync will slow |
| 7914 | * down. |
| 7915 | * If there is a lot of outstanding resync activity when |
| 7916 | * we set last_event to curr_events, then all that activity |
| 7917 | * completing might cause the array to appear non-idle |
| 7918 | * and resync will be slowed down even though there might |
| 7919 | * not have been non-resync activity. This will only |
| 7920 | * happen once though. 'last_events' will soon reflect |
| 7921 | * the state where there is little or no outstanding |
| 7922 | * resync requests, and further resync activity will |
| 7923 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7924 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7925 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7926 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7927 | rdev->last_events = curr_events; |
| 7928 | idle = 0; |
| 7929 | } |
| 7930 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7931 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7932 | return idle; |
| 7933 | } |
| 7934 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7935 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7936 | { |
| 7937 | /* another "blocks" (512byte) blocks have been synced */ |
| 7938 | atomic_sub(blocks, &mddev->recovery_active); |
| 7939 | wake_up(&mddev->recovery_wait); |
| 7940 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7941 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7942 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7943 | md_wakeup_thread(mddev->thread); |
| 7944 | // stop recovery, signal do_sync .... |
| 7945 | } |
| 7946 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7947 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7948 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7949 | /* md_write_start(mddev, bi) |
| 7950 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7951 | * in superblock) before writing, schedule a superblock update |
| 7952 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7953 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7954 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7955 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7956 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7957 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7958 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7959 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7960 | BUG_ON(mddev->ro == 1); |
| 7961 | if (mddev->ro == 2) { |
| 7962 | /* need to switch to read/write */ |
| 7963 | mddev->ro = 0; |
| 7964 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7965 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7966 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7967 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7968 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7969 | rcu_read_lock(); |
| 7970 | percpu_ref_get(&mddev->writes_pending); |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7971 | smp_mb(); /* Match smp_mb in set_in_sync() */ |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7972 | if (mddev->safemode == 1) |
| 7973 | mddev->safemode = 0; |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7974 | /* sync_checkers is always 0 when writes_pending is in per-cpu mode */ |
| 7975 | if (mddev->in_sync || !mddev->sync_checkers) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7976 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7977 | if (mddev->in_sync) { |
| 7978 | mddev->in_sync = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7979 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 7980 | set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7981 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7982 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7983 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7984 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7985 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7986 | rcu_read_unlock(); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7987 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7988 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7989 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7990 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7991 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7992 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7993 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 7994 | /* md_write_inc can only be called when md_write_start() has |
| 7995 | * already been called at least once of the current request. |
| 7996 | * It increments the counter and is useful when a single request |
| 7997 | * is split into several parts. Each part causes an increment and |
| 7998 | * so needs a matching md_write_end(). |
| 7999 | * Unlike md_write_start(), it is safe to call md_write_inc() inside |
| 8000 | * a spinlocked region. |
| 8001 | */ |
| 8002 | void md_write_inc(struct mddev *mddev, struct bio *bi) |
| 8003 | { |
| 8004 | if (bio_data_dir(bi) != WRITE) |
| 8005 | return; |
| 8006 | WARN_ON_ONCE(mddev->in_sync || mddev->ro); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8007 | percpu_ref_get(&mddev->writes_pending); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 8008 | } |
| 8009 | EXPORT_SYMBOL(md_write_inc); |
| 8010 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8011 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8012 | { |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8013 | percpu_ref_put(&mddev->writes_pending); |
| 8014 | |
| 8015 | if (mddev->safemode == 2) |
| 8016 | md_wakeup_thread(mddev->thread); |
| 8017 | else if (mddev->safemode_delay) |
| 8018 | /* The roundup() ensures this only performs locking once |
| 8019 | * every ->safemode_delay jiffies |
| 8020 | */ |
| 8021 | mod_timer(&mddev->safemode_timer, |
| 8022 | roundup(jiffies, mddev->safemode_delay) + |
| 8023 | mddev->safemode_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8024 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8025 | |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8026 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8027 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8028 | /* md_allow_write(mddev) |
| 8029 | * Calling this ensures that the array is marked 'active' so that writes |
| 8030 | * may proceed without blocking. It is important to call this before |
| 8031 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 8032 | * Must be called with mddev_lock held. |
| 8033 | */ |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8034 | void md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8035 | { |
| 8036 | if (!mddev->pers) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8037 | return; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8038 | if (mddev->ro) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8039 | return; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 8040 | if (!mddev->pers->sync_request) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8041 | return; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8042 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8043 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8044 | if (mddev->in_sync) { |
| 8045 | mddev->in_sync = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8046 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 8047 | set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8048 | if (mddev->safemode_delay && |
| 8049 | mddev->safemode == 0) |
| 8050 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8051 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8052 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8053 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8054 | /* wait for the dirty state to be recorded in the metadata */ |
| 8055 | wait_event(mddev->sb_wait, |
| 8056 | !test_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags) && |
| 8057 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8058 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8059 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8060 | } |
| 8061 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 8062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8063 | #define SYNC_MARKS 10 |
| 8064 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8065 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 8066 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8067 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 8068 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8069 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8070 | unsigned int currspeed = 0, |
| 8071 | window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8072 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8073 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8074 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8075 | sector_t mark_cnt[SYNC_MARKS]; |
| 8076 | int last_mark,m; |
| 8077 | struct list_head *tmp; |
| 8078 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8079 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8080 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8081 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8082 | struct blk_plug plug; |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8083 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8084 | |
| 8085 | /* just incase thread restarts... */ |
| 8086 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 8087 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 8088 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 8089 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8090 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 8091 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8092 | |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8093 | if (mddev_is_clustered(mddev)) { |
| 8094 | ret = md_cluster_ops->resync_start(mddev); |
| 8095 | if (ret) |
| 8096 | goto skip; |
| 8097 | |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 8098 | set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags); |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8099 | if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 8100 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) || |
| 8101 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
| 8102 | && ((unsigned long long)mddev->curr_resync_completed |
| 8103 | < (unsigned long long)mddev->resync_max_sectors)) |
| 8104 | goto skip; |
| 8105 | } |
| 8106 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8107 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8108 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8109 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8110 | action = "check"; |
| 8111 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8112 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8113 | action = "repair"; |
| 8114 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8115 | desc = "resync"; |
| 8116 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 8117 | desc = "reshape"; |
| 8118 | else |
| 8119 | desc = "recovery"; |
| 8120 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8121 | mddev->last_sync_action = action ?: desc; |
| 8122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8123 | /* we overload curr_resync somewhat here. |
| 8124 | * 0 == not engaged in resync at all |
| 8125 | * 2 == checking that there is no conflict with another sync |
| 8126 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 8127 | * commense |
| 8128 | * other == active in resync - this many blocks |
| 8129 | * |
| 8130 | * Before starting a resync we must have set curr_resync to |
| 8131 | * 2, and then checked that every "conflicting" array has curr_resync |
| 8132 | * less than ours. When we find one that is the same or higher |
| 8133 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 8134 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 8135 | * This will mean we have to start checking from the beginning again. |
| 8136 | * |
| 8137 | */ |
| 8138 | |
| 8139 | do { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8140 | int mddev2_minor = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8141 | mddev->curr_resync = 2; |
| 8142 | |
| 8143 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 8144 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8145 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8146 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8147 | if (mddev2 == mddev) |
| 8148 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 8149 | if (!mddev->parallel_resync |
| 8150 | && mddev2->curr_resync |
| 8151 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8152 | DEFINE_WAIT(wq); |
| 8153 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 8154 | /* arbitrarily yield */ |
| 8155 | mddev->curr_resync = 1; |
| 8156 | wake_up(&resync_wait); |
| 8157 | } |
| 8158 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 8159 | /* no need to wait here, we can wait the next |
| 8160 | * time 'round when curr_resync == 2 |
| 8161 | */ |
| 8162 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 8163 | /* We need to wait 'interruptible' so as not to |
| 8164 | * contribute to the load average, and not to |
| 8165 | * be caught by 'softlockup' |
| 8166 | */ |
| 8167 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8168 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 8169 | mddev2->curr_resync >= mddev->curr_resync) { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8170 | if (mddev2_minor != mddev2->md_minor) { |
| 8171 | mddev2_minor = mddev2->md_minor; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8172 | pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n", |
| 8173 | desc, mdname(mddev), |
| 8174 | mdname(mddev2)); |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8175 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8176 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 8177 | if (signal_pending(current)) |
| 8178 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8179 | schedule(); |
| 8180 | finish_wait(&resync_wait, &wq); |
| 8181 | goto try_again; |
| 8182 | } |
| 8183 | finish_wait(&resync_wait, &wq); |
| 8184 | } |
| 8185 | } |
| 8186 | } while (mddev->curr_resync < 2); |
| 8187 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8188 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8189 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8190 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8191 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8192 | */ |
| 8193 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 8194 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8195 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 8196 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8197 | j = mddev->resync_min; |
| 8198 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8199 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 8200 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 8201 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 8202 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8203 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8204 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8205 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8206 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8207 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8208 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8209 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8210 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8211 | !test_bit(Faulty, &rdev->flags) && |
| 8212 | !test_bit(In_sync, &rdev->flags) && |
| 8213 | rdev->recovery_offset < j) |
| 8214 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8215 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 8216 | |
| 8217 | /* If there is a bitmap, we need to make sure all |
| 8218 | * writes that started before we added a spare |
| 8219 | * complete before we start doing a recovery. |
| 8220 | * Otherwise the write might complete and (via |
| 8221 | * bitmap_endwrite) set a bit in the bitmap after the |
| 8222 | * recovery has checked that bit and skipped that |
| 8223 | * region. |
| 8224 | */ |
| 8225 | if (mddev->bitmap) { |
| 8226 | mddev->pers->quiesce(mddev, 1); |
| 8227 | mddev->pers->quiesce(mddev, 0); |
| 8228 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8230 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8231 | pr_info("md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 8232 | pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev)); |
| 8233 | pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n", |
| 8234 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8235 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8236 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8237 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8238 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8239 | for (m = 0; m < SYNC_MARKS; m++) { |
| 8240 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8241 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8242 | } |
| 8243 | last_mark = 0; |
| 8244 | mddev->resync_mark = mark[last_mark]; |
| 8245 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 8246 | |
| 8247 | /* |
| 8248 | * Tune reconstruction: |
| 8249 | */ |
| 8250 | window = 32*(PAGE_SIZE/512); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8251 | pr_debug("md: using %dk window, over a total of %lluk.\n", |
| 8252 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8253 | |
| 8254 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8255 | last_check = 0; |
| 8256 | |
| 8257 | if (j>2) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8258 | pr_debug("md: resuming %s of %s from checkpoint.\n", |
| 8259 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8260 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8261 | } else |
| 8262 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8263 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8264 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 8265 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8266 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8267 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8268 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8269 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8270 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8271 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8272 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8273 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8274 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8275 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 8276 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 8277 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8278 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8279 | (j - mddev->curr_resync_completed)*2 |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 8280 | >= mddev->resync_max - mddev->curr_resync_completed || |
| 8281 | mddev->curr_resync_completed > mddev->resync_max |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8282 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8283 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8284 | wait_event(mddev->recovery_wait, |
| 8285 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8286 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8287 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 8288 | j > mddev->recovery_cp) |
| 8289 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8290 | update_time = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8291 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8292 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8293 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8294 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8295 | while (j >= mddev->resync_max && |
| 8296 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8297 | /* As this condition is controlled by user-space, |
| 8298 | * we can block indefinitely, so use '_interruptible' |
| 8299 | * to avoid triggering warnings. |
| 8300 | */ |
| 8301 | flush_signals(current); /* just in case */ |
| 8302 | wait_event_interruptible(mddev->recovery_wait, |
| 8303 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8304 | || test_bit(MD_RECOVERY_INTR, |
| 8305 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8306 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8307 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8308 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8309 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8310 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8311 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8312 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8313 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8314 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8315 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8316 | |
| 8317 | if (!skipped) { /* actual IO requested */ |
| 8318 | io_sectors += sectors; |
| 8319 | atomic_add(sectors, &mddev->recovery_active); |
| 8320 | } |
| 8321 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8322 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8323 | break; |
| 8324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8325 | j += sectors; |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8326 | if (j > max_sectors) |
| 8327 | /* when skipping, extra large numbers can be returned. */ |
| 8328 | j = max_sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8329 | if (j > 2) |
| 8330 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8331 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8332 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8333 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8334 | * visible in /proc/mdstat |
| 8335 | */ |
| 8336 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8337 | |
| 8338 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8339 | continue; |
| 8340 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8341 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8342 | repeat: |
| 8343 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 8344 | /* step marks */ |
| 8345 | int next = (last_mark+1) % SYNC_MARKS; |
| 8346 | |
| 8347 | mddev->resync_mark = mark[next]; |
| 8348 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 8349 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8350 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8351 | last_mark = next; |
| 8352 | } |
| 8353 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8354 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8355 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8356 | |
| 8357 | /* |
| 8358 | * this loop exits only if either when we are slower than |
| 8359 | * the 'hard' speed limit, or the system was IO-idle for |
| 8360 | * a jiffy. |
| 8361 | * the system might be non-idle CPU-wise, but we only care |
| 8362 | * about not overloading the IO subsystem. (things like an |
| 8363 | * e2fsck being done on the RAID array should execute fast) |
| 8364 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8365 | cond_resched(); |
| 8366 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8367 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 8368 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8369 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8370 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 8371 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8372 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8373 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8374 | goto repeat; |
| 8375 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8376 | if (!is_mddev_idle(mddev, 0)) { |
| 8377 | /* |
| 8378 | * Give other IO more of a chance. |
| 8379 | * The faster the devices, the less we wait. |
| 8380 | */ |
| 8381 | wait_event(mddev->recovery_wait, |
| 8382 | !atomic_read(&mddev->recovery_active)); |
| 8383 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8384 | } |
| 8385 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8386 | pr_info("md: %s: %s %s.\n",mdname(mddev), desc, |
| 8387 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 8388 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8389 | /* |
| 8390 | * this also signals 'finished resyncing' to md_stop |
| 8391 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8392 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8393 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 8394 | |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8395 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8396 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 8397 | mddev->curr_resync > 3) { |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8398 | mddev->curr_resync_completed = mddev->curr_resync; |
| 8399 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 8400 | } |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8401 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8402 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8403 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 8404 | mddev->curr_resync > 3) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8405 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 8406 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8407 | if (mddev->curr_resync >= mddev->recovery_cp) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8408 | pr_debug("md: checkpointing %s of %s.\n", |
| 8409 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8410 | if (test_bit(MD_RECOVERY_ERROR, |
| 8411 | &mddev->recovery)) |
| 8412 | mddev->recovery_cp = |
| 8413 | mddev->curr_resync_completed; |
| 8414 | else |
| 8415 | mddev->recovery_cp = |
| 8416 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8417 | } |
| 8418 | } else |
| 8419 | mddev->recovery_cp = MaxSector; |
| 8420 | } else { |
| 8421 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8422 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8423 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8424 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8425 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 8426 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8427 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8428 | !test_bit(Faulty, &rdev->flags) && |
| 8429 | !test_bit(In_sync, &rdev->flags) && |
| 8430 | rdev->recovery_offset < mddev->curr_resync) |
| 8431 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8432 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8434 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 8435 | skip: |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 8436 | /* set CHANGE_PENDING here since maybe another update is needed, |
| 8437 | * so other nodes are informed. It should be harmless for normal |
| 8438 | * raid */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8439 | set_mask_bits(&mddev->sb_flags, 0, |
| 8440 | BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS)); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8441 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8442 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 8443 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8444 | /* We completed so min/max setting can be forgotten if used. */ |
| 8445 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8446 | mddev->resync_min = 0; |
| 8447 | mddev->resync_max = MaxSector; |
| 8448 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8449 | mddev->resync_min = mddev->curr_resync_completed; |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8450 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8451 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8452 | spin_unlock(&mddev->lock); |
| 8453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8454 | wake_up(&resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8455 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 8456 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8457 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8458 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8459 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8460 | static int remove_and_add_spares(struct mddev *mddev, |
| 8461 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8462 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8463 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8464 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8465 | int removed = 0; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8466 | bool remove_some = false; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8467 | |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8468 | rdev_for_each(rdev, mddev) { |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8469 | if ((this == NULL || rdev == this) && |
| 8470 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8471 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8472 | test_bit(Faulty, &rdev->flags) && |
| 8473 | atomic_read(&rdev->nr_pending)==0) { |
| 8474 | /* Faulty non-Blocked devices with nr_pending == 0 |
| 8475 | * never get nr_pending incremented, |
| 8476 | * never get Faulty cleared, and never get Blocked set. |
| 8477 | * So we can synchronize_rcu now rather than once per device |
| 8478 | */ |
| 8479 | remove_some = true; |
| 8480 | set_bit(RemoveSynchronized, &rdev->flags); |
| 8481 | } |
| 8482 | } |
| 8483 | |
| 8484 | if (remove_some) |
| 8485 | synchronize_rcu(); |
| 8486 | rdev_for_each(rdev, mddev) { |
| 8487 | if ((this == NULL || rdev == this) && |
| 8488 | rdev->raid_disk >= 0 && |
| 8489 | !test_bit(Blocked, &rdev->flags) && |
| 8490 | ((test_bit(RemoveSynchronized, &rdev->flags) || |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8491 | (!test_bit(In_sync, &rdev->flags) && |
| 8492 | !test_bit(Journal, &rdev->flags))) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8493 | atomic_read(&rdev->nr_pending)==0)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8494 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8495 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 8496 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8497 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8498 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8499 | } |
| 8500 | } |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8501 | if (remove_some && test_bit(RemoveSynchronized, &rdev->flags)) |
| 8502 | clear_bit(RemoveSynchronized, &rdev->flags); |
| 8503 | } |
| 8504 | |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 8505 | if (removed && mddev->kobj.sd) |
| 8506 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8507 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8508 | if (this && removed) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8509 | goto no_add; |
| 8510 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8511 | rdev_for_each(rdev, mddev) { |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8512 | if (this && this != rdev) |
| 8513 | continue; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 8514 | if (test_bit(Candidate, &rdev->flags)) |
| 8515 | continue; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8516 | if (rdev->raid_disk >= 0 && |
| 8517 | !test_bit(In_sync, &rdev->flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8518 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8519 | !test_bit(Faulty, &rdev->flags)) |
| 8520 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8521 | if (rdev->raid_disk >= 0) |
| 8522 | continue; |
| 8523 | if (test_bit(Faulty, &rdev->flags)) |
| 8524 | continue; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8525 | if (!test_bit(Journal, &rdev->flags)) { |
| 8526 | if (mddev->ro && |
| 8527 | ! (rdev->saved_raid_disk >= 0 && |
| 8528 | !test_bit(Bitmap_sync, &rdev->flags))) |
| 8529 | continue; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8530 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8531 | rdev->recovery_offset = 0; |
| 8532 | } |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8533 | if (mddev->pers-> |
| 8534 | hot_add_disk(mddev, rdev) == 0) { |
| 8535 | if (sysfs_link_rdev(mddev, rdev)) |
| 8536 | /* failure here is OK */; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8537 | if (!test_bit(Journal, &rdev->flags)) |
| 8538 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8539 | md_new_event(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8540 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8541 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8542 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8543 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 8544 | if (removed) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8545 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8546 | return spares; |
| 8547 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 8548 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8549 | static void md_start_sync(struct work_struct *ws) |
| 8550 | { |
| 8551 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8552 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8553 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 8554 | mddev, |
| 8555 | "resync"); |
| 8556 | if (!mddev->sync_thread) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8557 | pr_warn("%s: could not start resync thread...\n", |
| 8558 | mdname(mddev)); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8559 | /* leave the spares where they are, it shouldn't hurt */ |
| 8560 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8561 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8562 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8563 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 8564 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8565 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8566 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8567 | &mddev->recovery)) |
| 8568 | if (mddev->sysfs_action) |
| 8569 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8570 | } else |
| 8571 | md_wakeup_thread(mddev->sync_thread); |
| 8572 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8573 | md_new_event(mddev); |
| 8574 | } |
| 8575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8576 | /* |
| 8577 | * This routine is regularly called by all per-raid-array threads to |
| 8578 | * deal with generic issues like resync and super-block update. |
| 8579 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 8580 | * need this as they never do any recovery or update the superblock. |
| 8581 | * |
| 8582 | * It does not do any resync itself, but rather "forks" off other threads |
| 8583 | * to do that as needed. |
| 8584 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 8585 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8586 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8587 | * and wakeups up this thread which will reap the thread and finish up. |
| 8588 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 8589 | * |
| 8590 | * The overall approach is: |
| 8591 | * 1/ if the superblock needs updating, update it. |
| 8592 | * 2/ If a recovery thread is running, don't do anything else. |
| 8593 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 8594 | * 4/ If there are any faulty devices, remove them. |
| 8595 | * 5/ If array is degraded, try to add spares devices |
| 8596 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 8597 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8598 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8599 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 8600 | if (mddev->suspended) |
| 8601 | return; |
| 8602 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 8603 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 8604 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8605 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8606 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8607 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8608 | pr_debug("md: %s in immediate safe mode\n", |
| 8609 | mdname(mddev)); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8610 | mddev->safemode = 2; |
| 8611 | } |
| 8612 | flush_signals(current); |
| 8613 | } |
| 8614 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8615 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 8616 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8617 | if ( ! ( |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8618 | (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8619 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8620 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8621 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8622 | (mddev->safemode == 2 |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8623 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8624 | )) |
| 8625 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8626 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 8627 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8628 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8629 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8630 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 8631 | struct md_rdev *rdev; |
| 8632 | if (!mddev->external && mddev->in_sync) |
| 8633 | /* 'Blocked' flag not needed as failed devices |
| 8634 | * will be recorded if array switched to read/write. |
| 8635 | * Leaving it set will prevent the device |
| 8636 | * from being removed. |
| 8637 | */ |
| 8638 | rdev_for_each(rdev, mddev) |
| 8639 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8640 | /* On a read-only array we can: |
| 8641 | * - remove failed devices |
| 8642 | * - add already-in_sync devices if the array itself |
| 8643 | * is in-sync. |
| 8644 | * As we only add devices that are already in-sync, |
| 8645 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8646 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8647 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8648 | /* There is no thread, but we need to call |
| 8649 | * ->spare_active and clear saved_raid_disk |
| 8650 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 8651 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8652 | md_reap_sync_thread(mddev); |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 8653 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8654 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8655 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8656 | goto unlock; |
| 8657 | } |
| 8658 | |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 8659 | if (mddev_is_clustered(mddev)) { |
| 8660 | struct md_rdev *rdev; |
| 8661 | /* kick the device if another node issued a |
| 8662 | * remove disk. |
| 8663 | */ |
| 8664 | rdev_for_each(rdev, mddev) { |
| 8665 | if (test_and_clear_bit(ClusterRemove, &rdev->flags) && |
| 8666 | rdev->raid_disk < 0) |
| 8667 | md_kick_rdev_from_array(rdev); |
| 8668 | } |
| 8669 | } |
| 8670 | |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8671 | if (!mddev->external && !mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8672 | spin_lock(&mddev->lock); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8673 | set_in_sync(mddev); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8674 | spin_unlock(&mddev->lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8675 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8676 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8677 | if (mddev->sb_flags) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 8678 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8680 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 8681 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 8682 | /* resync/recovery still happening */ |
| 8683 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8684 | goto unlock; |
| 8685 | } |
| 8686 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8687 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8688 | goto unlock; |
| 8689 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8690 | /* Set RUNNING before clearing NEEDED to avoid |
| 8691 | * any transients in the value of "sync_action". |
| 8692 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8693 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8694 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8695 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8696 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8697 | /* Clear some bits that don't mean anything, but |
| 8698 | * might be left set |
| 8699 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8700 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 8701 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8702 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 8703 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 8704 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8705 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8706 | /* no recovery is running. |
| 8707 | * remove any failed drives, then |
| 8708 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8709 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8710 | * the personality to fail the re-add. |
| 8711 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8712 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8713 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8714 | if (mddev->pers->check_reshape == NULL || |
| 8715 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8716 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8717 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8718 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8719 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8720 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8721 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8722 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 8723 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8724 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8725 | } else if (mddev->recovery_cp < MaxSector) { |
| 8726 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8727 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8728 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 8729 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8730 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8732 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 8733 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 8734 | /* We are adding a device or devices to an array |
| 8735 | * which has the bitmap stored on all devices. |
| 8736 | * So make sure all bitmap pages get written |
| 8737 | */ |
| 8738 | bitmap_write_all(mddev->bitmap); |
| 8739 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8740 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 8741 | queue_work(md_misc_wq, &mddev->del_work); |
| 8742 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8743 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8744 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8745 | if (!mddev->sync_thread) { |
| 8746 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8747 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8748 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8749 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 8750 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8751 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8752 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8753 | unlock: |
| 8754 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8755 | mddev_unlock(mddev); |
| 8756 | } |
| 8757 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8758 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8759 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8760 | void md_reap_sync_thread(struct mddev *mddev) |
| 8761 | { |
| 8762 | struct md_rdev *rdev; |
| 8763 | |
| 8764 | /* resync has finished, collect result */ |
| 8765 | md_unregister_thread(&mddev->sync_thread); |
| 8766 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8767 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 8768 | /* success...*/ |
| 8769 | /* activate any spares */ |
| 8770 | if (mddev->pers->spare_active(mddev)) { |
| 8771 | sysfs_notify(&mddev->kobj, NULL, |
| 8772 | "degraded"); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8773 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8774 | } |
| 8775 | } |
| 8776 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8777 | mddev->pers->finish_reshape) |
| 8778 | mddev->pers->finish_reshape(mddev); |
| 8779 | |
| 8780 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8781 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8782 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8783 | if (!mddev->degraded) |
| 8784 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8785 | rdev->saved_raid_disk = -1; |
| 8786 | |
| 8787 | md_update_sb(mddev, 1); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8788 | /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 8789 | * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by |
| 8790 | * clustered raid */ |
| 8791 | if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags)) |
| 8792 | md_cluster_ops->resync_finish(mddev); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8793 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 8794 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8795 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8796 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8797 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8798 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8799 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8800 | /* flag recovery needed just to double check */ |
| 8801 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8802 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8803 | md_new_event(mddev); |
| 8804 | if (mddev->event_work.func) |
| 8805 | queue_work(md_misc_wq, &mddev->event_work); |
| 8806 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8807 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8808 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8809 | void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8810 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8811 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8812 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8813 | !test_bit(Blocked, &rdev->flags) && |
| 8814 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8815 | msecs_to_jiffies(5000)); |
| 8816 | rdev_dec_pending(rdev, mddev); |
| 8817 | } |
| 8818 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 8819 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8820 | void md_finish_reshape(struct mddev *mddev) |
| 8821 | { |
| 8822 | /* called be personality module when reshape completes. */ |
| 8823 | struct md_rdev *rdev; |
| 8824 | |
| 8825 | rdev_for_each(rdev, mddev) { |
| 8826 | if (rdev->data_offset > rdev->new_data_offset) |
| 8827 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 8828 | else |
| 8829 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 8830 | rdev->data_offset = rdev->new_data_offset; |
| 8831 | } |
| 8832 | } |
| 8833 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8834 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8835 | /* Bad block management */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8836 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8837 | /* Returns 1 on success, 0 on failure */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8838 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8839 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8840 | { |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 8841 | struct mddev *mddev = rdev->mddev; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8842 | int rv; |
| 8843 | if (is_new) |
| 8844 | s += rdev->new_data_offset; |
| 8845 | else |
| 8846 | s += rdev->data_offset; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8847 | rv = badblocks_set(&rdev->badblocks, s, sectors, 0); |
| 8848 | if (rv == 0) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8849 | /* Make sure they get written out promptly */ |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 8850 | if (test_bit(ExternalBbl, &rdev->flags)) |
| 8851 | sysfs_notify(&rdev->kobj, NULL, |
| 8852 | "unacknowledged_bad_blocks"); |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8853 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8854 | set_mask_bits(&mddev->sb_flags, 0, |
| 8855 | BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8856 | md_wakeup_thread(rdev->mddev->thread); |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8857 | return 1; |
| 8858 | } else |
| 8859 | return 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8860 | } |
| 8861 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8862 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8863 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8864 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8865 | { |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 8866 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8867 | if (is_new) |
| 8868 | s += rdev->new_data_offset; |
| 8869 | else |
| 8870 | s += rdev->data_offset; |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 8871 | rv = badblocks_clear(&rdev->badblocks, s, sectors); |
| 8872 | if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags)) |
| 8873 | sysfs_notify(&rdev->kobj, NULL, "bad_blocks"); |
| 8874 | return rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8875 | } |
| 8876 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8877 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8878 | static int md_notify_reboot(struct notifier_block *this, |
| 8879 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8880 | { |
| 8881 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8882 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8883 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8884 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8885 | for_each_mddev(mddev, tmp) { |
| 8886 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8887 | if (mddev->pers) |
| 8888 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 8889 | if (mddev->persistent) |
| 8890 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8891 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8892 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8893 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8894 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8895 | /* |
| 8896 | * certain more exotic SCSI devices are known to be |
| 8897 | * volatile wrt too early system reboots. While the |
| 8898 | * right place to handle this issue is the given |
| 8899 | * driver, we do want to have a safe RAID driver ... |
| 8900 | */ |
| 8901 | if (need_delay) |
| 8902 | mdelay(1000*1); |
| 8903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8904 | return NOTIFY_DONE; |
| 8905 | } |
| 8906 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8907 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8908 | .notifier_call = md_notify_reboot, |
| 8909 | .next = NULL, |
| 8910 | .priority = INT_MAX, /* before any real devices */ |
| 8911 | }; |
| 8912 | |
| 8913 | static void md_geninit(void) |
| 8914 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8915 | pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8916 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8917 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8918 | } |
| 8919 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8920 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8921 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8922 | int ret = -ENOMEM; |
| 8923 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8924 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8925 | if (!md_wq) |
| 8926 | goto err_wq; |
| 8927 | |
| 8928 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8929 | if (!md_misc_wq) |
| 8930 | goto err_misc_wq; |
| 8931 | |
| 8932 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8933 | goto err_md; |
| 8934 | |
| 8935 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8936 | goto err_mdp; |
| 8937 | mdp_major = ret; |
| 8938 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8939 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8940 | md_probe, NULL, NULL); |
| 8941 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8942 | md_probe, NULL, NULL); |
| 8943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8944 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8945 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8946 | |
| 8947 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8948 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8949 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8950 | err_mdp: |
| 8951 | unregister_blkdev(MD_MAJOR, "md"); |
| 8952 | err_md: |
| 8953 | destroy_workqueue(md_misc_wq); |
| 8954 | err_misc_wq: |
| 8955 | destroy_workqueue(md_wq); |
| 8956 | err_wq: |
| 8957 | return ret; |
| 8958 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8959 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8960 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8961 | { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8962 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 8963 | struct md_rdev *rdev2; |
| 8964 | int role, ret; |
| 8965 | char b[BDEVNAME_SIZE]; |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8966 | |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 8967 | /* |
| 8968 | * If size is changed in another node then we need to |
| 8969 | * do resize as well. |
| 8970 | */ |
| 8971 | if (mddev->dev_sectors != le64_to_cpu(sb->size)) { |
| 8972 | ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size)); |
| 8973 | if (ret) |
| 8974 | pr_info("md-cluster: resize failed\n"); |
| 8975 | else |
| 8976 | bitmap_update_sb(mddev->bitmap); |
| 8977 | } |
| 8978 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8979 | /* Check for change of roles in the active devices */ |
| 8980 | rdev_for_each(rdev2, mddev) { |
| 8981 | if (test_bit(Faulty, &rdev2->flags)) |
| 8982 | continue; |
| 8983 | |
| 8984 | /* Check if the roles changed */ |
| 8985 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 8986 | |
| 8987 | if (test_bit(Candidate, &rdev2->flags)) { |
| 8988 | if (role == 0xfffe) { |
| 8989 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); |
| 8990 | md_kick_rdev_from_array(rdev2); |
| 8991 | continue; |
| 8992 | } |
| 8993 | else |
| 8994 | clear_bit(Candidate, &rdev2->flags); |
| 8995 | } |
| 8996 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8997 | if (role != rdev2->raid_disk) { |
| 8998 | /* got activated */ |
| 8999 | if (rdev2->raid_disk == -1 && role != 0xffff) { |
| 9000 | rdev2->saved_raid_disk = role; |
| 9001 | ret = remove_and_add_spares(mddev, rdev2); |
| 9002 | pr_info("Activated spare: %s\n", |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9003 | bdevname(rdev2->bdev,b)); |
Guoqing Jiang | a578183 | 2016-05-02 11:33:14 -0400 | [diff] [blame] | 9004 | /* wakeup mddev->thread here, so array could |
| 9005 | * perform resync with the new activated disk */ |
| 9006 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9007 | md_wakeup_thread(mddev->thread); |
| 9008 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9009 | } |
| 9010 | /* device faulty |
| 9011 | * We just want to do the minimum to mark the disk |
| 9012 | * as faulty. The recovery is performed by the |
| 9013 | * one who initiated the error. |
| 9014 | */ |
| 9015 | if ((role == 0xfffe) || (role == 0xfffd)) { |
| 9016 | md_error(mddev, rdev2); |
| 9017 | clear_bit(Blocked, &rdev2->flags); |
| 9018 | } |
| 9019 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9020 | } |
| 9021 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 9022 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) |
| 9023 | update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9024 | |
| 9025 | /* Finally set the event to be up to date */ |
| 9026 | mddev->events = le64_to_cpu(sb->events); |
| 9027 | } |
| 9028 | |
| 9029 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) |
| 9030 | { |
| 9031 | int err; |
| 9032 | struct page *swapout = rdev->sb_page; |
| 9033 | struct mdp_superblock_1 *sb; |
| 9034 | |
| 9035 | /* Store the sb page of the rdev in the swapout temporary |
| 9036 | * variable in case we err in the future |
| 9037 | */ |
| 9038 | rdev->sb_page = NULL; |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9039 | err = alloc_disk_sb(rdev); |
| 9040 | if (err == 0) { |
| 9041 | ClearPageUptodate(rdev->sb_page); |
| 9042 | rdev->sb_loaded = 0; |
| 9043 | err = super_types[mddev->major_version]. |
| 9044 | load_super(rdev, NULL, mddev->minor_version); |
| 9045 | } |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9046 | if (err < 0) { |
| 9047 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", |
| 9048 | __func__, __LINE__, rdev->desc_nr, err); |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9049 | if (rdev->sb_page) |
| 9050 | put_page(rdev->sb_page); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9051 | rdev->sb_page = swapout; |
| 9052 | rdev->sb_loaded = 1; |
| 9053 | return err; |
| 9054 | } |
| 9055 | |
| 9056 | sb = page_address(rdev->sb_page); |
| 9057 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET |
| 9058 | * is not set |
| 9059 | */ |
| 9060 | |
| 9061 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) |
| 9062 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 9063 | |
| 9064 | /* The other node finished recovery, call spare_active to set |
| 9065 | * device In_sync and mddev->degraded |
| 9066 | */ |
| 9067 | if (rdev->recovery_offset == MaxSector && |
| 9068 | !test_bit(In_sync, &rdev->flags) && |
| 9069 | mddev->pers->spare_active(mddev)) |
| 9070 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
| 9071 | |
| 9072 | put_page(swapout); |
| 9073 | return 0; |
| 9074 | } |
| 9075 | |
| 9076 | void md_reload_sb(struct mddev *mddev, int nr) |
| 9077 | { |
| 9078 | struct md_rdev *rdev; |
| 9079 | int err; |
| 9080 | |
| 9081 | /* Find the rdev */ |
| 9082 | rdev_for_each_rcu(rdev, mddev) { |
| 9083 | if (rdev->desc_nr == nr) |
| 9084 | break; |
| 9085 | } |
| 9086 | |
| 9087 | if (!rdev || rdev->desc_nr != nr) { |
| 9088 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); |
| 9089 | return; |
| 9090 | } |
| 9091 | |
| 9092 | err = read_rdev(mddev, rdev); |
| 9093 | if (err < 0) |
| 9094 | return; |
| 9095 | |
| 9096 | check_sb_changes(mddev, rdev); |
| 9097 | |
| 9098 | /* Read all rdev's to update recovery_offset */ |
| 9099 | rdev_for_each_rcu(rdev, mddev) |
| 9100 | read_rdev(mddev, rdev); |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9101 | } |
| 9102 | EXPORT_SYMBOL(md_reload_sb); |
| 9103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9104 | #ifndef MODULE |
| 9105 | |
| 9106 | /* |
| 9107 | * Searches all registered partitions for autorun RAID arrays |
| 9108 | * at boot time. |
| 9109 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9110 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9111 | static DEFINE_MUTEX(detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9112 | static LIST_HEAD(all_detected_devices); |
| 9113 | struct detected_devices_node { |
| 9114 | struct list_head list; |
| 9115 | dev_t dev; |
| 9116 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9117 | |
| 9118 | void md_autodetect_dev(dev_t dev) |
| 9119 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9120 | struct detected_devices_node *node_detected_dev; |
| 9121 | |
| 9122 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 9123 | if (node_detected_dev) { |
| 9124 | node_detected_dev->dev = dev; |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9125 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9126 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9127 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9129 | } |
| 9130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9131 | static void autostart_arrays(int part) |
| 9132 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9133 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9134 | struct detected_devices_node *node_detected_dev; |
| 9135 | dev_t dev; |
| 9136 | int i_scanned, i_passed; |
| 9137 | |
| 9138 | i_scanned = 0; |
| 9139 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9140 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9141 | pr_info("md: Autodetecting RAID arrays.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9142 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9143 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9144 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 9145 | i_scanned++; |
| 9146 | node_detected_dev = list_entry(all_detected_devices.next, |
| 9147 | struct detected_devices_node, list); |
| 9148 | list_del(&node_detected_dev->list); |
| 9149 | dev = node_detected_dev->dev; |
| 9150 | kfree(node_detected_dev); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 9151 | mutex_unlock(&detected_devices_mutex); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 9152 | rdev = md_import_device(dev,0, 90); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 9153 | mutex_lock(&detected_devices_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9154 | if (IS_ERR(rdev)) |
| 9155 | continue; |
| 9156 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9157 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9158 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9159 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 9160 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9161 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9162 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9163 | } |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9164 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9165 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9166 | pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9167 | |
| 9168 | autorun_devices(part); |
| 9169 | } |
| 9170 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 9171 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9172 | |
| 9173 | static __exit void md_exit(void) |
| 9174 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9175 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9176 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9177 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 9178 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 9179 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 9180 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9181 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 9182 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9183 | unregister_blkdev(mdp_major, "mdp"); |
| 9184 | unregister_reboot_notifier(&md_notifier); |
| 9185 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9186 | |
| 9187 | /* We cannot unload the modules while some process is |
| 9188 | * waiting for us in select() or poll() - wake them up |
| 9189 | */ |
| 9190 | md_unloading = 1; |
| 9191 | while (waitqueue_active(&md_event_waiters)) { |
| 9192 | /* not safe to leave yet */ |
| 9193 | wake_up(&md_event_waiters); |
| 9194 | msleep(delay); |
| 9195 | delay += delay; |
| 9196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9197 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9198 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 9199 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9200 | export_array(mddev); |
NeilBrown | 9356863 | 2017-02-06 13:41:39 +1100 | [diff] [blame] | 9201 | mddev->ctime = 0; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9202 | mddev->hold_active = 0; |
NeilBrown | 9356863 | 2017-02-06 13:41:39 +1100 | [diff] [blame] | 9203 | /* |
| 9204 | * for_each_mddev() will call mddev_put() at the end of each |
| 9205 | * iteration. As the mddev is now fully clear, this will |
| 9206 | * schedule the mddev for destruction by a workqueue, and the |
| 9207 | * destroy_workqueue() below will wait for that to complete. |
| 9208 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9209 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9210 | destroy_workqueue(md_misc_wq); |
| 9211 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9212 | } |
| 9213 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 9214 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9215 | module_exit(md_exit) |
| 9216 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9217 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 9218 | { |
| 9219 | return sprintf(buffer, "%d", start_readonly); |
| 9220 | } |
| 9221 | static int set_ro(const char *val, struct kernel_param *kp) |
| 9222 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 9223 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9224 | } |
| 9225 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 9226 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 9227 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9228 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 9229 | module_param(create_on_open, bool, S_IRUSR|S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9231 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 9232 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 9233 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 9234 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |