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; |
Shaohua Li | 2073773 | 2016-12-13 12:40:15 -0800 | [diff] [blame] | 768 | bio->bi_opf = REQ_OP_WRITE | 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 | aeb9b211 | 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 | aeb9b211 | 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 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2106 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2107 | if (!test_bit(Journal, &rdev->flags) && |
| 2108 | rdev->sectors && |
| 2109 | (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2110 | if (mddev->pers) { |
| 2111 | /* Cannot change size, so fail |
| 2112 | * If mddev->level <= 0, then we don't care |
| 2113 | * about aligning sizes (e.g. linear) |
| 2114 | */ |
| 2115 | if (mddev->level > 0) |
| 2116 | return -ENOSPC; |
| 2117 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2118 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2119 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | |
| 2121 | /* Verify rdev->desc_nr is unique. |
| 2122 | * If it is -1, assign a free number, else |
| 2123 | * check number is not in use |
| 2124 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2125 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | if (rdev->desc_nr < 0) { |
| 2127 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2128 | if (mddev->pers) |
| 2129 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2130 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | choice++; |
| 2132 | rdev->desc_nr = choice; |
| 2133 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2134 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2135 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2137 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2139 | rcu_read_unlock(); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2140 | if (!test_bit(Journal, &rdev->flags) && |
| 2141 | mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2142 | pr_warn("md: %s: array is limited to %d devices\n", |
| 2143 | mdname(mddev), mddev->max_disks); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2144 | return -EBUSY; |
| 2145 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2146 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2147 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | rdev->mddev = mddev; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2150 | pr_debug("md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2151 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2152 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2153 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2154 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2155 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2156 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2157 | /* failure here is OK */; |
| 2158 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2159 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2160 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2161 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2162 | |
| 2163 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2164 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2167 | |
| 2168 | fail: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2169 | pr_warn("md: failed to register dev-%s for %s\n", |
| 2170 | b, mdname(mddev)); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2171 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2174 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2175 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2176 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2177 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2178 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2181 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | { |
| 2183 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2184 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2185 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2186 | list_del_rcu(&rdev->same_set); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2187 | pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2189 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2190 | sysfs_put(rdev->sysfs_state); |
| 2191 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2192 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2193 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2194 | * writing to 'remove' to "dev/state". We also need |
| 2195 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2196 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2197 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2198 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2199 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2200 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | /* |
| 2204 | * prevent the device from being mounted, repartitioned or |
| 2205 | * otherwise reused by a RAID array (or any other kernel |
| 2206 | * subsystem), by bd_claiming the device. |
| 2207 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2208 | 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] | 2209 | { |
| 2210 | int err = 0; |
| 2211 | struct block_device *bdev; |
| 2212 | char b[BDEVNAME_SIZE]; |
| 2213 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2214 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2215 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | if (IS_ERR(bdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2217 | pr_warn("md: could not open %s.\n", __bdevname(dev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | return PTR_ERR(bdev); |
| 2219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | rdev->bdev = bdev; |
| 2221 | return err; |
| 2222 | } |
| 2223 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2224 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | { |
| 2226 | struct block_device *bdev = rdev->bdev; |
| 2227 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2228 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | void md_autodetect_dev(dev_t dev); |
| 2232 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2233 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | { |
| 2235 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2236 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2237 | pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2238 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2240 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2241 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | #endif |
| 2243 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2244 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2247 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | { |
| 2249 | unbind_rdev_from_array(rdev); |
| 2250 | export_rdev(rdev); |
| 2251 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2252 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2254 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2256 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2258 | while (!list_empty(&mddev->disks)) { |
| 2259 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2260 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2261 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | mddev->raid_disks = 0; |
| 2264 | mddev->major_version = 0; |
| 2265 | } |
| 2266 | |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2267 | static bool set_in_sync(struct mddev *mddev) |
| 2268 | { |
| 2269 | WARN_ON_ONCE(!spin_is_locked(&mddev->lock)); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2270 | if (!mddev->in_sync) { |
| 2271 | mddev->sync_checkers++; |
| 2272 | spin_unlock(&mddev->lock); |
| 2273 | percpu_ref_switch_to_atomic_sync(&mddev->writes_pending); |
| 2274 | spin_lock(&mddev->lock); |
| 2275 | if (!mddev->in_sync && |
| 2276 | percpu_ref_is_zero(&mddev->writes_pending)) { |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2277 | mddev->in_sync = 1; |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2278 | /* |
| 2279 | * Ensure ->in_sync is visible before we clear |
| 2280 | * ->sync_checkers. |
| 2281 | */ |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2282 | smp_mb(); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2283 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 2284 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 2285 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2286 | if (--mddev->sync_checkers == 0) |
| 2287 | percpu_ref_switch_to_percpu(&mddev->writes_pending); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2288 | } |
| 2289 | if (mddev->safemode == 1) |
| 2290 | mddev->safemode = 0; |
| 2291 | return mddev->in_sync; |
| 2292 | } |
| 2293 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2294 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2296 | /* Update each superblock (in-memory image), but |
| 2297 | * if we are allowed to, skip spares which already |
| 2298 | * have the right event counter, or have one earlier |
| 2299 | * (which would mean they aren't being marked as dirty |
| 2300 | * with the rest of the array) |
| 2301 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2302 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2303 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2304 | if (rdev->sb_events == mddev->events || |
| 2305 | (nospares && |
| 2306 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2307 | rdev->sb_events+1 == mddev->events)) { |
| 2308 | /* Don't update this superblock */ |
| 2309 | rdev->sb_loaded = 2; |
| 2310 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2311 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2312 | rdev->sb_loaded = 1; |
| 2313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | } |
| 2315 | } |
| 2316 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2317 | static bool does_sb_need_changing(struct mddev *mddev) |
| 2318 | { |
| 2319 | struct md_rdev *rdev; |
| 2320 | struct mdp_superblock_1 *sb; |
| 2321 | int role; |
| 2322 | |
| 2323 | /* Find a good rdev */ |
| 2324 | rdev_for_each(rdev, mddev) |
| 2325 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) |
| 2326 | break; |
| 2327 | |
| 2328 | /* No good device found. */ |
| 2329 | if (!rdev) |
| 2330 | return false; |
| 2331 | |
| 2332 | sb = page_address(rdev->sb_page); |
| 2333 | /* Check if a device has become faulty or a spare become active */ |
| 2334 | rdev_for_each(rdev, mddev) { |
| 2335 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 2336 | /* Device activated? */ |
| 2337 | if (role == 0xffff && rdev->raid_disk >=0 && |
| 2338 | !test_bit(Faulty, &rdev->flags)) |
| 2339 | return true; |
| 2340 | /* Device turned faulty? */ |
| 2341 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) |
| 2342 | return true; |
| 2343 | } |
| 2344 | |
| 2345 | /* Check if any mddev parameters have changed */ |
| 2346 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || |
| 2347 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || |
Jason Yan | 1345921 | 2017-03-10 11:49:12 +0800 | [diff] [blame] | 2348 | (mddev->layout != le32_to_cpu(sb->layout)) || |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2349 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || |
| 2350 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) |
| 2351 | return true; |
| 2352 | |
| 2353 | return false; |
| 2354 | } |
| 2355 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2356 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2358 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2359 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2360 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2361 | int any_badblocks_changed = 0; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2362 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2364 | if (mddev->ro) { |
| 2365 | if (force_change) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2366 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2367 | return; |
| 2368 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2369 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2370 | repeat: |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2371 | if (mddev_is_clustered(mddev)) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2372 | if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2373 | force_change = 1; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2374 | if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags)) |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 2375 | nospares = 1; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2376 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2377 | /* Has someone else has updated the sb */ |
| 2378 | if (!does_sb_need_changing(mddev)) { |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2379 | if (ret == 0) |
| 2380 | md_cluster_ops->metadata_update_cancel(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2381 | bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING), |
| 2382 | BIT(MD_SB_CHANGE_DEVS) | |
| 2383 | BIT(MD_SB_CHANGE_CLEAN)); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2384 | return; |
| 2385 | } |
| 2386 | } |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2387 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2388 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2389 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2390 | if (rdev->raid_disk >= 0 && |
| 2391 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2392 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2393 | !test_bit(In_sync, &rdev->flags) && |
| 2394 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2395 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2396 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2397 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2398 | if (!mddev->persistent) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2399 | clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 2400 | clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2401 | if (!mddev->external) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2402 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2403 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2404 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2405 | rdev->badblocks.changed = 0; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2406 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2407 | md_error(mddev, rdev); |
| 2408 | } |
| 2409 | clear_bit(Blocked, &rdev->flags); |
| 2410 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2411 | wake_up(&rdev->blocked_wait); |
| 2412 | } |
| 2413 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2414 | wake_up(&mddev->sb_wait); |
| 2415 | return; |
| 2416 | } |
| 2417 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2418 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2419 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 2420 | mddev->utime = ktime_get_real_seconds(); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2421 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2422 | if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2423 | force_change = 1; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2424 | if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags)) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2425 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2426 | * though if events isn't the right even/odd, we will have to do |
| 2427 | * spares after all |
| 2428 | */ |
| 2429 | nospares = 1; |
| 2430 | if (force_change) |
| 2431 | nospares = 0; |
| 2432 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2433 | /* If the array is degraded, then skipping spares is both |
| 2434 | * dangerous and fairly pointless. |
| 2435 | * Dangerous because a device that was removed from the array |
| 2436 | * might have a event_count that still looks up-to-date, |
| 2437 | * so it can be re-added without a resync. |
| 2438 | * Pointless because if there are any spares to skip, |
| 2439 | * then a recovery will happen and soon that array won't |
| 2440 | * be degraded any more and the spare can go back to sleep then. |
| 2441 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2442 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2443 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2444 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2445 | |
| 2446 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2447 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2448 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2449 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2450 | && mddev->can_decrease_events |
| 2451 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2452 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2453 | mddev->can_decrease_events = 0; |
| 2454 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2455 | /* otherwise we have to go forward and ... */ |
| 2456 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2457 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2458 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2460 | /* |
| 2461 | * This 64-bit counter should never wrap. |
| 2462 | * Either we are in around ~1 trillion A.C., assuming |
| 2463 | * 1 reboot per second, or we have a bug... |
| 2464 | */ |
| 2465 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2466 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2467 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2468 | if (rdev->badblocks.changed) |
| 2469 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2470 | if (test_bit(Faulty, &rdev->flags)) |
| 2471 | set_bit(FaultRecorded, &rdev->flags); |
| 2472 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2473 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2474 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2475 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2477 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2478 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | |
Shaohua Li | 504634f | 2016-11-18 09:44:08 -0800 | [diff] [blame] | 2480 | if (mddev->queue) |
| 2481 | blk_add_trace_msg(mddev->queue, "md md_update_sb"); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2482 | rewrite: |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2483 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2484 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2486 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2487 | if (rdev->sb_loaded != 1) |
| 2488 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2490 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2491 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2492 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2493 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2494 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2495 | bdevname(rdev->bdev, b), |
| 2496 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2497 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2498 | if (rdev->badblocks.size) { |
| 2499 | md_super_write(mddev, rdev, |
| 2500 | rdev->badblocks.sector, |
| 2501 | rdev->badblocks.size << 9, |
| 2502 | rdev->bb_page); |
| 2503 | rdev->badblocks.size = 0; |
| 2504 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2505 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2506 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2507 | pr_debug("md: %s (skipping faulty)\n", |
| 2508 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2509 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2510 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | /* only need to write one superblock... */ |
| 2512 | break; |
| 2513 | } |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2514 | if (md_super_wait(mddev) < 0) |
| 2515 | goto rewrite; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2516 | /* 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] | 2517 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2518 | if (mddev_is_clustered(mddev) && ret == 0) |
| 2519 | md_cluster_ops->metadata_update_finish(mddev); |
| 2520 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2521 | if (mddev->in_sync != sync_req || |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2522 | !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING), |
| 2523 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN))) |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2524 | /* have to write it out again */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2525 | goto repeat; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2526 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2527 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2528 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2529 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2530 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2531 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2532 | clear_bit(Blocked, &rdev->flags); |
| 2533 | |
| 2534 | if (any_badblocks_changed) |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2535 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2536 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2537 | wake_up(&rdev->blocked_wait); |
| 2538 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2539 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2540 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2542 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2543 | { |
| 2544 | struct mddev *mddev = rdev->mddev; |
| 2545 | int err = 0; |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2546 | bool add_journal = test_bit(Journal, &rdev->flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2547 | |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2548 | if (!mddev->pers->hot_remove_disk || add_journal) { |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2549 | /* If there is hot_add_disk but no hot_remove_disk |
| 2550 | * then added disks for geometry changes, |
| 2551 | * and should be added immediately. |
| 2552 | */ |
| 2553 | super_types[mddev->major_version]. |
| 2554 | validate_super(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2555 | if (add_journal) |
| 2556 | mddev_suspend(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2557 | err = mddev->pers->hot_add_disk(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2558 | if (add_journal) |
| 2559 | mddev_resume(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2560 | if (err) { |
Guoqing Jiang | db76767 | 2016-06-02 23:32:05 -0400 | [diff] [blame] | 2561 | md_kick_rdev_from_array(rdev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2562 | return err; |
| 2563 | } |
| 2564 | } |
| 2565 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2566 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2567 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2568 | if (mddev->degraded) |
| 2569 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2570 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2571 | md_new_event(mddev); |
| 2572 | md_wakeup_thread(mddev->thread); |
| 2573 | return 0; |
| 2574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2576 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2577 | * We want to accept with case. For this we use cmd_match. |
| 2578 | */ |
| 2579 | static int cmd_match(const char *cmd, const char *str) |
| 2580 | { |
| 2581 | /* See if cmd, written into a sysfs file, matches |
| 2582 | * str. They must either be the same, or cmd can |
| 2583 | * have a trailing newline |
| 2584 | */ |
| 2585 | while (*cmd && *str && *cmd == *str) { |
| 2586 | cmd++; |
| 2587 | str++; |
| 2588 | } |
| 2589 | if (*cmd == '\n') |
| 2590 | cmd++; |
| 2591 | if (*str || *cmd) |
| 2592 | return 0; |
| 2593 | return 1; |
| 2594 | } |
| 2595 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2596 | struct rdev_sysfs_entry { |
| 2597 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2598 | ssize_t (*show)(struct md_rdev *, char *); |
| 2599 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2600 | }; |
| 2601 | |
| 2602 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2603 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2604 | { |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2605 | char *sep = ","; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2606 | size_t len = 0; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2607 | unsigned long flags = ACCESS_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2608 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2609 | if (test_bit(Faulty, &flags) || |
Tomasz Majchrzak | dcbcb48 | 2016-10-21 16:27:08 +0200 | [diff] [blame] | 2610 | (!test_bit(ExternalBbl, &flags) && |
| 2611 | rdev->badblocks.unacked_exist)) |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2612 | len += sprintf(page+len, "faulty%s", sep); |
| 2613 | if (test_bit(In_sync, &flags)) |
| 2614 | len += sprintf(page+len, "in_sync%s", sep); |
| 2615 | if (test_bit(Journal, &flags)) |
| 2616 | len += sprintf(page+len, "journal%s", sep); |
| 2617 | if (test_bit(WriteMostly, &flags)) |
| 2618 | len += sprintf(page+len, "write_mostly%s", sep); |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2619 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2620 | (rdev->badblocks.unacked_exist |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2621 | && !test_bit(Faulty, &flags))) |
| 2622 | len += sprintf(page+len, "blocked%s", sep); |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2623 | if (!test_bit(Faulty, &flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2624 | !test_bit(Journal, &flags) && |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2625 | !test_bit(In_sync, &flags)) |
| 2626 | len += sprintf(page+len, "spare%s", sep); |
| 2627 | if (test_bit(WriteErrorSeen, &flags)) |
| 2628 | len += sprintf(page+len, "write_error%s", sep); |
| 2629 | if (test_bit(WantReplacement, &flags)) |
| 2630 | len += sprintf(page+len, "want_replacement%s", sep); |
| 2631 | if (test_bit(Replacement, &flags)) |
| 2632 | len += sprintf(page+len, "replacement%s", sep); |
| 2633 | if (test_bit(ExternalBbl, &flags)) |
| 2634 | len += sprintf(page+len, "external_bbl%s", sep); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2635 | if (test_bit(FailFast, &flags)) |
| 2636 | len += sprintf(page+len, "failfast%s", sep); |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2637 | |
| 2638 | if (len) |
| 2639 | len -= strlen(sep); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2640 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2641 | return len+sprintf(page+len, "\n"); |
| 2642 | } |
| 2643 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2644 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2645 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2646 | { |
| 2647 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2648 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2649 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2650 | * writemostly - sets write_mostly |
| 2651 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2652 | * blocked - sets the Blocked flags |
| 2653 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2654 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2655 | * -insync - clear Insync for a device with a slot assigned, |
| 2656 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2657 | * write_error - sets WriteErrorSeen |
| 2658 | * -write_error - clears WriteErrorSeen |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2659 | * {,-}failfast - set/clear FailFast |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2660 | */ |
| 2661 | int err = -EINVAL; |
| 2662 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2663 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2664 | if (test_bit(Faulty, &rdev->flags)) |
| 2665 | err = 0; |
| 2666 | else |
| 2667 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2668 | } else if (cmd_match(buf, "remove")) { |
Shaohua Li | 5d88178 | 2016-07-28 09:06:34 -0700 | [diff] [blame] | 2669 | if (rdev->mddev->pers) { |
| 2670 | clear_bit(Blocked, &rdev->flags); |
| 2671 | remove_and_add_spares(rdev->mddev, rdev); |
| 2672 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2673 | if (rdev->raid_disk >= 0) |
| 2674 | err = -EBUSY; |
| 2675 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2676 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2677 | err = 0; |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 2678 | if (mddev_is_clustered(mddev)) |
| 2679 | err = md_cluster_ops->remove_disk(mddev, rdev); |
| 2680 | |
| 2681 | if (err == 0) { |
| 2682 | md_kick_rdev_from_array(rdev); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 2683 | if (mddev->pers) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2684 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 2685 | md_wakeup_thread(mddev->thread); |
| 2686 | } |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 2687 | md_new_event(mddev); |
| 2688 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2689 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2690 | } else if (cmd_match(buf, "writemostly")) { |
| 2691 | set_bit(WriteMostly, &rdev->flags); |
| 2692 | err = 0; |
| 2693 | } else if (cmd_match(buf, "-writemostly")) { |
| 2694 | clear_bit(WriteMostly, &rdev->flags); |
| 2695 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2696 | } else if (cmd_match(buf, "blocked")) { |
| 2697 | set_bit(Blocked, &rdev->flags); |
| 2698 | err = 0; |
| 2699 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2700 | if (!test_bit(Faulty, &rdev->flags) && |
Tomasz Majchrzak | dcbcb48 | 2016-10-21 16:27:08 +0200 | [diff] [blame] | 2701 | !test_bit(ExternalBbl, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2702 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2703 | /* metadata handler doesn't understand badblocks, |
| 2704 | * so we need to fail the device |
| 2705 | */ |
| 2706 | md_error(rdev->mddev, rdev); |
| 2707 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2708 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2709 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2710 | wake_up(&rdev->blocked_wait); |
| 2711 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2712 | md_wakeup_thread(rdev->mddev->thread); |
| 2713 | |
| 2714 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2715 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2716 | set_bit(In_sync, &rdev->flags); |
| 2717 | err = 0; |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2718 | } else if (cmd_match(buf, "failfast")) { |
| 2719 | set_bit(FailFast, &rdev->flags); |
| 2720 | err = 0; |
| 2721 | } else if (cmd_match(buf, "-failfast")) { |
| 2722 | clear_bit(FailFast, &rdev->flags); |
| 2723 | err = 0; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2724 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 && |
| 2725 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 2726 | if (rdev->mddev->pers == NULL) { |
| 2727 | clear_bit(In_sync, &rdev->flags); |
| 2728 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2729 | rdev->raid_disk = -1; |
| 2730 | err = 0; |
| 2731 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2732 | } else if (cmd_match(buf, "write_error")) { |
| 2733 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2734 | err = 0; |
| 2735 | } else if (cmd_match(buf, "-write_error")) { |
| 2736 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2737 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2738 | } else if (cmd_match(buf, "want_replacement")) { |
| 2739 | /* Any non-spare device that is not a replacement can |
| 2740 | * become want_replacement at any time, but we then need to |
| 2741 | * check if recovery is needed. |
| 2742 | */ |
| 2743 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2744 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2745 | !test_bit(Replacement, &rdev->flags)) |
| 2746 | set_bit(WantReplacement, &rdev->flags); |
| 2747 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2748 | md_wakeup_thread(rdev->mddev->thread); |
| 2749 | err = 0; |
| 2750 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2751 | /* Clearing 'want_replacement' is always allowed. |
| 2752 | * Once replacements starts it is too late though. |
| 2753 | */ |
| 2754 | err = 0; |
| 2755 | clear_bit(WantReplacement, &rdev->flags); |
| 2756 | } else if (cmd_match(buf, "replacement")) { |
| 2757 | /* Can only set a device as a replacement when array has not |
| 2758 | * yet been started. Once running, replacement is automatic |
| 2759 | * from spares, or by assigning 'slot'. |
| 2760 | */ |
| 2761 | if (rdev->mddev->pers) |
| 2762 | err = -EBUSY; |
| 2763 | else { |
| 2764 | set_bit(Replacement, &rdev->flags); |
| 2765 | err = 0; |
| 2766 | } |
| 2767 | } else if (cmd_match(buf, "-replacement")) { |
| 2768 | /* Similarly, can only clear Replacement before start */ |
| 2769 | if (rdev->mddev->pers) |
| 2770 | err = -EBUSY; |
| 2771 | else { |
| 2772 | clear_bit(Replacement, &rdev->flags); |
| 2773 | err = 0; |
| 2774 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2775 | } else if (cmd_match(buf, "re-add")) { |
| 2776 | if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 2777 | /* clear_bit is performed _after_ all the devices |
| 2778 | * have their local Faulty bit cleared. If any writes |
| 2779 | * happen in the meantime in the local node, they |
| 2780 | * will land in the local bitmap, which will be synced |
| 2781 | * by this node eventually |
| 2782 | */ |
| 2783 | if (!mddev_is_clustered(rdev->mddev) || |
| 2784 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 2785 | clear_bit(Faulty, &rdev->flags); |
| 2786 | err = add_bound_rdev(rdev); |
| 2787 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2788 | } else |
| 2789 | err = -EBUSY; |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2790 | } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) { |
| 2791 | set_bit(ExternalBbl, &rdev->flags); |
| 2792 | rdev->badblocks.shift = 0; |
| 2793 | err = 0; |
| 2794 | } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) { |
| 2795 | clear_bit(ExternalBbl, &rdev->flags); |
| 2796 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2797 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2798 | if (!err) |
| 2799 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2800 | return err ? err : len; |
| 2801 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2802 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 2803 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2804 | |
| 2805 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2806 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2807 | { |
| 2808 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2809 | } |
| 2810 | |
| 2811 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2812 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2813 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2814 | unsigned int n; |
| 2815 | int rv; |
| 2816 | |
| 2817 | rv = kstrtouint(buf, 10, &n); |
| 2818 | if (rv < 0) |
| 2819 | return rv; |
| 2820 | atomic_set(&rdev->corrected_errors, n); |
| 2821 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2822 | } |
| 2823 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2824 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2825 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2826 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2827 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2828 | { |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2829 | if (test_bit(Journal, &rdev->flags)) |
| 2830 | return sprintf(page, "journal\n"); |
| 2831 | else if (rdev->raid_disk < 0) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2832 | return sprintf(page, "none\n"); |
| 2833 | else |
| 2834 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2835 | } |
| 2836 | |
| 2837 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2838 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2839 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2840 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2841 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2842 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2843 | if (test_bit(Journal, &rdev->flags)) |
| 2844 | return -EBUSY; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2845 | if (strncmp(buf, "none", 4)==0) |
| 2846 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2847 | else { |
| 2848 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 2849 | if (err < 0) |
| 2850 | return err; |
| 2851 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2852 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2853 | /* Setting 'slot' on an active array requires also |
| 2854 | * updating the 'rd%d' link, and communicating |
| 2855 | * with the personality with ->hot_*_disk. |
| 2856 | * For now we only support removing |
| 2857 | * failed/spare devices. This normally happens automatically, |
| 2858 | * but not when the metadata is externally managed. |
| 2859 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2860 | if (rdev->raid_disk == -1) |
| 2861 | return -EEXIST; |
| 2862 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2863 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2864 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2865 | clear_bit(Blocked, &rdev->flags); |
| 2866 | remove_and_add_spares(rdev->mddev, rdev); |
| 2867 | if (rdev->raid_disk >= 0) |
| 2868 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2869 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2870 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2871 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2872 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2873 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2874 | */ |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2875 | int err; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2876 | |
| 2877 | if (rdev->raid_disk != -1) |
| 2878 | return -EBUSY; |
| 2879 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2880 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2881 | return -EBUSY; |
| 2882 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2883 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2884 | return -EINVAL; |
| 2885 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2886 | if (slot >= rdev->mddev->raid_disks && |
| 2887 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2888 | return -ENOSPC; |
| 2889 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2890 | rdev->raid_disk = slot; |
| 2891 | if (test_bit(In_sync, &rdev->flags)) |
| 2892 | rdev->saved_raid_disk = slot; |
| 2893 | else |
| 2894 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2895 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 2896 | clear_bit(Bitmap_sync, &rdev->flags); |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2897 | err = rdev->mddev->pers-> |
| 2898 | hot_add_disk(rdev->mddev, rdev); |
| 2899 | if (err) { |
| 2900 | rdev->raid_disk = -1; |
| 2901 | return err; |
| 2902 | } else |
| 2903 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2904 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
| 2905 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2906 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2907 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2908 | if (slot >= rdev->mddev->raid_disks && |
| 2909 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2910 | return -ENOSPC; |
| 2911 | rdev->raid_disk = slot; |
| 2912 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2913 | clear_bit(Faulty, &rdev->flags); |
| 2914 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2915 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2916 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2917 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2918 | return len; |
| 2919 | } |
| 2920 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2921 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2922 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2923 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2924 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2925 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2926 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2927 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2931 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2932 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2933 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2934 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2935 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2936 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2937 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2938 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2939 | /* Must set offset before size, so overlap checks |
| 2940 | * can be sane */ |
| 2941 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2942 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2943 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2944 | return len; |
| 2945 | } |
| 2946 | |
| 2947 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2948 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2949 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2950 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2951 | { |
| 2952 | return sprintf(page, "%llu\n", |
| 2953 | (unsigned long long)rdev->new_data_offset); |
| 2954 | } |
| 2955 | |
| 2956 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2957 | const char *buf, size_t len) |
| 2958 | { |
| 2959 | unsigned long long new_offset; |
| 2960 | struct mddev *mddev = rdev->mddev; |
| 2961 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2962 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2963 | return -EINVAL; |
| 2964 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 2965 | if (mddev->sync_thread || |
| 2966 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2967 | return -EBUSY; |
| 2968 | if (new_offset == rdev->data_offset) |
| 2969 | /* reset is always permitted */ |
| 2970 | ; |
| 2971 | else if (new_offset > rdev->data_offset) { |
| 2972 | /* must not push array size beyond rdev_sectors */ |
| 2973 | if (new_offset - rdev->data_offset |
| 2974 | + mddev->dev_sectors > rdev->sectors) |
| 2975 | return -E2BIG; |
| 2976 | } |
| 2977 | /* Metadata worries about other space details. */ |
| 2978 | |
| 2979 | /* decreasing the offset is inconsistent with a backwards |
| 2980 | * reshape. |
| 2981 | */ |
| 2982 | if (new_offset < rdev->data_offset && |
| 2983 | mddev->reshape_backwards) |
| 2984 | return -EINVAL; |
| 2985 | /* Increasing offset is inconsistent with forwards |
| 2986 | * reshape. reshape_direction should be set to |
| 2987 | * 'backwards' first. |
| 2988 | */ |
| 2989 | if (new_offset > rdev->data_offset && |
| 2990 | !mddev->reshape_backwards) |
| 2991 | return -EINVAL; |
| 2992 | |
| 2993 | if (mddev->pers && mddev->persistent && |
| 2994 | !super_types[mddev->major_version] |
| 2995 | .allow_new_offset(rdev, new_offset)) |
| 2996 | return -E2BIG; |
| 2997 | rdev->new_data_offset = new_offset; |
| 2998 | if (new_offset > rdev->data_offset) |
| 2999 | mddev->reshape_backwards = 1; |
| 3000 | else if (new_offset < rdev->data_offset) |
| 3001 | mddev->reshape_backwards = 0; |
| 3002 | |
| 3003 | return len; |
| 3004 | } |
| 3005 | static struct rdev_sysfs_entry rdev_new_offset = |
| 3006 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 3007 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3008 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3009 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3010 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3011 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3012 | } |
| 3013 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3014 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 3015 | { |
| 3016 | /* check if two start/length pairs overlap */ |
| 3017 | if (s1+l1 <= s2) |
| 3018 | return 0; |
| 3019 | if (s2+l2 <= s1) |
| 3020 | return 0; |
| 3021 | return 1; |
| 3022 | } |
| 3023 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3024 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 3025 | { |
| 3026 | unsigned long long blocks; |
| 3027 | sector_t new; |
| 3028 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3029 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3030 | return -EINVAL; |
| 3031 | |
| 3032 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 3033 | return -EINVAL; /* sector conversion overflow */ |
| 3034 | |
| 3035 | new = blocks * 2; |
| 3036 | if (new != blocks * 2) |
| 3037 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 3038 | |
| 3039 | *sectors = new; |
| 3040 | return 0; |
| 3041 | } |
| 3042 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3043 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3044 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3045 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3046 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3047 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3048 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3049 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3050 | if (test_bit(Journal, &rdev->flags)) |
| 3051 | return -EBUSY; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3052 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3053 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3054 | if (rdev->data_offset != rdev->new_data_offset) |
| 3055 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3056 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3057 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3058 | sectors = super_types[my_mddev->major_version]. |
| 3059 | rdev_size_change(rdev, sectors); |
| 3060 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3061 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3062 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3063 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3064 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 3065 | if (!my_mddev->pers->resize) |
| 3066 | /* Cannot change size for RAID0 or Linear etc */ |
| 3067 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3068 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3069 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3070 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3071 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3072 | rdev->sectors = sectors; |
| 3073 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3074 | /* Need to check that all other rdevs with the same |
| 3075 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 3076 | * the rdev lists safely. |
| 3077 | * This check does not provide a hard guarantee, it |
| 3078 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3079 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3080 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3081 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3082 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3083 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3084 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 3085 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3086 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3087 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3088 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 3089 | if (rdev->bdev == rdev2->bdev && |
| 3090 | rdev != rdev2 && |
| 3091 | overlaps(rdev->data_offset, rdev->sectors, |
| 3092 | rdev2->data_offset, |
| 3093 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3094 | overlap = 1; |
| 3095 | break; |
| 3096 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3097 | if (overlap) { |
| 3098 | mddev_put(mddev); |
| 3099 | break; |
| 3100 | } |
| 3101 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3102 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3103 | if (overlap) { |
| 3104 | /* Someone else could have slipped in a size |
| 3105 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3106 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3107 | * safe, and trust userspace not to race with |
| 3108 | * itself |
| 3109 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3110 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3111 | return -EBUSY; |
| 3112 | } |
| 3113 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3114 | return len; |
| 3115 | } |
| 3116 | |
| 3117 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3118 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3119 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3120 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3121 | { |
| 3122 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3123 | |
| 3124 | if (test_bit(In_sync, &rdev->flags) || |
| 3125 | recovery_start == MaxSector) |
| 3126 | return sprintf(page, "none\n"); |
| 3127 | |
| 3128 | return sprintf(page, "%llu\n", recovery_start); |
| 3129 | } |
| 3130 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3131 | 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] | 3132 | { |
| 3133 | unsigned long long recovery_start; |
| 3134 | |
| 3135 | if (cmd_match(buf, "none")) |
| 3136 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3137 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3138 | return -EINVAL; |
| 3139 | |
| 3140 | if (rdev->mddev->pers && |
| 3141 | rdev->raid_disk >= 0) |
| 3142 | return -EBUSY; |
| 3143 | |
| 3144 | rdev->recovery_offset = recovery_start; |
| 3145 | if (recovery_start == MaxSector) |
| 3146 | set_bit(In_sync, &rdev->flags); |
| 3147 | else |
| 3148 | clear_bit(In_sync, &rdev->flags); |
| 3149 | return len; |
| 3150 | } |
| 3151 | |
| 3152 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3153 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3154 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3155 | /* sysfs access to bad-blocks list. |
| 3156 | * We present two files. |
| 3157 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 3158 | * are recorded as bad. The list is truncated to fit within |
| 3159 | * the one-page limit of sysfs. |
| 3160 | * Writing "sector length" to this file adds an acknowledged |
| 3161 | * bad block list. |
| 3162 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 3163 | * been acknowledged. Writing to this file adds bad blocks |
| 3164 | * without acknowledging them. This is largely for testing. |
| 3165 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3166 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3167 | { |
| 3168 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3169 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3170 | 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] | 3171 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3172 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3173 | /* Maybe that ack was all we needed */ |
| 3174 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3175 | wake_up(&rdev->blocked_wait); |
| 3176 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3177 | } |
| 3178 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3179 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3180 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3181 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3182 | { |
| 3183 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3184 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3185 | 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] | 3186 | { |
| 3187 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3188 | } |
| 3189 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3190 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3191 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 3192 | static ssize_t |
| 3193 | ppl_sector_show(struct md_rdev *rdev, char *page) |
| 3194 | { |
| 3195 | return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector); |
| 3196 | } |
| 3197 | |
| 3198 | static ssize_t |
| 3199 | ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len) |
| 3200 | { |
| 3201 | unsigned long long sector; |
| 3202 | |
| 3203 | if (kstrtoull(buf, 10, §or) < 0) |
| 3204 | return -EINVAL; |
| 3205 | if (sector != (sector_t)sector) |
| 3206 | return -EINVAL; |
| 3207 | |
| 3208 | if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) && |
| 3209 | rdev->raid_disk >= 0) |
| 3210 | return -EBUSY; |
| 3211 | |
| 3212 | if (rdev->mddev->persistent) { |
| 3213 | if (rdev->mddev->major_version == 0) |
| 3214 | return -EINVAL; |
| 3215 | if ((sector > rdev->sb_start && |
| 3216 | sector - rdev->sb_start > S16_MAX) || |
| 3217 | (sector < rdev->sb_start && |
| 3218 | rdev->sb_start - sector > -S16_MIN)) |
| 3219 | return -EINVAL; |
| 3220 | rdev->ppl.offset = sector - rdev->sb_start; |
| 3221 | } else if (!rdev->mddev->external) { |
| 3222 | return -EBUSY; |
| 3223 | } |
| 3224 | rdev->ppl.sector = sector; |
| 3225 | return len; |
| 3226 | } |
| 3227 | |
| 3228 | static struct rdev_sysfs_entry rdev_ppl_sector = |
| 3229 | __ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store); |
| 3230 | |
| 3231 | static ssize_t |
| 3232 | ppl_size_show(struct md_rdev *rdev, char *page) |
| 3233 | { |
| 3234 | return sprintf(page, "%u\n", rdev->ppl.size); |
| 3235 | } |
| 3236 | |
| 3237 | static ssize_t |
| 3238 | ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
| 3239 | { |
| 3240 | unsigned int size; |
| 3241 | |
| 3242 | if (kstrtouint(buf, 10, &size) < 0) |
| 3243 | return -EINVAL; |
| 3244 | |
| 3245 | if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) && |
| 3246 | rdev->raid_disk >= 0) |
| 3247 | return -EBUSY; |
| 3248 | |
| 3249 | if (rdev->mddev->persistent) { |
| 3250 | if (rdev->mddev->major_version == 0) |
| 3251 | return -EINVAL; |
| 3252 | if (size > U16_MAX) |
| 3253 | return -EINVAL; |
| 3254 | } else if (!rdev->mddev->external) { |
| 3255 | return -EBUSY; |
| 3256 | } |
| 3257 | rdev->ppl.size = size; |
| 3258 | return len; |
| 3259 | } |
| 3260 | |
| 3261 | static struct rdev_sysfs_entry rdev_ppl_size = |
| 3262 | __ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store); |
| 3263 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3264 | static struct attribute *rdev_default_attrs[] = { |
| 3265 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3266 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3267 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3268 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3269 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3270 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3271 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3272 | &rdev_bad_blocks.attr, |
| 3273 | &rdev_unack_bad_blocks.attr, |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 3274 | &rdev_ppl_sector.attr, |
| 3275 | &rdev_ppl_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3276 | NULL, |
| 3277 | }; |
| 3278 | static ssize_t |
| 3279 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3280 | { |
| 3281 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3282 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3283 | |
| 3284 | if (!entry->show) |
| 3285 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3286 | if (!rdev->mddev) |
| 3287 | return -EBUSY; |
| 3288 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | static ssize_t |
| 3292 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3293 | const char *page, size_t length) |
| 3294 | { |
| 3295 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3296 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3297 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3298 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3299 | |
| 3300 | if (!entry->store) |
| 3301 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3302 | if (!capable(CAP_SYS_ADMIN)) |
| 3303 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3304 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3305 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3306 | if (rdev->mddev == NULL) |
| 3307 | rv = -EBUSY; |
| 3308 | else |
| 3309 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3310 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3311 | } |
| 3312 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | static void rdev_free(struct kobject *ko) |
| 3316 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3317 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3318 | kfree(rdev); |
| 3319 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3320 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3321 | .show = rdev_attr_show, |
| 3322 | .store = rdev_attr_store, |
| 3323 | }; |
| 3324 | static struct kobj_type rdev_ktype = { |
| 3325 | .release = rdev_free, |
| 3326 | .sysfs_ops = &rdev_sysfs_ops, |
| 3327 | .default_attrs = rdev_default_attrs, |
| 3328 | }; |
| 3329 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3330 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3331 | { |
| 3332 | rdev->desc_nr = -1; |
| 3333 | rdev->saved_raid_disk = -1; |
| 3334 | rdev->raid_disk = -1; |
| 3335 | rdev->flags = 0; |
| 3336 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3337 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3338 | rdev->sb_events = 0; |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 3339 | rdev->last_read_error = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3340 | rdev->sb_loaded = 0; |
| 3341 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3342 | atomic_set(&rdev->nr_pending, 0); |
| 3343 | atomic_set(&rdev->read_errors, 0); |
| 3344 | atomic_set(&rdev->corrected_errors, 0); |
| 3345 | |
| 3346 | INIT_LIST_HEAD(&rdev->same_set); |
| 3347 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3348 | |
| 3349 | /* Add space to store bad block list. |
| 3350 | * This reserves the space even on arrays where it cannot |
| 3351 | * be used - I wonder if that matters |
| 3352 | */ |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3353 | return badblocks_init(&rdev->badblocks, 0); |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3354 | } |
| 3355 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | /* |
| 3357 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3358 | * |
| 3359 | * mark the device faulty if: |
| 3360 | * |
| 3361 | * - the device is nonexistent (zero size) |
| 3362 | * - the device has no valid superblock |
| 3363 | * |
| 3364 | * a faulty rdev _never_ has rdev->sb set. |
| 3365 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3366 | 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] | 3367 | { |
| 3368 | char b[BDEVNAME_SIZE]; |
| 3369 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3370 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | sector_t size; |
| 3372 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3373 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3374 | if (!rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3376 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3377 | err = md_rdev_init(rdev); |
| 3378 | if (err) |
| 3379 | goto abort_free; |
| 3380 | err = alloc_disk_sb(rdev); |
| 3381 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3382 | goto abort_free; |
| 3383 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3384 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | if (err) |
| 3386 | goto abort_free; |
| 3387 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3388 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3389 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3390 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3391 | if (!size) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3392 | pr_warn("md: %s has zero or unknown size, marking faulty!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | bdevname(rdev->bdev,b)); |
| 3394 | err = -EINVAL; |
| 3395 | goto abort_free; |
| 3396 | } |
| 3397 | |
| 3398 | if (super_format >= 0) { |
| 3399 | err = super_types[super_format]. |
| 3400 | load_super(rdev, NULL, super_minor); |
| 3401 | if (err == -EINVAL) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3402 | 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] | 3403 | bdevname(rdev->bdev,b), |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3404 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3405 | goto abort_free; |
| 3406 | } |
| 3407 | if (err < 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3408 | pr_warn("md: could not read %s's sb, not importing!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | bdevname(rdev->bdev,b)); |
| 3410 | goto abort_free; |
| 3411 | } |
| 3412 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3414 | return rdev; |
| 3415 | |
| 3416 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3417 | if (rdev->bdev) |
| 3418 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3419 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3420 | kfree(rdev); |
| 3421 | return ERR_PTR(err); |
| 3422 | } |
| 3423 | |
| 3424 | /* |
| 3425 | * Check a full RAID array for plausibility |
| 3426 | */ |
| 3427 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3428 | static void analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | { |
| 3430 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3431 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3432 | char b[BDEVNAME_SIZE]; |
| 3433 | |
| 3434 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3435 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | switch (super_types[mddev->major_version]. |
| 3437 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3438 | case 1: |
| 3439 | freshest = rdev; |
| 3440 | break; |
| 3441 | case 0: |
| 3442 | break; |
| 3443 | default: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3444 | pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3446 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | } |
| 3448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | super_types[mddev->major_version]. |
| 3450 | validate_super(mddev, freshest); |
| 3451 | |
| 3452 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3453 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3454 | if (mddev->max_disks && |
| 3455 | (rdev->desc_nr >= mddev->max_disks || |
| 3456 | i > mddev->max_disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3457 | pr_warn("md: %s: %s: only %d devices permitted\n", |
| 3458 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3459 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3460 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3461 | continue; |
| 3462 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3463 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | if (super_types[mddev->major_version]. |
| 3465 | validate_super(mddev, rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3466 | pr_warn("md: kicking non-fresh %s from array!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3468 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | continue; |
| 3470 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3471 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3473 | rdev->desc_nr = i++; |
| 3474 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3475 | set_bit(In_sync, &rdev->flags); |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3476 | } else if (rdev->raid_disk >= |
| 3477 | (mddev->raid_disks - min(0, mddev->delta_disks)) && |
| 3478 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3479 | rdev->raid_disk = -1; |
| 3480 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | } |
| 3482 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3483 | } |
| 3484 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3485 | /* Read a fixed-point number. |
| 3486 | * Numbers in sysfs attributes should be in "standard" units where |
| 3487 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3488 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3489 | * milliseconds or jiffies. |
| 3490 | * This function takes a decimal number with a possible fractional |
| 3491 | * component, and produces an integer which is the result of |
| 3492 | * multiplying that number by 10^'scale'. |
| 3493 | * all without any floating-point arithmetic. |
| 3494 | */ |
| 3495 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3496 | { |
| 3497 | unsigned long result = 0; |
| 3498 | long decimals = -1; |
| 3499 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3500 | if (*cp == '.') |
| 3501 | decimals = 0; |
| 3502 | else if (decimals < scale) { |
| 3503 | unsigned int value; |
| 3504 | value = *cp - '0'; |
| 3505 | result = result * 10 + value; |
| 3506 | if (decimals >= 0) |
| 3507 | decimals++; |
| 3508 | } |
| 3509 | cp++; |
| 3510 | } |
| 3511 | if (*cp == '\n') |
| 3512 | cp++; |
| 3513 | if (*cp) |
| 3514 | return -EINVAL; |
| 3515 | if (decimals < 0) |
| 3516 | decimals = 0; |
| 3517 | while (decimals < scale) { |
| 3518 | result *= 10; |
| 3519 | decimals ++; |
| 3520 | } |
| 3521 | *res = result; |
| 3522 | return 0; |
| 3523 | } |
| 3524 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3525 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3526 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3527 | { |
| 3528 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3529 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3530 | } |
| 3531 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3532 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3533 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3534 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3535 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3536 | if (mddev_is_clustered(mddev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3537 | pr_warn("md: Safemode is disabled for clustered mode\n"); |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3538 | return -EINVAL; |
| 3539 | } |
| 3540 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3541 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3542 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3543 | if (msec == 0) |
| 3544 | mddev->safemode_delay = 0; |
| 3545 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3546 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3547 | unsigned long new_delay = (msec*HZ)/1000; |
| 3548 | |
| 3549 | if (new_delay == 0) |
| 3550 | new_delay = 1; |
| 3551 | mddev->safemode_delay = new_delay; |
| 3552 | if (new_delay < old_delay || old_delay == 0) |
| 3553 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3554 | } |
| 3555 | return len; |
| 3556 | } |
| 3557 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3558 | __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] | 3559 | |
| 3560 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3561 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3562 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3563 | struct md_personality *p; |
| 3564 | int ret; |
| 3565 | spin_lock(&mddev->lock); |
| 3566 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3567 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3568 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3569 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3570 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3571 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3572 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3573 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3574 | ret = 0; |
| 3575 | spin_unlock(&mddev->lock); |
| 3576 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3577 | } |
| 3578 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3579 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3580 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3581 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3582 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3583 | ssize_t rv; |
| 3584 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3585 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3586 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3587 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3588 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3589 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3590 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3591 | return -EINVAL; |
| 3592 | |
| 3593 | rv = mddev_lock(mddev); |
| 3594 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3595 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3596 | |
| 3597 | if (mddev->pers == NULL) { |
| 3598 | strncpy(mddev->clevel, buf, slen); |
| 3599 | if (mddev->clevel[slen-1] == '\n') |
| 3600 | slen--; |
| 3601 | mddev->clevel[slen] = 0; |
| 3602 | mddev->level = LEVEL_NONE; |
| 3603 | rv = len; |
| 3604 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3605 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3606 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3607 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3608 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3609 | |
| 3610 | /* request to change the personality. Need to ensure: |
| 3611 | * - array is not engaged in resync/recovery/reshape |
| 3612 | * - old personality can be suspended |
| 3613 | * - new personality will access other array. |
| 3614 | */ |
| 3615 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3616 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3617 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3618 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3619 | mddev->reshape_position != MaxSector || |
| 3620 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3621 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3622 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3623 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3624 | if (!mddev->pers->quiesce) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3625 | pr_warn("md: %s: %s does not support online personality change\n", |
| 3626 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3627 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3628 | } |
| 3629 | |
| 3630 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3631 | strncpy(clevel, buf, slen); |
| 3632 | if (clevel[slen-1] == '\n') |
| 3633 | slen--; |
| 3634 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3635 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3636 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3637 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3638 | if (request_module("md-%s", clevel) != 0) |
| 3639 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3640 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3641 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3642 | if (!pers || !try_module_get(pers->owner)) { |
| 3643 | spin_unlock(&pers_lock); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3644 | pr_warn("md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3645 | rv = -EINVAL; |
| 3646 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3647 | } |
| 3648 | spin_unlock(&pers_lock); |
| 3649 | |
| 3650 | if (pers == mddev->pers) { |
| 3651 | /* Nothing to do! */ |
| 3652 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3653 | rv = len; |
| 3654 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3655 | } |
| 3656 | if (!pers->takeover) { |
| 3657 | module_put(pers->owner); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3658 | pr_warn("md: %s: %s does not support personality takeover\n", |
| 3659 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3660 | rv = -EINVAL; |
| 3661 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3662 | } |
| 3663 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3664 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3665 | rdev->new_raid_disk = rdev->raid_disk; |
| 3666 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3667 | /* ->takeover must set new_* and/or delta_disks |
| 3668 | * if it succeeds, and may set them when it fails. |
| 3669 | */ |
| 3670 | priv = pers->takeover(mddev); |
| 3671 | if (IS_ERR(priv)) { |
| 3672 | mddev->new_level = mddev->level; |
| 3673 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3674 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3675 | mddev->raid_disks -= mddev->delta_disks; |
| 3676 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3677 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3678 | module_put(pers->owner); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3679 | pr_warn("md: %s: %s would not accept array\n", |
| 3680 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3681 | rv = PTR_ERR(priv); |
| 3682 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3683 | } |
| 3684 | |
| 3685 | /* Looks like we have a winner */ |
| 3686 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 3687 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3688 | |
| 3689 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3690 | oldpers = mddev->pers; |
| 3691 | oldpriv = mddev->private; |
| 3692 | mddev->pers = pers; |
| 3693 | mddev->private = priv; |
| 3694 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3695 | mddev->level = mddev->new_level; |
| 3696 | mddev->layout = mddev->new_layout; |
| 3697 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 3698 | mddev->delta_disks = 0; |
| 3699 | mddev->reshape_backwards = 0; |
| 3700 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3701 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3702 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3703 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3704 | mddev->external) { |
| 3705 | /* We are converting from a no-redundancy array |
| 3706 | * to a redundancy array and metadata is managed |
| 3707 | * externally so we need to be sure that writes |
| 3708 | * won't block due to a need to transition |
| 3709 | * clean->dirty |
| 3710 | * until external management is started. |
| 3711 | */ |
| 3712 | mddev->in_sync = 0; |
| 3713 | mddev->safemode_delay = 0; |
| 3714 | mddev->safemode = 0; |
| 3715 | } |
| 3716 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3717 | oldpers->free(mddev, oldpriv); |
| 3718 | |
| 3719 | if (oldpers->sync_request == NULL && |
| 3720 | pers->sync_request != NULL) { |
| 3721 | /* need to add the md_redundancy_group */ |
| 3722 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3723 | pr_warn("md: cannot register extra attributes for %s\n", |
| 3724 | mdname(mddev)); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3725 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3726 | } |
| 3727 | if (oldpers->sync_request != NULL && |
| 3728 | pers->sync_request == NULL) { |
| 3729 | /* need to remove the md_redundancy_group */ |
| 3730 | if (mddev->to_remove == NULL) |
| 3731 | mddev->to_remove = &md_redundancy_group; |
| 3732 | } |
| 3733 | |
Alexey Obitotskiy | 4cb9da7 | 2016-06-23 12:11:01 +0200 | [diff] [blame] | 3734 | module_put(oldpers->owner); |
| 3735 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3736 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3737 | if (rdev->raid_disk < 0) |
| 3738 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3739 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3740 | rdev->new_raid_disk = -1; |
| 3741 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3742 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3743 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3744 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3745 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3746 | if (rdev->raid_disk < 0) |
| 3747 | continue; |
| 3748 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3749 | continue; |
| 3750 | rdev->raid_disk = rdev->new_raid_disk; |
| 3751 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3752 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3753 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3754 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3755 | pr_warn("md: cannot register rd%d for %s after level change\n", |
| 3756 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3757 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3758 | } |
| 3759 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3760 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3761 | /* this is now an array without redundancy, so |
| 3762 | * it must always be in_sync |
| 3763 | */ |
| 3764 | mddev->in_sync = 1; |
| 3765 | del_timer_sync(&mddev->safemode_timer); |
| 3766 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 3767 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3768 | pers->run(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 3769 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3770 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 3771 | if (!mddev->thread) |
| 3772 | md_update_sb(mddev, 1); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3773 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3774 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3775 | rv = len; |
| 3776 | out_unlock: |
| 3777 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3778 | return rv; |
| 3779 | } |
| 3780 | |
| 3781 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3782 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3783 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3784 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3785 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3786 | { |
| 3787 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3788 | if (mddev->reshape_position != MaxSector && |
| 3789 | mddev->layout != mddev->new_layout) |
| 3790 | return sprintf(page, "%d (%d)\n", |
| 3791 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3792 | return sprintf(page, "%d\n", mddev->layout); |
| 3793 | } |
| 3794 | |
| 3795 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3796 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3797 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3798 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3799 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3800 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3801 | err = kstrtouint(buf, 10, &n); |
| 3802 | if (err < 0) |
| 3803 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3804 | err = mddev_lock(mddev); |
| 3805 | if (err) |
| 3806 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3807 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3808 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3809 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3810 | err = -EBUSY; |
| 3811 | else if (mddev->ro) |
| 3812 | err = -EROFS; |
| 3813 | else { |
| 3814 | mddev->new_layout = n; |
| 3815 | err = mddev->pers->check_reshape(mddev); |
| 3816 | if (err) |
| 3817 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3818 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3819 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3820 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3821 | if (mddev->reshape_position == MaxSector) |
| 3822 | mddev->layout = n; |
| 3823 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3824 | mddev_unlock(mddev); |
| 3825 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3826 | } |
| 3827 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3828 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3829 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3830 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3831 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3832 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3833 | if (mddev->raid_disks == 0) |
| 3834 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3835 | if (mddev->reshape_position != MaxSector && |
| 3836 | mddev->delta_disks != 0) |
| 3837 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3838 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3839 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3840 | } |
| 3841 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3842 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3843 | |
| 3844 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3845 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3846 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3847 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3848 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3849 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3850 | err = kstrtouint(buf, 10, &n); |
| 3851 | if (err < 0) |
| 3852 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3853 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3854 | err = mddev_lock(mddev); |
| 3855 | if (err) |
| 3856 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3857 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3858 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3859 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3860 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3861 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3862 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3863 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3864 | rdev_for_each(rdev, mddev) { |
| 3865 | if (olddisks < n && |
| 3866 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3867 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3868 | if (olddisks > n && |
| 3869 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3870 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3871 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3872 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3873 | mddev->delta_disks = n - olddisks; |
| 3874 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3875 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3876 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3877 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3878 | out_unlock: |
| 3879 | mddev_unlock(mddev); |
| 3880 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3881 | } |
| 3882 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3883 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3884 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3885 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3886 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3887 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3888 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3889 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3890 | return sprintf(page, "%d (%d)\n", |
| 3891 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3892 | mddev->chunk_sectors << 9); |
| 3893 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3897 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3898 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3899 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3900 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3901 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3902 | err = kstrtoul(buf, 10, &n); |
| 3903 | if (err < 0) |
| 3904 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3905 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3906 | err = mddev_lock(mddev); |
| 3907 | if (err) |
| 3908 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3909 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3910 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3911 | err = -EBUSY; |
| 3912 | else if (mddev->ro) |
| 3913 | err = -EROFS; |
| 3914 | else { |
| 3915 | mddev->new_chunk_sectors = n >> 9; |
| 3916 | err = mddev->pers->check_reshape(mddev); |
| 3917 | if (err) |
| 3918 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3919 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3920 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3921 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3922 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3923 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3924 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3925 | mddev_unlock(mddev); |
| 3926 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3927 | } |
| 3928 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3929 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3930 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3931 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3932 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3933 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3934 | if (mddev->recovery_cp == MaxSector) |
| 3935 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3936 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3937 | } |
| 3938 | |
| 3939 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3940 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3941 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3942 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3943 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3944 | |
| 3945 | if (cmd_match(buf, "none")) |
| 3946 | n = MaxSector; |
| 3947 | else { |
| 3948 | err = kstrtoull(buf, 10, &n); |
| 3949 | if (err < 0) |
| 3950 | return err; |
| 3951 | if (n != (sector_t)n) |
| 3952 | return -EINVAL; |
| 3953 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3954 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3955 | err = mddev_lock(mddev); |
| 3956 | if (err) |
| 3957 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3958 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3959 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3960 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3961 | if (!err) { |
| 3962 | mddev->recovery_cp = n; |
| 3963 | if (mddev->pers) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 3964 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3965 | } |
| 3966 | mddev_unlock(mddev); |
| 3967 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3968 | } |
| 3969 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3970 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 3971 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3972 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3973 | /* |
| 3974 | * The array state can be: |
| 3975 | * |
| 3976 | * clear |
| 3977 | * No devices, no size, no level |
| 3978 | * Equivalent to STOP_ARRAY ioctl |
| 3979 | * inactive |
| 3980 | * May have some settings, but array is not active |
| 3981 | * all IO results in error |
| 3982 | * When written, doesn't tear down array, but just stops it |
| 3983 | * suspended (not supported yet) |
| 3984 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3985 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3986 | * readonly |
| 3987 | * no resync can happen. no superblocks get written. |
| 3988 | * write requests fail |
| 3989 | * read-auto |
| 3990 | * like readonly, but behaves like 'clean' on a write request. |
| 3991 | * |
| 3992 | * clean - no pending writes, but otherwise active. |
| 3993 | * When written to inactive array, starts without resync |
| 3994 | * If a write request arrives then |
| 3995 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3996 | * if not known, block and switch to write-pending |
| 3997 | * If written to an active array that has pending writes, then fails. |
| 3998 | * active |
| 3999 | * fully active: IO and resync can be happening. |
| 4000 | * When written to inactive array, starts with resync |
| 4001 | * |
| 4002 | * write-pending |
| 4003 | * clean, but writes are blocked waiting for 'active' to be written. |
| 4004 | * |
| 4005 | * active-idle |
| 4006 | * like active, but no writes have been seen for a while (100msec). |
| 4007 | * |
| 4008 | */ |
| 4009 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 4010 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 4011 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4012 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 4013 | "write-pending", "active-idle", NULL }; |
| 4014 | |
| 4015 | static int match_word(const char *word, char **list) |
| 4016 | { |
| 4017 | int n; |
| 4018 | for (n=0; list[n]; n++) |
| 4019 | if (cmd_match(word, list[n])) |
| 4020 | break; |
| 4021 | return n; |
| 4022 | } |
| 4023 | |
| 4024 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4025 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4026 | { |
| 4027 | enum array_state st = inactive; |
| 4028 | |
| 4029 | if (mddev->pers) |
| 4030 | switch(mddev->ro) { |
| 4031 | case 1: |
| 4032 | st = readonly; |
| 4033 | break; |
| 4034 | case 2: |
| 4035 | st = read_auto; |
| 4036 | break; |
| 4037 | case 0: |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4038 | spin_lock(&mddev->lock); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4039 | if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4040 | st = write_pending; |
Tomasz Majchrzak | 16f8894 | 2016-10-24 12:47:28 +0200 | [diff] [blame] | 4041 | else if (mddev->in_sync) |
| 4042 | st = clean; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4043 | else if (mddev->safemode) |
| 4044 | st = active_idle; |
| 4045 | else |
| 4046 | st = active; |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4047 | spin_unlock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4048 | } |
| 4049 | else { |
| 4050 | if (list_empty(&mddev->disks) && |
| 4051 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4052 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4053 | st = clear; |
| 4054 | else |
| 4055 | st = inactive; |
| 4056 | } |
| 4057 | return sprintf(page, "%s\n", array_states[st]); |
| 4058 | } |
| 4059 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4060 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 4061 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 4062 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4063 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4064 | |
| 4065 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4066 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4067 | { |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4068 | int err = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4069 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4070 | |
| 4071 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 4072 | /* don't take reconfig_mutex when toggling between |
| 4073 | * clean and active |
| 4074 | */ |
| 4075 | spin_lock(&mddev->lock); |
| 4076 | if (st == active) { |
| 4077 | restart_array(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4078 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
Tomasz Majchrzak | 91a6c4a | 2016-10-25 17:07:08 +0200 | [diff] [blame] | 4079 | md_wakeup_thread(mddev->thread); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4080 | wake_up(&mddev->sb_wait); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4081 | } else /* st == clean */ { |
| 4082 | restart_array(mddev); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4083 | if (!set_in_sync(mddev)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4084 | err = -EBUSY; |
| 4085 | } |
Tomasz Majchrzak | 573275b | 2016-06-30 10:47:09 +0200 | [diff] [blame] | 4086 | if (!err) |
| 4087 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4088 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 4089 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4090 | } |
| 4091 | err = mddev_lock(mddev); |
| 4092 | if (err) |
| 4093 | return err; |
| 4094 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4095 | switch(st) { |
| 4096 | case bad_word: |
| 4097 | break; |
| 4098 | case clear: |
| 4099 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4100 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4101 | break; |
| 4102 | case inactive: |
| 4103 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 4104 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4105 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 4106 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4107 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4108 | break; |
| 4109 | case suspended: |
| 4110 | break; /* not supported yet */ |
| 4111 | case readonly: |
| 4112 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4113 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4114 | else { |
| 4115 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4116 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4117 | err = do_md_run(mddev); |
| 4118 | } |
| 4119 | break; |
| 4120 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4121 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4122 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4123 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4124 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4125 | err = restart_array(mddev); |
| 4126 | if (err == 0) { |
| 4127 | mddev->ro = 2; |
| 4128 | set_disk_ro(mddev->gendisk, 0); |
| 4129 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4130 | } else { |
| 4131 | mddev->ro = 2; |
| 4132 | err = do_md_run(mddev); |
| 4133 | } |
| 4134 | break; |
| 4135 | case clean: |
| 4136 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4137 | err = restart_array(mddev); |
| 4138 | if (err) |
| 4139 | break; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4140 | spin_lock(&mddev->lock); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4141 | if (!set_in_sync(mddev)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4142 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4143 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 4144 | } else |
| 4145 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4146 | break; |
| 4147 | case active: |
| 4148 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4149 | err = restart_array(mddev); |
| 4150 | if (err) |
| 4151 | break; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4152 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4153 | wake_up(&mddev->sb_wait); |
| 4154 | err = 0; |
| 4155 | } else { |
| 4156 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4157 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4158 | err = do_md_run(mddev); |
| 4159 | } |
| 4160 | break; |
| 4161 | case write_pending: |
| 4162 | case active_idle: |
| 4163 | /* these cannot be set */ |
| 4164 | break; |
| 4165 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4166 | |
| 4167 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 4168 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4169 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4170 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4171 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4172 | mddev_unlock(mddev); |
| 4173 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4174 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4175 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4176 | __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] | 4177 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4178 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4179 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4180 | return sprintf(page, "%d\n", |
| 4181 | atomic_read(&mddev->max_corr_read_errors)); |
| 4182 | } |
| 4183 | |
| 4184 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4185 | 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] | 4186 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4187 | unsigned int n; |
| 4188 | int rv; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4189 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4190 | rv = kstrtouint(buf, 10, &n); |
| 4191 | if (rv < 0) |
| 4192 | return rv; |
| 4193 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4194 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4195 | } |
| 4196 | |
| 4197 | static struct md_sysfs_entry max_corr_read_errors = |
| 4198 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4199 | max_corrected_read_errors_store); |
| 4200 | |
| 4201 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4202 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4203 | { |
| 4204 | return -EINVAL; |
| 4205 | } |
| 4206 | |
| 4207 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4208 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4209 | { |
| 4210 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4211 | /* The new device is added to the array. |
| 4212 | * If the array has a persistent superblock, we read the |
| 4213 | * superblock to initialise info and check validity. |
| 4214 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4215 | * which mainly checks size. |
| 4216 | */ |
| 4217 | char *e; |
| 4218 | int major = simple_strtoul(buf, &e, 10); |
| 4219 | int minor; |
| 4220 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4221 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4222 | int err; |
| 4223 | |
| 4224 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4225 | return -EINVAL; |
| 4226 | minor = simple_strtoul(e+1, &e, 10); |
| 4227 | if (*e && *e != '\n') |
| 4228 | return -EINVAL; |
| 4229 | dev = MKDEV(major, minor); |
| 4230 | if (major != MAJOR(dev) || |
| 4231 | minor != MINOR(dev)) |
| 4232 | return -EOVERFLOW; |
| 4233 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4234 | flush_workqueue(md_misc_wq); |
| 4235 | |
| 4236 | err = mddev_lock(mddev); |
| 4237 | if (err) |
| 4238 | return err; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4239 | if (mddev->persistent) { |
| 4240 | rdev = md_import_device(dev, mddev->major_version, |
| 4241 | mddev->minor_version); |
| 4242 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4243 | struct md_rdev *rdev0 |
| 4244 | = list_entry(mddev->disks.next, |
| 4245 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4246 | err = super_types[mddev->major_version] |
| 4247 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4248 | if (err < 0) |
| 4249 | goto out; |
| 4250 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4251 | } else if (mddev->external) |
| 4252 | rdev = md_import_device(dev, -2, -1); |
| 4253 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4254 | rdev = md_import_device(dev, -1, -1); |
| 4255 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4256 | if (IS_ERR(rdev)) { |
| 4257 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4258 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4259 | } |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4260 | err = bind_rdev_to_array(rdev, mddev); |
| 4261 | out: |
| 4262 | if (err) |
| 4263 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4264 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4265 | return err ? err : len; |
| 4266 | } |
| 4267 | |
| 4268 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4269 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4270 | |
| 4271 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4272 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4273 | { |
| 4274 | char *end; |
| 4275 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4276 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4277 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4278 | err = mddev_lock(mddev); |
| 4279 | if (err) |
| 4280 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4281 | if (!mddev->bitmap) |
| 4282 | goto out; |
| 4283 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4284 | while (*buf) { |
| 4285 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4286 | if (buf == end) break; |
| 4287 | if (*end == '-') { /* range */ |
| 4288 | buf = end + 1; |
| 4289 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4290 | if (buf == end) break; |
| 4291 | } |
| 4292 | if (*end && !isspace(*end)) break; |
| 4293 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4294 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4295 | } |
| 4296 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4297 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4298 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4299 | return len; |
| 4300 | } |
| 4301 | |
| 4302 | static struct md_sysfs_entry md_bitmap = |
| 4303 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4304 | |
| 4305 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4306 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4307 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4308 | return sprintf(page, "%llu\n", |
| 4309 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4310 | } |
| 4311 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4312 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4313 | |
| 4314 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4315 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4316 | { |
| 4317 | /* If array is inactive, we can reduce the component size, but |
| 4318 | * not increase it (except from 0). |
| 4319 | * If array is active, we can try an on-line resize |
| 4320 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4321 | sector_t sectors; |
| 4322 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4323 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4324 | if (err < 0) |
| 4325 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4326 | err = mddev_lock(mddev); |
| 4327 | if (err) |
| 4328 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4329 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4330 | err = update_size(mddev, sectors); |
Xiao Ni | 4ba1e78 | 2016-06-12 17:18:00 +0800 | [diff] [blame] | 4331 | if (err == 0) |
| 4332 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4333 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4334 | if (mddev->dev_sectors == 0 || |
| 4335 | mddev->dev_sectors > sectors) |
| 4336 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4337 | else |
| 4338 | err = -ENOSPC; |
| 4339 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4340 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4341 | return err ? err : len; |
| 4342 | } |
| 4343 | |
| 4344 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4345 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4346 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4347 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4348 | * This is one of |
| 4349 | * 'none' for arrays with no metadata (good luck...) |
| 4350 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4351 | * or N.M for internally known formats |
| 4352 | */ |
| 4353 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4354 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4355 | { |
| 4356 | if (mddev->persistent) |
| 4357 | return sprintf(page, "%d.%d\n", |
| 4358 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4359 | else if (mddev->external) |
| 4360 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4361 | else |
| 4362 | return sprintf(page, "none\n"); |
| 4363 | } |
| 4364 | |
| 4365 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4366 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4367 | { |
| 4368 | int major, minor; |
| 4369 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4370 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4371 | /* Changing the details of 'external' metadata is |
| 4372 | * always permitted. Otherwise there must be |
| 4373 | * no devices attached to the array. |
| 4374 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4375 | |
| 4376 | err = mddev_lock(mddev); |
| 4377 | if (err) |
| 4378 | return err; |
| 4379 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4380 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4381 | ; |
| 4382 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4383 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4384 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4385 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4386 | if (cmd_match(buf, "none")) { |
| 4387 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4388 | mddev->external = 0; |
| 4389 | mddev->major_version = 0; |
| 4390 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4391 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4392 | } |
| 4393 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4394 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4395 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4396 | namelen = sizeof(mddev->metadata_type)-1; |
| 4397 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4398 | mddev->metadata_type[namelen] = 0; |
| 4399 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4400 | mddev->metadata_type[--namelen] = 0; |
| 4401 | mddev->persistent = 0; |
| 4402 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4403 | mddev->major_version = 0; |
| 4404 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4405 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4406 | } |
| 4407 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4408 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4409 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4410 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4411 | buf = e+1; |
| 4412 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4413 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4414 | goto out_unlock; |
| 4415 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4416 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4417 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4418 | mddev->major_version = major; |
| 4419 | mddev->minor_version = minor; |
| 4420 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4421 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4422 | err = 0; |
| 4423 | out_unlock: |
| 4424 | mddev_unlock(mddev); |
| 4425 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4429 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4430 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4431 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4432 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4433 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4434 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4435 | unsigned long recovery = mddev->recovery; |
| 4436 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4437 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4438 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4439 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4440 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4441 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4442 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4443 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4444 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4445 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4446 | type = "check"; |
| 4447 | else |
| 4448 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4449 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4450 | type = "recover"; |
NeilBrown | 985ca97 | 2015-07-06 12:26:57 +1000 | [diff] [blame] | 4451 | else if (mddev->reshape_position != MaxSector) |
| 4452 | type = "reshape"; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4453 | } |
| 4454 | return sprintf(page, "%s\n", type); |
| 4455 | } |
| 4456 | |
| 4457 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4458 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4459 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4460 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4461 | return -EINVAL; |
| 4462 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4463 | |
| 4464 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4465 | if (cmd_match(page, "frozen")) |
| 4466 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4467 | else |
| 4468 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4469 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4470 | mddev_lock(mddev) == 0) { |
| 4471 | flush_workqueue(md_misc_wq); |
| 4472 | if (mddev->sync_thread) { |
| 4473 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4474 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4475 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4476 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4477 | } |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4478 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4479 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4480 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4481 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4482 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4483 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4484 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4485 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4486 | int err; |
| 4487 | if (mddev->pers->start_reshape == NULL) |
| 4488 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4489 | err = mddev_lock(mddev); |
| 4490 | if (!err) { |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4491 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4492 | err = -EBUSY; |
| 4493 | else { |
| 4494 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4495 | err = mddev->pers->start_reshape(mddev); |
| 4496 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4497 | mddev_unlock(mddev); |
| 4498 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4499 | if (err) |
| 4500 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4501 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4502 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4503 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4504 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4505 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4506 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4507 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4508 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4509 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4510 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4511 | if (mddev->ro == 2) { |
| 4512 | /* A write to sync_action is enough to justify |
| 4513 | * canceling read-auto mode |
| 4514 | */ |
| 4515 | mddev->ro = 0; |
| 4516 | md_wakeup_thread(mddev->sync_thread); |
| 4517 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4518 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4519 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4520 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4521 | return len; |
| 4522 | } |
| 4523 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4524 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4525 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4526 | |
| 4527 | static ssize_t |
| 4528 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4529 | { |
| 4530 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4531 | } |
| 4532 | |
| 4533 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4534 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4535 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4536 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4537 | { |
| 4538 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4539 | (unsigned long long) |
| 4540 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4541 | } |
| 4542 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4543 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4544 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4545 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4546 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4547 | { |
| 4548 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4549 | mddev->sync_speed_min ? "local": "system"); |
| 4550 | } |
| 4551 | |
| 4552 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4553 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4554 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4555 | unsigned int min; |
| 4556 | int rv; |
| 4557 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4558 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4559 | min = 0; |
| 4560 | } else { |
| 4561 | rv = kstrtouint(buf, 10, &min); |
| 4562 | if (rv < 0) |
| 4563 | return rv; |
| 4564 | if (min == 0) |
| 4565 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4566 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4567 | mddev->sync_speed_min = min; |
| 4568 | return len; |
| 4569 | } |
| 4570 | |
| 4571 | static struct md_sysfs_entry md_sync_min = |
| 4572 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4573 | |
| 4574 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4575 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4576 | { |
| 4577 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4578 | mddev->sync_speed_max ? "local": "system"); |
| 4579 | } |
| 4580 | |
| 4581 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4582 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4583 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4584 | unsigned int max; |
| 4585 | int rv; |
| 4586 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4587 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4588 | max = 0; |
| 4589 | } else { |
| 4590 | rv = kstrtouint(buf, 10, &max); |
| 4591 | if (rv < 0) |
| 4592 | return rv; |
| 4593 | if (max == 0) |
| 4594 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4595 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4596 | mddev->sync_speed_max = max; |
| 4597 | return len; |
| 4598 | } |
| 4599 | |
| 4600 | static struct md_sysfs_entry md_sync_max = |
| 4601 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4602 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4603 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4604 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4605 | { |
| 4606 | return sprintf(page, "%d\n", mddev->degraded); |
| 4607 | } |
| 4608 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4609 | |
| 4610 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4611 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4612 | { |
| 4613 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4614 | } |
| 4615 | |
| 4616 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4617 | 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] | 4618 | { |
| 4619 | long n; |
| 4620 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4621 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4622 | return -EINVAL; |
| 4623 | |
| 4624 | if (n != 0 && n != 1) |
| 4625 | return -EINVAL; |
| 4626 | |
| 4627 | mddev->parallel_resync = n; |
| 4628 | |
| 4629 | if (mddev->sync_thread) |
| 4630 | wake_up(&resync_wait); |
| 4631 | |
| 4632 | return len; |
| 4633 | } |
| 4634 | |
| 4635 | /* force parallel resync, even with shared block devices */ |
| 4636 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4637 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4638 | sync_force_parallel_show, sync_force_parallel_store); |
| 4639 | |
| 4640 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4641 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4642 | { |
| 4643 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4644 | if (mddev->curr_resync == 0) |
| 4645 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4646 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4647 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4648 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4649 | db = resync - mddev->resync_mark_cnt; |
| 4650 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4651 | } |
| 4652 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4653 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4654 | |
| 4655 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4656 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4657 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4658 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4659 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4660 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4661 | return sprintf(page, "none\n"); |
| 4662 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4663 | if (mddev->curr_resync == 1 || |
| 4664 | mddev->curr_resync == 2) |
| 4665 | return sprintf(page, "delayed\n"); |
| 4666 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4667 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4668 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4669 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4670 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4671 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4672 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4673 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4674 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4675 | } |
| 4676 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4677 | static struct md_sysfs_entry md_sync_completed = |
| 4678 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4679 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4680 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4681 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4682 | { |
| 4683 | return sprintf(page, "%llu\n", |
| 4684 | (unsigned long long)mddev->resync_min); |
| 4685 | } |
| 4686 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4687 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4688 | { |
| 4689 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4690 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4691 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4692 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4693 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4694 | |
| 4695 | spin_lock(&mddev->lock); |
| 4696 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4697 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4698 | goto out_unlock; |
| 4699 | |
| 4700 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4701 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4702 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4703 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 4704 | /* Round down to multiple of 4K for safety */ |
| 4705 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4706 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4707 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4708 | out_unlock: |
| 4709 | spin_unlock(&mddev->lock); |
| 4710 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | static struct md_sysfs_entry md_min_sync = |
| 4714 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4715 | |
| 4716 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4717 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4718 | { |
| 4719 | if (mddev->resync_max == MaxSector) |
| 4720 | return sprintf(page, "max\n"); |
| 4721 | else |
| 4722 | return sprintf(page, "%llu\n", |
| 4723 | (unsigned long long)mddev->resync_max); |
| 4724 | } |
| 4725 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4726 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4727 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4728 | int err; |
| 4729 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4730 | if (strncmp(buf, "max", 3) == 0) |
| 4731 | mddev->resync_max = MaxSector; |
| 4732 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4733 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4734 | int chunk; |
| 4735 | |
| 4736 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4737 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4738 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4739 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4740 | goto out_unlock; |
| 4741 | |
| 4742 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4743 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4744 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4745 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4746 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4747 | |
| 4748 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4749 | chunk = mddev->chunk_sectors; |
| 4750 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4751 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4752 | |
| 4753 | err = -EINVAL; |
| 4754 | if (sector_div(temp, chunk)) |
| 4755 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4756 | } |
| 4757 | mddev->resync_max = max; |
| 4758 | } |
| 4759 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4760 | err = 0; |
| 4761 | out_unlock: |
| 4762 | spin_unlock(&mddev->lock); |
| 4763 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4764 | } |
| 4765 | |
| 4766 | static struct md_sysfs_entry md_max_sync = |
| 4767 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4768 | |
| 4769 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4770 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4771 | { |
| 4772 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4773 | } |
| 4774 | |
| 4775 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4776 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4777 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4778 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4779 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4780 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4781 | err = kstrtoull(buf, 10, &new); |
| 4782 | if (err < 0) |
| 4783 | return err; |
| 4784 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4785 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4786 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4787 | err = mddev_lock(mddev); |
| 4788 | if (err) |
| 4789 | return err; |
| 4790 | err = -EINVAL; |
| 4791 | if (mddev->pers == NULL || |
| 4792 | mddev->pers->quiesce == NULL) |
| 4793 | goto unlock; |
| 4794 | old = mddev->suspend_lo; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4795 | mddev->suspend_lo = new; |
| 4796 | if (new >= old) |
| 4797 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4798 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4799 | else { |
| 4800 | /* Expanding suspended region - need to wait */ |
| 4801 | mddev->pers->quiesce(mddev, 1); |
| 4802 | mddev->pers->quiesce(mddev, 0); |
| 4803 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4804 | err = 0; |
| 4805 | unlock: |
| 4806 | mddev_unlock(mddev); |
| 4807 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4808 | } |
| 4809 | static struct md_sysfs_entry md_suspend_lo = |
| 4810 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4811 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4812 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4813 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4814 | { |
| 4815 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4816 | } |
| 4817 | |
| 4818 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4819 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4820 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4821 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4822 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4823 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4824 | err = kstrtoull(buf, 10, &new); |
| 4825 | if (err < 0) |
| 4826 | return err; |
| 4827 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4828 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4829 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4830 | err = mddev_lock(mddev); |
| 4831 | if (err) |
| 4832 | return err; |
| 4833 | err = -EINVAL; |
| 4834 | if (mddev->pers == NULL || |
| 4835 | mddev->pers->quiesce == NULL) |
| 4836 | goto unlock; |
| 4837 | old = mddev->suspend_hi; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4838 | mddev->suspend_hi = new; |
| 4839 | if (new <= old) |
| 4840 | /* Shrinking suspended region */ |
| 4841 | mddev->pers->quiesce(mddev, 2); |
| 4842 | else { |
| 4843 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4844 | mddev->pers->quiesce(mddev, 1); |
| 4845 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4846 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4847 | err = 0; |
| 4848 | unlock: |
| 4849 | mddev_unlock(mddev); |
| 4850 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4851 | } |
| 4852 | static struct md_sysfs_entry md_suspend_hi = |
| 4853 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4854 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4855 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4856 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4857 | { |
| 4858 | if (mddev->reshape_position != MaxSector) |
| 4859 | return sprintf(page, "%llu\n", |
| 4860 | (unsigned long long)mddev->reshape_position); |
| 4861 | strcpy(page, "none\n"); |
| 4862 | return 5; |
| 4863 | } |
| 4864 | |
| 4865 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4866 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4867 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4868 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4869 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4870 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4871 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4872 | err = kstrtoull(buf, 10, &new); |
| 4873 | if (err < 0) |
| 4874 | return err; |
| 4875 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4876 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4877 | err = mddev_lock(mddev); |
| 4878 | if (err) |
| 4879 | return err; |
| 4880 | err = -EBUSY; |
| 4881 | if (mddev->pers) |
| 4882 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4883 | mddev->reshape_position = new; |
| 4884 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4885 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4886 | mddev->new_level = mddev->level; |
| 4887 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4888 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4889 | rdev_for_each(rdev, mddev) |
| 4890 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4891 | err = 0; |
| 4892 | unlock: |
| 4893 | mddev_unlock(mddev); |
| 4894 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4895 | } |
| 4896 | |
| 4897 | static struct md_sysfs_entry md_reshape_position = |
| 4898 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4899 | reshape_position_store); |
| 4900 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4901 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4902 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4903 | { |
| 4904 | return sprintf(page, "%s\n", |
| 4905 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4906 | } |
| 4907 | |
| 4908 | static ssize_t |
| 4909 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4910 | { |
| 4911 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4912 | int err; |
| 4913 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4914 | if (cmd_match(buf, "forwards")) |
| 4915 | backwards = 0; |
| 4916 | else if (cmd_match(buf, "backwards")) |
| 4917 | backwards = 1; |
| 4918 | else |
| 4919 | return -EINVAL; |
| 4920 | if (mddev->reshape_backwards == backwards) |
| 4921 | return len; |
| 4922 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4923 | err = mddev_lock(mddev); |
| 4924 | if (err) |
| 4925 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4926 | /* check if we are allowed to change */ |
| 4927 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4928 | err = -EBUSY; |
| 4929 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4930 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4931 | err = -EINVAL; |
| 4932 | else |
| 4933 | mddev->reshape_backwards = backwards; |
| 4934 | mddev_unlock(mddev); |
| 4935 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4936 | } |
| 4937 | |
| 4938 | static struct md_sysfs_entry md_reshape_direction = |
| 4939 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4940 | reshape_direction_store); |
| 4941 | |
| 4942 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4943 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4944 | { |
| 4945 | if (mddev->external_size) |
| 4946 | return sprintf(page, "%llu\n", |
| 4947 | (unsigned long long)mddev->array_sectors/2); |
| 4948 | else |
| 4949 | return sprintf(page, "default\n"); |
| 4950 | } |
| 4951 | |
| 4952 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4953 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4954 | { |
| 4955 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4956 | int err; |
| 4957 | |
| 4958 | err = mddev_lock(mddev); |
| 4959 | if (err) |
| 4960 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4961 | |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 4962 | /* cluster raid doesn't support change array_sectors */ |
Zhilong Liu | b670883 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 4963 | if (mddev_is_clustered(mddev)) { |
| 4964 | mddev_unlock(mddev); |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 4965 | return -EINVAL; |
Zhilong Liu | b670883 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 4966 | } |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 4967 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4968 | if (strncmp(buf, "default", 7) == 0) { |
| 4969 | if (mddev->pers) |
| 4970 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4971 | else |
| 4972 | sectors = mddev->array_sectors; |
| 4973 | |
| 4974 | mddev->external_size = 0; |
| 4975 | } else { |
| 4976 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4977 | err = -EINVAL; |
| 4978 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 4979 | err = -E2BIG; |
| 4980 | else |
| 4981 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4982 | } |
| 4983 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4984 | if (!err) { |
| 4985 | mddev->array_sectors = sectors; |
| 4986 | if (mddev->pers) { |
| 4987 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4988 | revalidate_disk(mddev->gendisk); |
| 4989 | } |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4990 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4991 | mddev_unlock(mddev); |
| 4992 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4993 | } |
| 4994 | |
| 4995 | static struct md_sysfs_entry md_array_size = |
| 4996 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4997 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4998 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 4999 | static ssize_t |
| 5000 | consistency_policy_show(struct mddev *mddev, char *page) |
| 5001 | { |
| 5002 | int ret; |
| 5003 | |
| 5004 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 5005 | ret = sprintf(page, "journal\n"); |
| 5006 | } else if (test_bit(MD_HAS_PPL, &mddev->flags)) { |
| 5007 | ret = sprintf(page, "ppl\n"); |
| 5008 | } else if (mddev->bitmap) { |
| 5009 | ret = sprintf(page, "bitmap\n"); |
| 5010 | } else if (mddev->pers) { |
| 5011 | if (mddev->pers->sync_request) |
| 5012 | ret = sprintf(page, "resync\n"); |
| 5013 | else |
| 5014 | ret = sprintf(page, "none\n"); |
| 5015 | } else { |
| 5016 | ret = sprintf(page, "unknown\n"); |
| 5017 | } |
| 5018 | |
| 5019 | return ret; |
| 5020 | } |
| 5021 | |
| 5022 | static ssize_t |
| 5023 | consistency_policy_store(struct mddev *mddev, const char *buf, size_t len) |
| 5024 | { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5025 | int err = 0; |
| 5026 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5027 | if (mddev->pers) { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5028 | if (mddev->pers->change_consistency_policy) |
| 5029 | err = mddev->pers->change_consistency_policy(mddev, buf); |
| 5030 | else |
| 5031 | err = -EBUSY; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5032 | } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) { |
| 5033 | set_bit(MD_HAS_PPL, &mddev->flags); |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5034 | } else { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5035 | err = -EINVAL; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5036 | } |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5037 | |
| 5038 | return err ? err : len; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5039 | } |
| 5040 | |
| 5041 | static struct md_sysfs_entry md_consistency_policy = |
| 5042 | __ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show, |
| 5043 | consistency_policy_store); |
| 5044 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5045 | static struct attribute *md_default_attrs[] = { |
| 5046 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 5047 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5048 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 5049 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5050 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 5051 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 5052 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 5053 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5054 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5055 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5056 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5057 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5058 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5059 | &max_corr_read_errors.attr, |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5060 | &md_consistency_policy.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5061 | NULL, |
| 5062 | }; |
| 5063 | |
| 5064 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5065 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 5066 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5067 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5068 | &md_sync_min.attr, |
| 5069 | &md_sync_max.attr, |
| 5070 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5071 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5072 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5073 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5074 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5075 | &md_suspend_lo.attr, |
| 5076 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 5077 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 5078 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5079 | NULL, |
| 5080 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5081 | static struct attribute_group md_redundancy_group = { |
| 5082 | .name = NULL, |
| 5083 | .attrs = md_redundancy_attrs, |
| 5084 | }; |
| 5085 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5086 | static ssize_t |
| 5087 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 5088 | { |
| 5089 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5090 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5091 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5092 | |
| 5093 | if (!entry->show) |
| 5094 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5095 | spin_lock(&all_mddevs_lock); |
| 5096 | if (list_empty(&mddev->all_mddevs)) { |
| 5097 | spin_unlock(&all_mddevs_lock); |
| 5098 | return -EBUSY; |
| 5099 | } |
| 5100 | mddev_get(mddev); |
| 5101 | spin_unlock(&all_mddevs_lock); |
| 5102 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 5103 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5104 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5105 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | static ssize_t |
| 5109 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 5110 | const char *page, size_t length) |
| 5111 | { |
| 5112 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5113 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5114 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5115 | |
| 5116 | if (!entry->store) |
| 5117 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 5118 | if (!capable(CAP_SYS_ADMIN)) |
| 5119 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5120 | spin_lock(&all_mddevs_lock); |
| 5121 | if (list_empty(&mddev->all_mddevs)) { |
| 5122 | spin_unlock(&all_mddevs_lock); |
| 5123 | return -EBUSY; |
| 5124 | } |
| 5125 | mddev_get(mddev); |
| 5126 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5127 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5128 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5129 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5130 | } |
| 5131 | |
| 5132 | static void md_free(struct kobject *ko) |
| 5133 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5134 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5135 | |
| 5136 | if (mddev->sysfs_state) |
| 5137 | sysfs_put(mddev->sysfs_state); |
| 5138 | |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 5139 | if (mddev->queue) |
| 5140 | blk_cleanup_queue(mddev->queue); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5141 | if (mddev->gendisk) { |
| 5142 | del_gendisk(mddev->gendisk); |
| 5143 | put_disk(mddev->gendisk); |
| 5144 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5145 | percpu_ref_exit(&mddev->writes_pending); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5146 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5147 | kfree(mddev); |
| 5148 | } |
| 5149 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 5150 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5151 | .show = md_attr_show, |
| 5152 | .store = md_attr_store, |
| 5153 | }; |
| 5154 | static struct kobj_type md_ktype = { |
| 5155 | .release = md_free, |
| 5156 | .sysfs_ops = &md_sysfs_ops, |
| 5157 | .default_attrs = md_default_attrs, |
| 5158 | }; |
| 5159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5160 | int mdp_major = 0; |
| 5161 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5162 | static void mddev_delayed_delete(struct work_struct *ws) |
| 5163 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5164 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5165 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5166 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5167 | kobject_del(&mddev->kobj); |
| 5168 | kobject_put(&mddev->kobj); |
| 5169 | } |
| 5170 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5171 | static void no_op(struct percpu_ref *r) {} |
| 5172 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5173 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5174 | { |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5175 | /* |
| 5176 | * If dev is zero, name is the name of a device to allocate with |
| 5177 | * an arbitrary minor number. It will be "md_???" |
| 5178 | * If dev is non-zero it must be a device number with a MAJOR of |
| 5179 | * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then |
| 5180 | * the device is being created by opening a node in /dev. |
| 5181 | * If "name" is not NULL, the device is being created by |
| 5182 | * writing to /sys/module/md_mod/parameters/new_array. |
| 5183 | */ |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5184 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5185 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5187 | int partitioned; |
| 5188 | int shift; |
| 5189 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5190 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | |
| 5192 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5193 | return -ENODEV; |
| 5194 | |
| 5195 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 5196 | shift = partitioned ? MdpMinorShift : 0; |
| 5197 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5198 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5199 | /* wait for any previous instance of this device to be |
| 5200 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5201 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5202 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5203 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5204 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5205 | error = -EEXIST; |
| 5206 | if (mddev->gendisk) |
| 5207 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5208 | |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5209 | if (name && !dev) { |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5210 | /* Need to ensure that 'name' is not a duplicate. |
| 5211 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5212 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5213 | spin_lock(&all_mddevs_lock); |
| 5214 | |
| 5215 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 5216 | if (mddev2->gendisk && |
| 5217 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 5218 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5219 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5220 | } |
| 5221 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5222 | } |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5223 | if (name && dev) |
| 5224 | /* |
| 5225 | * Creating /dev/mdNNN via "newarray", so adjust hold_active. |
| 5226 | */ |
| 5227 | mddev->hold_active = UNTIL_STOP; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5228 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5229 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5230 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5231 | if (!mddev->queue) |
| 5232 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5233 | mddev->queue->queuedata = mddev; |
| 5234 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5235 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 5236 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5237 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5238 | if (percpu_ref_init(&mddev->writes_pending, no_op, 0, GFP_KERNEL) < 0) |
| 5239 | goto abort; |
| 5240 | /* We want to start with the refcount at zero */ |
| 5241 | percpu_ref_put(&mddev->writes_pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | disk = alloc_disk(1 << shift); |
| 5243 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5244 | blk_cleanup_queue(mddev->queue); |
| 5245 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5246 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5248 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5250 | if (name) |
| 5251 | strcpy(disk->disk_name, name); |
| 5252 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5254 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5256 | disk->fops = &md_fops; |
| 5257 | disk->private_data = mddev; |
| 5258 | disk->queue = mddev->queue; |
Jens Axboe | 56883a7 | 2016-03-30 10:16:53 -0600 | [diff] [blame] | 5259 | blk_queue_write_cache(mddev->queue, true, true); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5260 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5261 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5262 | * remove it now. |
| 5263 | */ |
| 5264 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5266 | /* As soon as we call add_disk(), another thread could get |
| 5267 | * through to md_open, so make sure it doesn't get too far |
| 5268 | */ |
| 5269 | mutex_lock(&mddev->open_mutex); |
| 5270 | add_disk(disk); |
| 5271 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 5272 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 5273 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5274 | if (error) { |
| 5275 | /* This isn't possible, but as kobject_init_and_add is marked |
| 5276 | * __must_check, we must do something with the result |
| 5277 | */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5278 | pr_debug("md: cannot register %s/md - name in use\n", |
| 5279 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5280 | error = 0; |
| 5281 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5282 | if (mddev->kobj.sd && |
| 5283 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5284 | pr_debug("pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5285 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5286 | abort: |
| 5287 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5288 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5289 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5290 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5291 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5292 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5293 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5294 | } |
| 5295 | |
| 5296 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 5297 | { |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame^] | 5298 | if (create_on_open) |
| 5299 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | return NULL; |
| 5301 | } |
| 5302 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5303 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 5304 | { |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5305 | /* |
| 5306 | * val must be "md_*" or "mdNNN". |
| 5307 | * For "md_*" we allocate an array with a large free minor number, and |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5308 | * set the name to val. val must not already be an active name. |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5309 | * For "mdNNN" we allocate an array with the minor number NNN |
| 5310 | * which must not already be in use. |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5311 | */ |
| 5312 | int len = strlen(val); |
| 5313 | char buf[DISK_NAME_LEN]; |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5314 | unsigned long devnum; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5315 | |
| 5316 | while (len && val[len-1] == '\n') |
| 5317 | len--; |
| 5318 | if (len >= DISK_NAME_LEN) |
| 5319 | return -E2BIG; |
| 5320 | strlcpy(buf, val, len+1); |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5321 | if (strncmp(buf, "md_", 3) == 0) |
| 5322 | return md_alloc(0, buf); |
| 5323 | if (strncmp(buf, "md", 2) == 0 && |
| 5324 | isdigit(buf[2]) && |
| 5325 | kstrtoul(buf+2, 10, &devnum) == 0 && |
| 5326 | devnum <= MINORMASK) |
| 5327 | return md_alloc(MKDEV(MD_MAJOR, devnum), NULL); |
| 5328 | |
| 5329 | return -EINVAL; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5330 | } |
| 5331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5332 | static void md_safemode_timeout(unsigned long data) |
| 5333 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5334 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5335 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5336 | mddev->safemode = 1; |
| 5337 | if (mddev->external) |
| 5338 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5340 | md_wakeup_thread(mddev->thread); |
| 5341 | } |
| 5342 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5343 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5345 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5347 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5348 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5349 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5351 | if (list_empty(&mddev->disks)) |
| 5352 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5353 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | |
| 5355 | if (mddev->pers) |
| 5356 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5357 | /* Cannot run until previous stop completes properly */ |
| 5358 | if (mddev->sysfs_active) |
| 5359 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5361 | /* |
| 5362 | * Analyze all RAID superblock(s) |
| 5363 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5364 | if (!mddev->raid_disks) { |
| 5365 | if (!mddev->persistent) |
| 5366 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5367 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5370 | if (mddev->level != LEVEL_NONE) |
| 5371 | request_module("md-level-%d", mddev->level); |
| 5372 | else if (mddev->clevel[0]) |
| 5373 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | |
| 5375 | /* |
| 5376 | * Drop all container device buffers, from now on |
| 5377 | * the only valid external interface is through the md |
| 5378 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5380 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5381 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | continue; |
| 5383 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5384 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5385 | |
| 5386 | /* perform some consistency tests on the device. |
| 5387 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5388 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5389 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5390 | if (rdev->meta_bdev) { |
| 5391 | /* Nothing to check */; |
| 5392 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5393 | if (mddev->dev_sectors && |
| 5394 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5395 | > rdev->sb_start) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5396 | pr_warn("md: %s: data overlaps metadata\n", |
| 5397 | mdname(mddev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5398 | return -EINVAL; |
| 5399 | } |
| 5400 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5401 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5402 | > rdev->data_offset) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5403 | pr_warn("md: %s: metadata overlaps data\n", |
| 5404 | mdname(mddev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5405 | return -EINVAL; |
| 5406 | } |
| 5407 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5408 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | } |
| 5410 | |
Ming Lei | 1027317 | 2017-02-14 23:29:00 +0800 | [diff] [blame] | 5411 | if (mddev->bio_set == NULL) { |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 5412 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
Ming Lei | 1027317 | 2017-02-14 23:29:00 +0800 | [diff] [blame] | 5413 | if (!mddev->bio_set) |
| 5414 | return -ENOMEM; |
| 5415 | } |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5418 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5419 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5420 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5421 | if (mddev->level != LEVEL_NONE) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5422 | pr_warn("md: personality for level %d is not loaded!\n", |
| 5423 | mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5424 | else |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5425 | pr_warn("md: personality for level %s is not loaded!\n", |
| 5426 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | return -EINVAL; |
| 5428 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5429 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5430 | if (mddev->level != pers->level) { |
| 5431 | mddev->level = pers->level; |
| 5432 | mddev->new_level = pers->level; |
| 5433 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5434 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5435 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5436 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5437 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5438 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5439 | module_put(pers->owner); |
| 5440 | return -EINVAL; |
| 5441 | } |
| 5442 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5443 | if (pers->sync_request) { |
| 5444 | /* Warn if this is a potentially silly |
| 5445 | * configuration. |
| 5446 | */ |
| 5447 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5448 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5449 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5450 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5451 | rdev_for_each(rdev, mddev) |
| 5452 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5453 | if (rdev < rdev2 && |
| 5454 | rdev->bdev->bd_contains == |
| 5455 | rdev2->bdev->bd_contains) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5456 | pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n", |
| 5457 | mdname(mddev), |
| 5458 | bdevname(rdev->bdev,b), |
| 5459 | bdevname(rdev2->bdev,b2)); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5460 | warned = 1; |
| 5461 | } |
| 5462 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5463 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5464 | if (warned) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5465 | pr_warn("True protection against single-disk failure might be compromised.\n"); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5466 | } |
| 5467 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5468 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5469 | /* may be over-ridden by personality */ |
| 5470 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5471 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5472 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5474 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5475 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5476 | |
Song Liu | a85dd7b | 2017-01-23 17:12:57 -0800 | [diff] [blame] | 5477 | /* |
| 5478 | * NOTE: some pers->run(), for example r5l_recovery_log(), wakes |
| 5479 | * up mddev->thread. It is important to initialize critical |
| 5480 | * resources for mddev->thread BEFORE calling pers->run(). |
| 5481 | */ |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5482 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5483 | if (err) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5484 | pr_warn("md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5485 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5486 | WARN_ONCE(!mddev->external_size, |
| 5487 | "%s: default size too small, but 'external_size' not in effect?\n", |
| 5488 | __func__); |
| 5489 | pr_warn("md: invalid array_size %llu > default size %llu\n", |
| 5490 | (unsigned long long)mddev->array_sectors / 2, |
| 5491 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5492 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5493 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5494 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5495 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5496 | struct bitmap *bitmap; |
| 5497 | |
| 5498 | bitmap = bitmap_create(mddev, -1); |
| 5499 | if (IS_ERR(bitmap)) { |
| 5500 | err = PTR_ERR(bitmap); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5501 | pr_warn("%s: failed to create bitmap (%d)\n", |
| 5502 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5503 | } else |
| 5504 | mddev->bitmap = bitmap; |
| 5505 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5506 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5507 | if (err) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5508 | mddev_detach(mddev); |
NeilBrown | 0c35bd4 | 2015-03-13 11:51:18 +1100 | [diff] [blame] | 5509 | if (mddev->private) |
| 5510 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5511 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5512 | module_put(pers->owner); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5513 | bitmap_destroy(mddev); |
| 5514 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5515 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5516 | if (mddev->queue) { |
Shaohua Li | bb086a8 | 2016-09-30 09:45:40 -0700 | [diff] [blame] | 5517 | bool nonrot = true; |
| 5518 | |
| 5519 | rdev_for_each(rdev, mddev) { |
| 5520 | if (rdev->raid_disk >= 0 && |
| 5521 | !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) { |
| 5522 | nonrot = false; |
| 5523 | break; |
| 5524 | } |
| 5525 | } |
| 5526 | if (mddev->degraded) |
| 5527 | nonrot = false; |
| 5528 | if (nonrot) |
| 5529 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue); |
| 5530 | else |
| 5531 | queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue); |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 5532 | mddev->queue->backing_dev_info->congested_data = mddev; |
| 5533 | mddev->queue->backing_dev_info->congested_fn = md_congested; |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5534 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5535 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5536 | if (mddev->kobj.sd && |
| 5537 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5538 | pr_warn("md: cannot register extra attributes for %s\n", |
| 5539 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5540 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5541 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5542 | mddev->ro = 0; |
| 5543 | |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5544 | atomic_set(&mddev->max_corr_read_errors, |
| 5545 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5546 | mddev->safemode = 0; |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5547 | if (mddev_is_clustered(mddev)) |
| 5548 | mddev->safemode_delay = 0; |
| 5549 | else |
| 5550 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5551 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5552 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5553 | spin_lock(&mddev->lock); |
| 5554 | mddev->pers = pers; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5555 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5556 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5557 | if (rdev->raid_disk >= 0) |
| 5558 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5559 | /* failure here is OK */; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5560 | |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 5561 | if (mddev->degraded && !mddev->ro) |
| 5562 | /* This ensures that recovering status is reported immediately |
| 5563 | * via sysfs - until a lack of spares is confirmed. |
| 5564 | */ |
| 5565 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5567 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5568 | if (mddev->sb_flags) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5569 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5570 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5571 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5572 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5573 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5574 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5575 | return 0; |
| 5576 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5577 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5578 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5579 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5580 | { |
| 5581 | int err; |
| 5582 | |
| 5583 | err = md_run(mddev); |
| 5584 | if (err) |
| 5585 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5586 | err = bitmap_load(mddev); |
| 5587 | if (err) { |
| 5588 | bitmap_destroy(mddev); |
| 5589 | goto out; |
| 5590 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5591 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5592 | if (mddev_is_clustered(mddev)) |
| 5593 | md_allow_write(mddev); |
| 5594 | |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5595 | md_wakeup_thread(mddev->thread); |
| 5596 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5597 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5598 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5599 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5600 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5601 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5602 | out: |
| 5603 | return err; |
| 5604 | } |
| 5605 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5606 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5607 | { |
| 5608 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5609 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5610 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5612 | return -ENXIO; |
| 5613 | if (!mddev->pers) |
| 5614 | return -EINVAL; |
| 5615 | if (!mddev->ro) |
| 5616 | return -EBUSY; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 5617 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 5618 | struct md_rdev *rdev; |
| 5619 | bool has_journal = false; |
| 5620 | |
| 5621 | rcu_read_lock(); |
| 5622 | rdev_for_each_rcu(rdev, mddev) { |
| 5623 | if (test_bit(Journal, &rdev->flags) && |
| 5624 | !test_bit(Faulty, &rdev->flags)) { |
| 5625 | has_journal = true; |
| 5626 | break; |
| 5627 | } |
| 5628 | } |
| 5629 | rcu_read_unlock(); |
| 5630 | |
| 5631 | /* Don't restart rw with journal missing/faulty */ |
| 5632 | if (!has_journal) |
| 5633 | return -EINVAL; |
| 5634 | } |
| 5635 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5636 | mddev->safemode = 0; |
| 5637 | mddev->ro = 0; |
| 5638 | set_disk_ro(disk, 0); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5639 | pr_debug("md: %s switched to read-write mode.\n", mdname(mddev)); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5640 | /* Kick recovery or resync if necessary */ |
| 5641 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5642 | md_wakeup_thread(mddev->thread); |
| 5643 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5644 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5645 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | } |
| 5647 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5648 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5649 | { |
| 5650 | mddev->array_sectors = 0; |
| 5651 | mddev->external_size = 0; |
| 5652 | mddev->dev_sectors = 0; |
| 5653 | mddev->raid_disks = 0; |
| 5654 | mddev->recovery_cp = 0; |
| 5655 | mddev->resync_min = 0; |
| 5656 | mddev->resync_max = MaxSector; |
| 5657 | mddev->reshape_position = MaxSector; |
| 5658 | mddev->external = 0; |
| 5659 | mddev->persistent = 0; |
| 5660 | mddev->level = LEVEL_NONE; |
| 5661 | mddev->clevel[0] = 0; |
| 5662 | mddev->flags = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5663 | mddev->sb_flags = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5664 | mddev->ro = 0; |
| 5665 | mddev->metadata_type[0] = 0; |
| 5666 | mddev->chunk_sectors = 0; |
| 5667 | mddev->ctime = mddev->utime = 0; |
| 5668 | mddev->layout = 0; |
| 5669 | mddev->max_disks = 0; |
| 5670 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5671 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5672 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5673 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5674 | mddev->new_level = LEVEL_NONE; |
| 5675 | mddev->new_layout = 0; |
| 5676 | mddev->new_chunk_sectors = 0; |
| 5677 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5678 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5679 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5680 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5681 | mddev->recovery = 0; |
| 5682 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5683 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5684 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5685 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5686 | mddev->private = NULL; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 5687 | mddev->cluster_info = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5688 | mddev->bitmap_info.offset = 0; |
| 5689 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5690 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5691 | mddev->bitmap_info.chunksize = 0; |
| 5692 | mddev->bitmap_info.daemon_sleep = 0; |
| 5693 | mddev->bitmap_info.max_write_behind = 0; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 5694 | mddev->bitmap_info.nodes = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5695 | } |
| 5696 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5697 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5698 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 5699 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5700 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5701 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5702 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5703 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5704 | } |
| 5705 | |
| 5706 | del_timer_sync(&mddev->safemode_timer); |
| 5707 | |
Shaohua Li | 034e33f | 2016-11-21 10:29:19 -0800 | [diff] [blame] | 5708 | if (mddev->pers && mddev->pers->quiesce) { |
| 5709 | mddev->pers->quiesce(mddev, 1); |
| 5710 | mddev->pers->quiesce(mddev, 0); |
| 5711 | } |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5712 | bitmap_flush(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5713 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5714 | if (mddev->ro == 0 && |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5715 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5716 | mddev->sb_flags)) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5717 | /* mark array as shutdown cleanly */ |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5718 | if (!mddev_is_clustered(mddev)) |
| 5719 | mddev->in_sync = 1; |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5720 | md_update_sb(mddev, 1); |
| 5721 | } |
| 5722 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5723 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5724 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5725 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 5726 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5727 | __md_stop_writes(mddev); |
| 5728 | mddev_unlock(mddev); |
| 5729 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5730 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5731 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5732 | static void mddev_detach(struct mddev *mddev) |
| 5733 | { |
Guoqing Jiang | 48df498 | 2017-03-14 09:40:20 +0800 | [diff] [blame] | 5734 | bitmap_wait_behind_writes(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5735 | if (mddev->pers && mddev->pers->quiesce) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5736 | mddev->pers->quiesce(mddev, 1); |
| 5737 | mddev->pers->quiesce(mddev, 0); |
| 5738 | } |
| 5739 | md_unregister_thread(&mddev->thread); |
| 5740 | if (mddev->queue) |
| 5741 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 5742 | } |
| 5743 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5744 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5745 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5746 | struct md_personality *pers = mddev->pers; |
Guoqing Jiang | 48df498 | 2017-03-14 09:40:20 +0800 | [diff] [blame] | 5747 | bitmap_destroy(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5748 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 5749 | /* Ensure ->event_work is done */ |
| 5750 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5751 | spin_lock(&mddev->lock); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5752 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5753 | spin_unlock(&mddev->lock); |
| 5754 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5755 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5756 | if (pers->sync_request && mddev->to_remove == NULL) |
| 5757 | mddev->to_remove = &md_redundancy_group; |
| 5758 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5759 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5760 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5761 | |
| 5762 | void md_stop(struct mddev *mddev) |
| 5763 | { |
| 5764 | /* stop the array and free an attached data structures. |
| 5765 | * This is called from dm-raid |
| 5766 | */ |
| 5767 | __md_stop(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5768 | if (mddev->bio_set) |
| 5769 | bioset_free(mddev->bio_set); |
| 5770 | } |
| 5771 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5772 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5773 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5774 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5775 | { |
| 5776 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5777 | int did_freeze = 0; |
| 5778 | |
| 5779 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5780 | did_freeze = 1; |
| 5781 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5782 | md_wakeup_thread(mddev->thread); |
| 5783 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5784 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5785 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5786 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5787 | /* Thread might be blocked waiting for metadata update |
| 5788 | * which will now never happen */ |
| 5789 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5790 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5791 | if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5792 | return -EBUSY; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5793 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5794 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 5795 | &mddev->recovery)); |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5796 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5797 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5798 | mddev_lock_nointr(mddev); |
| 5799 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5800 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5801 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5802 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 5803 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5804 | pr_warn("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5805 | if (did_freeze) { |
| 5806 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5807 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5808 | md_wakeup_thread(mddev->thread); |
| 5809 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5810 | err = -EBUSY; |
| 5811 | goto out; |
| 5812 | } |
| 5813 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5814 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5815 | |
| 5816 | err = -ENXIO; |
| 5817 | if (mddev->ro==1) |
| 5818 | goto out; |
| 5819 | mddev->ro = 1; |
| 5820 | set_disk_ro(mddev->gendisk, 1); |
| 5821 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5822 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5823 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5824 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5825 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5826 | } |
| 5827 | out: |
| 5828 | mutex_unlock(&mddev->open_mutex); |
| 5829 | return err; |
| 5830 | } |
| 5831 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5832 | /* mode: |
| 5833 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5834 | * 2 - stop but do not disassemble array |
| 5835 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5836 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5837 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5838 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5840 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5841 | int did_freeze = 0; |
| 5842 | |
| 5843 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5844 | did_freeze = 1; |
| 5845 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5846 | md_wakeup_thread(mddev->thread); |
| 5847 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5848 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5849 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5850 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5851 | /* Thread might be blocked waiting for metadata update |
| 5852 | * which will now never happen */ |
| 5853 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5854 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5855 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5856 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 5857 | !test_bit(MD_RECOVERY_RUNNING, |
| 5858 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5859 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5860 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5861 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5862 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5863 | mddev->sysfs_active || |
| 5864 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 5865 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5866 | pr_warn("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5867 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5868 | if (did_freeze) { |
| 5869 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5870 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5871 | md_wakeup_thread(mddev->thread); |
| 5872 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 5873 | return -EBUSY; |
| 5874 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5875 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5876 | if (mddev->ro) |
| 5877 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5878 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5879 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5880 | __md_stop(mddev); |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 5881 | mddev->queue->backing_dev_info->congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5882 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5883 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5884 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5885 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5886 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5887 | if (rdev->raid_disk >= 0) |
| 5888 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5889 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5890 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5891 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5892 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5893 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5894 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5895 | if (mddev->ro) |
| 5896 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5897 | } else |
| 5898 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5899 | /* |
| 5900 | * Free resources if final stop |
| 5901 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5902 | if (mode == 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5903 | pr_info("md: %s stopped.\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5905 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5906 | struct file *f = mddev->bitmap_info.file; |
| 5907 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5908 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5909 | spin_unlock(&mddev->lock); |
| 5910 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5911 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5912 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5913 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | export_array(mddev); |
| 5915 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5916 | md_clean(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5917 | if (mddev->hold_active == UNTIL_STOP) |
| 5918 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5919 | } |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5920 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5921 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5922 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | } |
| 5924 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5925 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5926 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5927 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5928 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | int err; |
| 5930 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5931 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5934 | pr_info("md: running: "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5935 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5936 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | char b[BDEVNAME_SIZE]; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5938 | pr_cont("<%s>", bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5940 | pr_cont("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5941 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5942 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5944 | pr_warn("md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5945 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | } |
| 5947 | } |
| 5948 | |
| 5949 | /* |
| 5950 | * lets try to run arrays based on all disks that have arrived |
| 5951 | * until now. (those are in pending_raid_disks) |
| 5952 | * |
| 5953 | * the method: pick the first pending disk, collect all disks with |
| 5954 | * the same UUID, remove all from the pending list and put them into |
| 5955 | * the 'same_array' list. Then order this list based on superblock |
| 5956 | * update time (freshest comes first), kick out 'old' disks and |
| 5957 | * compare superblocks. If everything's fine then run it. |
| 5958 | * |
| 5959 | * If "unit" is allocated, then bump its reference count |
| 5960 | */ |
| 5961 | static void autorun_devices(int part) |
| 5962 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5963 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5964 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5965 | char b[BDEVNAME_SIZE]; |
| 5966 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5967 | pr_info("md: autorun ...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5969 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5970 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5971 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5973 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5974 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5975 | pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5976 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5977 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5978 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5979 | pr_debug("md: adding %s ...\n", |
| 5980 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5981 | list_move(&rdev->same_set, &candidates); |
| 5982 | } |
| 5983 | /* |
| 5984 | * now we have a set of devices, with all of them having |
| 5985 | * mostly sane superblocks. It's time to allocate the |
| 5986 | * mddev. |
| 5987 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5988 | if (part) { |
| 5989 | dev = MKDEV(mdp_major, |
| 5990 | rdev0->preferred_minor << MdpMinorShift); |
| 5991 | unit = MINOR(dev) >> MdpMinorShift; |
| 5992 | } else { |
| 5993 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5994 | unit = MINOR(dev); |
| 5995 | } |
| 5996 | if (rdev0->preferred_minor != unit) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5997 | pr_warn("md: unit number in %s is bad: %d\n", |
| 5998 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5999 | break; |
| 6000 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | |
| 6002 | md_probe(dev, NULL, NULL); |
| 6003 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 6004 | if (!mddev || !mddev->gendisk) { |
| 6005 | if (mddev) |
| 6006 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6007 | break; |
| 6008 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6009 | if (mddev_lock(mddev)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6010 | pr_warn("md: %s locked, cannot run\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6011 | else if (mddev->raid_disks || mddev->major_version |
| 6012 | || !list_empty(&mddev->disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6013 | pr_warn("md: %s already running, cannot run %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 6015 | mddev_unlock(mddev); |
| 6016 | } else { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6017 | pr_debug("md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 6018 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6019 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | list_del_init(&rdev->same_set); |
| 6021 | if (bind_rdev_to_array(rdev, mddev)) |
| 6022 | export_rdev(rdev); |
| 6023 | } |
| 6024 | autorun_array(mddev); |
| 6025 | mddev_unlock(mddev); |
| 6026 | } |
| 6027 | /* on success, candidates will be empty, on error |
| 6028 | * it won't... |
| 6029 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6030 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6031 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6032 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6033 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6034 | mddev_put(mddev); |
| 6035 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6036 | pr_info("md: ... autorun DONE.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6037 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6038 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6039 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6040 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6041 | { |
| 6042 | mdu_version_t ver; |
| 6043 | |
| 6044 | ver.major = MD_MAJOR_VERSION; |
| 6045 | ver.minor = MD_MINOR_VERSION; |
| 6046 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 6047 | |
| 6048 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 6049 | return -EFAULT; |
| 6050 | |
| 6051 | return 0; |
| 6052 | } |
| 6053 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6054 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6055 | { |
| 6056 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6057 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6058 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6060 | nr = working = insync = failed = spare = 0; |
| 6061 | rcu_read_lock(); |
| 6062 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6064 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | failed++; |
| 6066 | else { |
| 6067 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6068 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6069 | insync++; |
Song Liu | b347af8 | 2016-08-11 17:14:45 -0700 | [diff] [blame] | 6070 | else if (test_bit(Journal, &rdev->flags)) |
| 6071 | /* TODO: add journal count to md_u.h */ |
| 6072 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6073 | else |
| 6074 | spare++; |
| 6075 | } |
| 6076 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6077 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | |
| 6079 | info.major_version = mddev->major_version; |
| 6080 | info.minor_version = mddev->minor_version; |
| 6081 | info.patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6082 | info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6083 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6084 | info.size = mddev->dev_sectors / 2; |
| 6085 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 6086 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6087 | info.nr_disks = nr; |
| 6088 | info.raid_disks = mddev->raid_disks; |
| 6089 | info.md_minor = mddev->md_minor; |
| 6090 | info.not_persistent= !mddev->persistent; |
| 6091 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6092 | info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6093 | info.state = 0; |
| 6094 | if (mddev->in_sync) |
| 6095 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6096 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 6097 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 6098 | if (mddev_is_clustered(mddev)) |
| 6099 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6100 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6101 | info.working_disks = working; |
| 6102 | info.failed_disks = failed; |
| 6103 | info.spare_disks = spare; |
| 6104 | |
| 6105 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6106 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6107 | |
| 6108 | if (copy_to_user(arg, &info, sizeof(info))) |
| 6109 | return -EFAULT; |
| 6110 | |
| 6111 | return 0; |
| 6112 | } |
| 6113 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6114 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6115 | { |
| 6116 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6117 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6118 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6119 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 6120 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6121 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6122 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6123 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6124 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6125 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 6126 | /* bitmap enabled */ |
| 6127 | if (mddev->bitmap_info.file) { |
| 6128 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 6129 | sizeof(file->pathname)); |
| 6130 | if (IS_ERR(ptr)) |
| 6131 | err = PTR_ERR(ptr); |
| 6132 | else |
| 6133 | memmove(file->pathname, ptr, |
| 6134 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 6135 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6136 | spin_unlock(&mddev->lock); |
| 6137 | |
| 6138 | if (err == 0 && |
| 6139 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6140 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6141 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6142 | kfree(file); |
| 6143 | return err; |
| 6144 | } |
| 6145 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6146 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6147 | { |
| 6148 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6149 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6150 | |
| 6151 | if (copy_from_user(&info, arg, sizeof(info))) |
| 6152 | return -EFAULT; |
| 6153 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6154 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 6155 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6156 | if (rdev) { |
| 6157 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 6158 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 6159 | info.raid_disk = rdev->raid_disk; |
| 6160 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6161 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6162 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6163 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6164 | info.state |= (1<<MD_DISK_ACTIVE); |
| 6165 | info.state |= (1<<MD_DISK_SYNC); |
| 6166 | } |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 6167 | if (test_bit(Journal, &rdev->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6168 | info.state |= (1<<MD_DISK_JOURNAL); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6169 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6170 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6171 | if (test_bit(FailFast, &rdev->flags)) |
| 6172 | info.state |= (1<<MD_DISK_FAILFAST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6173 | } else { |
| 6174 | info.major = info.minor = 0; |
| 6175 | info.raid_disk = -1; |
| 6176 | info.state = (1<<MD_DISK_REMOVED); |
| 6177 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6178 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | |
| 6180 | if (copy_to_user(arg, &info, sizeof(info))) |
| 6181 | return -EFAULT; |
| 6182 | |
| 6183 | return 0; |
| 6184 | } |
| 6185 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6186 | 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] | 6187 | { |
| 6188 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6189 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6190 | dev_t dev = MKDEV(info->major,info->minor); |
| 6191 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6192 | if (mddev_is_clustered(mddev) && |
| 6193 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6194 | pr_warn("%s: Cannot add to clustered mddev.\n", |
| 6195 | mdname(mddev)); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6196 | return -EINVAL; |
| 6197 | } |
| 6198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6199 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 6200 | return -EOVERFLOW; |
| 6201 | |
| 6202 | if (!mddev->raid_disks) { |
| 6203 | int err; |
| 6204 | /* expecting a device which has a superblock */ |
| 6205 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 6206 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6207 | pr_warn("md: md_import_device returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6208 | PTR_ERR(rdev)); |
| 6209 | return PTR_ERR(rdev); |
| 6210 | } |
| 6211 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6212 | struct md_rdev *rdev0 |
| 6213 | = list_entry(mddev->disks.next, |
| 6214 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6215 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6216 | .load_super(rdev, rdev0, mddev->minor_version); |
| 6217 | if (err < 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6218 | pr_warn("md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6219 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6220 | bdevname(rdev0->bdev,b2)); |
| 6221 | export_rdev(rdev); |
| 6222 | return -EINVAL; |
| 6223 | } |
| 6224 | } |
| 6225 | err = bind_rdev_to_array(rdev, mddev); |
| 6226 | if (err) |
| 6227 | export_rdev(rdev); |
| 6228 | return err; |
| 6229 | } |
| 6230 | |
| 6231 | /* |
| 6232 | * add_new_disk can be used once the array is assembled |
| 6233 | * to add "hot spares". They must already have a superblock |
| 6234 | * written |
| 6235 | */ |
| 6236 | if (mddev->pers) { |
| 6237 | int err; |
| 6238 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6239 | pr_warn("%s: personality does not support diskops!\n", |
| 6240 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6241 | return -EINVAL; |
| 6242 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 6243 | if (mddev->persistent) |
| 6244 | rdev = md_import_device(dev, mddev->major_version, |
| 6245 | mddev->minor_version); |
| 6246 | else |
| 6247 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6248 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6249 | pr_warn("md: md_import_device returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6250 | PTR_ERR(rdev)); |
| 6251 | return PTR_ERR(rdev); |
| 6252 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 6253 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6254 | if (!mddev->persistent) { |
| 6255 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6256 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6257 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6258 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 6259 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6260 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6261 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 6262 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6263 | } else |
| 6264 | super_types[mddev->major_version]. |
| 6265 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6266 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 6267 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6268 | /* This was a hot-add request, but events doesn't |
| 6269 | * match, so reject it. |
| 6270 | */ |
| 6271 | export_rdev(rdev); |
| 6272 | return -EINVAL; |
| 6273 | } |
| 6274 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6275 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6276 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6277 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6278 | else |
| 6279 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6280 | if (info->state & (1<<MD_DISK_FAILFAST)) |
| 6281 | set_bit(FailFast, &rdev->flags); |
| 6282 | else |
| 6283 | clear_bit(FailFast, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6284 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6285 | if (info->state & (1<<MD_DISK_JOURNAL)) { |
| 6286 | struct md_rdev *rdev2; |
| 6287 | bool has_journal = false; |
| 6288 | |
| 6289 | /* make sure no existing journal disk */ |
| 6290 | rdev_for_each(rdev2, mddev) { |
| 6291 | if (test_bit(Journal, &rdev2->flags)) { |
| 6292 | has_journal = true; |
| 6293 | break; |
| 6294 | } |
| 6295 | } |
| 6296 | if (has_journal) { |
| 6297 | export_rdev(rdev); |
| 6298 | return -EBUSY; |
| 6299 | } |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6300 | set_bit(Journal, &rdev->flags); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6301 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6302 | /* |
| 6303 | * check whether the device shows up in other nodes |
| 6304 | */ |
| 6305 | if (mddev_is_clustered(mddev)) { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6306 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6307 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6308 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6309 | /* --add initiated by this node */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6310 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6311 | if (err) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6312 | export_rdev(rdev); |
| 6313 | return err; |
| 6314 | } |
| 6315 | } |
| 6316 | } |
| 6317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6318 | rdev->raid_disk = -1; |
| 6319 | err = bind_rdev_to_array(rdev, mddev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6321 | if (err) |
| 6322 | export_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6323 | |
| 6324 | if (mddev_is_clustered(mddev)) { |
Guoqing Jiang | e566aef | 2016-08-12 13:42:34 +0800 | [diff] [blame] | 6325 | if (info->state & (1 << MD_DISK_CANDIDATE)) { |
| 6326 | if (!err) { |
| 6327 | err = md_cluster_ops->new_disk_ack(mddev, |
| 6328 | err == 0); |
| 6329 | if (err) |
| 6330 | md_kick_rdev_from_array(rdev); |
| 6331 | } |
| 6332 | } else { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6333 | if (err) |
| 6334 | md_cluster_ops->add_new_disk_cancel(mddev); |
| 6335 | else |
| 6336 | err = add_bound_rdev(rdev); |
| 6337 | } |
| 6338 | |
| 6339 | } else if (!err) |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 6340 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6342 | return err; |
| 6343 | } |
| 6344 | |
| 6345 | /* otherwise, add_new_disk is only allowed |
| 6346 | * for major_version==0 superblocks |
| 6347 | */ |
| 6348 | if (mddev->major_version != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6349 | pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6350 | return -EINVAL; |
| 6351 | } |
| 6352 | |
| 6353 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 6354 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6355 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6356 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6357 | pr_warn("md: error, md_import_device() returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6358 | PTR_ERR(rdev)); |
| 6359 | return PTR_ERR(rdev); |
| 6360 | } |
| 6361 | rdev->desc_nr = info->number; |
| 6362 | if (info->raid_disk < mddev->raid_disks) |
| 6363 | rdev->raid_disk = info->raid_disk; |
| 6364 | else |
| 6365 | rdev->raid_disk = -1; |
| 6366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6367 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6368 | if (info->state & (1<<MD_DISK_SYNC)) |
| 6369 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6370 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6371 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6372 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6373 | if (info->state & (1<<MD_DISK_FAILFAST)) |
| 6374 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6376 | if (!mddev->persistent) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6377 | pr_debug("md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6378 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 6379 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6380 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6381 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6382 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6383 | err = bind_rdev_to_array(rdev, mddev); |
| 6384 | if (err) { |
| 6385 | export_rdev(rdev); |
| 6386 | return err; |
| 6387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6388 | } |
| 6389 | |
| 6390 | return 0; |
| 6391 | } |
| 6392 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6393 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | { |
| 6395 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6396 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | rdev = find_rdev(mddev, dev); |
| 6399 | if (!rdev) |
| 6400 | return -ENXIO; |
| 6401 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6402 | if (rdev->raid_disk < 0) |
| 6403 | goto kick_rdev; |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6404 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6405 | clear_bit(Blocked, &rdev->flags); |
| 6406 | remove_and_add_spares(mddev, rdev); |
| 6407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6408 | if (rdev->raid_disk >= 0) |
| 6409 | goto busy; |
| 6410 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6411 | kick_rdev: |
Goldwyn Rodrigues | 54a8839 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6412 | if (mddev_is_clustered(mddev)) |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6413 | md_cluster_ops->remove_disk(mddev, rdev); |
| 6414 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6415 | md_kick_rdev_from_array(rdev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6416 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 6417 | if (mddev->thread) |
| 6418 | md_wakeup_thread(mddev->thread); |
| 6419 | else |
| 6420 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6421 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6422 | |
| 6423 | return 0; |
| 6424 | busy: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6425 | pr_debug("md: cannot remove active disk %s from %s ...\n", |
| 6426 | bdevname(rdev->bdev,b), mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6427 | return -EBUSY; |
| 6428 | } |
| 6429 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6430 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6431 | { |
| 6432 | char b[BDEVNAME_SIZE]; |
| 6433 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6434 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6435 | |
| 6436 | if (!mddev->pers) |
| 6437 | return -ENODEV; |
| 6438 | |
| 6439 | if (mddev->major_version != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6440 | 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] | 6441 | mdname(mddev)); |
| 6442 | return -EINVAL; |
| 6443 | } |
| 6444 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6445 | pr_warn("%s: personality does not support diskops!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6446 | mdname(mddev)); |
| 6447 | return -EINVAL; |
| 6448 | } |
| 6449 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6450 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6451 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6452 | pr_warn("md: error, md_import_device() returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | PTR_ERR(rdev)); |
| 6454 | return -EINVAL; |
| 6455 | } |
| 6456 | |
| 6457 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6458 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6459 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6460 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6461 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6462 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6463 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6464 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6465 | 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] | 6466 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6467 | err = -EINVAL; |
| 6468 | goto abort_export; |
| 6469 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6470 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6471 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 6473 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6474 | err = bind_rdev_to_array(rdev, mddev); |
| 6475 | if (err) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 6476 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6477 | |
| 6478 | /* |
| 6479 | * The rest should better be atomic, we can have disk failures |
| 6480 | * noticed in interrupt contexts ... |
| 6481 | */ |
| 6482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6483 | rdev->raid_disk = -1; |
| 6484 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6485 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 6486 | if (!mddev->thread) |
| 6487 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6488 | /* |
| 6489 | * Kick recovery, maybe this spare has to be added to the |
| 6490 | * array immediately. |
| 6491 | */ |
| 6492 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6493 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6494 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | return 0; |
| 6496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6497 | abort_export: |
| 6498 | export_rdev(rdev); |
| 6499 | return err; |
| 6500 | } |
| 6501 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6502 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6503 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6504 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6505 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6506 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 6507 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6508 | return -EBUSY; |
| 6509 | if (mddev->recovery || mddev->sync_thread) |
| 6510 | return -EBUSY; |
| 6511 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6512 | } |
| 6513 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6514 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6515 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6516 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6517 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6518 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 6519 | return -EEXIST; /* cannot add when bitmap is present */ |
| 6520 | f = fget(fd); |
| 6521 | |
| 6522 | if (f == NULL) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6523 | pr_warn("%s: error: failed to get bitmap file\n", |
| 6524 | mdname(mddev)); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6525 | return -EBADF; |
| 6526 | } |
| 6527 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6528 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6529 | if (!S_ISREG(inode->i_mode)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6530 | pr_warn("%s: error: bitmap file must be a regular file\n", |
| 6531 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6532 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6533 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6534 | pr_warn("%s: error: bitmap file must open for write\n", |
| 6535 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6536 | err = -EBADF; |
| 6537 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6538 | pr_warn("%s: error: bitmap file is already in use\n", |
| 6539 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6540 | err = -EBUSY; |
| 6541 | } |
| 6542 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6543 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6544 | return err; |
| 6545 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6546 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6547 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6548 | } else if (mddev->bitmap == NULL) |
| 6549 | return -ENOENT; /* cannot remove what isn't there */ |
| 6550 | err = 0; |
| 6551 | if (mddev->pers) { |
| 6552 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6553 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6554 | struct bitmap *bitmap; |
| 6555 | |
| 6556 | bitmap = bitmap_create(mddev, -1); |
| 6557 | if (!IS_ERR(bitmap)) { |
| 6558 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6559 | err = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6560 | } else |
| 6561 | err = PTR_ERR(bitmap); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6562 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6563 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6564 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6565 | fd = -1; /* make sure to put the file */ |
| 6566 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6567 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6568 | } |
| 6569 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6570 | struct file *f = mddev->bitmap_info.file; |
| 6571 | if (f) { |
| 6572 | spin_lock(&mddev->lock); |
| 6573 | mddev->bitmap_info.file = NULL; |
| 6574 | spin_unlock(&mddev->lock); |
| 6575 | fput(f); |
| 6576 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6577 | } |
| 6578 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6579 | return err; |
| 6580 | } |
| 6581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6582 | /* |
| 6583 | * set_array_info is used two different ways |
| 6584 | * The original usage is when creating a new array. |
| 6585 | * In this usage, raid_disks is > 0 and it together with |
| 6586 | * level, size, not_persistent,layout,chunksize determine the |
| 6587 | * shape of the array. |
| 6588 | * This will always create an array with a type-0.90.0 superblock. |
| 6589 | * The newer usage is when assembling an array. |
| 6590 | * In this case raid_disks will be 0, and the major_version field is |
| 6591 | * use to determine which style super-blocks are to be found on the devices. |
| 6592 | * The minor and patch _version numbers are also kept incase the |
| 6593 | * super_block handler wishes to interpret them. |
| 6594 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6595 | 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] | 6596 | { |
| 6597 | |
| 6598 | if (info->raid_disks == 0) { |
| 6599 | /* just setting version number for superblock loading */ |
| 6600 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6601 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6602 | super_types[info->major_version].name == NULL) { |
| 6603 | /* maybe try to auto-load a module? */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6604 | pr_warn("md: superblock version %d not known\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6605 | info->major_version); |
| 6606 | return -EINVAL; |
| 6607 | } |
| 6608 | mddev->major_version = info->major_version; |
| 6609 | mddev->minor_version = info->minor_version; |
| 6610 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6611 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6612 | /* ensure mddev_put doesn't delete this now that there |
| 6613 | * is some minimal configuration. |
| 6614 | */ |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6615 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6616 | return 0; |
| 6617 | } |
| 6618 | mddev->major_version = MD_MAJOR_VERSION; |
| 6619 | mddev->minor_version = MD_MINOR_VERSION; |
| 6620 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6621 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6622 | |
| 6623 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6624 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6625 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6626 | mddev->raid_disks = info->raid_disks; |
| 6627 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6628 | * openned |
| 6629 | */ |
| 6630 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6631 | mddev->recovery_cp = MaxSector; |
| 6632 | else |
| 6633 | mddev->recovery_cp = 0; |
| 6634 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6635 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6636 | |
| 6637 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6638 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6639 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6640 | if (mddev->persistent) { |
NeilBrown | 1b3bae4 | 2017-03-01 07:31:28 +1100 | [diff] [blame] | 6641 | mddev->max_disks = MD_SB_DISKS; |
| 6642 | mddev->flags = 0; |
| 6643 | mddev->sb_flags = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6644 | } |
| 6645 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6646 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6647 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6648 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6649 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6650 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6651 | mddev->reshape_position = MaxSector; |
| 6652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6653 | /* |
| 6654 | * Generate a 128 bit UUID |
| 6655 | */ |
| 6656 | get_random_bytes(mddev->uuid, 16); |
| 6657 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6658 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6659 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6660 | mddev->new_layout = mddev->layout; |
| 6661 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6662 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6664 | return 0; |
| 6665 | } |
| 6666 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6667 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6668 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6669 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6670 | |
| 6671 | if (mddev->external_size) |
| 6672 | return; |
| 6673 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6674 | mddev->array_sectors = array_sectors; |
| 6675 | } |
| 6676 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6677 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6678 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6679 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6680 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6681 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6682 | int fit = (num_sectors == 0); |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 6683 | sector_t old_dev_sectors = mddev->dev_sectors; |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 6684 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6685 | if (mddev->pers->resize == NULL) |
| 6686 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6687 | /* The "num_sectors" is the number of sectors of each device that |
| 6688 | * is used. This can only make sense for arrays with redundancy. |
| 6689 | * linear and raid0 always use whatever space is available. We can only |
| 6690 | * consider changing this number if no resync or reconstruction is |
| 6691 | * 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] | 6692 | * 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] | 6693 | * of each device. If num_sectors is zero, we find the largest size |
| 6694 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6695 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6696 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6697 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6698 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6699 | if (mddev->ro) |
| 6700 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6701 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6702 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6703 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6704 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6705 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6706 | num_sectors = avail; |
| 6707 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6708 | return -ENOSPC; |
| 6709 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6710 | rv = mddev->pers->resize(mddev, num_sectors); |
Guoqing Jiang | c948363 | 2017-02-24 11:15:23 +0800 | [diff] [blame] | 6711 | if (!rv) { |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 6712 | if (mddev_is_clustered(mddev)) |
| 6713 | md_cluster_ops->update_size(mddev, old_dev_sectors); |
| 6714 | else if (mddev->queue) { |
Guoqing Jiang | c948363 | 2017-02-24 11:15:23 +0800 | [diff] [blame] | 6715 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 6716 | revalidate_disk(mddev->gendisk); |
| 6717 | } |
| 6718 | } |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6719 | return rv; |
| 6720 | } |
| 6721 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6722 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6723 | { |
| 6724 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6725 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6726 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6727 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6728 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6729 | if (mddev->ro) |
| 6730 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6731 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6732 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6733 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6734 | if (mddev->sync_thread || |
| 6735 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6736 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6737 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6738 | |
| 6739 | rdev_for_each(rdev, mddev) { |
| 6740 | if (mddev->raid_disks < raid_disks && |
| 6741 | rdev->data_offset < rdev->new_data_offset) |
| 6742 | return -EINVAL; |
| 6743 | if (mddev->raid_disks > raid_disks && |
| 6744 | rdev->data_offset > rdev->new_data_offset) |
| 6745 | return -EINVAL; |
| 6746 | } |
| 6747 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6748 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6749 | if (mddev->delta_disks < 0) |
| 6750 | mddev->reshape_backwards = 1; |
| 6751 | else if (mddev->delta_disks > 0) |
| 6752 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6753 | |
| 6754 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6755 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6756 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6757 | mddev->reshape_backwards = 0; |
| 6758 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6759 | return rv; |
| 6760 | } |
| 6761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | /* |
| 6763 | * update_array_info is used to change the configuration of an |
| 6764 | * on-line array. |
| 6765 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6766 | * fields in the info are checked against the array. |
| 6767 | * Any differences that cannot be handled will cause an error. |
| 6768 | * Normally, only one change can be managed at a time. |
| 6769 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6770 | 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] | 6771 | { |
| 6772 | int rv = 0; |
| 6773 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6774 | int state = 0; |
| 6775 | |
| 6776 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6777 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6778 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6779 | |
| 6780 | if (mddev->major_version != info->major_version || |
| 6781 | mddev->minor_version != info->minor_version || |
| 6782 | /* mddev->patch_version != info->patch_version || */ |
| 6783 | mddev->ctime != info->ctime || |
| 6784 | mddev->level != info->level || |
| 6785 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 6786 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6787 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6788 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6789 | ((state^info->state) & 0xfffffe00) |
| 6790 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6791 | return -EINVAL; |
| 6792 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6793 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6794 | cnt++; |
| 6795 | if (mddev->raid_disks != info->raid_disks) |
| 6796 | cnt++; |
| 6797 | if (mddev->layout != info->layout) |
| 6798 | cnt++; |
| 6799 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6800 | cnt++; |
| 6801 | if (cnt == 0) |
| 6802 | return 0; |
| 6803 | if (cnt > 1) |
| 6804 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6805 | |
| 6806 | if (mddev->layout != info->layout) { |
| 6807 | /* Change layout |
| 6808 | * we don't need to do anything at the md level, the |
| 6809 | * personality will take care of it all. |
| 6810 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6811 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6812 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6813 | else { |
| 6814 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6815 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6816 | if (rv) |
| 6817 | mddev->new_layout = mddev->layout; |
| 6818 | return rv; |
| 6819 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6820 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6821 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6822 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6823 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6824 | if (mddev->raid_disks != info->raid_disks) |
| 6825 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6826 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6827 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6828 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 6829 | rv = -EINVAL; |
| 6830 | goto err; |
| 6831 | } |
| 6832 | if (mddev->recovery || mddev->sync_thread) { |
| 6833 | rv = -EBUSY; |
| 6834 | goto err; |
| 6835 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6836 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6837 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6838 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6839 | if (mddev->bitmap) { |
| 6840 | rv = -EEXIST; |
| 6841 | goto err; |
| 6842 | } |
| 6843 | if (mddev->bitmap_info.default_offset == 0) { |
| 6844 | rv = -EINVAL; |
| 6845 | goto err; |
| 6846 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6847 | mddev->bitmap_info.offset = |
| 6848 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6849 | mddev->bitmap_info.space = |
| 6850 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6851 | mddev->pers->quiesce(mddev, 1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6852 | bitmap = bitmap_create(mddev, -1); |
| 6853 | if (!IS_ERR(bitmap)) { |
| 6854 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6855 | rv = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6856 | } else |
| 6857 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6858 | if (rv) |
| 6859 | bitmap_destroy(mddev); |
| 6860 | mddev->pers->quiesce(mddev, 0); |
| 6861 | } else { |
| 6862 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6863 | if (!mddev->bitmap) { |
| 6864 | rv = -ENOENT; |
| 6865 | goto err; |
| 6866 | } |
| 6867 | if (mddev->bitmap->storage.file) { |
| 6868 | rv = -EINVAL; |
| 6869 | goto err; |
| 6870 | } |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6871 | if (mddev->bitmap_info.nodes) { |
| 6872 | /* hold PW on all the bitmap lock */ |
| 6873 | if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6874 | 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] | 6875 | rv = -EPERM; |
| 6876 | md_cluster_ops->unlock_all_bitmaps(mddev); |
| 6877 | goto err; |
| 6878 | } |
| 6879 | |
| 6880 | mddev->bitmap_info.nodes = 0; |
| 6881 | md_cluster_ops->leave(mddev); |
| 6882 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6883 | mddev->pers->quiesce(mddev, 1); |
| 6884 | bitmap_destroy(mddev); |
| 6885 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6886 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6887 | } |
| 6888 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6889 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6890 | return rv; |
| 6891 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | return rv; |
| 6893 | } |
| 6894 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6895 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6896 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6897 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6898 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6899 | |
| 6900 | if (mddev->pers == NULL) |
| 6901 | return -ENODEV; |
| 6902 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6903 | rcu_read_lock(); |
| 6904 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6905 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6906 | err = -ENODEV; |
| 6907 | else { |
| 6908 | md_error(mddev, rdev); |
| 6909 | if (!test_bit(Faulty, &rdev->flags)) |
| 6910 | err = -EBUSY; |
| 6911 | } |
| 6912 | rcu_read_unlock(); |
| 6913 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6914 | } |
| 6915 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6916 | /* |
| 6917 | * We have a problem here : there is no easy way to give a CHS |
| 6918 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6919 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6920 | * dosfs just mad... ;-) |
| 6921 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6922 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6923 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6924 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6925 | |
| 6926 | geo->heads = 2; |
| 6927 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6928 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6929 | return 0; |
| 6930 | } |
| 6931 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6932 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 6933 | { |
| 6934 | switch (cmd) { |
| 6935 | case ADD_NEW_DISK: |
| 6936 | case BLKROSET: |
| 6937 | case GET_ARRAY_INFO: |
| 6938 | case GET_BITMAP_FILE: |
| 6939 | case GET_DISK_INFO: |
| 6940 | case HOT_ADD_DISK: |
| 6941 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6942 | case RAID_AUTORUN: |
| 6943 | case RAID_VERSION: |
| 6944 | case RESTART_ARRAY_RW: |
| 6945 | case RUN_ARRAY: |
| 6946 | case SET_ARRAY_INFO: |
| 6947 | case SET_BITMAP_FILE: |
| 6948 | case SET_DISK_FAULTY: |
| 6949 | case STOP_ARRAY: |
| 6950 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6951 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6952 | return true; |
| 6953 | default: |
| 6954 | return false; |
| 6955 | } |
| 6956 | } |
| 6957 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6958 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6959 | unsigned int cmd, unsigned long arg) |
| 6960 | { |
| 6961 | int err = 0; |
| 6962 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6963 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6964 | int ro; |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 6965 | bool did_set_md_closing = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6966 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6967 | if (!md_ioctl_valid(cmd)) |
| 6968 | return -ENOTTY; |
| 6969 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6970 | switch (cmd) { |
| 6971 | case RAID_VERSION: |
| 6972 | case GET_ARRAY_INFO: |
| 6973 | case GET_DISK_INFO: |
| 6974 | break; |
| 6975 | default: |
| 6976 | if (!capable(CAP_SYS_ADMIN)) |
| 6977 | return -EACCES; |
| 6978 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6979 | |
| 6980 | /* |
| 6981 | * Commands dealing with the RAID driver but not any |
| 6982 | * particular array: |
| 6983 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6984 | switch (cmd) { |
| 6985 | case RAID_VERSION: |
| 6986 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6987 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6989 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6990 | case RAID_AUTORUN: |
| 6991 | err = 0; |
| 6992 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6993 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6994 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6995 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6996 | } |
| 6997 | |
| 6998 | /* |
| 6999 | * Commands creating/starting a new array: |
| 7000 | */ |
| 7001 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7002 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7003 | |
| 7004 | if (!mddev) { |
| 7005 | BUG(); |
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 | } |
| 7008 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7009 | /* Some actions do not requires the mutex */ |
| 7010 | switch (cmd) { |
| 7011 | case GET_ARRAY_INFO: |
| 7012 | if (!mddev->raid_disks && !mddev->external) |
| 7013 | err = -ENODEV; |
| 7014 | else |
| 7015 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7016 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7017 | |
| 7018 | case GET_DISK_INFO: |
| 7019 | if (!mddev->raid_disks && !mddev->external) |
| 7020 | err = -ENODEV; |
| 7021 | else |
| 7022 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7023 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7024 | |
| 7025 | case SET_DISK_FAULTY: |
| 7026 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7027 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7028 | |
| 7029 | case GET_BITMAP_FILE: |
| 7030 | err = get_bitmap_file(mddev, argp); |
| 7031 | goto out; |
| 7032 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7033 | } |
| 7034 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 7035 | if (cmd == ADD_NEW_DISK) |
| 7036 | /* need to ensure md_delayed_delete() has completed */ |
| 7037 | flush_workqueue(md_misc_wq); |
| 7038 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7039 | if (cmd == HOT_REMOVE_DISK) |
| 7040 | /* need to ensure recovery thread has run */ |
| 7041 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 7042 | !test_bit(MD_RECOVERY_NEEDED, |
Shaohua Li | 82a301c | 2016-12-08 15:48:18 -0800 | [diff] [blame] | 7043 | &mddev->recovery), |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7044 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7045 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 7046 | /* Need to flush page cache, and ensure no-one else opens |
| 7047 | * and writes |
| 7048 | */ |
| 7049 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 7050 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7051 | mutex_unlock(&mddev->open_mutex); |
| 7052 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7053 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7054 | } |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7055 | WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags)); |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7056 | set_bit(MD_CLOSING, &mddev->flags); |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7057 | did_set_md_closing = true; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7058 | mutex_unlock(&mddev->open_mutex); |
| 7059 | sync_blockdev(bdev); |
| 7060 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7061 | err = mddev_lock(mddev); |
| 7062 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7063 | pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 7064 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7065 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7066 | } |
| 7067 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7068 | if (cmd == SET_ARRAY_INFO) { |
| 7069 | mdu_array_info_t info; |
| 7070 | if (!arg) |
| 7071 | memset(&info, 0, sizeof(info)); |
| 7072 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 7073 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7074 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7075 | } |
| 7076 | if (mddev->pers) { |
| 7077 | err = update_array_info(mddev, &info); |
| 7078 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7079 | pr_warn("md: couldn't update array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7080 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7081 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7082 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7083 | } |
| 7084 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7085 | pr_warn("md: array %s already has disks!\n", mdname(mddev)); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7086 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7087 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7088 | } |
| 7089 | if (mddev->raid_disks) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7090 | pr_warn("md: array %s already initialised!\n", mdname(mddev)); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7091 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7092 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7093 | } |
| 7094 | err = set_array_info(mddev, &info); |
| 7095 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7096 | pr_warn("md: couldn't set array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7097 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7098 | } |
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 | } |
| 7101 | |
| 7102 | /* |
| 7103 | * Commands querying/configuring an existing array: |
| 7104 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7105 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 7106 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 7107 | if ((!mddev->raid_disks && !mddev->external) |
| 7108 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 7109 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 7110 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7111 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7112 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7113 | } |
| 7114 | |
| 7115 | /* |
| 7116 | * Commands even a read-only array can execute: |
| 7117 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7118 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7119 | case RESTART_ARRAY_RW: |
| 7120 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7121 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7122 | |
| 7123 | case STOP_ARRAY: |
| 7124 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7125 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7126 | |
| 7127 | case STOP_ARRAY_RO: |
| 7128 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7129 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7130 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7131 | case HOT_REMOVE_DISK: |
| 7132 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7133 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7134 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7135 | case ADD_NEW_DISK: |
| 7136 | /* We can support ADD_NEW_DISK on read-only arrays |
Wei Fang | 466ad29 | 2016-03-21 19:19:30 +0800 | [diff] [blame] | 7137 | * only if we are re-adding a preexisting device. |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7138 | * So require mddev->pers and MD_DISK_SYNC. |
| 7139 | */ |
| 7140 | if (mddev->pers) { |
| 7141 | mdu_disk_info_t info; |
| 7142 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7143 | err = -EFAULT; |
| 7144 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 7145 | /* Need to clear read-only for this */ |
| 7146 | break; |
| 7147 | else |
| 7148 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7149 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7150 | } |
| 7151 | break; |
| 7152 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7153 | case BLKROSET: |
| 7154 | if (get_user(ro, (int __user *)(arg))) { |
| 7155 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7156 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7157 | } |
| 7158 | err = -EINVAL; |
| 7159 | |
| 7160 | /* if the bdev is going readonly the value of mddev->ro |
| 7161 | * does not matter, no writes are coming |
| 7162 | */ |
| 7163 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7164 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7165 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7166 | /* are we are already prepared for writes? */ |
| 7167 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7168 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7169 | |
| 7170 | /* transitioning to readauto need only happen for |
| 7171 | * arrays that call md_write_start |
| 7172 | */ |
| 7173 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7174 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7175 | if (err == 0) { |
| 7176 | mddev->ro = 2; |
| 7177 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 7178 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7179 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7180 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7181 | } |
| 7182 | |
| 7183 | /* |
| 7184 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7185 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7186 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 7187 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7188 | if (mddev->ro == 2) { |
| 7189 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7190 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7191 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7192 | /* mddev_unlock will wake thread */ |
| 7193 | /* If a device failed while we were read-only, we |
| 7194 | * need to make sure the metadata is updated now. |
| 7195 | */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7196 | if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) { |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7197 | mddev_unlock(mddev); |
| 7198 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7199 | !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) && |
| 7200 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 7201 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7202 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7203 | } else { |
| 7204 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7205 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7206 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7207 | } |
| 7208 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7209 | switch (cmd) { |
| 7210 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7211 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7212 | mdu_disk_info_t info; |
| 7213 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7214 | err = -EFAULT; |
| 7215 | else |
| 7216 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7217 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7219 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 7220 | case CLUSTERED_DISK_NACK: |
| 7221 | if (mddev_is_clustered(mddev)) |
| 7222 | md_cluster_ops->new_disk_ack(mddev, false); |
| 7223 | else |
| 7224 | err = -EINVAL; |
| 7225 | goto unlock; |
| 7226 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7227 | case HOT_ADD_DISK: |
| 7228 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7229 | goto unlock; |
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 | case RUN_ARRAY: |
| 7232 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7233 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7234 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7235 | case SET_BITMAP_FILE: |
| 7236 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7237 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7238 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7239 | default: |
| 7240 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7241 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7242 | } |
| 7243 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7244 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7245 | if (mddev->hold_active == UNTIL_IOCTL && |
| 7246 | err != -EINVAL) |
| 7247 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7248 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7249 | out: |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7250 | if(did_set_md_closing) |
| 7251 | clear_bit(MD_CLOSING, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7252 | return err; |
| 7253 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7254 | #ifdef CONFIG_COMPAT |
| 7255 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 7256 | unsigned int cmd, unsigned long arg) |
| 7257 | { |
| 7258 | switch (cmd) { |
| 7259 | case HOT_REMOVE_DISK: |
| 7260 | case HOT_ADD_DISK: |
| 7261 | case SET_DISK_FAULTY: |
| 7262 | case SET_BITMAP_FILE: |
| 7263 | /* These take in integer arg, do not convert */ |
| 7264 | break; |
| 7265 | default: |
| 7266 | arg = (unsigned long)compat_ptr(arg); |
| 7267 | break; |
| 7268 | } |
| 7269 | |
| 7270 | return md_ioctl(bdev, mode, cmd, arg); |
| 7271 | } |
| 7272 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7273 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7274 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7275 | { |
| 7276 | /* |
| 7277 | * Succeed if we can lock the mddev, which confirms that |
| 7278 | * it isn't being stopped right now. |
| 7279 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7280 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7281 | int err; |
| 7282 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 7283 | if (!mddev) |
| 7284 | return -ENODEV; |
| 7285 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7286 | if (mddev->gendisk != bdev->bd_disk) { |
| 7287 | /* we are racing with mddev_put which is discarding this |
| 7288 | * bd_disk. |
| 7289 | */ |
| 7290 | mddev_put(mddev); |
| 7291 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7292 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7293 | /* Then retry the open from the top */ |
| 7294 | return -ERESTARTSYS; |
| 7295 | } |
| 7296 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 7297 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7298 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7299 | goto out; |
| 7300 | |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7301 | if (test_bit(MD_CLOSING, &mddev->flags)) { |
| 7302 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | e2342ca | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7303 | err = -ENODEV; |
| 7304 | goto out; |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7305 | } |
| 7306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7307 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7308 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7309 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7310 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7311 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7312 | out: |
NeilBrown | e2342ca | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7313 | if (err) |
| 7314 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7315 | return err; |
| 7316 | } |
| 7317 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 7318 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7319 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7320 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7321 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 7322 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7323 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7324 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7325 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7326 | |
| 7327 | static int md_media_changed(struct gendisk *disk) |
| 7328 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7329 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7330 | |
| 7331 | return mddev->changed; |
| 7332 | } |
| 7333 | |
| 7334 | static int md_revalidate(struct gendisk *disk) |
| 7335 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7336 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7337 | |
| 7338 | mddev->changed = 0; |
| 7339 | return 0; |
| 7340 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 7341 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7342 | { |
| 7343 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7344 | .open = md_open, |
| 7345 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 7346 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7347 | #ifdef CONFIG_COMPAT |
| 7348 | .compat_ioctl = md_compat_ioctl, |
| 7349 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7350 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7351 | .media_changed = md_media_changed, |
| 7352 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7353 | }; |
| 7354 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7355 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7356 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7357 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7359 | /* |
| 7360 | * md_thread is a 'system-thread', it's priority should be very |
| 7361 | * high. We avoid resource deadlocks individually in each |
| 7362 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 7363 | * the very same RT priority as kswapd, thus we will never get |
| 7364 | * into a priority inversion deadlock. |
| 7365 | * |
| 7366 | * we definitely have to have equal or higher priority than |
| 7367 | * bdflush, otherwise bdflush will deadlock if there are too |
| 7368 | * many dirty RAID5 blocks. |
| 7369 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7370 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7371 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7372 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7373 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7374 | /* We need to wait INTERRUPTIBLE so that |
| 7375 | * we don't add to the load-average. |
| 7376 | * That means we need to be sure no signals are |
| 7377 | * pending |
| 7378 | */ |
| 7379 | if (signal_pending(current)) |
| 7380 | flush_signals(current); |
| 7381 | |
| 7382 | wait_event_interruptible_timeout |
| 7383 | (thread->wqueue, |
| 7384 | test_bit(THREAD_WAKEUP, &thread->flags) |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 7385 | || kthread_should_stop() || kthread_should_park(), |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7386 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7387 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7388 | clear_bit(THREAD_WAKEUP, &thread->flags); |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 7389 | if (kthread_should_park()) |
| 7390 | kthread_parkme(); |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7391 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7392 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7393 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7395 | return 0; |
| 7396 | } |
| 7397 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7398 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7399 | { |
| 7400 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7401 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7402 | if (!test_and_set_bit(THREAD_WAKEUP, &thread->flags)) |
| 7403 | wake_up(&thread->wqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7404 | } |
| 7405 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7406 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7407 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7408 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7409 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7410 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7411 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7412 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7413 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7414 | if (!thread) |
| 7415 | return NULL; |
| 7416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7417 | init_waitqueue_head(&thread->wqueue); |
| 7418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7419 | thread->run = run; |
| 7420 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7421 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7422 | thread->tsk = kthread_run(md_thread, thread, |
| 7423 | "%s_%s", |
| 7424 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7425 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7426 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7427 | kfree(thread); |
| 7428 | return NULL; |
| 7429 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7430 | return thread; |
| 7431 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7432 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7433 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7434 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7435 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7436 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7437 | if (!thread) |
| 7438 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7439 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7440 | /* Locking ensures that mddev_unlock does not wake_up a |
| 7441 | * non-existent thread |
| 7442 | */ |
| 7443 | spin_lock(&pers_lock); |
| 7444 | *threadp = NULL; |
| 7445 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7446 | |
| 7447 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7448 | kfree(thread); |
| 7449 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7450 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7451 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7452 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7453 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7454 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7455 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7456 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7457 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7458 | return; |
| 7459 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7460 | if (mddev->degraded) |
| 7461 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7462 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7463 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7464 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7465 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 7466 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7467 | queue_work(md_misc_wq, &mddev->event_work); |
Guoqing Jiang | bb9ef71 | 2015-12-28 10:46:38 +0800 | [diff] [blame] | 7468 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7469 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7470 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7471 | |
| 7472 | /* seq_file implementation /proc/mdstat */ |
| 7473 | |
| 7474 | static void status_unused(struct seq_file *seq) |
| 7475 | { |
| 7476 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7477 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7478 | |
| 7479 | seq_printf(seq, "unused devices: "); |
| 7480 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7481 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7482 | char b[BDEVNAME_SIZE]; |
| 7483 | i++; |
| 7484 | seq_printf(seq, "%s ", |
| 7485 | bdevname(rdev->bdev,b)); |
| 7486 | } |
| 7487 | if (!i) |
| 7488 | seq_printf(seq, "<none>"); |
| 7489 | |
| 7490 | seq_printf(seq, "\n"); |
| 7491 | } |
| 7492 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7493 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7494 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7495 | sector_t max_sectors, resync, res; |
| 7496 | unsigned long dt, db; |
| 7497 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7498 | int scale; |
| 7499 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7500 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7501 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 7502 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7503 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7504 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7505 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7506 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7507 | resync = mddev->curr_resync; |
| 7508 | if (resync <= 3) { |
| 7509 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7510 | /* Still cleaning up */ |
| 7511 | resync = max_sectors; |
| 7512 | } else |
| 7513 | resync -= atomic_read(&mddev->recovery_active); |
| 7514 | |
| 7515 | if (resync == 0) { |
| 7516 | if (mddev->recovery_cp < MaxSector) { |
| 7517 | seq_printf(seq, "\tresync=PENDING"); |
| 7518 | return 1; |
| 7519 | } |
| 7520 | return 0; |
| 7521 | } |
| 7522 | if (resync < 3) { |
| 7523 | seq_printf(seq, "\tresync=DELAYED"); |
| 7524 | return 1; |
| 7525 | } |
| 7526 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 7527 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7528 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7529 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7530 | * u32, as those are the requirements for sector_div. |
| 7531 | * Thus 'scale' must be at least 10 |
| 7532 | */ |
| 7533 | scale = 10; |
| 7534 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7535 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7536 | scale++; |
| 7537 | } |
| 7538 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7539 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7540 | |
| 7541 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7542 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7543 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7544 | seq_printf(seq, "["); |
| 7545 | for (i = 0; i < x; i++) |
| 7546 | seq_printf(seq, "="); |
| 7547 | seq_printf(seq, ">"); |
| 7548 | for (i = 0; i < y; i++) |
| 7549 | seq_printf(seq, "."); |
| 7550 | seq_printf(seq, "] "); |
| 7551 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7552 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7553 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 7554 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7555 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 7556 | "check" : |
| 7557 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 7558 | "resync" : "recovery"))), |
| 7559 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7560 | (unsigned long long) resync/2, |
| 7561 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7562 | |
| 7563 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7564 | * dt: time from mark until now |
| 7565 | * db: blocks written from mark until now |
| 7566 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7567 | * |
| 7568 | * rt is a sector_t, so could be 32bit or 64bit. |
| 7569 | * So we divide before multiply in case it is 32bit and close |
| 7570 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 7571 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7572 | * near the end of resync when the number of remaining sectors |
| 7573 | * is close to 'db'. |
| 7574 | * We then divide rt by 32 after multiplying by db to compensate. |
| 7575 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7576 | */ |
| 7577 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 7578 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7579 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 7580 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7581 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7582 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 7583 | sector_div(rt, db/32+1); |
| 7584 | rt *= dt; |
| 7585 | rt >>= 5; |
| 7586 | |
| 7587 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 7588 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7589 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7590 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7591 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7592 | } |
| 7593 | |
| 7594 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 7595 | { |
| 7596 | struct list_head *tmp; |
| 7597 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7598 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7599 | |
| 7600 | if (l >= 0x10000) |
| 7601 | return NULL; |
| 7602 | if (!l--) |
| 7603 | /* header */ |
| 7604 | return (void*)1; |
| 7605 | |
| 7606 | spin_lock(&all_mddevs_lock); |
| 7607 | list_for_each(tmp,&all_mddevs) |
| 7608 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7609 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7610 | mddev_get(mddev); |
| 7611 | spin_unlock(&all_mddevs_lock); |
| 7612 | return mddev; |
| 7613 | } |
| 7614 | spin_unlock(&all_mddevs_lock); |
| 7615 | if (!l--) |
| 7616 | return (void*)2;/* tail */ |
| 7617 | return NULL; |
| 7618 | } |
| 7619 | |
| 7620 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 7621 | { |
| 7622 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7623 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7625 | ++*pos; |
| 7626 | if (v == (void*)2) |
| 7627 | return NULL; |
| 7628 | |
| 7629 | spin_lock(&all_mddevs_lock); |
| 7630 | if (v == (void*)1) |
| 7631 | tmp = all_mddevs.next; |
| 7632 | else |
| 7633 | tmp = mddev->all_mddevs.next; |
| 7634 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7635 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7636 | else { |
| 7637 | next_mddev = (void*)2; |
| 7638 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7639 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7640 | spin_unlock(&all_mddevs_lock); |
| 7641 | |
| 7642 | if (v != (void*)1) |
| 7643 | mddev_put(mddev); |
| 7644 | return next_mddev; |
| 7645 | |
| 7646 | } |
| 7647 | |
| 7648 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 7649 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7650 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7651 | |
| 7652 | if (mddev && v != (void*)1 && v != (void*)2) |
| 7653 | mddev_put(mddev); |
| 7654 | } |
| 7655 | |
| 7656 | static int md_seq_show(struct seq_file *seq, void *v) |
| 7657 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7658 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7659 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7660 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7661 | |
| 7662 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7663 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7664 | seq_printf(seq, "Personalities : "); |
| 7665 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7666 | list_for_each_entry(pers, &pers_list, list) |
| 7667 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7668 | |
| 7669 | spin_unlock(&pers_lock); |
| 7670 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7671 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7672 | return 0; |
| 7673 | } |
| 7674 | if (v == (void*)2) { |
| 7675 | status_unused(seq); |
| 7676 | return 0; |
| 7677 | } |
| 7678 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7679 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7680 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7681 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7682 | mddev->pers ? "" : "in"); |
| 7683 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7684 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7685 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7686 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7687 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7688 | seq_printf(seq, " %s", mddev->pers->name); |
| 7689 | } |
| 7690 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7691 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7692 | rcu_read_lock(); |
| 7693 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7694 | char b[BDEVNAME_SIZE]; |
| 7695 | seq_printf(seq, " %s[%d]", |
| 7696 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7697 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7698 | seq_printf(seq, "(W)"); |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 7699 | if (test_bit(Journal, &rdev->flags)) |
| 7700 | seq_printf(seq, "(J)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7701 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7702 | seq_printf(seq, "(F)"); |
| 7703 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7704 | } |
| 7705 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7706 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7707 | if (test_bit(Replacement, &rdev->flags)) |
| 7708 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7709 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7710 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7711 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7712 | |
| 7713 | if (!list_empty(&mddev->disks)) { |
| 7714 | if (mddev->pers) |
| 7715 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7716 | (unsigned long long) |
| 7717 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7718 | else |
| 7719 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7720 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7721 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7722 | if (mddev->persistent) { |
| 7723 | if (mddev->major_version != 0 || |
| 7724 | mddev->minor_version != 90) { |
| 7725 | seq_printf(seq," super %d.%d", |
| 7726 | mddev->major_version, |
| 7727 | mddev->minor_version); |
| 7728 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7729 | } else if (mddev->external) |
| 7730 | seq_printf(seq, " super external:%s", |
| 7731 | mddev->metadata_type); |
| 7732 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7733 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7734 | |
| 7735 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7736 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7737 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7738 | if (mddev->pers->sync_request) { |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7739 | if (status_resync(seq, mddev)) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7740 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7741 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7742 | } else |
| 7743 | seq_printf(seq, "\n "); |
| 7744 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7745 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7746 | |
| 7747 | seq_printf(seq, "\n"); |
| 7748 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7749 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7751 | return 0; |
| 7752 | } |
| 7753 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7754 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7755 | .start = md_seq_start, |
| 7756 | .next = md_seq_next, |
| 7757 | .stop = md_seq_stop, |
| 7758 | .show = md_seq_show, |
| 7759 | }; |
| 7760 | |
| 7761 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7762 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7763 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7764 | int error; |
| 7765 | |
| 7766 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7767 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7768 | return error; |
| 7769 | |
| 7770 | seq = file->private_data; |
| 7771 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7772 | return error; |
| 7773 | } |
| 7774 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7775 | static int md_unloading; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7776 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7777 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7778 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7779 | int mask; |
| 7780 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7781 | if (md_unloading) |
kbuild test robot | 7d7e64f | 2014-12-03 16:07:59 +1100 | [diff] [blame] | 7782 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7783 | poll_wait(filp, &md_event_waiters, wait); |
| 7784 | |
| 7785 | /* always allow read */ |
| 7786 | mask = POLLIN | POLLRDNORM; |
| 7787 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7788 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7789 | mask |= POLLERR | POLLPRI; |
| 7790 | return mask; |
| 7791 | } |
| 7792 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7793 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7794 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7795 | .open = md_seq_open, |
| 7796 | .read = seq_read, |
| 7797 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7798 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7799 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7800 | }; |
| 7801 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7802 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7803 | { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7804 | pr_debug("md: %s personality registered for level %d\n", |
| 7805 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7806 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7807 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7808 | spin_unlock(&pers_lock); |
| 7809 | return 0; |
| 7810 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7811 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7812 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7813 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7814 | { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7815 | pr_debug("md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7816 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7817 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7818 | spin_unlock(&pers_lock); |
| 7819 | return 0; |
| 7820 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7821 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7822 | |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7823 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
| 7824 | struct module *module) |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7825 | { |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7826 | int ret = 0; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7827 | spin_lock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7828 | if (md_cluster_ops != NULL) |
| 7829 | ret = -EALREADY; |
| 7830 | else { |
| 7831 | md_cluster_ops = ops; |
| 7832 | md_cluster_mod = module; |
| 7833 | } |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7834 | spin_unlock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7835 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7836 | } |
| 7837 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 7838 | |
| 7839 | int unregister_md_cluster_operations(void) |
| 7840 | { |
| 7841 | spin_lock(&pers_lock); |
| 7842 | md_cluster_ops = NULL; |
| 7843 | spin_unlock(&pers_lock); |
| 7844 | return 0; |
| 7845 | } |
| 7846 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 7847 | |
| 7848 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 7849 | { |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 7850 | if (!md_cluster_ops) |
| 7851 | request_module("md-cluster"); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7852 | spin_lock(&pers_lock); |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 7853 | /* ensure module won't be unloaded */ |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7854 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7855 | 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] | 7856 | spin_unlock(&pers_lock); |
| 7857 | return -ENOENT; |
| 7858 | } |
| 7859 | spin_unlock(&pers_lock); |
| 7860 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 7861 | return md_cluster_ops->join(mddev, nodes); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7862 | } |
| 7863 | |
| 7864 | void md_cluster_stop(struct mddev *mddev) |
| 7865 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 7866 | if (!md_cluster_ops) |
| 7867 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7868 | md_cluster_ops->leave(mddev); |
| 7869 | module_put(md_cluster_mod); |
| 7870 | } |
| 7871 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7872 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7873 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7874 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7875 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7876 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7877 | |
| 7878 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7879 | rcu_read_lock(); |
| 7880 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7881 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7882 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7883 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7884 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7885 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7886 | * as sync_io is counted when a request starts, and |
| 7887 | * disk_stats is counted when it completes. |
| 7888 | * So resync activity will cause curr_events to be smaller than |
| 7889 | * when there was no such activity. |
| 7890 | * non-sync IO will cause disk_stat to increase without |
| 7891 | * increasing sync_io so curr_events will (eventually) |
| 7892 | * be larger than it was before. Once it becomes |
| 7893 | * substantially larger, the test below will cause |
| 7894 | * the array to appear non-idle, and resync will slow |
| 7895 | * down. |
| 7896 | * If there is a lot of outstanding resync activity when |
| 7897 | * we set last_event to curr_events, then all that activity |
| 7898 | * completing might cause the array to appear non-idle |
| 7899 | * and resync will be slowed down even though there might |
| 7900 | * not have been non-resync activity. This will only |
| 7901 | * happen once though. 'last_events' will soon reflect |
| 7902 | * the state where there is little or no outstanding |
| 7903 | * resync requests, and further resync activity will |
| 7904 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7905 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7906 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7907 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7908 | rdev->last_events = curr_events; |
| 7909 | idle = 0; |
| 7910 | } |
| 7911 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7912 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7913 | return idle; |
| 7914 | } |
| 7915 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7916 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7917 | { |
| 7918 | /* another "blocks" (512byte) blocks have been synced */ |
| 7919 | atomic_sub(blocks, &mddev->recovery_active); |
| 7920 | wake_up(&mddev->recovery_wait); |
| 7921 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7922 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7923 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7924 | md_wakeup_thread(mddev->thread); |
| 7925 | // stop recovery, signal do_sync .... |
| 7926 | } |
| 7927 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7928 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7929 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7930 | /* md_write_start(mddev, bi) |
| 7931 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7932 | * in superblock) before writing, schedule a superblock update |
| 7933 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7934 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7935 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7936 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7937 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7938 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7939 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7940 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7941 | BUG_ON(mddev->ro == 1); |
| 7942 | if (mddev->ro == 2) { |
| 7943 | /* need to switch to read/write */ |
| 7944 | mddev->ro = 0; |
| 7945 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7946 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7947 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7948 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7949 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7950 | rcu_read_lock(); |
| 7951 | percpu_ref_get(&mddev->writes_pending); |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7952 | smp_mb(); /* Match smp_mb in set_in_sync() */ |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7953 | if (mddev->safemode == 1) |
| 7954 | mddev->safemode = 0; |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7955 | /* sync_checkers is always 0 when writes_pending is in per-cpu mode */ |
| 7956 | if (mddev->in_sync || !mddev->sync_checkers) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7957 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7958 | if (mddev->in_sync) { |
| 7959 | mddev->in_sync = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7960 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 7961 | set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7962 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7963 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7964 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7965 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7966 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7967 | rcu_read_unlock(); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7968 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7969 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7970 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7971 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7972 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7973 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7974 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 7975 | /* md_write_inc can only be called when md_write_start() has |
| 7976 | * already been called at least once of the current request. |
| 7977 | * It increments the counter and is useful when a single request |
| 7978 | * is split into several parts. Each part causes an increment and |
| 7979 | * so needs a matching md_write_end(). |
| 7980 | * Unlike md_write_start(), it is safe to call md_write_inc() inside |
| 7981 | * a spinlocked region. |
| 7982 | */ |
| 7983 | void md_write_inc(struct mddev *mddev, struct bio *bi) |
| 7984 | { |
| 7985 | if (bio_data_dir(bi) != WRITE) |
| 7986 | return; |
| 7987 | WARN_ON_ONCE(mddev->in_sync || mddev->ro); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7988 | percpu_ref_get(&mddev->writes_pending); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 7989 | } |
| 7990 | EXPORT_SYMBOL(md_write_inc); |
| 7991 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7992 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7993 | { |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7994 | percpu_ref_put(&mddev->writes_pending); |
| 7995 | |
| 7996 | if (mddev->safemode == 2) |
| 7997 | md_wakeup_thread(mddev->thread); |
| 7998 | else if (mddev->safemode_delay) |
| 7999 | /* The roundup() ensures this only performs locking once |
| 8000 | * every ->safemode_delay jiffies |
| 8001 | */ |
| 8002 | mod_timer(&mddev->safemode_timer, |
| 8003 | roundup(jiffies, mddev->safemode_delay) + |
| 8004 | mddev->safemode_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8005 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8006 | |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8007 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8008 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8009 | /* md_allow_write(mddev) |
| 8010 | * Calling this ensures that the array is marked 'active' so that writes |
| 8011 | * may proceed without blocking. It is important to call this before |
| 8012 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 8013 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 8014 | * |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8015 | * In the ->external case MD_SB_CHANGE_PENDING can not be cleared until mddev->lock |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 8016 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8017 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8018 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8019 | { |
| 8020 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 8021 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8022 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 8023 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 8024 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 8025 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8026 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8027 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8028 | if (mddev->in_sync) { |
| 8029 | mddev->in_sync = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8030 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 8031 | set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8032 | if (mddev->safemode_delay && |
| 8033 | mddev->safemode == 0) |
| 8034 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8035 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8036 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8037 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8038 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8039 | spin_unlock(&mddev->lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 8040 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8041 | if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 8042 | return -EAGAIN; |
| 8043 | else |
| 8044 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8045 | } |
| 8046 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 8047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8048 | #define SYNC_MARKS 10 |
| 8049 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8050 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 8051 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8052 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 8053 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8054 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8055 | unsigned int currspeed = 0, |
| 8056 | window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8057 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8058 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8059 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8060 | sector_t mark_cnt[SYNC_MARKS]; |
| 8061 | int last_mark,m; |
| 8062 | struct list_head *tmp; |
| 8063 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8064 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8065 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8066 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8067 | struct blk_plug plug; |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8068 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8069 | |
| 8070 | /* just incase thread restarts... */ |
| 8071 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 8072 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 8073 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 8074 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8075 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 8076 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8077 | |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8078 | if (mddev_is_clustered(mddev)) { |
| 8079 | ret = md_cluster_ops->resync_start(mddev); |
| 8080 | if (ret) |
| 8081 | goto skip; |
| 8082 | |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 8083 | set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags); |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8084 | if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 8085 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) || |
| 8086 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
| 8087 | && ((unsigned long long)mddev->curr_resync_completed |
| 8088 | < (unsigned long long)mddev->resync_max_sectors)) |
| 8089 | goto skip; |
| 8090 | } |
| 8091 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8092 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8093 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8094 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8095 | action = "check"; |
| 8096 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8097 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8098 | action = "repair"; |
| 8099 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8100 | desc = "resync"; |
| 8101 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 8102 | desc = "reshape"; |
| 8103 | else |
| 8104 | desc = "recovery"; |
| 8105 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8106 | mddev->last_sync_action = action ?: desc; |
| 8107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8108 | /* we overload curr_resync somewhat here. |
| 8109 | * 0 == not engaged in resync at all |
| 8110 | * 2 == checking that there is no conflict with another sync |
| 8111 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 8112 | * commense |
| 8113 | * other == active in resync - this many blocks |
| 8114 | * |
| 8115 | * Before starting a resync we must have set curr_resync to |
| 8116 | * 2, and then checked that every "conflicting" array has curr_resync |
| 8117 | * less than ours. When we find one that is the same or higher |
| 8118 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 8119 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 8120 | * This will mean we have to start checking from the beginning again. |
| 8121 | * |
| 8122 | */ |
| 8123 | |
| 8124 | do { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8125 | int mddev2_minor = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8126 | mddev->curr_resync = 2; |
| 8127 | |
| 8128 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 8129 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8130 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8131 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8132 | if (mddev2 == mddev) |
| 8133 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 8134 | if (!mddev->parallel_resync |
| 8135 | && mddev2->curr_resync |
| 8136 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8137 | DEFINE_WAIT(wq); |
| 8138 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 8139 | /* arbitrarily yield */ |
| 8140 | mddev->curr_resync = 1; |
| 8141 | wake_up(&resync_wait); |
| 8142 | } |
| 8143 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 8144 | /* no need to wait here, we can wait the next |
| 8145 | * time 'round when curr_resync == 2 |
| 8146 | */ |
| 8147 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 8148 | /* We need to wait 'interruptible' so as not to |
| 8149 | * contribute to the load average, and not to |
| 8150 | * be caught by 'softlockup' |
| 8151 | */ |
| 8152 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8153 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 8154 | mddev2->curr_resync >= mddev->curr_resync) { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8155 | if (mddev2_minor != mddev2->md_minor) { |
| 8156 | mddev2_minor = mddev2->md_minor; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8157 | pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n", |
| 8158 | desc, mdname(mddev), |
| 8159 | mdname(mddev2)); |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8161 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 8162 | if (signal_pending(current)) |
| 8163 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8164 | schedule(); |
| 8165 | finish_wait(&resync_wait, &wq); |
| 8166 | goto try_again; |
| 8167 | } |
| 8168 | finish_wait(&resync_wait, &wq); |
| 8169 | } |
| 8170 | } |
| 8171 | } while (mddev->curr_resync < 2); |
| 8172 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8173 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8174 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8175 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8176 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8177 | */ |
| 8178 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 8179 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8180 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 8181 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8182 | j = mddev->resync_min; |
| 8183 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8184 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 8185 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 8186 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 8187 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8188 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8189 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8190 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8191 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8192 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8193 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8194 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8195 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8196 | !test_bit(Faulty, &rdev->flags) && |
| 8197 | !test_bit(In_sync, &rdev->flags) && |
| 8198 | rdev->recovery_offset < j) |
| 8199 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8200 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 8201 | |
| 8202 | /* If there is a bitmap, we need to make sure all |
| 8203 | * writes that started before we added a spare |
| 8204 | * complete before we start doing a recovery. |
| 8205 | * Otherwise the write might complete and (via |
| 8206 | * bitmap_endwrite) set a bit in the bitmap after the |
| 8207 | * recovery has checked that bit and skipped that |
| 8208 | * region. |
| 8209 | */ |
| 8210 | if (mddev->bitmap) { |
| 8211 | mddev->pers->quiesce(mddev, 1); |
| 8212 | mddev->pers->quiesce(mddev, 0); |
| 8213 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8215 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8216 | pr_info("md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 8217 | pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev)); |
| 8218 | pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n", |
| 8219 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8220 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8221 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8222 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8223 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8224 | for (m = 0; m < SYNC_MARKS; m++) { |
| 8225 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8226 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8227 | } |
| 8228 | last_mark = 0; |
| 8229 | mddev->resync_mark = mark[last_mark]; |
| 8230 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 8231 | |
| 8232 | /* |
| 8233 | * Tune reconstruction: |
| 8234 | */ |
| 8235 | window = 32*(PAGE_SIZE/512); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8236 | pr_debug("md: using %dk window, over a total of %lluk.\n", |
| 8237 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8238 | |
| 8239 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8240 | last_check = 0; |
| 8241 | |
| 8242 | if (j>2) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8243 | pr_debug("md: resuming %s of %s from checkpoint.\n", |
| 8244 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8245 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8246 | } else |
| 8247 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8248 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8249 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 8250 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8251 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8252 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8253 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8254 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8255 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8256 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8257 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8258 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8259 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8260 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 8261 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 8262 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8263 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8264 | (j - mddev->curr_resync_completed)*2 |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 8265 | >= mddev->resync_max - mddev->curr_resync_completed || |
| 8266 | mddev->curr_resync_completed > mddev->resync_max |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8267 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8268 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8269 | wait_event(mddev->recovery_wait, |
| 8270 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8271 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8272 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 8273 | j > mddev->recovery_cp) |
| 8274 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8275 | update_time = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8276 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8277 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8278 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8279 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8280 | while (j >= mddev->resync_max && |
| 8281 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8282 | /* As this condition is controlled by user-space, |
| 8283 | * we can block indefinitely, so use '_interruptible' |
| 8284 | * to avoid triggering warnings. |
| 8285 | */ |
| 8286 | flush_signals(current); /* just in case */ |
| 8287 | wait_event_interruptible(mddev->recovery_wait, |
| 8288 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8289 | || test_bit(MD_RECOVERY_INTR, |
| 8290 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8291 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8292 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8293 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8294 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8295 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8296 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8297 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8298 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8299 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8300 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8301 | |
| 8302 | if (!skipped) { /* actual IO requested */ |
| 8303 | io_sectors += sectors; |
| 8304 | atomic_add(sectors, &mddev->recovery_active); |
| 8305 | } |
| 8306 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8307 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8308 | break; |
| 8309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8310 | j += sectors; |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8311 | if (j > max_sectors) |
| 8312 | /* when skipping, extra large numbers can be returned. */ |
| 8313 | j = max_sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8314 | if (j > 2) |
| 8315 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8316 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8317 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8318 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8319 | * visible in /proc/mdstat |
| 8320 | */ |
| 8321 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8322 | |
| 8323 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8324 | continue; |
| 8325 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8326 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8327 | repeat: |
| 8328 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 8329 | /* step marks */ |
| 8330 | int next = (last_mark+1) % SYNC_MARKS; |
| 8331 | |
| 8332 | mddev->resync_mark = mark[next]; |
| 8333 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 8334 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8335 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8336 | last_mark = next; |
| 8337 | } |
| 8338 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8339 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8340 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8341 | |
| 8342 | /* |
| 8343 | * this loop exits only if either when we are slower than |
| 8344 | * the 'hard' speed limit, or the system was IO-idle for |
| 8345 | * a jiffy. |
| 8346 | * the system might be non-idle CPU-wise, but we only care |
| 8347 | * about not overloading the IO subsystem. (things like an |
| 8348 | * e2fsck being done on the RAID array should execute fast) |
| 8349 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8350 | cond_resched(); |
| 8351 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8352 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 8353 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8354 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8355 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 8356 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8357 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8358 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8359 | goto repeat; |
| 8360 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8361 | if (!is_mddev_idle(mddev, 0)) { |
| 8362 | /* |
| 8363 | * Give other IO more of a chance. |
| 8364 | * The faster the devices, the less we wait. |
| 8365 | */ |
| 8366 | wait_event(mddev->recovery_wait, |
| 8367 | !atomic_read(&mddev->recovery_active)); |
| 8368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8369 | } |
| 8370 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8371 | pr_info("md: %s: %s %s.\n",mdname(mddev), desc, |
| 8372 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 8373 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8374 | /* |
| 8375 | * this also signals 'finished resyncing' to md_stop |
| 8376 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8377 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8378 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 8379 | |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8380 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8381 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 8382 | mddev->curr_resync > 3) { |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8383 | mddev->curr_resync_completed = mddev->curr_resync; |
| 8384 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 8385 | } |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8386 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8387 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8388 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 8389 | mddev->curr_resync > 3) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8390 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 8391 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8392 | if (mddev->curr_resync >= mddev->recovery_cp) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8393 | pr_debug("md: checkpointing %s of %s.\n", |
| 8394 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8395 | if (test_bit(MD_RECOVERY_ERROR, |
| 8396 | &mddev->recovery)) |
| 8397 | mddev->recovery_cp = |
| 8398 | mddev->curr_resync_completed; |
| 8399 | else |
| 8400 | mddev->recovery_cp = |
| 8401 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8402 | } |
| 8403 | } else |
| 8404 | mddev->recovery_cp = MaxSector; |
| 8405 | } else { |
| 8406 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8407 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8408 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8409 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8410 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 8411 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8412 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8413 | !test_bit(Faulty, &rdev->flags) && |
| 8414 | !test_bit(In_sync, &rdev->flags) && |
| 8415 | rdev->recovery_offset < mddev->curr_resync) |
| 8416 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8417 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8419 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 8420 | skip: |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 8421 | /* set CHANGE_PENDING here since maybe another update is needed, |
| 8422 | * so other nodes are informed. It should be harmless for normal |
| 8423 | * raid */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8424 | set_mask_bits(&mddev->sb_flags, 0, |
| 8425 | BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS)); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8426 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8427 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 8428 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8429 | /* We completed so min/max setting can be forgotten if used. */ |
| 8430 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8431 | mddev->resync_min = 0; |
| 8432 | mddev->resync_max = MaxSector; |
| 8433 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8434 | mddev->resync_min = mddev->curr_resync_completed; |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8435 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8436 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8437 | spin_unlock(&mddev->lock); |
| 8438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8439 | wake_up(&resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8440 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 8441 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8442 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8443 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8444 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8445 | static int remove_and_add_spares(struct mddev *mddev, |
| 8446 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8447 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8448 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8449 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8450 | int removed = 0; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8451 | bool remove_some = false; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8452 | |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8453 | rdev_for_each(rdev, mddev) { |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8454 | if ((this == NULL || rdev == this) && |
| 8455 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8456 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8457 | test_bit(Faulty, &rdev->flags) && |
| 8458 | atomic_read(&rdev->nr_pending)==0) { |
| 8459 | /* Faulty non-Blocked devices with nr_pending == 0 |
| 8460 | * never get nr_pending incremented, |
| 8461 | * never get Faulty cleared, and never get Blocked set. |
| 8462 | * So we can synchronize_rcu now rather than once per device |
| 8463 | */ |
| 8464 | remove_some = true; |
| 8465 | set_bit(RemoveSynchronized, &rdev->flags); |
| 8466 | } |
| 8467 | } |
| 8468 | |
| 8469 | if (remove_some) |
| 8470 | synchronize_rcu(); |
| 8471 | rdev_for_each(rdev, mddev) { |
| 8472 | if ((this == NULL || rdev == this) && |
| 8473 | rdev->raid_disk >= 0 && |
| 8474 | !test_bit(Blocked, &rdev->flags) && |
| 8475 | ((test_bit(RemoveSynchronized, &rdev->flags) || |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8476 | (!test_bit(In_sync, &rdev->flags) && |
| 8477 | !test_bit(Journal, &rdev->flags))) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8478 | atomic_read(&rdev->nr_pending)==0)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8479 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8480 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 8481 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8482 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8483 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8484 | } |
| 8485 | } |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8486 | if (remove_some && test_bit(RemoveSynchronized, &rdev->flags)) |
| 8487 | clear_bit(RemoveSynchronized, &rdev->flags); |
| 8488 | } |
| 8489 | |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 8490 | if (removed && mddev->kobj.sd) |
| 8491 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8492 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8493 | if (this && removed) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8494 | goto no_add; |
| 8495 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8496 | rdev_for_each(rdev, mddev) { |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8497 | if (this && this != rdev) |
| 8498 | continue; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 8499 | if (test_bit(Candidate, &rdev->flags)) |
| 8500 | continue; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8501 | if (rdev->raid_disk >= 0 && |
| 8502 | !test_bit(In_sync, &rdev->flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8503 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8504 | !test_bit(Faulty, &rdev->flags)) |
| 8505 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8506 | if (rdev->raid_disk >= 0) |
| 8507 | continue; |
| 8508 | if (test_bit(Faulty, &rdev->flags)) |
| 8509 | continue; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8510 | if (!test_bit(Journal, &rdev->flags)) { |
| 8511 | if (mddev->ro && |
| 8512 | ! (rdev->saved_raid_disk >= 0 && |
| 8513 | !test_bit(Bitmap_sync, &rdev->flags))) |
| 8514 | continue; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8515 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8516 | rdev->recovery_offset = 0; |
| 8517 | } |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8518 | if (mddev->pers-> |
| 8519 | hot_add_disk(mddev, rdev) == 0) { |
| 8520 | if (sysfs_link_rdev(mddev, rdev)) |
| 8521 | /* failure here is OK */; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8522 | if (!test_bit(Journal, &rdev->flags)) |
| 8523 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8524 | md_new_event(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8525 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8526 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8527 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8528 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 8529 | if (removed) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8530 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8531 | return spares; |
| 8532 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 8533 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8534 | static void md_start_sync(struct work_struct *ws) |
| 8535 | { |
| 8536 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8537 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8538 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 8539 | mddev, |
| 8540 | "resync"); |
| 8541 | if (!mddev->sync_thread) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8542 | pr_warn("%s: could not start resync thread...\n", |
| 8543 | mdname(mddev)); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8544 | /* leave the spares where they are, it shouldn't hurt */ |
| 8545 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8546 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8547 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8548 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 8549 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8550 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8551 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8552 | &mddev->recovery)) |
| 8553 | if (mddev->sysfs_action) |
| 8554 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8555 | } else |
| 8556 | md_wakeup_thread(mddev->sync_thread); |
| 8557 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8558 | md_new_event(mddev); |
| 8559 | } |
| 8560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8561 | /* |
| 8562 | * This routine is regularly called by all per-raid-array threads to |
| 8563 | * deal with generic issues like resync and super-block update. |
| 8564 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 8565 | * need this as they never do any recovery or update the superblock. |
| 8566 | * |
| 8567 | * It does not do any resync itself, but rather "forks" off other threads |
| 8568 | * to do that as needed. |
| 8569 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 8570 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8571 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8572 | * and wakeups up this thread which will reap the thread and finish up. |
| 8573 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 8574 | * |
| 8575 | * The overall approach is: |
| 8576 | * 1/ if the superblock needs updating, update it. |
| 8577 | * 2/ If a recovery thread is running, don't do anything else. |
| 8578 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 8579 | * 4/ If there are any faulty devices, remove them. |
| 8580 | * 5/ If array is degraded, try to add spares devices |
| 8581 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 8582 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8583 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8584 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 8585 | if (mddev->suspended) |
| 8586 | return; |
| 8587 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 8588 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 8589 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8590 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8591 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8592 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8593 | pr_debug("md: %s in immediate safe mode\n", |
| 8594 | mdname(mddev)); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8595 | mddev->safemode = 2; |
| 8596 | } |
| 8597 | flush_signals(current); |
| 8598 | } |
| 8599 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8600 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 8601 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8602 | if ( ! ( |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8603 | (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8604 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8605 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8606 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8607 | (mddev->safemode == 2 |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8608 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8609 | )) |
| 8610 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8611 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 8612 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8613 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8614 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8615 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 8616 | struct md_rdev *rdev; |
| 8617 | if (!mddev->external && mddev->in_sync) |
| 8618 | /* 'Blocked' flag not needed as failed devices |
| 8619 | * will be recorded if array switched to read/write. |
| 8620 | * Leaving it set will prevent the device |
| 8621 | * from being removed. |
| 8622 | */ |
| 8623 | rdev_for_each(rdev, mddev) |
| 8624 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8625 | /* On a read-only array we can: |
| 8626 | * - remove failed devices |
| 8627 | * - add already-in_sync devices if the array itself |
| 8628 | * is in-sync. |
| 8629 | * As we only add devices that are already in-sync, |
| 8630 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8631 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8632 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8633 | /* There is no thread, but we need to call |
| 8634 | * ->spare_active and clear saved_raid_disk |
| 8635 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 8636 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8637 | md_reap_sync_thread(mddev); |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 8638 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8639 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8640 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8641 | goto unlock; |
| 8642 | } |
| 8643 | |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 8644 | if (mddev_is_clustered(mddev)) { |
| 8645 | struct md_rdev *rdev; |
| 8646 | /* kick the device if another node issued a |
| 8647 | * remove disk. |
| 8648 | */ |
| 8649 | rdev_for_each(rdev, mddev) { |
| 8650 | if (test_and_clear_bit(ClusterRemove, &rdev->flags) && |
| 8651 | rdev->raid_disk < 0) |
| 8652 | md_kick_rdev_from_array(rdev); |
| 8653 | } |
| 8654 | } |
| 8655 | |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8656 | if (!mddev->external && !mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8657 | spin_lock(&mddev->lock); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8658 | set_in_sync(mddev); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8659 | spin_unlock(&mddev->lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8660 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8661 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8662 | if (mddev->sb_flags) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 8663 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8665 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 8666 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 8667 | /* resync/recovery still happening */ |
| 8668 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8669 | goto unlock; |
| 8670 | } |
| 8671 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8672 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8673 | goto unlock; |
| 8674 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8675 | /* Set RUNNING before clearing NEEDED to avoid |
| 8676 | * any transients in the value of "sync_action". |
| 8677 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8678 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8679 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8680 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8681 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8682 | /* Clear some bits that don't mean anything, but |
| 8683 | * might be left set |
| 8684 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8685 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 8686 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8687 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 8688 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 8689 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8690 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8691 | /* no recovery is running. |
| 8692 | * remove any failed drives, then |
| 8693 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8694 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8695 | * the personality to fail the re-add. |
| 8696 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8697 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8698 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8699 | if (mddev->pers->check_reshape == NULL || |
| 8700 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8701 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8702 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8703 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8704 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8705 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8706 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8707 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 8708 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8709 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8710 | } else if (mddev->recovery_cp < MaxSector) { |
| 8711 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8712 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8713 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 8714 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8715 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8717 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 8718 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 8719 | /* We are adding a device or devices to an array |
| 8720 | * which has the bitmap stored on all devices. |
| 8721 | * So make sure all bitmap pages get written |
| 8722 | */ |
| 8723 | bitmap_write_all(mddev->bitmap); |
| 8724 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8725 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 8726 | queue_work(md_misc_wq, &mddev->del_work); |
| 8727 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8728 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8729 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8730 | if (!mddev->sync_thread) { |
| 8731 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8732 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8733 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8734 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 8735 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8736 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8737 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8738 | unlock: |
| 8739 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8740 | mddev_unlock(mddev); |
| 8741 | } |
| 8742 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8743 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8744 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8745 | void md_reap_sync_thread(struct mddev *mddev) |
| 8746 | { |
| 8747 | struct md_rdev *rdev; |
| 8748 | |
| 8749 | /* resync has finished, collect result */ |
| 8750 | md_unregister_thread(&mddev->sync_thread); |
| 8751 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8752 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 8753 | /* success...*/ |
| 8754 | /* activate any spares */ |
| 8755 | if (mddev->pers->spare_active(mddev)) { |
| 8756 | sysfs_notify(&mddev->kobj, NULL, |
| 8757 | "degraded"); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8758 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8759 | } |
| 8760 | } |
| 8761 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8762 | mddev->pers->finish_reshape) |
| 8763 | mddev->pers->finish_reshape(mddev); |
| 8764 | |
| 8765 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8766 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8767 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8768 | if (!mddev->degraded) |
| 8769 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8770 | rdev->saved_raid_disk = -1; |
| 8771 | |
| 8772 | md_update_sb(mddev, 1); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8773 | /* 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] | 8774 | * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by |
| 8775 | * clustered raid */ |
| 8776 | if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags)) |
| 8777 | md_cluster_ops->resync_finish(mddev); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8778 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 8779 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8780 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8781 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8782 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8783 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8784 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8785 | /* flag recovery needed just to double check */ |
| 8786 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8787 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8788 | md_new_event(mddev); |
| 8789 | if (mddev->event_work.func) |
| 8790 | queue_work(md_misc_wq, &mddev->event_work); |
| 8791 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8792 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8793 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8794 | 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] | 8795 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8796 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8797 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8798 | !test_bit(Blocked, &rdev->flags) && |
| 8799 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8800 | msecs_to_jiffies(5000)); |
| 8801 | rdev_dec_pending(rdev, mddev); |
| 8802 | } |
| 8803 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 8804 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8805 | void md_finish_reshape(struct mddev *mddev) |
| 8806 | { |
| 8807 | /* called be personality module when reshape completes. */ |
| 8808 | struct md_rdev *rdev; |
| 8809 | |
| 8810 | rdev_for_each(rdev, mddev) { |
| 8811 | if (rdev->data_offset > rdev->new_data_offset) |
| 8812 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 8813 | else |
| 8814 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 8815 | rdev->data_offset = rdev->new_data_offset; |
| 8816 | } |
| 8817 | } |
| 8818 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8819 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8820 | /* Bad block management */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8821 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8822 | /* Returns 1 on success, 0 on failure */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8823 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8824 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8825 | { |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 8826 | struct mddev *mddev = rdev->mddev; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8827 | int rv; |
| 8828 | if (is_new) |
| 8829 | s += rdev->new_data_offset; |
| 8830 | else |
| 8831 | s += rdev->data_offset; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8832 | rv = badblocks_set(&rdev->badblocks, s, sectors, 0); |
| 8833 | if (rv == 0) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8834 | /* Make sure they get written out promptly */ |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 8835 | if (test_bit(ExternalBbl, &rdev->flags)) |
| 8836 | sysfs_notify(&rdev->kobj, NULL, |
| 8837 | "unacknowledged_bad_blocks"); |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8838 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8839 | set_mask_bits(&mddev->sb_flags, 0, |
| 8840 | BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8841 | md_wakeup_thread(rdev->mddev->thread); |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8842 | return 1; |
| 8843 | } else |
| 8844 | return 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8845 | } |
| 8846 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8847 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8848 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8849 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8850 | { |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 8851 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8852 | if (is_new) |
| 8853 | s += rdev->new_data_offset; |
| 8854 | else |
| 8855 | s += rdev->data_offset; |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 8856 | rv = badblocks_clear(&rdev->badblocks, s, sectors); |
| 8857 | if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags)) |
| 8858 | sysfs_notify(&rdev->kobj, NULL, "bad_blocks"); |
| 8859 | return rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8860 | } |
| 8861 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8862 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8863 | static int md_notify_reboot(struct notifier_block *this, |
| 8864 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8865 | { |
| 8866 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8867 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8868 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8869 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8870 | for_each_mddev(mddev, tmp) { |
| 8871 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8872 | if (mddev->pers) |
| 8873 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 8874 | if (mddev->persistent) |
| 8875 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8876 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8877 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8878 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8879 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8880 | /* |
| 8881 | * certain more exotic SCSI devices are known to be |
| 8882 | * volatile wrt too early system reboots. While the |
| 8883 | * right place to handle this issue is the given |
| 8884 | * driver, we do want to have a safe RAID driver ... |
| 8885 | */ |
| 8886 | if (need_delay) |
| 8887 | mdelay(1000*1); |
| 8888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8889 | return NOTIFY_DONE; |
| 8890 | } |
| 8891 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8892 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8893 | .notifier_call = md_notify_reboot, |
| 8894 | .next = NULL, |
| 8895 | .priority = INT_MAX, /* before any real devices */ |
| 8896 | }; |
| 8897 | |
| 8898 | static void md_geninit(void) |
| 8899 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8900 | 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] | 8901 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8902 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8903 | } |
| 8904 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8905 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8906 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8907 | int ret = -ENOMEM; |
| 8908 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8909 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8910 | if (!md_wq) |
| 8911 | goto err_wq; |
| 8912 | |
| 8913 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8914 | if (!md_misc_wq) |
| 8915 | goto err_misc_wq; |
| 8916 | |
| 8917 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8918 | goto err_md; |
| 8919 | |
| 8920 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8921 | goto err_mdp; |
| 8922 | mdp_major = ret; |
| 8923 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8924 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8925 | md_probe, NULL, NULL); |
| 8926 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8927 | md_probe, NULL, NULL); |
| 8928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8929 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8930 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8931 | |
| 8932 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8933 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8934 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8935 | err_mdp: |
| 8936 | unregister_blkdev(MD_MAJOR, "md"); |
| 8937 | err_md: |
| 8938 | destroy_workqueue(md_misc_wq); |
| 8939 | err_misc_wq: |
| 8940 | destroy_workqueue(md_wq); |
| 8941 | err_wq: |
| 8942 | return ret; |
| 8943 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8944 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8945 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8946 | { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8947 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 8948 | struct md_rdev *rdev2; |
| 8949 | int role, ret; |
| 8950 | char b[BDEVNAME_SIZE]; |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8951 | |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 8952 | /* |
| 8953 | * If size is changed in another node then we need to |
| 8954 | * do resize as well. |
| 8955 | */ |
| 8956 | if (mddev->dev_sectors != le64_to_cpu(sb->size)) { |
| 8957 | ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size)); |
| 8958 | if (ret) |
| 8959 | pr_info("md-cluster: resize failed\n"); |
| 8960 | else |
| 8961 | bitmap_update_sb(mddev->bitmap); |
| 8962 | } |
| 8963 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8964 | /* Check for change of roles in the active devices */ |
| 8965 | rdev_for_each(rdev2, mddev) { |
| 8966 | if (test_bit(Faulty, &rdev2->flags)) |
| 8967 | continue; |
| 8968 | |
| 8969 | /* Check if the roles changed */ |
| 8970 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 8971 | |
| 8972 | if (test_bit(Candidate, &rdev2->flags)) { |
| 8973 | if (role == 0xfffe) { |
| 8974 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); |
| 8975 | md_kick_rdev_from_array(rdev2); |
| 8976 | continue; |
| 8977 | } |
| 8978 | else |
| 8979 | clear_bit(Candidate, &rdev2->flags); |
| 8980 | } |
| 8981 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8982 | if (role != rdev2->raid_disk) { |
| 8983 | /* got activated */ |
| 8984 | if (rdev2->raid_disk == -1 && role != 0xffff) { |
| 8985 | rdev2->saved_raid_disk = role; |
| 8986 | ret = remove_and_add_spares(mddev, rdev2); |
| 8987 | pr_info("Activated spare: %s\n", |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8988 | bdevname(rdev2->bdev,b)); |
Guoqing Jiang | a578183 | 2016-05-02 11:33:14 -0400 | [diff] [blame] | 8989 | /* wakeup mddev->thread here, so array could |
| 8990 | * perform resync with the new activated disk */ |
| 8991 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8992 | md_wakeup_thread(mddev->thread); |
| 8993 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8994 | } |
| 8995 | /* device faulty |
| 8996 | * We just want to do the minimum to mark the disk |
| 8997 | * as faulty. The recovery is performed by the |
| 8998 | * one who initiated the error. |
| 8999 | */ |
| 9000 | if ((role == 0xfffe) || (role == 0xfffd)) { |
| 9001 | md_error(mddev, rdev2); |
| 9002 | clear_bit(Blocked, &rdev2->flags); |
| 9003 | } |
| 9004 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9005 | } |
| 9006 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 9007 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) |
| 9008 | update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9009 | |
| 9010 | /* Finally set the event to be up to date */ |
| 9011 | mddev->events = le64_to_cpu(sb->events); |
| 9012 | } |
| 9013 | |
| 9014 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) |
| 9015 | { |
| 9016 | int err; |
| 9017 | struct page *swapout = rdev->sb_page; |
| 9018 | struct mdp_superblock_1 *sb; |
| 9019 | |
| 9020 | /* Store the sb page of the rdev in the swapout temporary |
| 9021 | * variable in case we err in the future |
| 9022 | */ |
| 9023 | rdev->sb_page = NULL; |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9024 | err = alloc_disk_sb(rdev); |
| 9025 | if (err == 0) { |
| 9026 | ClearPageUptodate(rdev->sb_page); |
| 9027 | rdev->sb_loaded = 0; |
| 9028 | err = super_types[mddev->major_version]. |
| 9029 | load_super(rdev, NULL, mddev->minor_version); |
| 9030 | } |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9031 | if (err < 0) { |
| 9032 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", |
| 9033 | __func__, __LINE__, rdev->desc_nr, err); |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9034 | if (rdev->sb_page) |
| 9035 | put_page(rdev->sb_page); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9036 | rdev->sb_page = swapout; |
| 9037 | rdev->sb_loaded = 1; |
| 9038 | return err; |
| 9039 | } |
| 9040 | |
| 9041 | sb = page_address(rdev->sb_page); |
| 9042 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET |
| 9043 | * is not set |
| 9044 | */ |
| 9045 | |
| 9046 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) |
| 9047 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 9048 | |
| 9049 | /* The other node finished recovery, call spare_active to set |
| 9050 | * device In_sync and mddev->degraded |
| 9051 | */ |
| 9052 | if (rdev->recovery_offset == MaxSector && |
| 9053 | !test_bit(In_sync, &rdev->flags) && |
| 9054 | mddev->pers->spare_active(mddev)) |
| 9055 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
| 9056 | |
| 9057 | put_page(swapout); |
| 9058 | return 0; |
| 9059 | } |
| 9060 | |
| 9061 | void md_reload_sb(struct mddev *mddev, int nr) |
| 9062 | { |
| 9063 | struct md_rdev *rdev; |
| 9064 | int err; |
| 9065 | |
| 9066 | /* Find the rdev */ |
| 9067 | rdev_for_each_rcu(rdev, mddev) { |
| 9068 | if (rdev->desc_nr == nr) |
| 9069 | break; |
| 9070 | } |
| 9071 | |
| 9072 | if (!rdev || rdev->desc_nr != nr) { |
| 9073 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); |
| 9074 | return; |
| 9075 | } |
| 9076 | |
| 9077 | err = read_rdev(mddev, rdev); |
| 9078 | if (err < 0) |
| 9079 | return; |
| 9080 | |
| 9081 | check_sb_changes(mddev, rdev); |
| 9082 | |
| 9083 | /* Read all rdev's to update recovery_offset */ |
| 9084 | rdev_for_each_rcu(rdev, mddev) |
| 9085 | read_rdev(mddev, rdev); |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9086 | } |
| 9087 | EXPORT_SYMBOL(md_reload_sb); |
| 9088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9089 | #ifndef MODULE |
| 9090 | |
| 9091 | /* |
| 9092 | * Searches all registered partitions for autorun RAID arrays |
| 9093 | * at boot time. |
| 9094 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9095 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9096 | static DEFINE_MUTEX(detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9097 | static LIST_HEAD(all_detected_devices); |
| 9098 | struct detected_devices_node { |
| 9099 | struct list_head list; |
| 9100 | dev_t dev; |
| 9101 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9102 | |
| 9103 | void md_autodetect_dev(dev_t dev) |
| 9104 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9105 | struct detected_devices_node *node_detected_dev; |
| 9106 | |
| 9107 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 9108 | if (node_detected_dev) { |
| 9109 | node_detected_dev->dev = dev; |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9110 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9111 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9112 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9113 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9114 | } |
| 9115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9116 | static void autostart_arrays(int part) |
| 9117 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9118 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9119 | struct detected_devices_node *node_detected_dev; |
| 9120 | dev_t dev; |
| 9121 | int i_scanned, i_passed; |
| 9122 | |
| 9123 | i_scanned = 0; |
| 9124 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9125 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9126 | pr_info("md: Autodetecting RAID arrays.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9127 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9128 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9129 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 9130 | i_scanned++; |
| 9131 | node_detected_dev = list_entry(all_detected_devices.next, |
| 9132 | struct detected_devices_node, list); |
| 9133 | list_del(&node_detected_dev->list); |
| 9134 | dev = node_detected_dev->dev; |
| 9135 | kfree(node_detected_dev); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 9136 | mutex_unlock(&detected_devices_mutex); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 9137 | rdev = md_import_device(dev,0, 90); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 9138 | mutex_lock(&detected_devices_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9139 | if (IS_ERR(rdev)) |
| 9140 | continue; |
| 9141 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9142 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9143 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9144 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 9145 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9146 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9147 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9148 | } |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9149 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9150 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9151 | 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] | 9152 | |
| 9153 | autorun_devices(part); |
| 9154 | } |
| 9155 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 9156 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9157 | |
| 9158 | static __exit void md_exit(void) |
| 9159 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9160 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9161 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9162 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 9163 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 9164 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 9165 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9166 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 9167 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9168 | unregister_blkdev(mdp_major, "mdp"); |
| 9169 | unregister_reboot_notifier(&md_notifier); |
| 9170 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9171 | |
| 9172 | /* We cannot unload the modules while some process is |
| 9173 | * waiting for us in select() or poll() - wake them up |
| 9174 | */ |
| 9175 | md_unloading = 1; |
| 9176 | while (waitqueue_active(&md_event_waiters)) { |
| 9177 | /* not safe to leave yet */ |
| 9178 | wake_up(&md_event_waiters); |
| 9179 | msleep(delay); |
| 9180 | delay += delay; |
| 9181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9182 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9183 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 9184 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9185 | export_array(mddev); |
NeilBrown | 9356863 | 2017-02-06 13:41:39 +1100 | [diff] [blame] | 9186 | mddev->ctime = 0; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9187 | mddev->hold_active = 0; |
NeilBrown | 9356863 | 2017-02-06 13:41:39 +1100 | [diff] [blame] | 9188 | /* |
| 9189 | * for_each_mddev() will call mddev_put() at the end of each |
| 9190 | * iteration. As the mddev is now fully clear, this will |
| 9191 | * schedule the mddev for destruction by a workqueue, and the |
| 9192 | * destroy_workqueue() below will wait for that to complete. |
| 9193 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9194 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9195 | destroy_workqueue(md_misc_wq); |
| 9196 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9197 | } |
| 9198 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 9199 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9200 | module_exit(md_exit) |
| 9201 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9202 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 9203 | { |
| 9204 | return sprintf(buffer, "%d", start_readonly); |
| 9205 | } |
| 9206 | static int set_ro(const char *val, struct kernel_param *kp) |
| 9207 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 9208 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9209 | } |
| 9210 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 9211 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 9212 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9213 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame^] | 9214 | module_param(create_on_open, bool, S_IRUSR|S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9216 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 9217 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 9218 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 9219 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |