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. |
| 33 | */ |
| 34 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 37 | #include <linux/badblocks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 39 | #include <linux/seq_file.h> |
Al Viro | ff01bb48 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 40 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 41 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 42 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 43 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 44 | #include <linux/hdreg.h> |
| 45 | #include <linux/proc_fs.h> |
| 46 | #include <linux/random.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 47 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 48 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 49 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 50 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 51 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 52 | #include <linux/raid/md_p.h> |
| 53 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 54 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 55 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 56 | #include "bitmap.h" |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 57 | #include "md-cluster.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 60 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #endif |
| 62 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 63 | /* pers_list is a list of registered personalities protected |
| 64 | * by pers_lock. |
| 65 | * pers_lock does extra service to protect accesses to |
| 66 | * mddev->thread when the mutex cannot be held. |
| 67 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 68 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | static DEFINE_SPINLOCK(pers_lock); |
| 70 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 71 | struct md_cluster_operations *md_cluster_ops; |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame] | 72 | EXPORT_SYMBOL(md_cluster_ops); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 73 | struct module *md_cluster_mod; |
| 74 | EXPORT_SYMBOL(md_cluster_mod); |
| 75 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 76 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 77 | static struct workqueue_struct *md_wq; |
| 78 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 79 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 80 | static int remove_and_add_spares(struct mddev *mddev, |
| 81 | struct md_rdev *this); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 82 | static void mddev_detach(struct mddev *mddev); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 85 | * Default number of read corrections we'll attempt on an rdev |
| 86 | * before ejecting it from the array. We divide the read error |
| 87 | * count by 2 for every hour elapsed between read errors. |
| 88 | */ |
| 89 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 90 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 92 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 93 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 94 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 96 | * speed limit - in case reconstruction slows down your system despite |
| 97 | * idle IO detection. |
| 98 | * |
| 99 | * 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] | 100 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | */ |
| 102 | |
| 103 | static int sysctl_speed_limit_min = 1000; |
| 104 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 105 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 106 | { |
| 107 | return mddev->sync_speed_min ? |
| 108 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 109 | } |
| 110 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 111 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 112 | { |
| 113 | return mddev->sync_speed_max ? |
| 114 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | static struct ctl_table_header *raid_table_header; |
| 118 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 119 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | .procname = "speed_limit_min", |
| 122 | .data = &sysctl_speed_limit_min, |
| 123 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 124 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 125 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | }, |
| 127 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | .procname = "speed_limit_max", |
| 129 | .data = &sysctl_speed_limit_max, |
| 130 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 131 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 132 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 134 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 137 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | .procname = "raid", |
| 140 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 141 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | .child = raid_table, |
| 143 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 144 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 147 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | .procname = "dev", |
| 150 | .maxlen = 0, |
| 151 | .mode = 0555, |
| 152 | .child = raid_dir_table, |
| 153 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 154 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 157 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 159 | static int start_readonly; |
| 160 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 161 | /* bio_clone_mddev |
| 162 | * like bio_clone, but with a local bio set |
| 163 | */ |
| 164 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 165 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 166 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 167 | { |
| 168 | struct bio *b; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 169 | |
| 170 | if (!mddev || !mddev->bio_set) |
| 171 | return bio_alloc(gfp_mask, nr_iovecs); |
| 172 | |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 173 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 174 | if (!b) |
| 175 | return NULL; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 176 | return b; |
| 177 | } |
| 178 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 179 | |
| 180 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 181 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 182 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 183 | if (!mddev || !mddev->bio_set) |
| 184 | return bio_clone(bio, gfp_mask); |
| 185 | |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 186 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 187 | } |
| 188 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 191 | * We have a system wide 'event count' that is incremented |
| 192 | * on any 'interesting' event, and readers of /proc/mdstat |
| 193 | * can use 'poll' or 'select' to find out when the event |
| 194 | * count increases. |
| 195 | * |
| 196 | * Events are: |
| 197 | * start array, stop array, error, add device, remove device, |
| 198 | * start build, activate spare |
| 199 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 200 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 201 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 202 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 203 | { |
| 204 | atomic_inc(&md_event_count); |
| 205 | wake_up(&md_event_waiters); |
| 206 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 207 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 208 | |
| 209 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | * Enables to iterate over all existing md arrays |
| 211 | * all_mddevs_lock protects this list. |
| 212 | */ |
| 213 | static LIST_HEAD(all_mddevs); |
| 214 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | /* |
| 217 | * iterates through all used mddevs in the system. |
| 218 | * We take care to grab the all_mddevs_lock whenever navigating |
| 219 | * the list, and to always hold a refcount when unlocked. |
| 220 | * Any code which breaks out of this loop while own |
| 221 | * a reference to the current mddev and must mddev_put it. |
| 222 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 223 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | \ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 225 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 226 | _tmp = all_mddevs.next; \ |
| 227 | _mddev = NULL;}); \ |
| 228 | ({ if (_tmp != &all_mddevs) \ |
| 229 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 231 | if (_mddev) mddev_put(_mddev); \ |
| 232 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 233 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 235 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | ) |
| 237 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 238 | /* Rather than calling directly into the personality make_request function, |
| 239 | * IO requests come here first so that we can check if the device is |
| 240 | * being suspended pending a reconfiguration. |
| 241 | * We hold a refcount over the call to ->make_request. By the time that |
| 242 | * call has finished, the bio has been linked into some internal structure |
| 243 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 244 | */ |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 245 | 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] | 246 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 247 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 248 | struct mddev *mddev = q->queuedata; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 249 | unsigned int sectors; |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 250 | int cpu; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 251 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 252 | blk_queue_split(q, &bio, q->bio_split); |
| 253 | |
NeilBrown | 274d8cb | 2016-01-04 16:16:58 +1100 | [diff] [blame] | 254 | if (mddev == NULL || mddev->pers == NULL) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 255 | bio_io_error(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 256 | return BLK_QC_T_NONE; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 257 | } |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 258 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 259 | if (bio_sectors(bio) != 0) |
| 260 | bio->bi_error = -EROFS; |
| 261 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 262 | return BLK_QC_T_NONE; |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 263 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 264 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 265 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 266 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 267 | DEFINE_WAIT(__wait); |
| 268 | for (;;) { |
| 269 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 270 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 271 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 272 | break; |
| 273 | rcu_read_unlock(); |
| 274 | schedule(); |
| 275 | rcu_read_lock(); |
| 276 | } |
| 277 | finish_wait(&mddev->sb_wait, &__wait); |
| 278 | } |
| 279 | atomic_inc(&mddev->active_io); |
| 280 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 281 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 282 | /* |
| 283 | * save the sectors now since our bio can |
| 284 | * go away inside make_request |
| 285 | */ |
| 286 | sectors = bio_sectors(bio); |
Shaohua Li | 9c573de | 2016-04-25 16:52:38 -0700 | [diff] [blame] | 287 | /* bio could be mergeable after passing to underlayer */ |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 288 | bio->bi_opf &= ~REQ_NOMERGE; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 289 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 290 | |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 291 | cpu = part_stat_lock(); |
| 292 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 293 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
| 294 | part_stat_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 295 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 296 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 297 | wake_up(&mddev->sb_wait); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 298 | |
| 299 | return BLK_QC_T_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 302 | /* mddev_suspend makes sure no new requests are submitted |
| 303 | * to the device, and that any requests that have been submitted |
| 304 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 305 | * Once mddev_detach() is called and completes, the module will be |
| 306 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 307 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 308 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 309 | { |
Heinz Mauelshagen | 092398d | 2016-05-03 19:43:57 +0200 | [diff] [blame] | 310 | WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk); |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 311 | if (mddev->suspended++) |
| 312 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 313 | synchronize_rcu(); |
| 314 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 315 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 316 | |
| 317 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 318 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 319 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 320 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 321 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 322 | { |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 323 | if (--mddev->suspended) |
| 324 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 325 | wake_up(&mddev->sb_wait); |
| 326 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 327 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 328 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 329 | md_wakeup_thread(mddev->thread); |
| 330 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 331 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 332 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 333 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 334 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 335 | { |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 336 | struct md_personality *pers = mddev->pers; |
| 337 | int ret = 0; |
| 338 | |
| 339 | rcu_read_lock(); |
| 340 | if (mddev->suspended) |
| 341 | ret = 1; |
| 342 | else if (pers && pers->congested) |
| 343 | ret = pers->congested(mddev, bits); |
| 344 | rcu_read_unlock(); |
| 345 | return ret; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 346 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 347 | EXPORT_SYMBOL_GPL(mddev_congested); |
| 348 | static int md_congested(void *data, int bits) |
| 349 | { |
| 350 | struct mddev *mddev = data; |
| 351 | return mddev_congested(mddev, bits); |
| 352 | } |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 353 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 354 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 355 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 356 | */ |
| 357 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 358 | static void md_end_flush(struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 359 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 360 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 361 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 362 | |
| 363 | rdev_dec_pending(rdev, mddev); |
| 364 | |
| 365 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 366 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 367 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 368 | } |
| 369 | bio_put(bio); |
| 370 | } |
| 371 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 372 | static void md_submit_flush_data(struct work_struct *ws); |
| 373 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 374 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 375 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 376 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 377 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 378 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 379 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 380 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 381 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 382 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 383 | if (rdev->raid_disk >= 0 && |
| 384 | !test_bit(Faulty, &rdev->flags)) { |
| 385 | /* Take two references, one is dropped |
| 386 | * when request finishes, one after |
| 387 | * we reclaim rcu_read_lock |
| 388 | */ |
| 389 | struct bio *bi; |
| 390 | atomic_inc(&rdev->nr_pending); |
| 391 | atomic_inc(&rdev->nr_pending); |
| 392 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 393 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 394 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 395 | bi->bi_private = rdev; |
| 396 | bi->bi_bdev = rdev->bdev; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 397 | bio_set_op_attrs(bi, REQ_OP_WRITE, WRITE_FLUSH); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 398 | atomic_inc(&mddev->flush_pending); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 399 | submit_bio(bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 400 | rcu_read_lock(); |
| 401 | rdev_dec_pending(rdev, mddev); |
| 402 | } |
| 403 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 404 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 405 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 406 | } |
| 407 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 408 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 409 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 410 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 411 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 412 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 413 | if (bio->bi_iter.bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 414 | /* an empty barrier - all done */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 415 | bio_endio(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 416 | else { |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 417 | bio->bi_opf &= ~REQ_PREFLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 418 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 419 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 420 | |
| 421 | mddev->flush_bio = NULL; |
| 422 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 423 | } |
| 424 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 425 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 426 | { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 427 | spin_lock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 428 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 429 | !mddev->flush_bio, |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 430 | mddev->lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 431 | mddev->flush_bio = bio; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 432 | spin_unlock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 433 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 434 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 435 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 436 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 437 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 438 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 439 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 440 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 441 | struct mddev *mddev = cb->data; |
| 442 | md_wakeup_thread(mddev->thread); |
| 443 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 444 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 445 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 446 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 447 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | { |
| 449 | atomic_inc(&mddev->active); |
| 450 | return mddev; |
| 451 | } |
| 452 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 453 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 454 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 455 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 457 | struct bio_set *bs = NULL; |
| 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 460 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 461 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 462 | mddev->ctime == 0 && !mddev->hold_active) { |
| 463 | /* Array is not configured at all, and not held active, |
| 464 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 465 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 466 | bs = mddev->bio_set; |
| 467 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 468 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 469 | /* We did a probe so need to clean up. Call |
| 470 | * queue_work inside the spinlock so that |
| 471 | * flush_workqueue() after mddev_find will |
| 472 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 473 | */ |
| 474 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 475 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 476 | } else |
| 477 | kfree(mddev); |
| 478 | } |
| 479 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 480 | if (bs) |
| 481 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 484 | static void md_safemode_timeout(unsigned long data); |
| 485 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 486 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 487 | { |
| 488 | mutex_init(&mddev->open_mutex); |
| 489 | mutex_init(&mddev->reconfig_mutex); |
| 490 | mutex_init(&mddev->bitmap_info.mutex); |
| 491 | INIT_LIST_HEAD(&mddev->disks); |
| 492 | INIT_LIST_HEAD(&mddev->all_mddevs); |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 493 | setup_timer(&mddev->safemode_timer, md_safemode_timeout, |
| 494 | (unsigned long) mddev); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 495 | atomic_set(&mddev->active, 1); |
| 496 | atomic_set(&mddev->openers, 0); |
| 497 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 498 | spin_lock_init(&mddev->lock); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 499 | atomic_set(&mddev->flush_pending, 0); |
| 500 | init_waitqueue_head(&mddev->sb_wait); |
| 501 | init_waitqueue_head(&mddev->recovery_wait); |
| 502 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 503 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 504 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 505 | mddev->resync_min = 0; |
| 506 | mddev->resync_max = MaxSector; |
| 507 | mddev->level = LEVEL_NONE; |
| 508 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 509 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 510 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 511 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 513 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 515 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 516 | unit &= ~((1<<MdpMinorShift)-1); |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | retry: |
| 519 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 521 | if (unit) { |
| 522 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 523 | if (mddev->unit == unit) { |
| 524 | mddev_get(mddev); |
| 525 | spin_unlock(&all_mddevs_lock); |
| 526 | kfree(new); |
| 527 | return mddev; |
| 528 | } |
| 529 | |
| 530 | if (new) { |
| 531 | list_add(&new->all_mddevs, &all_mddevs); |
| 532 | spin_unlock(&all_mddevs_lock); |
| 533 | new->hold_active = UNTIL_IOCTL; |
| 534 | return new; |
| 535 | } |
| 536 | } else if (new) { |
| 537 | /* find an unused unit number */ |
| 538 | static int next_minor = 512; |
| 539 | int start = next_minor; |
| 540 | int is_free = 0; |
| 541 | int dev = 0; |
| 542 | while (!is_free) { |
| 543 | dev = MKDEV(MD_MAJOR, next_minor); |
| 544 | next_minor++; |
| 545 | if (next_minor > MINORMASK) |
| 546 | next_minor = 0; |
| 547 | if (next_minor == start) { |
| 548 | /* Oh dear, all in use. */ |
| 549 | spin_unlock(&all_mddevs_lock); |
| 550 | kfree(new); |
| 551 | return NULL; |
| 552 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 553 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 554 | is_free = 1; |
| 555 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 556 | if (mddev->unit == dev) { |
| 557 | is_free = 0; |
| 558 | break; |
| 559 | } |
| 560 | } |
| 561 | new->unit = dev; |
| 562 | new->md_minor = MINOR(dev); |
| 563 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | list_add(&new->all_mddevs, &all_mddevs); |
| 565 | spin_unlock(&all_mddevs_lock); |
| 566 | return new; |
| 567 | } |
| 568 | spin_unlock(&all_mddevs_lock); |
| 569 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 570 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | if (!new) |
| 572 | return NULL; |
| 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | new->unit = unit; |
| 575 | if (MAJOR(unit) == MD_MAJOR) |
| 576 | new->md_minor = MINOR(unit); |
| 577 | else |
| 578 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 579 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 580 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | goto retry; |
| 583 | } |
| 584 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 585 | static struct attribute_group md_redundancy_group; |
| 586 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 587 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 589 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 590 | /* These cannot be removed under reconfig_mutex as |
| 591 | * an access to the files will try to take reconfig_mutex |
| 592 | * while holding the file unremovable, which leads to |
| 593 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 594 | * So hold set sysfs_active while the remove in happeing, |
| 595 | * and anything else which might set ->to_remove or my |
| 596 | * otherwise change the sysfs namespace will fail with |
| 597 | * -EBUSY if sysfs_active is still set. |
| 598 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 599 | * test it under the same mutex to ensure its correct value |
| 600 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 601 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 602 | struct attribute_group *to_remove = mddev->to_remove; |
| 603 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 604 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 605 | mutex_unlock(&mddev->reconfig_mutex); |
| 606 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 607 | if (mddev->kobj.sd) { |
| 608 | if (to_remove != &md_redundancy_group) |
| 609 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 610 | if (mddev->pers == NULL || |
| 611 | mddev->pers->sync_request == NULL) { |
| 612 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 613 | if (mddev->sysfs_action) |
| 614 | sysfs_put(mddev->sysfs_action); |
| 615 | mddev->sysfs_action = NULL; |
| 616 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 617 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 618 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 619 | } else |
| 620 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 622 | /* As we've dropped the mutex we need a spinlock to |
| 623 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 624 | */ |
| 625 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 626 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 627 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 629 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 631 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 632 | { |
| 633 | struct md_rdev *rdev; |
| 634 | |
| 635 | rdev_for_each_rcu(rdev, mddev) |
| 636 | if (rdev->desc_nr == nr) |
| 637 | return rdev; |
| 638 | |
| 639 | return NULL; |
| 640 | } |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 641 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 642 | |
| 643 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 645 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 647 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | if (rdev->bdev->bd_dev == dev) |
| 649 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | return NULL; |
| 652 | } |
| 653 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 654 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 655 | { |
| 656 | struct md_rdev *rdev; |
| 657 | |
| 658 | rdev_for_each_rcu(rdev, mddev) |
| 659 | if (rdev->bdev->bd_dev == dev) |
| 660 | return rdev; |
| 661 | |
| 662 | return NULL; |
| 663 | } |
| 664 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 665 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 666 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 667 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 668 | list_for_each_entry(pers, &pers_list, list) { |
| 669 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 670 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 671 | if (strcmp(pers->name, clevel)==0) |
| 672 | return pers; |
| 673 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 674 | return NULL; |
| 675 | } |
| 676 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 677 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 678 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 680 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 681 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
| 683 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 684 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 687 | if (!rdev->sb_page) { |
| 688 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 689 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 695 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | { |
| 697 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 698 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | rdev->sb_loaded = 0; |
| 700 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 701 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 702 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 704 | if (rdev->bb_page) { |
| 705 | put_page(rdev->bb_page); |
| 706 | rdev->bb_page = NULL; |
| 707 | } |
Dan Williams | d3b407f | 2016-01-06 12:19:22 -0800 | [diff] [blame] | 708 | badblocks_exit(&rdev->badblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 710 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 712 | static void super_written(struct bio *bio) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 713 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 714 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 715 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 716 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 717 | if (bio->bi_error) { |
| 718 | printk("md: super_written gets error=%d\n", bio->bi_error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 719 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 720 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 721 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 722 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 723 | wake_up(&mddev->sb_wait); |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 724 | rdev_dec_pending(rdev, mddev); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 725 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 726 | } |
| 727 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 728 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 729 | sector_t sector, int size, struct page *page) |
| 730 | { |
| 731 | /* write first size bytes of page to sector of rdev |
| 732 | * Increment mddev->pending_writes before returning |
| 733 | * and decrement it on completion, waking up sb_wait |
| 734 | * if zero is reached. |
| 735 | * If an error occurred, call md_error |
| 736 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 737 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 738 | |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 739 | atomic_inc(&rdev->nr_pending); |
| 740 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 741 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 742 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 743 | bio_add_page(bio, page, size, 0); |
| 744 | bio->bi_private = rdev; |
| 745 | bio->bi_end_io = super_written; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 746 | bio_set_op_attrs(bio, REQ_OP_WRITE, WRITE_FLUSH_FUA); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 747 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 748 | atomic_inc(&mddev->pending_writes); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 749 | submit_bio(bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 750 | } |
| 751 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 752 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 753 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 754 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 755 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 756 | } |
| 757 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 758 | 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] | 759 | struct page *page, int op, int op_flags, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 761 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | int ret; |
| 763 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 764 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 765 | rdev->meta_bdev : rdev->bdev; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 766 | bio_set_op_attrs(bio, op, op_flags); |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 767 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 768 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 769 | else if (rdev->mddev->reshape_position != MaxSector && |
| 770 | (rdev->mddev->reshape_backwards == |
| 771 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 772 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 773 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 774 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | bio_add_page(bio, page, size, 0); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 776 | |
| 777 | submit_bio_wait(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 779 | ret = !bio->bi_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | bio_put(bio); |
| 781 | return ret; |
| 782 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 783 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 785 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | { |
| 787 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | if (rdev->sb_loaded) |
| 790 | return 0; |
| 791 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 792 | 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] | 793 | goto fail; |
| 794 | rdev->sb_loaded = 1; |
| 795 | return 0; |
| 796 | |
| 797 | fail: |
| 798 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 799 | bdevname(rdev->bdev,b)); |
| 800 | return -EINVAL; |
| 801 | } |
| 802 | |
| 803 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 804 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 805 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 806 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 807 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 808 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 812 | { |
| 813 | int ret; |
| 814 | mdp_super_t *tmp1, *tmp2; |
| 815 | |
| 816 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 817 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 818 | |
| 819 | if (!tmp1 || !tmp2) { |
| 820 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 821 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | goto abort; |
| 823 | } |
| 824 | |
| 825 | *tmp1 = *sb1; |
| 826 | *tmp2 = *sb2; |
| 827 | |
| 828 | /* |
| 829 | * nr_disks is not constant |
| 830 | */ |
| 831 | tmp1->nr_disks = 0; |
| 832 | tmp2->nr_disks = 0; |
| 833 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 834 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 836 | kfree(tmp1); |
| 837 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | return ret; |
| 839 | } |
| 840 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 841 | static u32 md_csum_fold(u32 csum) |
| 842 | { |
| 843 | csum = (csum & 0xffff) + (csum >> 16); |
| 844 | return (csum & 0xffff) + (csum >> 16); |
| 845 | } |
| 846 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 847 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 849 | u64 newcsum = 0; |
| 850 | u32 *sb32 = (u32*)sb; |
| 851 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | unsigned int disk_csum, csum; |
| 853 | |
| 854 | disk_csum = sb->sb_csum; |
| 855 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 856 | |
| 857 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 858 | newcsum += sb32[i]; |
| 859 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 860 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 861 | #ifdef CONFIG_ALPHA |
| 862 | /* This used to use csum_partial, which was wrong for several |
| 863 | * reasons including that different results are returned on |
| 864 | * different architectures. It isn't critical that we get exactly |
| 865 | * the same return value as before (we always csum_fold before |
| 866 | * testing, and that removes any differences). However as we |
| 867 | * know that csum_partial always returned a 16bit value on |
| 868 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 869 | */ |
| 870 | sb->sb_csum = md_csum_fold(disk_csum); |
| 871 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 873 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | return csum; |
| 875 | } |
| 876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | /* |
| 878 | * Handle superblock details. |
| 879 | * We want to be able to handle multiple superblock formats |
| 880 | * so we have a common interface to them all, and an array of |
| 881 | * different handlers. |
| 882 | * We rely on user-space to write the initial superblock, and support |
| 883 | * reading and updating of superblocks. |
| 884 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 885 | * 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] | 886 | * loads and validates a superblock on dev. |
| 887 | * if refdev != NULL, compare superblocks on both devices |
| 888 | * Return: |
| 889 | * 0 - dev has a superblock that is compatible with refdev |
| 890 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 891 | * so dev should be used as the refdev in future |
| 892 | * -EINVAL superblock incompatible or invalid |
| 893 | * -othererror e.g. -EIO |
| 894 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 895 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | * Verify that dev is acceptable into mddev. |
| 897 | * The first time, mddev->raid_disks will be 0, and data from |
| 898 | * dev should be merged in. Subsequent calls check that dev |
| 899 | * is new enough. Return 0 or -EINVAL |
| 900 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 901 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | * Update the superblock for rdev with data in mddev |
| 903 | * This does not write to disc. |
| 904 | * |
| 905 | */ |
| 906 | |
| 907 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 908 | char *name; |
| 909 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 910 | int (*load_super)(struct md_rdev *rdev, |
| 911 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 912 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 913 | int (*validate_super)(struct mddev *mddev, |
| 914 | struct md_rdev *rdev); |
| 915 | void (*sync_super)(struct mddev *mddev, |
| 916 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 917 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 918 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 919 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 920 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | }; |
| 922 | |
| 923 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 924 | * Check that the given mddev has no bitmap. |
| 925 | * |
| 926 | * This function is called from the run method of all personalities that do not |
| 927 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 928 | * has a bitmap. Otherwise, it returns 0. |
| 929 | * |
| 930 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 931 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 932 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 933 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 934 | return 0; |
| 935 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 936 | mdname(mddev), mddev->pers->name); |
| 937 | return 1; |
| 938 | } |
| 939 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 940 | |
| 941 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 942 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 944 | 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] | 945 | { |
| 946 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 947 | mdp_super_t *sb; |
| 948 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | |
| 950 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 951 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | * it's at the end of the disk. |
| 953 | * |
| 954 | * It also happens to be a multiple of 4Kb. |
| 955 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 956 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 958 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | if (ret) return ret; |
| 960 | |
| 961 | ret = -EINVAL; |
| 962 | |
| 963 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 964 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | |
| 966 | if (sb->md_magic != MD_SB_MAGIC) { |
| 967 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 968 | b); |
| 969 | goto abort; |
| 970 | } |
| 971 | |
| 972 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 973 | sb->minor_version < 90 || |
| 974 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 976 | sb->major_version, sb->minor_version, |
| 977 | b); |
| 978 | goto abort; |
| 979 | } |
| 980 | |
| 981 | if (sb->raid_disks <= 0) |
| 982 | goto abort; |
| 983 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 984 | if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 986 | b); |
| 987 | goto abort; |
| 988 | } |
| 989 | |
| 990 | rdev->preferred_minor = sb->md_minor; |
| 991 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 992 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 993 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 994 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
| 996 | if (sb->level == LEVEL_MULTIPATH) |
| 997 | rdev->desc_nr = -1; |
| 998 | else |
| 999 | rdev->desc_nr = sb->this_disk.number; |
| 1000 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1001 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1003 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1005 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | if (!uuid_equal(refsb, sb)) { |
| 1007 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1008 | b, bdevname(refdev->bdev,b2)); |
| 1009 | goto abort; |
| 1010 | } |
| 1011 | if (!sb_equal(refsb, sb)) { |
| 1012 | printk(KERN_WARNING "md: %s has same UUID" |
| 1013 | " but different superblock to %s\n", |
| 1014 | b, bdevname(refdev->bdev, b2)); |
| 1015 | goto abort; |
| 1016 | } |
| 1017 | ev1 = md_event(sb); |
| 1018 | ev2 = md_event(refsb); |
| 1019 | if (ev1 > ev2) |
| 1020 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1021 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | ret = 0; |
| 1023 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1024 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1025 | /* Limit to 4TB as metadata cannot record more than that. |
| 1026 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1027 | * record this size) |
| 1028 | */ |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1029 | if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) && |
| 1030 | sb->level >= 1) |
| 1031 | rdev->sectors = (sector_t)(2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1033 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1034 | /* "this cannot possibly happen" ... */ |
| 1035 | ret = -EINVAL; |
| 1036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | abort: |
| 1038 | return ret; |
| 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | * validate_super for 0.90.0 |
| 1043 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1044 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | { |
| 1046 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1047 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1048 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1050 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1051 | clear_bit(Faulty, &rdev->flags); |
| 1052 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1053 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1054 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | if (mddev->raid_disks == 0) { |
| 1057 | mddev->major_version = 0; |
| 1058 | mddev->minor_version = sb->minor_version; |
| 1059 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1060 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1061 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | mddev->ctime = sb->ctime; |
| 1063 | mddev->utime = sb->utime; |
| 1064 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1065 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | mddev->layout = sb->layout; |
| 1067 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1068 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1069 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1070 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1071 | mddev->bitmap_info.space = 0; |
| 1072 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1073 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1074 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1075 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1077 | if (mddev->minor_version >= 91) { |
| 1078 | mddev->reshape_position = sb->reshape_position; |
| 1079 | mddev->delta_disks = sb->delta_disks; |
| 1080 | mddev->new_level = sb->new_level; |
| 1081 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1082 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1083 | if (mddev->delta_disks < 0) |
| 1084 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1085 | } else { |
| 1086 | mddev->reshape_position = MaxSector; |
| 1087 | mddev->delta_disks = 0; |
| 1088 | mddev->new_level = mddev->level; |
| 1089 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1090 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1091 | } |
| 1092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1094 | mddev->recovery_cp = MaxSector; |
| 1095 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1096 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | sb->events_lo == sb->cp_events_lo) { |
| 1098 | mddev->recovery_cp = sb->recovery_cp; |
| 1099 | } else |
| 1100 | mddev->recovery_cp = 0; |
| 1101 | } |
| 1102 | |
| 1103 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1104 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1105 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1106 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1107 | |
| 1108 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1109 | |
| 1110 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1111 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1112 | mddev->bitmap_info.offset = |
| 1113 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1114 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1115 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1116 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1117 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1118 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1119 | /* Insist on good event counter while assembling, except |
| 1120 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1122 | if (sb->disks[rdev->desc_nr].state & ( |
| 1123 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1124 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1125 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1126 | } else if (mddev->bitmap) { |
| 1127 | /* if adding to array with a bitmap, then we can accept an |
| 1128 | * older device ... but not too old. |
| 1129 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1130 | if (ev1 < mddev->bitmap->events_cleared) |
| 1131 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1132 | if (ev1 < mddev->events) |
| 1133 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1134 | } else { |
| 1135 | if (ev1 < mddev->events) |
| 1136 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1137 | return 0; |
| 1138 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | desc = sb->disks + rdev->desc_nr; |
| 1142 | |
| 1143 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1144 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1145 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1146 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1147 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1149 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1150 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1151 | /* active but not in sync implies recovery up to |
| 1152 | * reshape position. We don't know exactly where |
| 1153 | * that is, so set to zero for now */ |
| 1154 | if (mddev->minor_version >= 91) { |
| 1155 | rdev->recovery_offset = 0; |
| 1156 | rdev->raid_disk = desc->raid_disk; |
| 1157 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1159 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1160 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1161 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1162 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | return 0; |
| 1164 | } |
| 1165 | |
| 1166 | /* |
| 1167 | * sync_super for 0.90.0 |
| 1168 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1169 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | { |
| 1171 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1172 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | /* make rdev->sb match mddev data.. |
| 1176 | * |
| 1177 | * 1/ zero out disks |
| 1178 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1179 | * 3/ any empty disks < next_spare become removed |
| 1180 | * |
| 1181 | * disks[0] gets initialised to REMOVED because |
| 1182 | * we cannot be sure from other fields if it has |
| 1183 | * been initialised or not. |
| 1184 | */ |
| 1185 | int i; |
| 1186 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1187 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1188 | rdev->sb_size = MD_SB_BYTES; |
| 1189 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1190 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | |
| 1192 | memset(sb, 0, sizeof(*sb)); |
| 1193 | |
| 1194 | sb->md_magic = MD_SB_MAGIC; |
| 1195 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | sb->patch_version = mddev->patch_version; |
| 1197 | sb->gvalid_words = 0; /* ignored */ |
| 1198 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1199 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1200 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1201 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1202 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1203 | sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1205 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | sb->raid_disks = mddev->raid_disks; |
| 1207 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1208 | sb->not_persistent = 0; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1209 | sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | sb->state = 0; |
| 1211 | sb->events_hi = (mddev->events>>32); |
| 1212 | sb->events_lo = (u32)mddev->events; |
| 1213 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1214 | if (mddev->reshape_position == MaxSector) |
| 1215 | sb->minor_version = 90; |
| 1216 | else { |
| 1217 | sb->minor_version = 91; |
| 1218 | sb->reshape_position = mddev->reshape_position; |
| 1219 | sb->new_level = mddev->new_level; |
| 1220 | sb->delta_disks = mddev->delta_disks; |
| 1221 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1222 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1223 | } |
| 1224 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | if (mddev->in_sync) |
| 1226 | { |
| 1227 | sb->recovery_cp = mddev->recovery_cp; |
| 1228 | sb->cp_events_hi = (mddev->events>>32); |
| 1229 | sb->cp_events_lo = (u32)mddev->events; |
| 1230 | if (mddev->recovery_cp == MaxSector) |
| 1231 | sb->state = (1<< MD_SB_CLEAN); |
| 1232 | } else |
| 1233 | sb->recovery_cp = 0; |
| 1234 | |
| 1235 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1236 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1238 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1239 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1242 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1244 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1245 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1246 | |
| 1247 | if (rdev2->raid_disk >= 0 && |
| 1248 | sb->minor_version >= 91) |
| 1249 | /* we have nowhere to store the recovery_offset, |
| 1250 | * but if it is not below the reshape_position, |
| 1251 | * we can piggy-back on that. |
| 1252 | */ |
| 1253 | is_active = 1; |
| 1254 | if (rdev2->raid_disk < 0 || |
| 1255 | test_bit(Faulty, &rdev2->flags)) |
| 1256 | is_active = 0; |
| 1257 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1258 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1260 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1261 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | d = &sb->disks[rdev2->desc_nr]; |
| 1263 | nr_disks++; |
| 1264 | d->number = rdev2->desc_nr; |
| 1265 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1266 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1267 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | d->raid_disk = rdev2->raid_disk; |
| 1269 | else |
| 1270 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1271 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1273 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1275 | if (test_bit(In_sync, &rdev2->flags)) |
| 1276 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | active++; |
| 1278 | working++; |
| 1279 | } else { |
| 1280 | d->state = 0; |
| 1281 | spare++; |
| 1282 | working++; |
| 1283 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1284 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1285 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1288 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1289 | mdp_disk_t *d = &sb->disks[i]; |
| 1290 | if (d->state == 0 && d->number == 0) { |
| 1291 | d->number = i; |
| 1292 | d->raid_disk = i; |
| 1293 | d->state = (1<<MD_DISK_REMOVED); |
| 1294 | d->state |= (1<<MD_DISK_FAULTY); |
| 1295 | failed++; |
| 1296 | } |
| 1297 | } |
| 1298 | sb->nr_disks = nr_disks; |
| 1299 | sb->active_disks = active; |
| 1300 | sb->working_disks = working; |
| 1301 | sb->failed_disks = failed; |
| 1302 | sb->spare_disks = spare; |
| 1303 | |
| 1304 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1305 | sb->sb_csum = calc_sb_csum(sb); |
| 1306 | } |
| 1307 | |
| 1308 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1309 | * rdev_size_change for 0.90.0 |
| 1310 | */ |
| 1311 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1312 | 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] | 1313 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1314 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1315 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1316 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1317 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1318 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1319 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1320 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1321 | /* Limit to 4TB as metadata cannot record more than that. |
| 1322 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1323 | */ |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1324 | if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) && |
| 1325 | rdev->mddev->level >= 1) |
| 1326 | num_sectors = (sector_t)(2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1327 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1328 | rdev->sb_page); |
| 1329 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1330 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1331 | } |
| 1332 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1333 | static int |
| 1334 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1335 | { |
| 1336 | /* non-zero offset changes not possible with v0.90 */ |
| 1337 | return new_offset == 0; |
| 1338 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1339 | |
| 1340 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | * version 1 superblock |
| 1342 | */ |
| 1343 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1344 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1346 | __le32 disk_csum; |
| 1347 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | unsigned long long newcsum; |
| 1349 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1350 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | |
| 1352 | disk_csum = sb->sb_csum; |
| 1353 | sb->sb_csum = 0; |
| 1354 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1355 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | newcsum += le32_to_cpu(*isuper++); |
| 1357 | |
| 1358 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1359 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | |
| 1361 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1362 | sb->sb_csum = disk_csum; |
| 1363 | return cpu_to_le32(csum); |
| 1364 | } |
| 1365 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1366 | 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] | 1367 | { |
| 1368 | struct mdp_superblock_1 *sb; |
| 1369 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1370 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1371 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1373 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | |
| 1375 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1376 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | * It is always aligned to a 4K boundary and |
| 1378 | * depeding on minor_version, it can be: |
| 1379 | * 0: At least 8K, but less than 12K, from end of device |
| 1380 | * 1: At start of device |
| 1381 | * 2: 4K from start of device. |
| 1382 | */ |
| 1383 | switch(minor_version) { |
| 1384 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1385 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1386 | sb_start -= 8*2; |
| 1387 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | break; |
| 1389 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1390 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | break; |
| 1392 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1393 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | break; |
| 1395 | default: |
| 1396 | return -EINVAL; |
| 1397 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1398 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1400 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1401 | * and it is safe to read 4k, so we do that |
| 1402 | */ |
| 1403 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | if (ret) return ret; |
| 1405 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1406 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | |
| 1408 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1409 | sb->major_version != cpu_to_le32(1) || |
| 1410 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1411 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1412 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | return -EINVAL; |
| 1414 | |
| 1415 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1416 | printk("md: invalid superblock checksum on %s\n", |
| 1417 | bdevname(rdev->bdev,b)); |
| 1418 | return -EINVAL; |
| 1419 | } |
| 1420 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1421 | printk("md: data_size too small on %s\n", |
| 1422 | bdevname(rdev->bdev,b)); |
| 1423 | return -EINVAL; |
| 1424 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1425 | if (sb->pad0 || |
| 1426 | sb->pad3[0] || |
| 1427 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1428 | /* Some padding is non-zero, might be a new feature */ |
| 1429 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | rdev->preferred_minor = 0xffff; |
| 1432 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1433 | rdev->new_data_offset = rdev->data_offset; |
| 1434 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1435 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1436 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1437 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1439 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1440 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1441 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1442 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1443 | |
| 1444 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1445 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1446 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1447 | if (minor_version |
| 1448 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1449 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1450 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1451 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1452 | rdev->desc_nr = -1; |
| 1453 | else |
| 1454 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1455 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1456 | if (!rdev->bb_page) { |
| 1457 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1458 | if (!rdev->bb_page) |
| 1459 | return -ENOMEM; |
| 1460 | } |
| 1461 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1462 | rdev->badblocks.count == 0) { |
| 1463 | /* need to load the bad block list. |
| 1464 | * Currently we limit it to one page. |
| 1465 | */ |
| 1466 | s32 offset; |
| 1467 | sector_t bb_sector; |
| 1468 | u64 *bbp; |
| 1469 | int i; |
| 1470 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1471 | if (sectors > (PAGE_SIZE / 512)) |
| 1472 | return -EINVAL; |
| 1473 | offset = le32_to_cpu(sb->bblog_offset); |
| 1474 | if (offset == 0) |
| 1475 | return -EINVAL; |
| 1476 | bb_sector = (long long)offset; |
| 1477 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1478 | rdev->bb_page, REQ_OP_READ, 0, true)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1479 | return -EIO; |
| 1480 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1481 | rdev->badblocks.shift = sb->bblog_shift; |
| 1482 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1483 | u64 bb = le64_to_cpu(*bbp); |
| 1484 | int count = bb & (0x3ff); |
| 1485 | u64 sector = bb >> 10; |
| 1486 | sector <<= sb->bblog_shift; |
| 1487 | count <<= sb->bblog_shift; |
| 1488 | if (bb + 1 == 0) |
| 1489 | break; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 1490 | if (badblocks_set(&rdev->badblocks, sector, count, 1)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1491 | return -EINVAL; |
| 1492 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1493 | } else if (sb->bblog_offset != 0) |
| 1494 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1495 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1496 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1497 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1498 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1500 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | |
| 1502 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1503 | sb->level != refsb->level || |
| 1504 | sb->layout != refsb->layout || |
| 1505 | sb->chunksize != refsb->chunksize) { |
| 1506 | printk(KERN_WARNING "md: %s has strangely different" |
| 1507 | " superblock to %s\n", |
| 1508 | bdevname(rdev->bdev,b), |
| 1509 | bdevname(refdev->bdev,b2)); |
| 1510 | return -EINVAL; |
| 1511 | } |
| 1512 | ev1 = le64_to_cpu(sb->events); |
| 1513 | ev2 = le64_to_cpu(refsb->events); |
| 1514 | |
| 1515 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1516 | ret = 1; |
| 1517 | else |
| 1518 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1520 | if (minor_version) { |
| 1521 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1522 | sectors -= rdev->data_offset; |
| 1523 | } else |
| 1524 | sectors = rdev->sb_start; |
| 1525 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1527 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1528 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1531 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1533 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1534 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1536 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1537 | clear_bit(Faulty, &rdev->flags); |
| 1538 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1539 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1540 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | if (mddev->raid_disks == 0) { |
| 1543 | mddev->major_version = 1; |
| 1544 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1545 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1546 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1547 | mddev->ctime = le64_to_cpu(sb->ctime); |
| 1548 | mddev->utime = le64_to_cpu(sb->utime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1550 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | mddev->layout = le32_to_cpu(sb->layout); |
| 1552 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1553 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1554 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1555 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1556 | mddev->bitmap_info.space = 0; |
| 1557 | /* Default location for bitmap is 1K after superblock |
| 1558 | * using 3K - total of 4K |
| 1559 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1560 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1561 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1562 | mddev->reshape_backwards = 0; |
| 1563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1565 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1566 | |
| 1567 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1568 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1569 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1570 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1571 | mddev->bitmap_info.offset = |
| 1572 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1573 | /* Metadata doesn't record how much space is available. |
| 1574 | * For 1.0, we assume we can use up to the superblock |
| 1575 | * if before, else to 4K beyond superblock. |
| 1576 | * For others, assume no change is possible. |
| 1577 | */ |
| 1578 | if (mddev->minor_version > 0) |
| 1579 | mddev->bitmap_info.space = 0; |
| 1580 | else if (mddev->bitmap_info.offset > 0) |
| 1581 | mddev->bitmap_info.space = |
| 1582 | 8 - mddev->bitmap_info.offset; |
| 1583 | else |
| 1584 | mddev->bitmap_info.space = |
| 1585 | -mddev->bitmap_info.offset; |
| 1586 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1587 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1588 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1589 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1590 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1591 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1592 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1593 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1594 | if (mddev->delta_disks < 0 || |
| 1595 | (mddev->delta_disks == 0 && |
| 1596 | (le32_to_cpu(sb->feature_map) |
| 1597 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1598 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1599 | } else { |
| 1600 | mddev->reshape_position = MaxSector; |
| 1601 | mddev->delta_disks = 0; |
| 1602 | mddev->new_level = mddev->level; |
| 1603 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1604 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1605 | } |
| 1606 | |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 1607 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) |
Shaohua Li | a62ab49 | 2016-01-06 14:37:13 -0800 | [diff] [blame] | 1608 | set_bit(MD_HAS_JOURNAL, &mddev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1609 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1610 | /* Insist of good event counter while assembling, except for |
| 1611 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1613 | if (rdev->desc_nr >= 0 && |
| 1614 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
Song Liu | a3dfbda | 2015-10-08 21:54:11 -0700 | [diff] [blame] | 1615 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1616 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1617 | if (ev1 < mddev->events) |
| 1618 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1619 | } else if (mddev->bitmap) { |
| 1620 | /* If adding to array with a bitmap, then we can accept an |
| 1621 | * older device, but not too old. |
| 1622 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1623 | if (ev1 < mddev->bitmap->events_cleared) |
| 1624 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1625 | if (ev1 < mddev->events) |
| 1626 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1627 | } else { |
| 1628 | if (ev1 < mddev->events) |
| 1629 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1630 | return 0; |
| 1631 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1633 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1634 | if (rdev->desc_nr < 0 || |
| 1635 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1636 | role = MD_DISK_ROLE_SPARE; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1637 | rdev->desc_nr = -1; |
| 1638 | } else |
| 1639 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | switch(role) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1641 | case MD_DISK_ROLE_SPARE: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | break; |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1643 | case MD_DISK_ROLE_FAULTY: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1644 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | break; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1646 | case MD_DISK_ROLE_JOURNAL: /* journal device */ |
| 1647 | if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) { |
| 1648 | /* journal device without journal feature */ |
| 1649 | printk(KERN_WARNING |
| 1650 | "md: journal device provided without journal feature, ignoring the device\n"); |
| 1651 | return -EINVAL; |
| 1652 | } |
| 1653 | set_bit(Journal, &rdev->flags); |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1654 | rdev->journal_tail = le64_to_cpu(sb->journal_tail); |
Shaohua Li | 9b15603 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 1655 | rdev->raid_disk = 0; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1656 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1658 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1659 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1660 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1661 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1662 | if (!(le32_to_cpu(sb->feature_map) & |
| 1663 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1664 | rdev->saved_raid_disk = -1; |
Guoqing Jiang | bdab1a2 | 2019-07-24 11:09:20 +0200 | [diff] [blame] | 1665 | } else { |
| 1666 | /* |
| 1667 | * If the array is FROZEN, then the device can't |
| 1668 | * be in_sync with rest of array. |
| 1669 | */ |
| 1670 | if (!test_bit(MD_RECOVERY_FROZEN, |
| 1671 | &mddev->recovery)) |
| 1672 | set_bit(In_sync, &rdev->flags); |
| 1673 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | rdev->raid_disk = role; |
| 1675 | break; |
| 1676 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1677 | if (sb->devflags & WriteMostly1) |
| 1678 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1679 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1680 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1681 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1682 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | return 0; |
| 1685 | } |
| 1686 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1687 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | { |
| 1689 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1690 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | int max_dev, i; |
| 1692 | /* make rdev->sb match mddev and rdev data. */ |
| 1693 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1694 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | |
| 1696 | sb->feature_map = 0; |
| 1697 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1698 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1700 | |
| 1701 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1702 | sb->events = cpu_to_le64(mddev->events); |
| 1703 | if (mddev->in_sync) |
| 1704 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
Shaohua Li | bd18f64 | 2015-09-02 13:49:50 -0700 | [diff] [blame] | 1705 | else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags)) |
| 1706 | sb->resync_offset = cpu_to_le64(MaxSector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | else |
| 1708 | sb->resync_offset = cpu_to_le64(0); |
| 1709 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1710 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1711 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1712 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1713 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1714 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1715 | sb->level = cpu_to_le32(mddev->level); |
| 1716 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1717 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1718 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1719 | sb->devflags |= WriteMostly1; |
| 1720 | else |
| 1721 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1722 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1723 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1724 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1725 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1726 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1727 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1728 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1729 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 1730 | if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1731 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1732 | sb->feature_map |= |
| 1733 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1734 | sb->recovery_offset = |
| 1735 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1736 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 1737 | sb->feature_map |= |
| 1738 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1739 | } |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1740 | /* Note: recovery_offset and journal_tail share space */ |
| 1741 | if (test_bit(Journal, &rdev->flags)) |
| 1742 | sb->journal_tail = cpu_to_le64(rdev->journal_tail); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1743 | if (test_bit(Replacement, &rdev->flags)) |
| 1744 | sb->feature_map |= |
| 1745 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1746 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1747 | if (mddev->reshape_position != MaxSector) { |
| 1748 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1749 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1750 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1751 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1752 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1753 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1754 | if (mddev->delta_disks == 0 && |
| 1755 | mddev->reshape_backwards) |
| 1756 | sb->feature_map |
| 1757 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1758 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1759 | sb->feature_map |
| 1760 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1761 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1762 | - rdev->data_offset)); |
| 1763 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1764 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1765 | |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 1766 | if (mddev_is_clustered(mddev)) |
| 1767 | sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED); |
| 1768 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1769 | if (rdev->badblocks.count == 0) |
| 1770 | /* Nothing to do for bad blocks*/ ; |
| 1771 | else if (sb->bblog_offset == 0) |
| 1772 | /* Cannot record bad blocks on this device */ |
| 1773 | md_error(mddev, rdev); |
| 1774 | else { |
| 1775 | struct badblocks *bb = &rdev->badblocks; |
| 1776 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1777 | u64 *p = bb->page; |
| 1778 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1779 | if (bb->changed) { |
| 1780 | unsigned seq; |
| 1781 | |
| 1782 | retry: |
| 1783 | seq = read_seqbegin(&bb->lock); |
| 1784 | |
| 1785 | memset(bbp, 0xff, PAGE_SIZE); |
| 1786 | |
| 1787 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1788 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1789 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1790 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1791 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1792 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1793 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1794 | if (read_seqretry(&bb->lock, seq)) |
| 1795 | goto retry; |
| 1796 | |
| 1797 | bb->sector = (rdev->sb_start + |
| 1798 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1799 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1800 | } |
| 1801 | } |
| 1802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1804 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | if (rdev2->desc_nr+1 > max_dev) |
| 1806 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1807 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1808 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1809 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1810 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1811 | rdev->sb_size = max_dev * 2 + 256; |
| 1812 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1813 | if (rdev->sb_size & bmask) |
| 1814 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1815 | } else |
| 1816 | max_dev = le32_to_cpu(sb->max_dev); |
| 1817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | for (i=0; i<max_dev;i++) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1819 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1820 | |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1821 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) |
| 1822 | sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1824 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1826 | if (test_bit(Faulty, &rdev2->flags)) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1827 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1828 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1830 | else if (test_bit(Journal, &rdev2->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1831 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1832 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1833 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | else |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1835 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1839 | } |
| 1840 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1841 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1842 | 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] | 1843 | { |
| 1844 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1845 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1846 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1847 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1848 | if (rdev->data_offset != rdev->new_data_offset) |
| 1849 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1850 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1851 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1852 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1853 | max_sectors -= rdev->data_offset; |
| 1854 | if (!num_sectors || num_sectors > max_sectors) |
| 1855 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1856 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1857 | /* minor version 0 with bitmap we can't move */ |
| 1858 | return 0; |
| 1859 | } else { |
| 1860 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1861 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1862 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1863 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1864 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1865 | if (!num_sectors || num_sectors > max_sectors) |
| 1866 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1867 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1868 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1869 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1870 | sb->data_size = cpu_to_le64(num_sectors); |
Jason Yan | 3953403 | 2017-03-10 11:27:23 +0800 | [diff] [blame] | 1871 | sb->super_offset = cpu_to_le64(rdev->sb_start); |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1872 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1873 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1874 | rdev->sb_page); |
| 1875 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1876 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1877 | |
| 1878 | } |
| 1879 | |
| 1880 | static int |
| 1881 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1882 | unsigned long long new_offset) |
| 1883 | { |
| 1884 | /* All necessary checks on new >= old have been done */ |
| 1885 | struct bitmap *bitmap; |
| 1886 | if (new_offset >= rdev->data_offset) |
| 1887 | return 1; |
| 1888 | |
| 1889 | /* with 1.0 metadata, there is no metadata to tread on |
| 1890 | * so we can always move back */ |
| 1891 | if (rdev->mddev->minor_version == 0) |
| 1892 | return 1; |
| 1893 | |
| 1894 | /* otherwise we must be sure not to step on |
| 1895 | * any metadata, so stay: |
| 1896 | * 36K beyond start of superblock |
| 1897 | * beyond end of badblocks |
| 1898 | * beyond write-intent bitmap |
| 1899 | */ |
| 1900 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1901 | return 0; |
| 1902 | bitmap = rdev->mddev->bitmap; |
| 1903 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1904 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1905 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1906 | return 0; |
| 1907 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1908 | return 0; |
| 1909 | |
| 1910 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1911 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1913 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | [0] = { |
| 1915 | .name = "0.90.0", |
| 1916 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1917 | .load_super = super_90_load, |
| 1918 | .validate_super = super_90_validate, |
| 1919 | .sync_super = super_90_sync, |
| 1920 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1921 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | }, |
| 1923 | [1] = { |
| 1924 | .name = "md-1", |
| 1925 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1926 | .load_super = super_1_load, |
| 1927 | .validate_super = super_1_validate, |
| 1928 | .sync_super = super_1_sync, |
| 1929 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1930 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | }, |
| 1932 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1934 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1935 | { |
| 1936 | if (mddev->sync_super) { |
| 1937 | mddev->sync_super(mddev, rdev); |
| 1938 | return; |
| 1939 | } |
| 1940 | |
| 1941 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1942 | |
| 1943 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1944 | } |
| 1945 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1946 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1948 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1950 | rcu_read_lock(); |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 1951 | rdev_for_each_rcu(rdev, mddev1) { |
| 1952 | if (test_bit(Faulty, &rdev->flags) || |
| 1953 | test_bit(Journal, &rdev->flags) || |
| 1954 | rdev->raid_disk == -1) |
| 1955 | continue; |
| 1956 | rdev_for_each_rcu(rdev2, mddev2) { |
| 1957 | if (test_bit(Faulty, &rdev2->flags) || |
| 1958 | test_bit(Journal, &rdev2->flags) || |
| 1959 | rdev2->raid_disk == -1) |
| 1960 | continue; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1961 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1962 | rdev2->bdev->bd_contains) { |
| 1963 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1964 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1965 | } |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 1966 | } |
| 1967 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1968 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | return 0; |
| 1970 | } |
| 1971 | |
| 1972 | static LIST_HEAD(pending_raid_disks); |
| 1973 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1974 | /* |
| 1975 | * Try to register data integrity profile for an mddev |
| 1976 | * |
| 1977 | * This is called when an array is started and after a disk has been kicked |
| 1978 | * from the array. It only succeeds if all working and active component devices |
| 1979 | * are integrity capable with matching profiles. |
| 1980 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1981 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1982 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1983 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1984 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1985 | if (list_empty(&mddev->disks)) |
| 1986 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1987 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1988 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1989 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1990 | /* skip spares and non-functional disks */ |
| 1991 | if (test_bit(Faulty, &rdev->flags)) |
| 1992 | continue; |
| 1993 | if (rdev->raid_disk < 0) |
| 1994 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1995 | if (!reference) { |
| 1996 | /* Use the first rdev as the reference */ |
| 1997 | reference = rdev; |
| 1998 | continue; |
| 1999 | } |
| 2000 | /* does this rdev's profile match the reference profile? */ |
| 2001 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 2002 | rdev->bdev->bd_disk) < 0) |
| 2003 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2004 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2005 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2006 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2007 | /* |
| 2008 | * All component devices are integrity capable and have matching |
| 2009 | * profiles, register the common profile for the md device. |
| 2010 | */ |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 2011 | blk_integrity_register(mddev->gendisk, |
| 2012 | bdev_get_integrity(reference->bdev)); |
| 2013 | |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2014 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 2015 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 2016 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 2017 | mdname(mddev)); |
| 2018 | return -EINVAL; |
| 2019 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2020 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2021 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2022 | EXPORT_SYMBOL(md_integrity_register); |
| 2023 | |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2024 | /* |
| 2025 | * Attempt to add an rdev, but only if it is consistent with the current |
| 2026 | * integrity profile |
| 2027 | */ |
| 2028 | int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2029 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2030 | struct blk_integrity *bi_rdev; |
| 2031 | struct blk_integrity *bi_mddev; |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2032 | char name[BDEVNAME_SIZE]; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2033 | |
| 2034 | if (!mddev->gendisk) |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2035 | return 0; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2036 | |
| 2037 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2038 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2039 | |
| 2040 | if (!bi_mddev) /* nothing to do */ |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2041 | return 0; |
| 2042 | |
| 2043 | if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) { |
| 2044 | printk(KERN_NOTICE "%s: incompatible integrity profile for %s\n", |
| 2045 | mdname(mddev), bdevname(rdev->bdev, name)); |
| 2046 | return -ENXIO; |
| 2047 | } |
| 2048 | |
| 2049 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2050 | } |
| 2051 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2052 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2053 | 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] | 2054 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2055 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2056 | struct kobject *ko; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2057 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2059 | /* prevent duplicates */ |
| 2060 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2061 | return -EEXIST; |
| 2062 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2063 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2064 | if (!test_bit(Journal, &rdev->flags) && |
| 2065 | rdev->sectors && |
| 2066 | (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2067 | if (mddev->pers) { |
| 2068 | /* Cannot change size, so fail |
| 2069 | * If mddev->level <= 0, then we don't care |
| 2070 | * about aligning sizes (e.g. linear) |
| 2071 | */ |
| 2072 | if (mddev->level > 0) |
| 2073 | return -ENOSPC; |
| 2074 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2075 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2076 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | |
| 2078 | /* Verify rdev->desc_nr is unique. |
| 2079 | * If it is -1, assign a free number, else |
| 2080 | * check number is not in use |
| 2081 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2082 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | if (rdev->desc_nr < 0) { |
| 2084 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2085 | if (mddev->pers) |
| 2086 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2087 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | choice++; |
| 2089 | rdev->desc_nr = choice; |
| 2090 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2091 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2092 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2094 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2096 | rcu_read_unlock(); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2097 | if (!test_bit(Journal, &rdev->flags) && |
| 2098 | mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2099 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2100 | mdname(mddev), mddev->max_disks); |
| 2101 | return -EBUSY; |
| 2102 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2103 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2104 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2107 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2108 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2109 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2110 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2111 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2112 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2113 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2114 | /* failure here is OK */; |
| 2115 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2116 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2117 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2118 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2119 | |
| 2120 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2121 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2124 | |
| 2125 | fail: |
| 2126 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2127 | b, mdname(mddev)); |
| 2128 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | } |
| 2130 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2131 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2132 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2133 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2134 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2135 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2136 | } |
| 2137 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2138 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | { |
| 2140 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2141 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2142 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2143 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2145 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2146 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2147 | sysfs_put(rdev->sysfs_state); |
| 2148 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2149 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2150 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2151 | * writing to 'remove' to "dev/state". We also need |
| 2152 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2153 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2154 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2155 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2156 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2157 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | /* |
| 2161 | * prevent the device from being mounted, repartitioned or |
| 2162 | * otherwise reused by a RAID array (or any other kernel |
| 2163 | * subsystem), by bd_claiming the device. |
| 2164 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2165 | 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] | 2166 | { |
| 2167 | int err = 0; |
| 2168 | struct block_device *bdev; |
| 2169 | char b[BDEVNAME_SIZE]; |
| 2170 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2171 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2172 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | if (IS_ERR(bdev)) { |
| 2174 | printk(KERN_ERR "md: could not open %s.\n", |
| 2175 | __bdevname(dev, b)); |
| 2176 | return PTR_ERR(bdev); |
| 2177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | rdev->bdev = bdev; |
| 2179 | return err; |
| 2180 | } |
| 2181 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2182 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | { |
| 2184 | struct block_device *bdev = rdev->bdev; |
| 2185 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2186 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | void md_autodetect_dev(dev_t dev); |
| 2190 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2191 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | { |
| 2193 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2196 | bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2197 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2199 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2200 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | #endif |
| 2202 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2203 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | } |
| 2205 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2206 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | { |
| 2208 | unbind_rdev_from_array(rdev); |
| 2209 | export_rdev(rdev); |
| 2210 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2211 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2213 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2215 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2217 | while (!list_empty(&mddev->disks)) { |
| 2218 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2219 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2220 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | mddev->raid_disks = 0; |
| 2223 | mddev->major_version = 0; |
| 2224 | } |
| 2225 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2226 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2228 | /* Update each superblock (in-memory image), but |
| 2229 | * if we are allowed to, skip spares which already |
| 2230 | * have the right event counter, or have one earlier |
| 2231 | * (which would mean they aren't being marked as dirty |
| 2232 | * with the rest of the array) |
| 2233 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2234 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2235 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2236 | if (rdev->sb_events == mddev->events || |
| 2237 | (nospares && |
| 2238 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2239 | rdev->sb_events+1 == mddev->events)) { |
| 2240 | /* Don't update this superblock */ |
| 2241 | rdev->sb_loaded = 2; |
| 2242 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2243 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2244 | rdev->sb_loaded = 1; |
| 2245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | } |
| 2247 | } |
| 2248 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2249 | static bool does_sb_need_changing(struct mddev *mddev) |
| 2250 | { |
| 2251 | struct md_rdev *rdev; |
| 2252 | struct mdp_superblock_1 *sb; |
| 2253 | int role; |
| 2254 | |
| 2255 | /* Find a good rdev */ |
| 2256 | rdev_for_each(rdev, mddev) |
| 2257 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) |
| 2258 | break; |
| 2259 | |
| 2260 | /* No good device found. */ |
| 2261 | if (!rdev) |
| 2262 | return false; |
| 2263 | |
| 2264 | sb = page_address(rdev->sb_page); |
| 2265 | /* Check if a device has become faulty or a spare become active */ |
| 2266 | rdev_for_each(rdev, mddev) { |
| 2267 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 2268 | /* Device activated? */ |
| 2269 | if (role == 0xffff && rdev->raid_disk >=0 && |
| 2270 | !test_bit(Faulty, &rdev->flags)) |
| 2271 | return true; |
| 2272 | /* Device turned faulty? */ |
| 2273 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) |
| 2274 | return true; |
| 2275 | } |
| 2276 | |
| 2277 | /* Check if any mddev parameters have changed */ |
| 2278 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || |
| 2279 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || |
Jason Yan | 9a37d02 | 2017-03-10 11:49:12 +0800 | [diff] [blame] | 2280 | (mddev->layout != le32_to_cpu(sb->layout)) || |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2281 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || |
| 2282 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) |
| 2283 | return true; |
| 2284 | |
| 2285 | return false; |
| 2286 | } |
| 2287 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2288 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2290 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2291 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2292 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2293 | int any_badblocks_changed = 0; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2294 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2296 | if (mddev->ro) { |
| 2297 | if (force_change) |
| 2298 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2299 | return; |
| 2300 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2301 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2302 | repeat: |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2303 | if (mddev_is_clustered(mddev)) { |
| 2304 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2305 | force_change = 1; |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 2306 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2307 | nospares = 1; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2308 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2309 | /* Has someone else has updated the sb */ |
| 2310 | if (!does_sb_need_changing(mddev)) { |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2311 | if (ret == 0) |
| 2312 | md_cluster_ops->metadata_update_cancel(mddev); |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 2313 | bit_clear_unless(&mddev->flags, BIT(MD_CHANGE_PENDING), |
| 2314 | BIT(MD_CHANGE_DEVS) | |
| 2315 | BIT(MD_CHANGE_CLEAN)); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2316 | return; |
| 2317 | } |
| 2318 | } |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2319 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2320 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2321 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2322 | if (rdev->raid_disk >= 0 && |
| 2323 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2324 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2325 | !test_bit(In_sync, &rdev->flags) && |
| 2326 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2327 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2328 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2329 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2330 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2331 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2332 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2333 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2334 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2335 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2336 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2337 | rdev->badblocks.changed = 0; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2338 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2339 | md_error(mddev, rdev); |
| 2340 | } |
| 2341 | clear_bit(Blocked, &rdev->flags); |
| 2342 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2343 | wake_up(&rdev->blocked_wait); |
| 2344 | } |
| 2345 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2346 | wake_up(&mddev->sb_wait); |
| 2347 | return; |
| 2348 | } |
| 2349 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2350 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2351 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 2352 | mddev->utime = ktime_get_real_seconds(); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2353 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2354 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2355 | force_change = 1; |
| 2356 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2357 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2358 | * though if events isn't the right even/odd, we will have to do |
| 2359 | * spares after all |
| 2360 | */ |
| 2361 | nospares = 1; |
| 2362 | if (force_change) |
| 2363 | nospares = 0; |
| 2364 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2365 | /* If the array is degraded, then skipping spares is both |
| 2366 | * dangerous and fairly pointless. |
| 2367 | * Dangerous because a device that was removed from the array |
| 2368 | * might have a event_count that still looks up-to-date, |
| 2369 | * so it can be re-added without a resync. |
| 2370 | * Pointless because if there are any spares to skip, |
| 2371 | * then a recovery will happen and soon that array won't |
| 2372 | * be degraded any more and the spare can go back to sleep then. |
| 2373 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2374 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2375 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2376 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2377 | |
| 2378 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2379 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2380 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2381 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2382 | && mddev->can_decrease_events |
| 2383 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2384 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2385 | mddev->can_decrease_events = 0; |
| 2386 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2387 | /* otherwise we have to go forward and ... */ |
| 2388 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2389 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2392 | /* |
| 2393 | * This 64-bit counter should never wrap. |
| 2394 | * Either we are in around ~1 trillion A.C., assuming |
| 2395 | * 1 reboot per second, or we have a bug... |
| 2396 | */ |
| 2397 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2398 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2399 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2400 | if (rdev->badblocks.changed) |
| 2401 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2402 | if (test_bit(Faulty, &rdev->flags)) |
| 2403 | set_bit(FaultRecorded, &rdev->flags); |
| 2404 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2405 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2406 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2407 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2409 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2410 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2412 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2413 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2415 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2416 | if (rdev->sb_loaded != 1) |
| 2417 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2419 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2420 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2421 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2422 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2423 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2424 | bdevname(rdev->bdev, b), |
| 2425 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2426 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2427 | if (rdev->badblocks.size) { |
| 2428 | md_super_write(mddev, rdev, |
| 2429 | rdev->badblocks.sector, |
| 2430 | rdev->badblocks.size << 9, |
| 2431 | rdev->bb_page); |
| 2432 | rdev->badblocks.size = 0; |
| 2433 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2434 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2435 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2436 | pr_debug("md: %s (skipping faulty)\n", |
| 2437 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2438 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2439 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | /* only need to write one superblock... */ |
| 2441 | break; |
| 2442 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2443 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2444 | /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */ |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2445 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2446 | if (mddev_is_clustered(mddev) && ret == 0) |
| 2447 | md_cluster_ops->metadata_update_finish(mddev); |
| 2448 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2449 | if (mddev->in_sync != sync_req || |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 2450 | !bit_clear_unless(&mddev->flags, BIT(MD_CHANGE_PENDING), |
| 2451 | BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_CLEAN))) |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2452 | /* have to write it out again */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2453 | goto repeat; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2454 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2455 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2456 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2457 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2458 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2459 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2460 | clear_bit(Blocked, &rdev->flags); |
| 2461 | |
| 2462 | if (any_badblocks_changed) |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2463 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2464 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2465 | wake_up(&rdev->blocked_wait); |
| 2466 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2468 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2470 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2471 | { |
| 2472 | struct mddev *mddev = rdev->mddev; |
| 2473 | int err = 0; |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2474 | bool add_journal = test_bit(Journal, &rdev->flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2475 | |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2476 | if (!mddev->pers->hot_remove_disk || add_journal) { |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2477 | /* If there is hot_add_disk but no hot_remove_disk |
| 2478 | * then added disks for geometry changes, |
| 2479 | * and should be added immediately. |
| 2480 | */ |
| 2481 | super_types[mddev->major_version]. |
| 2482 | validate_super(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2483 | if (add_journal) |
| 2484 | mddev_suspend(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2485 | err = mddev->pers->hot_add_disk(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2486 | if (add_journal) |
| 2487 | mddev_resume(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2488 | if (err) { |
Guoqing Jiang | db76767 | 2016-06-02 23:32:05 -0400 | [diff] [blame] | 2489 | md_kick_rdev_from_array(rdev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2490 | return err; |
| 2491 | } |
| 2492 | } |
| 2493 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2494 | |
| 2495 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2496 | if (mddev->degraded) |
| 2497 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2498 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2499 | md_new_event(mddev); |
| 2500 | md_wakeup_thread(mddev->thread); |
| 2501 | return 0; |
| 2502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2504 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2505 | * We want to accept with case. For this we use cmd_match. |
| 2506 | */ |
| 2507 | static int cmd_match(const char *cmd, const char *str) |
| 2508 | { |
| 2509 | /* See if cmd, written into a sysfs file, matches |
| 2510 | * str. They must either be the same, or cmd can |
| 2511 | * have a trailing newline |
| 2512 | */ |
| 2513 | while (*cmd && *str && *cmd == *str) { |
| 2514 | cmd++; |
| 2515 | str++; |
| 2516 | } |
| 2517 | if (*cmd == '\n') |
| 2518 | cmd++; |
| 2519 | if (*str || *cmd) |
| 2520 | return 0; |
| 2521 | return 1; |
| 2522 | } |
| 2523 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2524 | struct rdev_sysfs_entry { |
| 2525 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2526 | ssize_t (*show)(struct md_rdev *, char *); |
| 2527 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2528 | }; |
| 2529 | |
| 2530 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2531 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2532 | { |
| 2533 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2534 | size_t len = 0; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2535 | unsigned long flags = ACCESS_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2536 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2537 | if (test_bit(Faulty, &flags) || |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2538 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2539 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2540 | sep = ","; |
| 2541 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2542 | if (test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2543 | len += sprintf(page+len, "%sin_sync",sep); |
| 2544 | sep = ","; |
| 2545 | } |
Shaohua Li | ac6096e | 2015-10-04 09:20:11 -0700 | [diff] [blame] | 2546 | if (test_bit(Journal, &flags)) { |
| 2547 | len += sprintf(page+len, "%sjournal",sep); |
| 2548 | sep = ","; |
| 2549 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2550 | if (test_bit(WriteMostly, &flags)) { |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2551 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2552 | sep = ","; |
| 2553 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2554 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2555 | (rdev->badblocks.unacked_exist |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2556 | && !test_bit(Faulty, &flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2557 | len += sprintf(page+len, "%sblocked", sep); |
| 2558 | sep = ","; |
| 2559 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2560 | if (!test_bit(Faulty, &flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2561 | !test_bit(Journal, &flags) && |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2562 | !test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2563 | len += sprintf(page+len, "%sspare", sep); |
| 2564 | sep = ","; |
| 2565 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2566 | if (test_bit(WriteErrorSeen, &flags)) { |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2567 | len += sprintf(page+len, "%swrite_error", sep); |
| 2568 | sep = ","; |
| 2569 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2570 | if (test_bit(WantReplacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2571 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2572 | sep = ","; |
| 2573 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2574 | if (test_bit(Replacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2575 | len += sprintf(page+len, "%sreplacement", sep); |
| 2576 | sep = ","; |
| 2577 | } |
| 2578 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2579 | return len+sprintf(page+len, "\n"); |
| 2580 | } |
| 2581 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2582 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2583 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2584 | { |
| 2585 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2586 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2587 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2588 | * writemostly - sets write_mostly |
| 2589 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2590 | * blocked - sets the Blocked flags |
| 2591 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2592 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2593 | * -insync - clear Insync for a device with a slot assigned, |
| 2594 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2595 | * write_error - sets WriteErrorSeen |
| 2596 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2597 | */ |
| 2598 | int err = -EINVAL; |
| 2599 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2600 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2601 | if (test_bit(Faulty, &rdev->flags)) |
| 2602 | err = 0; |
| 2603 | else |
| 2604 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2605 | } else if (cmd_match(buf, "remove")) { |
Shaohua Li | 5d88178 | 2016-07-28 09:06:34 -0700 | [diff] [blame] | 2606 | if (rdev->mddev->pers) { |
| 2607 | clear_bit(Blocked, &rdev->flags); |
| 2608 | remove_and_add_spares(rdev->mddev, rdev); |
| 2609 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2610 | if (rdev->raid_disk >= 0) |
| 2611 | err = -EBUSY; |
| 2612 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2613 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2614 | err = 0; |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 2615 | if (mddev_is_clustered(mddev)) |
| 2616 | err = md_cluster_ops->remove_disk(mddev, rdev); |
| 2617 | |
| 2618 | if (err == 0) { |
| 2619 | md_kick_rdev_from_array(rdev); |
| 2620 | if (mddev->pers) |
| 2621 | md_update_sb(mddev, 1); |
| 2622 | md_new_event(mddev); |
| 2623 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2624 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2625 | } else if (cmd_match(buf, "writemostly")) { |
| 2626 | set_bit(WriteMostly, &rdev->flags); |
| 2627 | err = 0; |
| 2628 | } else if (cmd_match(buf, "-writemostly")) { |
| 2629 | clear_bit(WriteMostly, &rdev->flags); |
| 2630 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2631 | } else if (cmd_match(buf, "blocked")) { |
| 2632 | set_bit(Blocked, &rdev->flags); |
| 2633 | err = 0; |
| 2634 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2635 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2636 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2637 | /* metadata handler doesn't understand badblocks, |
| 2638 | * so we need to fail the device |
| 2639 | */ |
| 2640 | md_error(rdev->mddev, rdev); |
| 2641 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2642 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2643 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2644 | wake_up(&rdev->blocked_wait); |
| 2645 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2646 | md_wakeup_thread(rdev->mddev->thread); |
| 2647 | |
| 2648 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2649 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2650 | set_bit(In_sync, &rdev->flags); |
| 2651 | err = 0; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2652 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 && |
| 2653 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 2654 | if (rdev->mddev->pers == NULL) { |
| 2655 | clear_bit(In_sync, &rdev->flags); |
| 2656 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2657 | rdev->raid_disk = -1; |
| 2658 | err = 0; |
| 2659 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2660 | } else if (cmd_match(buf, "write_error")) { |
| 2661 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2662 | err = 0; |
| 2663 | } else if (cmd_match(buf, "-write_error")) { |
| 2664 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2665 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2666 | } else if (cmd_match(buf, "want_replacement")) { |
| 2667 | /* Any non-spare device that is not a replacement can |
| 2668 | * become want_replacement at any time, but we then need to |
| 2669 | * check if recovery is needed. |
| 2670 | */ |
| 2671 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2672 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2673 | !test_bit(Replacement, &rdev->flags)) |
| 2674 | set_bit(WantReplacement, &rdev->flags); |
| 2675 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2676 | md_wakeup_thread(rdev->mddev->thread); |
| 2677 | err = 0; |
| 2678 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2679 | /* Clearing 'want_replacement' is always allowed. |
| 2680 | * Once replacements starts it is too late though. |
| 2681 | */ |
| 2682 | err = 0; |
| 2683 | clear_bit(WantReplacement, &rdev->flags); |
| 2684 | } else if (cmd_match(buf, "replacement")) { |
| 2685 | /* Can only set a device as a replacement when array has not |
| 2686 | * yet been started. Once running, replacement is automatic |
| 2687 | * from spares, or by assigning 'slot'. |
| 2688 | */ |
| 2689 | if (rdev->mddev->pers) |
| 2690 | err = -EBUSY; |
| 2691 | else { |
| 2692 | set_bit(Replacement, &rdev->flags); |
| 2693 | err = 0; |
| 2694 | } |
| 2695 | } else if (cmd_match(buf, "-replacement")) { |
| 2696 | /* Similarly, can only clear Replacement before start */ |
| 2697 | if (rdev->mddev->pers) |
| 2698 | err = -EBUSY; |
| 2699 | else { |
| 2700 | clear_bit(Replacement, &rdev->flags); |
| 2701 | err = 0; |
| 2702 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2703 | } else if (cmd_match(buf, "re-add")) { |
Yufen Yu | cd72ddca | 2019-04-02 14:22:14 +0800 | [diff] [blame] | 2704 | if (!rdev->mddev->pers) |
| 2705 | err = -EINVAL; |
| 2706 | else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) && |
| 2707 | rdev->saved_raid_disk >= 0) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 2708 | /* clear_bit is performed _after_ all the devices |
| 2709 | * have their local Faulty bit cleared. If any writes |
| 2710 | * happen in the meantime in the local node, they |
| 2711 | * will land in the local bitmap, which will be synced |
| 2712 | * by this node eventually |
| 2713 | */ |
| 2714 | if (!mddev_is_clustered(rdev->mddev) || |
| 2715 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 2716 | clear_bit(Faulty, &rdev->flags); |
| 2717 | err = add_bound_rdev(rdev); |
| 2718 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2719 | } else |
| 2720 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2721 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2722 | if (!err) |
| 2723 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2724 | return err ? err : len; |
| 2725 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2726 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 2727 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2728 | |
| 2729 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2730 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2731 | { |
| 2732 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2733 | } |
| 2734 | |
| 2735 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2736 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2737 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2738 | unsigned int n; |
| 2739 | int rv; |
| 2740 | |
| 2741 | rv = kstrtouint(buf, 10, &n); |
| 2742 | if (rv < 0) |
| 2743 | return rv; |
| 2744 | atomic_set(&rdev->corrected_errors, n); |
| 2745 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2746 | } |
| 2747 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2748 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2749 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2750 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2751 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2752 | { |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2753 | if (test_bit(Journal, &rdev->flags)) |
| 2754 | return sprintf(page, "journal\n"); |
| 2755 | else if (rdev->raid_disk < 0) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2756 | return sprintf(page, "none\n"); |
| 2757 | else |
| 2758 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2759 | } |
| 2760 | |
| 2761 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2762 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2763 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2764 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2765 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2766 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2767 | if (test_bit(Journal, &rdev->flags)) |
| 2768 | return -EBUSY; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2769 | if (strncmp(buf, "none", 4)==0) |
| 2770 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2771 | else { |
| 2772 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 2773 | if (err < 0) |
| 2774 | return err; |
| 2775 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2776 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2777 | /* Setting 'slot' on an active array requires also |
| 2778 | * updating the 'rd%d' link, and communicating |
| 2779 | * with the personality with ->hot_*_disk. |
| 2780 | * For now we only support removing |
| 2781 | * failed/spare devices. This normally happens automatically, |
| 2782 | * but not when the metadata is externally managed. |
| 2783 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2784 | if (rdev->raid_disk == -1) |
| 2785 | return -EEXIST; |
| 2786 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2787 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2788 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2789 | clear_bit(Blocked, &rdev->flags); |
| 2790 | remove_and_add_spares(rdev->mddev, rdev); |
| 2791 | if (rdev->raid_disk >= 0) |
| 2792 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2793 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2794 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2795 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2796 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2797 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2798 | */ |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2799 | int err; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2800 | |
| 2801 | if (rdev->raid_disk != -1) |
| 2802 | return -EBUSY; |
| 2803 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2804 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2805 | return -EBUSY; |
| 2806 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2807 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2808 | return -EINVAL; |
| 2809 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2810 | if (slot >= rdev->mddev->raid_disks && |
| 2811 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2812 | return -ENOSPC; |
| 2813 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2814 | rdev->raid_disk = slot; |
| 2815 | if (test_bit(In_sync, &rdev->flags)) |
| 2816 | rdev->saved_raid_disk = slot; |
| 2817 | else |
| 2818 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2819 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 2820 | clear_bit(Bitmap_sync, &rdev->flags); |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2821 | err = rdev->mddev->pers-> |
| 2822 | hot_add_disk(rdev->mddev, rdev); |
| 2823 | if (err) { |
| 2824 | rdev->raid_disk = -1; |
| 2825 | return err; |
| 2826 | } else |
| 2827 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2828 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
| 2829 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2830 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2831 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2832 | if (slot >= rdev->mddev->raid_disks && |
| 2833 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2834 | return -ENOSPC; |
| 2835 | rdev->raid_disk = slot; |
| 2836 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2837 | clear_bit(Faulty, &rdev->flags); |
| 2838 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2839 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2840 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2841 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2842 | return len; |
| 2843 | } |
| 2844 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2845 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2846 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2847 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2848 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2849 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2850 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2851 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2855 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2856 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2857 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2858 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2859 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2860 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2861 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2862 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2863 | /* Must set offset before size, so overlap checks |
| 2864 | * can be sane */ |
| 2865 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2866 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2867 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2868 | return len; |
| 2869 | } |
| 2870 | |
| 2871 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2872 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2873 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2874 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2875 | { |
| 2876 | return sprintf(page, "%llu\n", |
| 2877 | (unsigned long long)rdev->new_data_offset); |
| 2878 | } |
| 2879 | |
| 2880 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2881 | const char *buf, size_t len) |
| 2882 | { |
| 2883 | unsigned long long new_offset; |
| 2884 | struct mddev *mddev = rdev->mddev; |
| 2885 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2886 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2887 | return -EINVAL; |
| 2888 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 2889 | if (mddev->sync_thread || |
| 2890 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2891 | return -EBUSY; |
| 2892 | if (new_offset == rdev->data_offset) |
| 2893 | /* reset is always permitted */ |
| 2894 | ; |
| 2895 | else if (new_offset > rdev->data_offset) { |
| 2896 | /* must not push array size beyond rdev_sectors */ |
| 2897 | if (new_offset - rdev->data_offset |
| 2898 | + mddev->dev_sectors > rdev->sectors) |
| 2899 | return -E2BIG; |
| 2900 | } |
| 2901 | /* Metadata worries about other space details. */ |
| 2902 | |
| 2903 | /* decreasing the offset is inconsistent with a backwards |
| 2904 | * reshape. |
| 2905 | */ |
| 2906 | if (new_offset < rdev->data_offset && |
| 2907 | mddev->reshape_backwards) |
| 2908 | return -EINVAL; |
| 2909 | /* Increasing offset is inconsistent with forwards |
| 2910 | * reshape. reshape_direction should be set to |
| 2911 | * 'backwards' first. |
| 2912 | */ |
| 2913 | if (new_offset > rdev->data_offset && |
| 2914 | !mddev->reshape_backwards) |
| 2915 | return -EINVAL; |
| 2916 | |
| 2917 | if (mddev->pers && mddev->persistent && |
| 2918 | !super_types[mddev->major_version] |
| 2919 | .allow_new_offset(rdev, new_offset)) |
| 2920 | return -E2BIG; |
| 2921 | rdev->new_data_offset = new_offset; |
| 2922 | if (new_offset > rdev->data_offset) |
| 2923 | mddev->reshape_backwards = 1; |
| 2924 | else if (new_offset < rdev->data_offset) |
| 2925 | mddev->reshape_backwards = 0; |
| 2926 | |
| 2927 | return len; |
| 2928 | } |
| 2929 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2930 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2931 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2932 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2933 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2934 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2935 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2936 | } |
| 2937 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2938 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2939 | { |
| 2940 | /* check if two start/length pairs overlap */ |
| 2941 | if (s1+l1 <= s2) |
| 2942 | return 0; |
| 2943 | if (s2+l2 <= s1) |
| 2944 | return 0; |
| 2945 | return 1; |
| 2946 | } |
| 2947 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2948 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2949 | { |
| 2950 | unsigned long long blocks; |
| 2951 | sector_t new; |
| 2952 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2953 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2954 | return -EINVAL; |
| 2955 | |
| 2956 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2957 | return -EINVAL; /* sector conversion overflow */ |
| 2958 | |
| 2959 | new = blocks * 2; |
| 2960 | if (new != blocks * 2) |
| 2961 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2962 | |
| 2963 | *sectors = new; |
| 2964 | return 0; |
| 2965 | } |
| 2966 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2967 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2968 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2969 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2970 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2971 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2972 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2973 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2974 | if (test_bit(Journal, &rdev->flags)) |
| 2975 | return -EBUSY; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2976 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2977 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2978 | if (rdev->data_offset != rdev->new_data_offset) |
| 2979 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2980 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2981 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2982 | sectors = super_types[my_mddev->major_version]. |
| 2983 | rdev_size_change(rdev, sectors); |
| 2984 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2985 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2986 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2987 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2988 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 2989 | if (!my_mddev->pers->resize) |
| 2990 | /* Cannot change size for RAID0 or Linear etc */ |
| 2991 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2992 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2993 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2994 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2995 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2996 | rdev->sectors = sectors; |
| 2997 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2998 | /* Need to check that all other rdevs with the same |
| 2999 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 3000 | * the rdev lists safely. |
| 3001 | * This check does not provide a hard guarantee, it |
| 3002 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3003 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3004 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3005 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3006 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3007 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3008 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 3009 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3010 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3011 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3012 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 3013 | if (rdev->bdev == rdev2->bdev && |
| 3014 | rdev != rdev2 && |
| 3015 | overlaps(rdev->data_offset, rdev->sectors, |
| 3016 | rdev2->data_offset, |
| 3017 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3018 | overlap = 1; |
| 3019 | break; |
| 3020 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3021 | if (overlap) { |
| 3022 | mddev_put(mddev); |
| 3023 | break; |
| 3024 | } |
| 3025 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3026 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3027 | if (overlap) { |
| 3028 | /* Someone else could have slipped in a size |
| 3029 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3030 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3031 | * safe, and trust userspace not to race with |
| 3032 | * itself |
| 3033 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3034 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3035 | return -EBUSY; |
| 3036 | } |
| 3037 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3038 | return len; |
| 3039 | } |
| 3040 | |
| 3041 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3042 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3043 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3044 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3045 | { |
| 3046 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3047 | |
| 3048 | if (test_bit(In_sync, &rdev->flags) || |
| 3049 | recovery_start == MaxSector) |
| 3050 | return sprintf(page, "none\n"); |
| 3051 | |
| 3052 | return sprintf(page, "%llu\n", recovery_start); |
| 3053 | } |
| 3054 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3055 | 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] | 3056 | { |
| 3057 | unsigned long long recovery_start; |
| 3058 | |
| 3059 | if (cmd_match(buf, "none")) |
| 3060 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3061 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3062 | return -EINVAL; |
| 3063 | |
| 3064 | if (rdev->mddev->pers && |
| 3065 | rdev->raid_disk >= 0) |
| 3066 | return -EBUSY; |
| 3067 | |
| 3068 | rdev->recovery_offset = recovery_start; |
| 3069 | if (recovery_start == MaxSector) |
| 3070 | set_bit(In_sync, &rdev->flags); |
| 3071 | else |
| 3072 | clear_bit(In_sync, &rdev->flags); |
| 3073 | return len; |
| 3074 | } |
| 3075 | |
| 3076 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3077 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3078 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3079 | /* sysfs access to bad-blocks list. |
| 3080 | * We present two files. |
| 3081 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 3082 | * are recorded as bad. The list is truncated to fit within |
| 3083 | * the one-page limit of sysfs. |
| 3084 | * Writing "sector length" to this file adds an acknowledged |
| 3085 | * bad block list. |
| 3086 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 3087 | * been acknowledged. Writing to this file adds bad blocks |
| 3088 | * without acknowledging them. This is largely for testing. |
| 3089 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3090 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3091 | { |
| 3092 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3093 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3094 | 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] | 3095 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3096 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3097 | /* Maybe that ack was all we needed */ |
| 3098 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3099 | wake_up(&rdev->blocked_wait); |
| 3100 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3101 | } |
| 3102 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3103 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3104 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3105 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3106 | { |
| 3107 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3108 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3109 | 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] | 3110 | { |
| 3111 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3112 | } |
| 3113 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3114 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3115 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3116 | static struct attribute *rdev_default_attrs[] = { |
| 3117 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3118 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3119 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3120 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3121 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3122 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3123 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3124 | &rdev_bad_blocks.attr, |
| 3125 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3126 | NULL, |
| 3127 | }; |
| 3128 | static ssize_t |
| 3129 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3130 | { |
| 3131 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3132 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3133 | |
| 3134 | if (!entry->show) |
| 3135 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3136 | if (!rdev->mddev) |
| 3137 | return -EBUSY; |
| 3138 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | static ssize_t |
| 3142 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3143 | const char *page, size_t length) |
| 3144 | { |
| 3145 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3146 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3147 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3148 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3149 | |
| 3150 | if (!entry->store) |
| 3151 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3152 | if (!capable(CAP_SYS_ADMIN)) |
| 3153 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3154 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3155 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3156 | if (rdev->mddev == NULL) |
| 3157 | rv = -EBUSY; |
| 3158 | else |
| 3159 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3160 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3161 | } |
| 3162 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | static void rdev_free(struct kobject *ko) |
| 3166 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3167 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3168 | kfree(rdev); |
| 3169 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3170 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3171 | .show = rdev_attr_show, |
| 3172 | .store = rdev_attr_store, |
| 3173 | }; |
| 3174 | static struct kobj_type rdev_ktype = { |
| 3175 | .release = rdev_free, |
| 3176 | .sysfs_ops = &rdev_sysfs_ops, |
| 3177 | .default_attrs = rdev_default_attrs, |
| 3178 | }; |
| 3179 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3180 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3181 | { |
| 3182 | rdev->desc_nr = -1; |
| 3183 | rdev->saved_raid_disk = -1; |
| 3184 | rdev->raid_disk = -1; |
| 3185 | rdev->flags = 0; |
| 3186 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3187 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3188 | rdev->sb_events = 0; |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 3189 | rdev->last_read_error = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3190 | rdev->sb_loaded = 0; |
| 3191 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3192 | atomic_set(&rdev->nr_pending, 0); |
| 3193 | atomic_set(&rdev->read_errors, 0); |
| 3194 | atomic_set(&rdev->corrected_errors, 0); |
| 3195 | |
| 3196 | INIT_LIST_HEAD(&rdev->same_set); |
| 3197 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3198 | |
| 3199 | /* Add space to store bad block list. |
| 3200 | * This reserves the space even on arrays where it cannot |
| 3201 | * be used - I wonder if that matters |
| 3202 | */ |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3203 | return badblocks_init(&rdev->badblocks, 0); |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3204 | } |
| 3205 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | /* |
| 3207 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3208 | * |
| 3209 | * mark the device faulty if: |
| 3210 | * |
| 3211 | * - the device is nonexistent (zero size) |
| 3212 | * - the device has no valid superblock |
| 3213 | * |
| 3214 | * a faulty rdev _never_ has rdev->sb set. |
| 3215 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3216 | 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] | 3217 | { |
| 3218 | char b[BDEVNAME_SIZE]; |
| 3219 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3220 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | sector_t size; |
| 3222 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3223 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3224 | if (!rdev) { |
| 3225 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3226 | return ERR_PTR(-ENOMEM); |
| 3227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3228 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3229 | err = md_rdev_init(rdev); |
| 3230 | if (err) |
| 3231 | goto abort_free; |
| 3232 | err = alloc_disk_sb(rdev); |
| 3233 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3234 | goto abort_free; |
| 3235 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3236 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3237 | if (err) |
| 3238 | goto abort_free; |
| 3239 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3240 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3241 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3242 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3243 | if (!size) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3244 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3246 | bdevname(rdev->bdev,b)); |
| 3247 | err = -EINVAL; |
| 3248 | goto abort_free; |
| 3249 | } |
| 3250 | |
| 3251 | if (super_format >= 0) { |
| 3252 | err = super_types[super_format]. |
| 3253 | load_super(rdev, NULL, super_minor); |
| 3254 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3255 | printk(KERN_WARNING |
| 3256 | "md: %s does not have a valid v%d.%d " |
| 3257 | "superblock, not importing!\n", |
| 3258 | bdevname(rdev->bdev,b), |
| 3259 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | goto abort_free; |
| 3261 | } |
| 3262 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3263 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | "md: could not read %s's sb, not importing!\n", |
| 3265 | bdevname(rdev->bdev,b)); |
| 3266 | goto abort_free; |
| 3267 | } |
| 3268 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | return rdev; |
| 3271 | |
| 3272 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3273 | if (rdev->bdev) |
| 3274 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3275 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | kfree(rdev); |
| 3277 | return ERR_PTR(err); |
| 3278 | } |
| 3279 | |
| 3280 | /* |
| 3281 | * Check a full RAID array for plausibility |
| 3282 | */ |
| 3283 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3284 | static void analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | { |
| 3286 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3287 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | char b[BDEVNAME_SIZE]; |
| 3289 | |
| 3290 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3291 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3292 | switch (super_types[mddev->major_version]. |
| 3293 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3294 | case 1: |
| 3295 | freshest = rdev; |
| 3296 | break; |
| 3297 | case 0: |
| 3298 | break; |
| 3299 | default: |
| 3300 | printk( KERN_ERR \ |
| 3301 | "md: fatal superblock inconsistency in %s" |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3302 | " -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3304 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | } |
| 3306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | super_types[mddev->major_version]. |
| 3308 | validate_super(mddev, freshest); |
| 3309 | |
| 3310 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3311 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3312 | if (mddev->max_disks && |
| 3313 | (rdev->desc_nr >= mddev->max_disks || |
| 3314 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3315 | printk(KERN_WARNING |
| 3316 | "md: %s: %s: only %d devices permitted\n", |
| 3317 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3318 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3319 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3320 | continue; |
| 3321 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3322 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | if (super_types[mddev->major_version]. |
| 3324 | validate_super(mddev, rdev)) { |
| 3325 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3326 | " from array!\n", |
| 3327 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3328 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | continue; |
| 3330 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3331 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3332 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3333 | rdev->desc_nr = i++; |
| 3334 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3335 | set_bit(In_sync, &rdev->flags); |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3336 | } else if (rdev->raid_disk >= |
| 3337 | (mddev->raid_disks - min(0, mddev->delta_disks)) && |
| 3338 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3339 | rdev->raid_disk = -1; |
| 3340 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3341 | } |
| 3342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3343 | } |
| 3344 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3345 | /* Read a fixed-point number. |
| 3346 | * Numbers in sysfs attributes should be in "standard" units where |
| 3347 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3348 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3349 | * milliseconds or jiffies. |
| 3350 | * This function takes a decimal number with a possible fractional |
| 3351 | * component, and produces an integer which is the result of |
| 3352 | * multiplying that number by 10^'scale'. |
| 3353 | * all without any floating-point arithmetic. |
| 3354 | */ |
| 3355 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3356 | { |
| 3357 | unsigned long result = 0; |
| 3358 | long decimals = -1; |
| 3359 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3360 | if (*cp == '.') |
| 3361 | decimals = 0; |
| 3362 | else if (decimals < scale) { |
| 3363 | unsigned int value; |
| 3364 | value = *cp - '0'; |
| 3365 | result = result * 10 + value; |
| 3366 | if (decimals >= 0) |
| 3367 | decimals++; |
| 3368 | } |
| 3369 | cp++; |
| 3370 | } |
| 3371 | if (*cp == '\n') |
| 3372 | cp++; |
| 3373 | if (*cp) |
| 3374 | return -EINVAL; |
| 3375 | if (decimals < 0) |
| 3376 | decimals = 0; |
| 3377 | while (decimals < scale) { |
| 3378 | result *= 10; |
| 3379 | decimals ++; |
| 3380 | } |
| 3381 | *res = result; |
| 3382 | return 0; |
| 3383 | } |
| 3384 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3385 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3386 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3387 | { |
| 3388 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3389 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3390 | } |
| 3391 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3392 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3393 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3394 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3395 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3396 | if (mddev_is_clustered(mddev)) { |
| 3397 | pr_info("md: Safemode is disabled for clustered mode\n"); |
| 3398 | return -EINVAL; |
| 3399 | } |
| 3400 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3401 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3402 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3403 | if (msec == 0) |
| 3404 | mddev->safemode_delay = 0; |
| 3405 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3406 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3407 | unsigned long new_delay = (msec*HZ)/1000; |
| 3408 | |
| 3409 | if (new_delay == 0) |
| 3410 | new_delay = 1; |
| 3411 | mddev->safemode_delay = new_delay; |
| 3412 | if (new_delay < old_delay || old_delay == 0) |
| 3413 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3414 | } |
| 3415 | return len; |
| 3416 | } |
| 3417 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3418 | __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] | 3419 | |
| 3420 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3421 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3422 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3423 | struct md_personality *p; |
| 3424 | int ret; |
| 3425 | spin_lock(&mddev->lock); |
| 3426 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3427 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3428 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3429 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3430 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3431 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3432 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3433 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3434 | ret = 0; |
| 3435 | spin_unlock(&mddev->lock); |
| 3436 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3437 | } |
| 3438 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3439 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3440 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3441 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3442 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3443 | ssize_t rv; |
| 3444 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3445 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3446 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3447 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3448 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3449 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3450 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3451 | return -EINVAL; |
| 3452 | |
| 3453 | rv = mddev_lock(mddev); |
| 3454 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3455 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3456 | |
| 3457 | if (mddev->pers == NULL) { |
| 3458 | strncpy(mddev->clevel, buf, slen); |
| 3459 | if (mddev->clevel[slen-1] == '\n') |
| 3460 | slen--; |
| 3461 | mddev->clevel[slen] = 0; |
| 3462 | mddev->level = LEVEL_NONE; |
| 3463 | rv = len; |
| 3464 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3465 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3466 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3467 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3468 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3469 | |
| 3470 | /* request to change the personality. Need to ensure: |
| 3471 | * - array is not engaged in resync/recovery/reshape |
| 3472 | * - old personality can be suspended |
| 3473 | * - new personality will access other array. |
| 3474 | */ |
| 3475 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3476 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3477 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3478 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3479 | mddev->reshape_position != MaxSector || |
| 3480 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3481 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3482 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3483 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3484 | if (!mddev->pers->quiesce) { |
| 3485 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3486 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3487 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3491 | strncpy(clevel, buf, slen); |
| 3492 | if (clevel[slen-1] == '\n') |
| 3493 | slen--; |
| 3494 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3495 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3496 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3497 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3498 | if (request_module("md-%s", clevel) != 0) |
| 3499 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3500 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3501 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3502 | if (!pers || !try_module_get(pers->owner)) { |
| 3503 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3504 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3505 | rv = -EINVAL; |
| 3506 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3507 | } |
| 3508 | spin_unlock(&pers_lock); |
| 3509 | |
| 3510 | if (pers == mddev->pers) { |
| 3511 | /* Nothing to do! */ |
| 3512 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3513 | rv = len; |
| 3514 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3515 | } |
| 3516 | if (!pers->takeover) { |
| 3517 | module_put(pers->owner); |
| 3518 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3519 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3520 | rv = -EINVAL; |
| 3521 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3522 | } |
| 3523 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3524 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3525 | rdev->new_raid_disk = rdev->raid_disk; |
| 3526 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3527 | /* ->takeover must set new_* and/or delta_disks |
| 3528 | * if it succeeds, and may set them when it fails. |
| 3529 | */ |
| 3530 | priv = pers->takeover(mddev); |
| 3531 | if (IS_ERR(priv)) { |
| 3532 | mddev->new_level = mddev->level; |
| 3533 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3534 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3535 | mddev->raid_disks -= mddev->delta_disks; |
| 3536 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3537 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3538 | module_put(pers->owner); |
| 3539 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3540 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3541 | rv = PTR_ERR(priv); |
| 3542 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | /* Looks like we have a winner */ |
| 3546 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 3547 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3548 | |
| 3549 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3550 | oldpers = mddev->pers; |
| 3551 | oldpriv = mddev->private; |
| 3552 | mddev->pers = pers; |
| 3553 | mddev->private = priv; |
| 3554 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3555 | mddev->level = mddev->new_level; |
| 3556 | mddev->layout = mddev->new_layout; |
| 3557 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 3558 | mddev->delta_disks = 0; |
| 3559 | mddev->reshape_backwards = 0; |
| 3560 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3561 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3562 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3563 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3564 | mddev->external) { |
| 3565 | /* We are converting from a no-redundancy array |
| 3566 | * to a redundancy array and metadata is managed |
| 3567 | * externally so we need to be sure that writes |
| 3568 | * won't block due to a need to transition |
| 3569 | * clean->dirty |
| 3570 | * until external management is started. |
| 3571 | */ |
| 3572 | mddev->in_sync = 0; |
| 3573 | mddev->safemode_delay = 0; |
| 3574 | mddev->safemode = 0; |
| 3575 | } |
| 3576 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3577 | oldpers->free(mddev, oldpriv); |
| 3578 | |
| 3579 | if (oldpers->sync_request == NULL && |
| 3580 | pers->sync_request != NULL) { |
| 3581 | /* need to add the md_redundancy_group */ |
| 3582 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3583 | printk(KERN_WARNING |
| 3584 | "md: cannot register extra attributes for %s\n", |
| 3585 | mdname(mddev)); |
| 3586 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3587 | } |
| 3588 | if (oldpers->sync_request != NULL && |
| 3589 | pers->sync_request == NULL) { |
| 3590 | /* need to remove the md_redundancy_group */ |
| 3591 | if (mddev->to_remove == NULL) |
| 3592 | mddev->to_remove = &md_redundancy_group; |
| 3593 | } |
| 3594 | |
Alexey Obitotskiy | 4cb9da7 | 2016-06-23 12:11:01 +0200 | [diff] [blame] | 3595 | module_put(oldpers->owner); |
| 3596 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3597 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3598 | if (rdev->raid_disk < 0) |
| 3599 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3600 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3601 | rdev->new_raid_disk = -1; |
| 3602 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3603 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3604 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3605 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3606 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3607 | if (rdev->raid_disk < 0) |
| 3608 | continue; |
| 3609 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3610 | continue; |
| 3611 | rdev->raid_disk = rdev->new_raid_disk; |
| 3612 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3613 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3614 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3615 | if (sysfs_link_rdev(mddev, rdev)) |
| 3616 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3617 | " for %s after level change\n", |
| 3618 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3619 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3620 | } |
| 3621 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3622 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3623 | /* this is now an array without redundancy, so |
| 3624 | * it must always be in_sync |
| 3625 | */ |
| 3626 | mddev->in_sync = 1; |
| 3627 | del_timer_sync(&mddev->safemode_timer); |
| 3628 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 3629 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3630 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3631 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3632 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 3633 | if (!mddev->thread) |
| 3634 | md_update_sb(mddev, 1); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3635 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3636 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3637 | rv = len; |
| 3638 | out_unlock: |
| 3639 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3640 | return rv; |
| 3641 | } |
| 3642 | |
| 3643 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3644 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3645 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3646 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3647 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3648 | { |
| 3649 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3650 | if (mddev->reshape_position != MaxSector && |
| 3651 | mddev->layout != mddev->new_layout) |
| 3652 | return sprintf(page, "%d (%d)\n", |
| 3653 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3654 | return sprintf(page, "%d\n", mddev->layout); |
| 3655 | } |
| 3656 | |
| 3657 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3658 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3659 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3660 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3661 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3662 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3663 | err = kstrtouint(buf, 10, &n); |
| 3664 | if (err < 0) |
| 3665 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3666 | err = mddev_lock(mddev); |
| 3667 | if (err) |
| 3668 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3669 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3670 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3671 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3672 | err = -EBUSY; |
| 3673 | else if (mddev->ro) |
| 3674 | err = -EROFS; |
| 3675 | else { |
| 3676 | mddev->new_layout = n; |
| 3677 | err = mddev->pers->check_reshape(mddev); |
| 3678 | if (err) |
| 3679 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3680 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3681 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3682 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3683 | if (mddev->reshape_position == MaxSector) |
| 3684 | mddev->layout = n; |
| 3685 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3686 | mddev_unlock(mddev); |
| 3687 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3688 | } |
| 3689 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3690 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3691 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3692 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3693 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3694 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3695 | if (mddev->raid_disks == 0) |
| 3696 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3697 | if (mddev->reshape_position != MaxSector && |
| 3698 | mddev->delta_disks != 0) |
| 3699 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3700 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3701 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3702 | } |
| 3703 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3704 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3705 | |
| 3706 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3707 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3708 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3709 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3710 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3711 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3712 | err = kstrtouint(buf, 10, &n); |
| 3713 | if (err < 0) |
| 3714 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3715 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3716 | err = mddev_lock(mddev); |
| 3717 | if (err) |
| 3718 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3719 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3720 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3721 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3722 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3723 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3724 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3725 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3726 | rdev_for_each(rdev, mddev) { |
| 3727 | if (olddisks < n && |
| 3728 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3729 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3730 | if (olddisks > n && |
| 3731 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3732 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3733 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3734 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3735 | mddev->delta_disks = n - olddisks; |
| 3736 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3737 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3738 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3739 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3740 | out_unlock: |
| 3741 | mddev_unlock(mddev); |
| 3742 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3743 | } |
| 3744 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3745 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3746 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3747 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3748 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3749 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3750 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3751 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3752 | return sprintf(page, "%d (%d)\n", |
| 3753 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3754 | mddev->chunk_sectors << 9); |
| 3755 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3756 | } |
| 3757 | |
| 3758 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3759 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3760 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3761 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3762 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3763 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3764 | err = kstrtoul(buf, 10, &n); |
| 3765 | if (err < 0) |
| 3766 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3767 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3768 | err = mddev_lock(mddev); |
| 3769 | if (err) |
| 3770 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3771 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3772 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3773 | err = -EBUSY; |
| 3774 | else if (mddev->ro) |
| 3775 | err = -EROFS; |
| 3776 | else { |
| 3777 | mddev->new_chunk_sectors = n >> 9; |
| 3778 | err = mddev->pers->check_reshape(mddev); |
| 3779 | if (err) |
| 3780 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3781 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3782 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3783 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3784 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3785 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3786 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3787 | mddev_unlock(mddev); |
| 3788 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3789 | } |
| 3790 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3791 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3792 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3793 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3794 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3795 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3796 | if (mddev->recovery_cp == MaxSector) |
| 3797 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3798 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3799 | } |
| 3800 | |
| 3801 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3802 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3803 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3804 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3805 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3806 | |
| 3807 | if (cmd_match(buf, "none")) |
| 3808 | n = MaxSector; |
| 3809 | else { |
| 3810 | err = kstrtoull(buf, 10, &n); |
| 3811 | if (err < 0) |
| 3812 | return err; |
| 3813 | if (n != (sector_t)n) |
| 3814 | return -EINVAL; |
| 3815 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3816 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3817 | err = mddev_lock(mddev); |
| 3818 | if (err) |
| 3819 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3820 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3821 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3822 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3823 | if (!err) { |
| 3824 | mddev->recovery_cp = n; |
| 3825 | if (mddev->pers) |
| 3826 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3827 | } |
| 3828 | mddev_unlock(mddev); |
| 3829 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3830 | } |
| 3831 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3832 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 3833 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3834 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3835 | /* |
| 3836 | * The array state can be: |
| 3837 | * |
| 3838 | * clear |
| 3839 | * No devices, no size, no level |
| 3840 | * Equivalent to STOP_ARRAY ioctl |
| 3841 | * inactive |
| 3842 | * May have some settings, but array is not active |
| 3843 | * all IO results in error |
| 3844 | * When written, doesn't tear down array, but just stops it |
| 3845 | * suspended (not supported yet) |
| 3846 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3847 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3848 | * readonly |
| 3849 | * no resync can happen. no superblocks get written. |
| 3850 | * write requests fail |
| 3851 | * read-auto |
| 3852 | * like readonly, but behaves like 'clean' on a write request. |
| 3853 | * |
| 3854 | * clean - no pending writes, but otherwise active. |
| 3855 | * When written to inactive array, starts without resync |
| 3856 | * If a write request arrives then |
| 3857 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3858 | * if not known, block and switch to write-pending |
| 3859 | * If written to an active array that has pending writes, then fails. |
| 3860 | * active |
| 3861 | * fully active: IO and resync can be happening. |
| 3862 | * When written to inactive array, starts with resync |
| 3863 | * |
| 3864 | * write-pending |
| 3865 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3866 | * |
| 3867 | * active-idle |
| 3868 | * like active, but no writes have been seen for a while (100msec). |
| 3869 | * |
| 3870 | */ |
| 3871 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3872 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3873 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3874 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3875 | "write-pending", "active-idle", NULL }; |
| 3876 | |
| 3877 | static int match_word(const char *word, char **list) |
| 3878 | { |
| 3879 | int n; |
| 3880 | for (n=0; list[n]; n++) |
| 3881 | if (cmd_match(word, list[n])) |
| 3882 | break; |
| 3883 | return n; |
| 3884 | } |
| 3885 | |
| 3886 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3887 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3888 | { |
| 3889 | enum array_state st = inactive; |
| 3890 | |
| 3891 | if (mddev->pers) |
| 3892 | switch(mddev->ro) { |
| 3893 | case 1: |
| 3894 | st = readonly; |
| 3895 | break; |
| 3896 | case 2: |
| 3897 | st = read_auto; |
| 3898 | break; |
| 3899 | case 0: |
Tomasz Majchrzak | 16f8894 | 2016-10-24 12:47:28 +0200 | [diff] [blame] | 3900 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3901 | st = write_pending; |
Tomasz Majchrzak | 16f8894 | 2016-10-24 12:47:28 +0200 | [diff] [blame] | 3902 | else if (mddev->in_sync) |
| 3903 | st = clean; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3904 | else if (mddev->safemode) |
| 3905 | st = active_idle; |
| 3906 | else |
| 3907 | st = active; |
| 3908 | } |
| 3909 | else { |
| 3910 | if (list_empty(&mddev->disks) && |
| 3911 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3912 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3913 | st = clear; |
| 3914 | else |
| 3915 | st = inactive; |
| 3916 | } |
| 3917 | return sprintf(page, "%s\n", array_states[st]); |
| 3918 | } |
| 3919 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3920 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 3921 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 3922 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3923 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3924 | |
| 3925 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3926 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3927 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3928 | int err; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3929 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3930 | |
| 3931 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 3932 | /* don't take reconfig_mutex when toggling between |
| 3933 | * clean and active |
| 3934 | */ |
| 3935 | spin_lock(&mddev->lock); |
| 3936 | if (st == active) { |
| 3937 | restart_array(mddev); |
| 3938 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 3939 | wake_up(&mddev->sb_wait); |
| 3940 | err = 0; |
| 3941 | } else /* st == clean */ { |
| 3942 | restart_array(mddev); |
| 3943 | if (atomic_read(&mddev->writes_pending) == 0) { |
| 3944 | if (mddev->in_sync == 0) { |
| 3945 | mddev->in_sync = 1; |
| 3946 | if (mddev->safemode == 1) |
| 3947 | mddev->safemode = 0; |
| 3948 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3949 | } |
| 3950 | err = 0; |
| 3951 | } else |
| 3952 | err = -EBUSY; |
| 3953 | } |
Tomasz Majchrzak | 573275b | 2016-06-30 10:47:09 +0200 | [diff] [blame] | 3954 | if (!err) |
| 3955 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3956 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 3957 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3958 | } |
| 3959 | err = mddev_lock(mddev); |
| 3960 | if (err) |
| 3961 | return err; |
| 3962 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3963 | switch(st) { |
| 3964 | case bad_word: |
| 3965 | break; |
| 3966 | case clear: |
| 3967 | /* stopping an active array */ |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3968 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3969 | break; |
| 3970 | case inactive: |
| 3971 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3972 | if (mddev->pers) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3973 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3974 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3975 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3976 | break; |
| 3977 | case suspended: |
| 3978 | break; /* not supported yet */ |
| 3979 | case readonly: |
| 3980 | if (mddev->pers) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3981 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3982 | else { |
| 3983 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3984 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3985 | err = do_md_run(mddev); |
| 3986 | } |
| 3987 | break; |
| 3988 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3989 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3990 | if (mddev->ro == 0) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3991 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3992 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3993 | err = restart_array(mddev); |
| 3994 | if (err == 0) { |
| 3995 | mddev->ro = 2; |
| 3996 | set_disk_ro(mddev->gendisk, 0); |
| 3997 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3998 | } else { |
| 3999 | mddev->ro = 2; |
| 4000 | err = do_md_run(mddev); |
| 4001 | } |
| 4002 | break; |
| 4003 | case clean: |
| 4004 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4005 | err = restart_array(mddev); |
| 4006 | if (err) |
| 4007 | break; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4008 | spin_lock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4009 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4010 | if (mddev->in_sync == 0) { |
| 4011 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4012 | if (mddev->safemode == 1) |
| 4013 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 4014 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4015 | } |
| 4016 | err = 0; |
| 4017 | } else |
| 4018 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4019 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 4020 | } else |
| 4021 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4022 | break; |
| 4023 | case active: |
| 4024 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4025 | err = restart_array(mddev); |
| 4026 | if (err) |
| 4027 | break; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 4028 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4029 | wake_up(&mddev->sb_wait); |
| 4030 | err = 0; |
| 4031 | } else { |
| 4032 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4033 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4034 | err = do_md_run(mddev); |
| 4035 | } |
| 4036 | break; |
| 4037 | case write_pending: |
| 4038 | case active_idle: |
| 4039 | /* these cannot be set */ |
| 4040 | break; |
| 4041 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4042 | |
| 4043 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 4044 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4045 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4046 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4047 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4048 | mddev_unlock(mddev); |
| 4049 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4050 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4051 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4052 | __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] | 4053 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4054 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4055 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4056 | return sprintf(page, "%d\n", |
| 4057 | atomic_read(&mddev->max_corr_read_errors)); |
| 4058 | } |
| 4059 | |
| 4060 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4061 | 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] | 4062 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4063 | unsigned int n; |
| 4064 | int rv; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4065 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4066 | rv = kstrtouint(buf, 10, &n); |
| 4067 | if (rv < 0) |
| 4068 | return rv; |
| 4069 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4070 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4071 | } |
| 4072 | |
| 4073 | static struct md_sysfs_entry max_corr_read_errors = |
| 4074 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4075 | max_corrected_read_errors_store); |
| 4076 | |
| 4077 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4078 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4079 | { |
| 4080 | return -EINVAL; |
| 4081 | } |
| 4082 | |
| 4083 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4084 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4085 | { |
| 4086 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4087 | /* The new device is added to the array. |
| 4088 | * If the array has a persistent superblock, we read the |
| 4089 | * superblock to initialise info and check validity. |
| 4090 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4091 | * which mainly checks size. |
| 4092 | */ |
| 4093 | char *e; |
| 4094 | int major = simple_strtoul(buf, &e, 10); |
| 4095 | int minor; |
| 4096 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4097 | struct md_rdev *rdev; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4098 | int err; |
| 4099 | |
| 4100 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4101 | return -EINVAL; |
| 4102 | minor = simple_strtoul(e+1, &e, 10); |
| 4103 | if (*e && *e != '\n') |
| 4104 | return -EINVAL; |
| 4105 | dev = MKDEV(major, minor); |
| 4106 | if (major != MAJOR(dev) || |
| 4107 | minor != MINOR(dev)) |
| 4108 | return -EOVERFLOW; |
| 4109 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4110 | flush_workqueue(md_misc_wq); |
| 4111 | |
| 4112 | err = mddev_lock(mddev); |
| 4113 | if (err) |
| 4114 | return err; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4115 | if (mddev->persistent) { |
| 4116 | rdev = md_import_device(dev, mddev->major_version, |
| 4117 | mddev->minor_version); |
| 4118 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4119 | struct md_rdev *rdev0 |
| 4120 | = list_entry(mddev->disks.next, |
| 4121 | struct md_rdev, same_set); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4122 | err = super_types[mddev->major_version] |
| 4123 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4124 | if (err < 0) |
| 4125 | goto out; |
| 4126 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4127 | } else if (mddev->external) |
| 4128 | rdev = md_import_device(dev, -2, -1); |
| 4129 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4130 | rdev = md_import_device(dev, -1, -1); |
| 4131 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4132 | if (IS_ERR(rdev)) { |
| 4133 | mddev_unlock(mddev); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4134 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4135 | } |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4136 | err = bind_rdev_to_array(rdev, mddev); |
| 4137 | out: |
| 4138 | if (err) |
| 4139 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4140 | mddev_unlock(mddev); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4141 | return err ? err : len; |
| 4142 | } |
| 4143 | |
| 4144 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4145 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4146 | |
| 4147 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4148 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4149 | { |
| 4150 | char *end; |
| 4151 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4152 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4153 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4154 | err = mddev_lock(mddev); |
| 4155 | if (err) |
| 4156 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4157 | if (!mddev->bitmap) |
| 4158 | goto out; |
| 4159 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4160 | while (*buf) { |
| 4161 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4162 | if (buf == end) break; |
| 4163 | if (*end == '-') { /* range */ |
| 4164 | buf = end + 1; |
| 4165 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4166 | if (buf == end) break; |
| 4167 | } |
| 4168 | if (*end && !isspace(*end)) break; |
| 4169 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4170 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4171 | } |
| 4172 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4173 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4174 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4175 | return len; |
| 4176 | } |
| 4177 | |
| 4178 | static struct md_sysfs_entry md_bitmap = |
| 4179 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4180 | |
| 4181 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4182 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4183 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4184 | return sprintf(page, "%llu\n", |
| 4185 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4186 | } |
| 4187 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4188 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4189 | |
| 4190 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4191 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4192 | { |
| 4193 | /* If array is inactive, we can reduce the component size, but |
| 4194 | * not increase it (except from 0). |
| 4195 | * If array is active, we can try an on-line resize |
| 4196 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4197 | sector_t sectors; |
| 4198 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4199 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4200 | if (err < 0) |
| 4201 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4202 | err = mddev_lock(mddev); |
| 4203 | if (err) |
| 4204 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4205 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4206 | err = update_size(mddev, sectors); |
Xiao Ni | 4ba1e78 | 2016-06-12 17:18:00 +0800 | [diff] [blame] | 4207 | if (err == 0) |
| 4208 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4209 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4210 | if (mddev->dev_sectors == 0 || |
| 4211 | mddev->dev_sectors > sectors) |
| 4212 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4213 | else |
| 4214 | err = -ENOSPC; |
| 4215 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4216 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4217 | return err ? err : len; |
| 4218 | } |
| 4219 | |
| 4220 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4221 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4222 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4223 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4224 | * This is one of |
| 4225 | * 'none' for arrays with no metadata (good luck...) |
| 4226 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4227 | * or N.M for internally known formats |
| 4228 | */ |
| 4229 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4230 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4231 | { |
| 4232 | if (mddev->persistent) |
| 4233 | return sprintf(page, "%d.%d\n", |
| 4234 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4235 | else if (mddev->external) |
| 4236 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4237 | else |
| 4238 | return sprintf(page, "none\n"); |
| 4239 | } |
| 4240 | |
| 4241 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4242 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4243 | { |
| 4244 | int major, minor; |
| 4245 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4246 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4247 | /* Changing the details of 'external' metadata is |
| 4248 | * always permitted. Otherwise there must be |
| 4249 | * no devices attached to the array. |
| 4250 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4251 | |
| 4252 | err = mddev_lock(mddev); |
| 4253 | if (err) |
| 4254 | return err; |
| 4255 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4256 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4257 | ; |
| 4258 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4259 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4260 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4261 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4262 | if (cmd_match(buf, "none")) { |
| 4263 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4264 | mddev->external = 0; |
| 4265 | mddev->major_version = 0; |
| 4266 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4267 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4268 | } |
| 4269 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4270 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4271 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4272 | namelen = sizeof(mddev->metadata_type)-1; |
| 4273 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4274 | mddev->metadata_type[namelen] = 0; |
| 4275 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4276 | mddev->metadata_type[--namelen] = 0; |
| 4277 | mddev->persistent = 0; |
| 4278 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4279 | mddev->major_version = 0; |
| 4280 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4281 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4282 | } |
| 4283 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4284 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4285 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4286 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4287 | buf = e+1; |
| 4288 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4289 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4290 | goto out_unlock; |
| 4291 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4292 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4293 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4294 | mddev->major_version = major; |
| 4295 | mddev->minor_version = minor; |
| 4296 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4297 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4298 | err = 0; |
| 4299 | out_unlock: |
| 4300 | mddev_unlock(mddev); |
| 4301 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4302 | } |
| 4303 | |
| 4304 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4305 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4306 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4307 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4308 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4309 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4310 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4311 | unsigned long recovery = mddev->recovery; |
| 4312 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4313 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4314 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4315 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4316 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4317 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4318 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4319 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4320 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4321 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4322 | type = "check"; |
| 4323 | else |
| 4324 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4325 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4326 | type = "recover"; |
NeilBrown | 985ca97 | 2015-07-06 12:26:57 +1000 | [diff] [blame] | 4327 | else if (mddev->reshape_position != MaxSector) |
| 4328 | type = "reshape"; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4329 | } |
| 4330 | return sprintf(page, "%s\n", type); |
| 4331 | } |
| 4332 | |
| 4333 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4334 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4335 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4336 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4337 | return -EINVAL; |
| 4338 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4339 | |
| 4340 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4341 | if (cmd_match(page, "frozen")) |
| 4342 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4343 | else |
| 4344 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4345 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4346 | mddev_lock(mddev) == 0) { |
| 4347 | flush_workqueue(md_misc_wq); |
| 4348 | if (mddev->sync_thread) { |
| 4349 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4350 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4351 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4352 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4353 | } |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4354 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4355 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4356 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4357 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4358 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4359 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4360 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4361 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4362 | int err; |
| 4363 | if (mddev->pers->start_reshape == NULL) |
| 4364 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4365 | err = mddev_lock(mddev); |
| 4366 | if (!err) { |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4367 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4368 | err = -EBUSY; |
| 4369 | else { |
| 4370 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4371 | err = mddev->pers->start_reshape(mddev); |
| 4372 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4373 | mddev_unlock(mddev); |
| 4374 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4375 | if (err) |
| 4376 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4377 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4378 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4379 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4380 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4381 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4382 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4383 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4384 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4385 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4386 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4387 | if (mddev->ro == 2) { |
| 4388 | /* A write to sync_action is enough to justify |
| 4389 | * canceling read-auto mode |
| 4390 | */ |
| 4391 | mddev->ro = 0; |
| 4392 | md_wakeup_thread(mddev->sync_thread); |
| 4393 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4394 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4395 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4396 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4397 | return len; |
| 4398 | } |
| 4399 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4400 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4401 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4402 | |
| 4403 | static ssize_t |
| 4404 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4405 | { |
| 4406 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4407 | } |
| 4408 | |
| 4409 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4410 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4411 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4412 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4413 | { |
| 4414 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4415 | (unsigned long long) |
| 4416 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4417 | } |
| 4418 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4419 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4420 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4421 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4422 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4423 | { |
| 4424 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4425 | mddev->sync_speed_min ? "local": "system"); |
| 4426 | } |
| 4427 | |
| 4428 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4429 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4430 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4431 | unsigned int min; |
| 4432 | int rv; |
| 4433 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4434 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4435 | min = 0; |
| 4436 | } else { |
| 4437 | rv = kstrtouint(buf, 10, &min); |
| 4438 | if (rv < 0) |
| 4439 | return rv; |
| 4440 | if (min == 0) |
| 4441 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4442 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4443 | mddev->sync_speed_min = min; |
| 4444 | return len; |
| 4445 | } |
| 4446 | |
| 4447 | static struct md_sysfs_entry md_sync_min = |
| 4448 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4449 | |
| 4450 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4451 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4452 | { |
| 4453 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4454 | mddev->sync_speed_max ? "local": "system"); |
| 4455 | } |
| 4456 | |
| 4457 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4458 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4459 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4460 | unsigned int max; |
| 4461 | int rv; |
| 4462 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4463 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4464 | max = 0; |
| 4465 | } else { |
| 4466 | rv = kstrtouint(buf, 10, &max); |
| 4467 | if (rv < 0) |
| 4468 | return rv; |
| 4469 | if (max == 0) |
| 4470 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4471 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4472 | mddev->sync_speed_max = max; |
| 4473 | return len; |
| 4474 | } |
| 4475 | |
| 4476 | static struct md_sysfs_entry md_sync_max = |
| 4477 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4478 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4479 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4480 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4481 | { |
| 4482 | return sprintf(page, "%d\n", mddev->degraded); |
| 4483 | } |
| 4484 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4485 | |
| 4486 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4487 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4488 | { |
| 4489 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4490 | } |
| 4491 | |
| 4492 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4493 | 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] | 4494 | { |
| 4495 | long n; |
| 4496 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4497 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4498 | return -EINVAL; |
| 4499 | |
| 4500 | if (n != 0 && n != 1) |
| 4501 | return -EINVAL; |
| 4502 | |
| 4503 | mddev->parallel_resync = n; |
| 4504 | |
| 4505 | if (mddev->sync_thread) |
| 4506 | wake_up(&resync_wait); |
| 4507 | |
| 4508 | return len; |
| 4509 | } |
| 4510 | |
| 4511 | /* force parallel resync, even with shared block devices */ |
| 4512 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4513 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4514 | sync_force_parallel_show, sync_force_parallel_store); |
| 4515 | |
| 4516 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4517 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4518 | { |
| 4519 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4520 | if (mddev->curr_resync == 0) |
| 4521 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4522 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4523 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4524 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4525 | db = resync - mddev->resync_mark_cnt; |
| 4526 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4527 | } |
| 4528 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4529 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4530 | |
| 4531 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4532 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4533 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4534 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4535 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4536 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4537 | return sprintf(page, "none\n"); |
| 4538 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4539 | if (mddev->curr_resync == 1 || |
| 4540 | mddev->curr_resync == 2) |
| 4541 | return sprintf(page, "delayed\n"); |
| 4542 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4543 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4544 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4545 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4546 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4547 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4548 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4549 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4550 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4551 | } |
| 4552 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4553 | static struct md_sysfs_entry md_sync_completed = |
| 4554 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4555 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4556 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4557 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4558 | { |
| 4559 | return sprintf(page, "%llu\n", |
| 4560 | (unsigned long long)mddev->resync_min); |
| 4561 | } |
| 4562 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4563 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4564 | { |
| 4565 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4566 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4567 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4568 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4569 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4570 | |
| 4571 | spin_lock(&mddev->lock); |
| 4572 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4573 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4574 | goto out_unlock; |
| 4575 | |
| 4576 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4577 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4578 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4579 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 4580 | /* Round down to multiple of 4K for safety */ |
| 4581 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4582 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4583 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4584 | out_unlock: |
| 4585 | spin_unlock(&mddev->lock); |
| 4586 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | static struct md_sysfs_entry md_min_sync = |
| 4590 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4591 | |
| 4592 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4593 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4594 | { |
| 4595 | if (mddev->resync_max == MaxSector) |
| 4596 | return sprintf(page, "max\n"); |
| 4597 | else |
| 4598 | return sprintf(page, "%llu\n", |
| 4599 | (unsigned long long)mddev->resync_max); |
| 4600 | } |
| 4601 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4602 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4603 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4604 | int err; |
| 4605 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4606 | if (strncmp(buf, "max", 3) == 0) |
| 4607 | mddev->resync_max = MaxSector; |
| 4608 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4609 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4610 | int chunk; |
| 4611 | |
| 4612 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4613 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4614 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4615 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4616 | goto out_unlock; |
| 4617 | |
| 4618 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4619 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4620 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4621 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4622 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4623 | |
| 4624 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4625 | chunk = mddev->chunk_sectors; |
| 4626 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4627 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4628 | |
| 4629 | err = -EINVAL; |
| 4630 | if (sector_div(temp, chunk)) |
| 4631 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4632 | } |
| 4633 | mddev->resync_max = max; |
| 4634 | } |
| 4635 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4636 | err = 0; |
| 4637 | out_unlock: |
| 4638 | spin_unlock(&mddev->lock); |
| 4639 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4640 | } |
| 4641 | |
| 4642 | static struct md_sysfs_entry md_max_sync = |
| 4643 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4644 | |
| 4645 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4646 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4647 | { |
| 4648 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4649 | } |
| 4650 | |
| 4651 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4652 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4653 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4654 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4655 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4656 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4657 | err = kstrtoull(buf, 10, &new); |
| 4658 | if (err < 0) |
| 4659 | return err; |
| 4660 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4661 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4662 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4663 | err = mddev_lock(mddev); |
| 4664 | if (err) |
| 4665 | return err; |
| 4666 | err = -EINVAL; |
| 4667 | if (mddev->pers == NULL || |
| 4668 | mddev->pers->quiesce == NULL) |
| 4669 | goto unlock; |
| 4670 | old = mddev->suspend_lo; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4671 | mddev->suspend_lo = new; |
| 4672 | if (new >= old) |
| 4673 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4674 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4675 | else { |
| 4676 | /* Expanding suspended region - need to wait */ |
| 4677 | mddev->pers->quiesce(mddev, 1); |
| 4678 | mddev->pers->quiesce(mddev, 0); |
| 4679 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4680 | err = 0; |
| 4681 | unlock: |
| 4682 | mddev_unlock(mddev); |
| 4683 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4684 | } |
| 4685 | static struct md_sysfs_entry md_suspend_lo = |
| 4686 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4687 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4688 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4689 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4690 | { |
| 4691 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4692 | } |
| 4693 | |
| 4694 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4695 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4696 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4697 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4698 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4699 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4700 | err = kstrtoull(buf, 10, &new); |
| 4701 | if (err < 0) |
| 4702 | return err; |
| 4703 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4704 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4705 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4706 | err = mddev_lock(mddev); |
| 4707 | if (err) |
| 4708 | return err; |
| 4709 | err = -EINVAL; |
| 4710 | if (mddev->pers == NULL || |
| 4711 | mddev->pers->quiesce == NULL) |
| 4712 | goto unlock; |
| 4713 | old = mddev->suspend_hi; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4714 | mddev->suspend_hi = new; |
| 4715 | if (new <= old) |
| 4716 | /* Shrinking suspended region */ |
| 4717 | mddev->pers->quiesce(mddev, 2); |
| 4718 | else { |
| 4719 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4720 | mddev->pers->quiesce(mddev, 1); |
| 4721 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4722 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4723 | err = 0; |
| 4724 | unlock: |
| 4725 | mddev_unlock(mddev); |
| 4726 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4727 | } |
| 4728 | static struct md_sysfs_entry md_suspend_hi = |
| 4729 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4730 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4731 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4732 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4733 | { |
| 4734 | if (mddev->reshape_position != MaxSector) |
| 4735 | return sprintf(page, "%llu\n", |
| 4736 | (unsigned long long)mddev->reshape_position); |
| 4737 | strcpy(page, "none\n"); |
| 4738 | return 5; |
| 4739 | } |
| 4740 | |
| 4741 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4742 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4743 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4744 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4745 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4746 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4747 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4748 | err = kstrtoull(buf, 10, &new); |
| 4749 | if (err < 0) |
| 4750 | return err; |
| 4751 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4752 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4753 | err = mddev_lock(mddev); |
| 4754 | if (err) |
| 4755 | return err; |
| 4756 | err = -EBUSY; |
| 4757 | if (mddev->pers) |
| 4758 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4759 | mddev->reshape_position = new; |
| 4760 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4761 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4762 | mddev->new_level = mddev->level; |
| 4763 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4764 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4765 | rdev_for_each(rdev, mddev) |
| 4766 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4767 | err = 0; |
| 4768 | unlock: |
| 4769 | mddev_unlock(mddev); |
| 4770 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | static struct md_sysfs_entry md_reshape_position = |
| 4774 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4775 | reshape_position_store); |
| 4776 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4777 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4778 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4779 | { |
| 4780 | return sprintf(page, "%s\n", |
| 4781 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4782 | } |
| 4783 | |
| 4784 | static ssize_t |
| 4785 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4786 | { |
| 4787 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4788 | int err; |
| 4789 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4790 | if (cmd_match(buf, "forwards")) |
| 4791 | backwards = 0; |
| 4792 | else if (cmd_match(buf, "backwards")) |
| 4793 | backwards = 1; |
| 4794 | else |
| 4795 | return -EINVAL; |
| 4796 | if (mddev->reshape_backwards == backwards) |
| 4797 | return len; |
| 4798 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4799 | err = mddev_lock(mddev); |
| 4800 | if (err) |
| 4801 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4802 | /* check if we are allowed to change */ |
| 4803 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4804 | err = -EBUSY; |
| 4805 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4806 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4807 | err = -EINVAL; |
| 4808 | else |
| 4809 | mddev->reshape_backwards = backwards; |
| 4810 | mddev_unlock(mddev); |
| 4811 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | static struct md_sysfs_entry md_reshape_direction = |
| 4815 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4816 | reshape_direction_store); |
| 4817 | |
| 4818 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4819 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4820 | { |
| 4821 | if (mddev->external_size) |
| 4822 | return sprintf(page, "%llu\n", |
| 4823 | (unsigned long long)mddev->array_sectors/2); |
| 4824 | else |
| 4825 | return sprintf(page, "default\n"); |
| 4826 | } |
| 4827 | |
| 4828 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4829 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4830 | { |
| 4831 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4832 | int err; |
| 4833 | |
| 4834 | err = mddev_lock(mddev); |
| 4835 | if (err) |
| 4836 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4837 | |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 4838 | /* cluster raid doesn't support change array_sectors */ |
Zhilong Liu | 1f8fe98 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 4839 | if (mddev_is_clustered(mddev)) { |
| 4840 | mddev_unlock(mddev); |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 4841 | return -EINVAL; |
Zhilong Liu | 1f8fe98 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 4842 | } |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 4843 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4844 | if (strncmp(buf, "default", 7) == 0) { |
| 4845 | if (mddev->pers) |
| 4846 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4847 | else |
| 4848 | sectors = mddev->array_sectors; |
| 4849 | |
| 4850 | mddev->external_size = 0; |
| 4851 | } else { |
| 4852 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4853 | err = -EINVAL; |
| 4854 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 4855 | err = -E2BIG; |
| 4856 | else |
| 4857 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4858 | } |
| 4859 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4860 | if (!err) { |
| 4861 | mddev->array_sectors = sectors; |
| 4862 | if (mddev->pers) { |
| 4863 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4864 | revalidate_disk(mddev->gendisk); |
| 4865 | } |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4866 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4867 | mddev_unlock(mddev); |
| 4868 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4869 | } |
| 4870 | |
| 4871 | static struct md_sysfs_entry md_array_size = |
| 4872 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4873 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4874 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4875 | static struct attribute *md_default_attrs[] = { |
| 4876 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4877 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4878 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4879 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4880 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4881 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4882 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4883 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4884 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4885 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4886 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4887 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4888 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4889 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4890 | NULL, |
| 4891 | }; |
| 4892 | |
| 4893 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4894 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4895 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4896 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4897 | &md_sync_min.attr, |
| 4898 | &md_sync_max.attr, |
| 4899 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4900 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4901 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4902 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4903 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4904 | &md_suspend_lo.attr, |
| 4905 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4906 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4907 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4908 | NULL, |
| 4909 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4910 | static struct attribute_group md_redundancy_group = { |
| 4911 | .name = NULL, |
| 4912 | .attrs = md_redundancy_attrs, |
| 4913 | }; |
| 4914 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4915 | static ssize_t |
| 4916 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4917 | { |
| 4918 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4919 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4920 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4921 | |
| 4922 | if (!entry->show) |
| 4923 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4924 | spin_lock(&all_mddevs_lock); |
| 4925 | if (list_empty(&mddev->all_mddevs)) { |
| 4926 | spin_unlock(&all_mddevs_lock); |
| 4927 | return -EBUSY; |
| 4928 | } |
| 4929 | mddev_get(mddev); |
| 4930 | spin_unlock(&all_mddevs_lock); |
| 4931 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4932 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4933 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4934 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | static ssize_t |
| 4938 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4939 | const char *page, size_t length) |
| 4940 | { |
| 4941 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4942 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4943 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4944 | |
| 4945 | if (!entry->store) |
| 4946 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4947 | if (!capable(CAP_SYS_ADMIN)) |
| 4948 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4949 | spin_lock(&all_mddevs_lock); |
| 4950 | if (list_empty(&mddev->all_mddevs)) { |
| 4951 | spin_unlock(&all_mddevs_lock); |
| 4952 | return -EBUSY; |
| 4953 | } |
| 4954 | mddev_get(mddev); |
| 4955 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4956 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4957 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4958 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4959 | } |
| 4960 | |
| 4961 | static void md_free(struct kobject *ko) |
| 4962 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4963 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4964 | |
| 4965 | if (mddev->sysfs_state) |
| 4966 | sysfs_put(mddev->sysfs_state); |
| 4967 | |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 4968 | if (mddev->queue) |
| 4969 | blk_cleanup_queue(mddev->queue); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4970 | if (mddev->gendisk) { |
| 4971 | del_gendisk(mddev->gendisk); |
| 4972 | put_disk(mddev->gendisk); |
| 4973 | } |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4974 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4975 | kfree(mddev); |
| 4976 | } |
| 4977 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4978 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4979 | .show = md_attr_show, |
| 4980 | .store = md_attr_store, |
| 4981 | }; |
| 4982 | static struct kobj_type md_ktype = { |
| 4983 | .release = md_free, |
| 4984 | .sysfs_ops = &md_sysfs_ops, |
| 4985 | .default_attrs = md_default_attrs, |
| 4986 | }; |
| 4987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4988 | int mdp_major = 0; |
| 4989 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4990 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4991 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4992 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4993 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4994 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4995 | kobject_del(&mddev->kobj); |
| 4996 | kobject_put(&mddev->kobj); |
| 4997 | } |
| 4998 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4999 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5001 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5002 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5003 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5004 | int partitioned; |
| 5005 | int shift; |
| 5006 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5007 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | |
| 5009 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5010 | return -ENODEV; |
| 5011 | |
| 5012 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 5013 | shift = partitioned ? MdpMinorShift : 0; |
| 5014 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5016 | /* wait for any previous instance of this device to be |
| 5017 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5018 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5019 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5020 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5021 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5022 | error = -EEXIST; |
| 5023 | if (mddev->gendisk) |
| 5024 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5025 | |
| 5026 | if (name) { |
| 5027 | /* Need to ensure that 'name' is not a duplicate. |
| 5028 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5029 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5030 | spin_lock(&all_mddevs_lock); |
| 5031 | |
| 5032 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 5033 | if (mddev2->gendisk && |
| 5034 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 5035 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5036 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5037 | } |
| 5038 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5039 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5040 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5041 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5042 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5043 | if (!mddev->queue) |
| 5044 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5045 | mddev->queue->queuedata = mddev; |
| 5046 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5047 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 5048 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | disk = alloc_disk(1 << shift); |
| 5051 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5052 | blk_cleanup_queue(mddev->queue); |
| 5053 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5054 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5055 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5056 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5058 | if (name) |
| 5059 | strcpy(disk->disk_name, name); |
| 5060 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5061 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5062 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5063 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | disk->fops = &md_fops; |
| 5065 | disk->private_data = mddev; |
| 5066 | disk->queue = mddev->queue; |
Jens Axboe | 56883a7 | 2016-03-30 10:16:53 -0600 | [diff] [blame] | 5067 | blk_queue_write_cache(mddev->queue, true, true); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5068 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5069 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5070 | * remove it now. |
| 5071 | */ |
| 5072 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5073 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5074 | /* As soon as we call add_disk(), another thread could get |
| 5075 | * through to md_open, so make sure it doesn't get too far |
| 5076 | */ |
| 5077 | mutex_lock(&mddev->open_mutex); |
| 5078 | add_disk(disk); |
| 5079 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 5080 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 5081 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5082 | if (error) { |
| 5083 | /* This isn't possible, but as kobject_init_and_add is marked |
| 5084 | * __must_check, we must do something with the result |
| 5085 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5086 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 5087 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5088 | error = 0; |
| 5089 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5090 | if (mddev->kobj.sd && |
| 5091 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5092 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5093 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5094 | abort: |
| 5095 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5096 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5097 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5098 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5099 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5100 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5101 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5102 | } |
| 5103 | |
| 5104 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 5105 | { |
| 5106 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | return NULL; |
| 5108 | } |
| 5109 | |
Kees Cook | 24da2c8 | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 5110 | static int add_named_array(const char *val, const struct kernel_param *kp) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5111 | { |
| 5112 | /* val must be "md_*" where * is not all digits. |
| 5113 | * We allocate an array with a large free minor number, and |
| 5114 | * set the name to val. val must not already be an active name. |
| 5115 | */ |
| 5116 | int len = strlen(val); |
| 5117 | char buf[DISK_NAME_LEN]; |
| 5118 | |
| 5119 | while (len && val[len-1] == '\n') |
| 5120 | len--; |
| 5121 | if (len >= DISK_NAME_LEN) |
| 5122 | return -E2BIG; |
| 5123 | strlcpy(buf, val, len+1); |
| 5124 | if (strncmp(buf, "md_", 3) != 0) |
| 5125 | return -EINVAL; |
| 5126 | return md_alloc(0, buf); |
| 5127 | } |
| 5128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5129 | static void md_safemode_timeout(unsigned long data) |
| 5130 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5131 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5133 | if (!atomic_read(&mddev->writes_pending)) { |
| 5134 | mddev->safemode = 1; |
| 5135 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5136 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5137 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5138 | md_wakeup_thread(mddev->thread); |
| 5139 | } |
| 5140 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5141 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5143 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5145 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5146 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5147 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5149 | if (list_empty(&mddev->disks)) |
| 5150 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | |
| 5153 | if (mddev->pers) |
| 5154 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5155 | /* Cannot run until previous stop completes properly */ |
| 5156 | if (mddev->sysfs_active) |
| 5157 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | /* |
| 5160 | * Analyze all RAID superblock(s) |
| 5161 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5162 | if (!mddev->raid_disks) { |
| 5163 | if (!mddev->persistent) |
| 5164 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5165 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5167 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5168 | if (mddev->level != LEVEL_NONE) |
| 5169 | request_module("md-level-%d", mddev->level); |
| 5170 | else if (mddev->clevel[0]) |
| 5171 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | |
| 5173 | /* |
| 5174 | * Drop all container device buffers, from now on |
| 5175 | * the only valid external interface is through the md |
| 5176 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5177 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5178 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5179 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | continue; |
| 5181 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5182 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5183 | |
| 5184 | /* perform some consistency tests on the device. |
| 5185 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5186 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5187 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5188 | if (rdev->meta_bdev) { |
| 5189 | /* Nothing to check */; |
| 5190 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5191 | if (mddev->dev_sectors && |
| 5192 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5193 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5194 | printk("md: %s: data overlaps metadata\n", |
| 5195 | mdname(mddev)); |
| 5196 | return -EINVAL; |
| 5197 | } |
| 5198 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5199 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5200 | > rdev->data_offset) { |
| 5201 | printk("md: %s: metadata overlaps data\n", |
| 5202 | mdname(mddev)); |
| 5203 | return -EINVAL; |
| 5204 | } |
| 5205 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5206 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | } |
| 5208 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5209 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 5210 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5212 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5213 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5214 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5216 | if (mddev->level != LEVEL_NONE) |
| 5217 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5218 | mddev->level); |
| 5219 | else |
| 5220 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5221 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5222 | return -EINVAL; |
| 5223 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5224 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5225 | if (mddev->level != pers->level) { |
| 5226 | mddev->level = pers->level; |
| 5227 | mddev->new_level = pers->level; |
| 5228 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5229 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5231 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5232 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5233 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5234 | module_put(pers->owner); |
| 5235 | return -EINVAL; |
| 5236 | } |
| 5237 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5238 | if (pers->sync_request) { |
| 5239 | /* Warn if this is a potentially silly |
| 5240 | * configuration. |
| 5241 | */ |
| 5242 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5243 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5244 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5245 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5246 | rdev_for_each(rdev, mddev) |
| 5247 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5248 | if (rdev < rdev2 && |
| 5249 | rdev->bdev->bd_contains == |
| 5250 | rdev2->bdev->bd_contains) { |
| 5251 | printk(KERN_WARNING |
| 5252 | "%s: WARNING: %s appears to be" |
| 5253 | " on the same physical disk as" |
| 5254 | " %s.\n", |
| 5255 | mdname(mddev), |
| 5256 | bdevname(rdev->bdev,b), |
| 5257 | bdevname(rdev2->bdev,b2)); |
| 5258 | warned = 1; |
| 5259 | } |
| 5260 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5261 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5262 | if (warned) |
| 5263 | printk(KERN_WARNING |
| 5264 | "True protection against single-disk" |
| 5265 | " failure might be compromised.\n"); |
| 5266 | } |
| 5267 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5268 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5269 | /* may be over-ridden by personality */ |
| 5270 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5271 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5272 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5274 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5275 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5276 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5277 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5278 | if (err) |
| 5279 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5280 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5281 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5282 | " but 'external_size' not in effect?\n", __func__); |
| 5283 | printk(KERN_ERR |
| 5284 | "md: invalid array_size %llu > default size %llu\n", |
| 5285 | (unsigned long long)mddev->array_sectors / 2, |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5286 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5287 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5288 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5289 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5290 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5291 | struct bitmap *bitmap; |
| 5292 | |
| 5293 | bitmap = bitmap_create(mddev, -1); |
| 5294 | if (IS_ERR(bitmap)) { |
| 5295 | err = PTR_ERR(bitmap); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5296 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5297 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5298 | } else |
| 5299 | mddev->bitmap = bitmap; |
| 5300 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | if (err) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5303 | mddev_detach(mddev); |
NeilBrown | 0c35bd4 | 2015-03-13 11:51:18 +1100 | [diff] [blame] | 5304 | if (mddev->private) |
| 5305 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5306 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5307 | module_put(pers->owner); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5308 | bitmap_destroy(mddev); |
| 5309 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5310 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5311 | if (mddev->queue) { |
Shaohua Li | bb086a8 | 2016-09-30 09:45:40 -0700 | [diff] [blame] | 5312 | bool nonrot = true; |
| 5313 | |
| 5314 | rdev_for_each(rdev, mddev) { |
| 5315 | if (rdev->raid_disk >= 0 && |
| 5316 | !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) { |
| 5317 | nonrot = false; |
| 5318 | break; |
| 5319 | } |
| 5320 | } |
| 5321 | if (mddev->degraded) |
| 5322 | nonrot = false; |
| 5323 | if (nonrot) |
| 5324 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mddev->queue); |
| 5325 | else |
| 5326 | queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, mddev->queue); |
Jan Kara | 1cdb7f9 | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 5327 | mddev->queue->backing_dev_info->congested_data = mddev; |
| 5328 | mddev->queue->backing_dev_info->congested_fn = md_congested; |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5329 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5330 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5331 | if (mddev->kobj.sd && |
| 5332 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5333 | printk(KERN_WARNING |
| 5334 | "md: cannot register extra attributes for %s\n", |
| 5335 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5336 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5337 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5338 | mddev->ro = 0; |
| 5339 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5340 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5341 | atomic_set(&mddev->max_corr_read_errors, |
| 5342 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5343 | mddev->safemode = 0; |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5344 | if (mddev_is_clustered(mddev)) |
| 5345 | mddev->safemode_delay = 0; |
| 5346 | else |
| 5347 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5349 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5350 | spin_lock(&mddev->lock); |
| 5351 | mddev->pers = pers; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5352 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5353 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5354 | if (rdev->raid_disk >= 0) |
| 5355 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5356 | /* failure here is OK */; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5357 | |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 5358 | if (mddev->degraded && !mddev->ro) |
| 5359 | /* This ensures that recovering status is reported immediately |
| 5360 | * via sysfs - until a lack of spares is confirmed. |
| 5361 | */ |
| 5362 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5363 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5364 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5365 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5366 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5367 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5368 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5369 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5370 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5371 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5372 | return 0; |
| 5373 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5374 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5375 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5376 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5377 | { |
| 5378 | int err; |
| 5379 | |
| 5380 | err = md_run(mddev); |
| 5381 | if (err) |
| 5382 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5383 | err = bitmap_load(mddev); |
| 5384 | if (err) { |
| 5385 | bitmap_destroy(mddev); |
| 5386 | goto out; |
| 5387 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5388 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5389 | if (mddev_is_clustered(mddev)) |
| 5390 | md_allow_write(mddev); |
| 5391 | |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5392 | md_wakeup_thread(mddev->thread); |
| 5393 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5394 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5395 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5396 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5397 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5398 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5399 | out: |
| 5400 | return err; |
| 5401 | } |
| 5402 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5403 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | { |
| 5405 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5406 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5407 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5409 | return -ENXIO; |
| 5410 | if (!mddev->pers) |
| 5411 | return -EINVAL; |
| 5412 | if (!mddev->ro) |
| 5413 | return -EBUSY; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 5414 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 5415 | struct md_rdev *rdev; |
| 5416 | bool has_journal = false; |
| 5417 | |
| 5418 | rcu_read_lock(); |
| 5419 | rdev_for_each_rcu(rdev, mddev) { |
| 5420 | if (test_bit(Journal, &rdev->flags) && |
| 5421 | !test_bit(Faulty, &rdev->flags)) { |
| 5422 | has_journal = true; |
| 5423 | break; |
| 5424 | } |
| 5425 | } |
| 5426 | rcu_read_unlock(); |
| 5427 | |
| 5428 | /* Don't restart rw with journal missing/faulty */ |
| 5429 | if (!has_journal) |
| 5430 | return -EINVAL; |
| 5431 | } |
| 5432 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5433 | mddev->safemode = 0; |
| 5434 | mddev->ro = 0; |
| 5435 | set_disk_ro(disk, 0); |
| 5436 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5437 | mdname(mddev)); |
| 5438 | /* Kick recovery or resync if necessary */ |
| 5439 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5440 | md_wakeup_thread(mddev->thread); |
| 5441 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5442 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5443 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | } |
| 5445 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5446 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5447 | { |
| 5448 | mddev->array_sectors = 0; |
| 5449 | mddev->external_size = 0; |
| 5450 | mddev->dev_sectors = 0; |
| 5451 | mddev->raid_disks = 0; |
| 5452 | mddev->recovery_cp = 0; |
| 5453 | mddev->resync_min = 0; |
| 5454 | mddev->resync_max = MaxSector; |
| 5455 | mddev->reshape_position = MaxSector; |
| 5456 | mddev->external = 0; |
| 5457 | mddev->persistent = 0; |
| 5458 | mddev->level = LEVEL_NONE; |
| 5459 | mddev->clevel[0] = 0; |
| 5460 | mddev->flags = 0; |
| 5461 | mddev->ro = 0; |
| 5462 | mddev->metadata_type[0] = 0; |
| 5463 | mddev->chunk_sectors = 0; |
| 5464 | mddev->ctime = mddev->utime = 0; |
| 5465 | mddev->layout = 0; |
| 5466 | mddev->max_disks = 0; |
| 5467 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5468 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5469 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5470 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5471 | mddev->new_level = LEVEL_NONE; |
| 5472 | mddev->new_layout = 0; |
| 5473 | mddev->new_chunk_sectors = 0; |
| 5474 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5475 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5476 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5477 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5478 | mddev->recovery = 0; |
| 5479 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5480 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5481 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5482 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5483 | mddev->private = NULL; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 5484 | mddev->cluster_info = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5485 | mddev->bitmap_info.offset = 0; |
| 5486 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5487 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5488 | mddev->bitmap_info.chunksize = 0; |
| 5489 | mddev->bitmap_info.daemon_sleep = 0; |
| 5490 | mddev->bitmap_info.max_write_behind = 0; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 5491 | mddev->bitmap_info.nodes = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5492 | } |
| 5493 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5494 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5495 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 5496 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5497 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5498 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5499 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5500 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | del_timer_sync(&mddev->safemode_timer); |
| 5504 | |
| 5505 | bitmap_flush(mddev); |
| 5506 | md_super_wait(mddev); |
| 5507 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5508 | if (mddev->ro == 0 && |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5509 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
| 5510 | (mddev->flags & MD_UPDATE_SB_FLAGS))) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5511 | /* mark array as shutdown cleanly */ |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5512 | if (!mddev_is_clustered(mddev)) |
| 5513 | mddev->in_sync = 1; |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5514 | md_update_sb(mddev, 1); |
| 5515 | } |
| 5516 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5517 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5518 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5519 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 5520 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5521 | __md_stop_writes(mddev); |
| 5522 | mddev_unlock(mddev); |
| 5523 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5524 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5525 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5526 | static void mddev_detach(struct mddev *mddev) |
| 5527 | { |
| 5528 | struct bitmap *bitmap = mddev->bitmap; |
| 5529 | /* wait for behind writes to complete */ |
| 5530 | if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { |
| 5531 | printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n", |
| 5532 | mdname(mddev)); |
| 5533 | /* need to kick something here to make sure I/O goes? */ |
| 5534 | wait_event(bitmap->behind_wait, |
| 5535 | atomic_read(&bitmap->behind_writes) == 0); |
| 5536 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5537 | if (mddev->pers && mddev->pers->quiesce) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5538 | mddev->pers->quiesce(mddev, 1); |
| 5539 | mddev->pers->quiesce(mddev, 0); |
| 5540 | } |
| 5541 | md_unregister_thread(&mddev->thread); |
| 5542 | if (mddev->queue) |
| 5543 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 5544 | } |
| 5545 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5546 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5547 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5548 | struct md_personality *pers = mddev->pers; |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5549 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 5550 | /* Ensure ->event_work is done */ |
| 5551 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5552 | spin_lock(&mddev->lock); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5553 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5554 | spin_unlock(&mddev->lock); |
| 5555 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5556 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5557 | if (pers->sync_request && mddev->to_remove == NULL) |
| 5558 | mddev->to_remove = &md_redundancy_group; |
| 5559 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5560 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5561 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5562 | |
| 5563 | void md_stop(struct mddev *mddev) |
| 5564 | { |
| 5565 | /* stop the array and free an attached data structures. |
| 5566 | * This is called from dm-raid |
| 5567 | */ |
| 5568 | __md_stop(mddev); |
| 5569 | bitmap_destroy(mddev); |
| 5570 | if (mddev->bio_set) |
| 5571 | bioset_free(mddev->bio_set); |
| 5572 | } |
| 5573 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5574 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5575 | |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5576 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5577 | { |
| 5578 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5579 | int did_freeze = 0; |
| 5580 | |
| 5581 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5582 | did_freeze = 1; |
| 5583 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5584 | md_wakeup_thread(mddev->thread); |
| 5585 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5586 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5587 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5588 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5589 | /* Thread might be blocked waiting for metadata update |
| 5590 | * which will now never happen */ |
| 5591 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5592 | |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5593 | if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
| 5594 | return -EBUSY; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5595 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5596 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 5597 | &mddev->recovery)); |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5598 | wait_event(mddev->sb_wait, |
| 5599 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5600 | mddev_lock_nointr(mddev); |
| 5601 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5602 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5603 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5604 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 5605 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5606 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5607 | if (did_freeze) { |
| 5608 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5609 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5610 | md_wakeup_thread(mddev->thread); |
| 5611 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5612 | err = -EBUSY; |
| 5613 | goto out; |
| 5614 | } |
| 5615 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5616 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5617 | |
| 5618 | err = -ENXIO; |
| 5619 | if (mddev->ro==1) |
| 5620 | goto out; |
| 5621 | mddev->ro = 1; |
| 5622 | set_disk_ro(mddev->gendisk, 1); |
| 5623 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5624 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5625 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5626 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5627 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5628 | } |
| 5629 | out: |
| 5630 | mutex_unlock(&mddev->open_mutex); |
| 5631 | return err; |
| 5632 | } |
| 5633 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5634 | /* mode: |
| 5635 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5636 | * 2 - stop but do not disassemble array |
| 5637 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5638 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5639 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5640 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5641 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5642 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5643 | int did_freeze = 0; |
| 5644 | |
| 5645 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5646 | did_freeze = 1; |
| 5647 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5648 | md_wakeup_thread(mddev->thread); |
| 5649 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5650 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5651 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5652 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5653 | /* Thread might be blocked waiting for metadata update |
| 5654 | * which will now never happen */ |
| 5655 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5656 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5657 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5658 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 5659 | !test_bit(MD_RECOVERY_RUNNING, |
| 5660 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5661 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5662 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5663 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5664 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5665 | mddev->sysfs_active || |
| 5666 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 5667 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5668 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5669 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5670 | if (did_freeze) { |
| 5671 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5672 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5673 | md_wakeup_thread(mddev->thread); |
| 5674 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 5675 | return -EBUSY; |
| 5676 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5677 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5678 | if (mddev->ro) |
| 5679 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5680 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5681 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5682 | __md_stop(mddev); |
Jan Kara | 1cdb7f9 | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 5683 | mddev->queue->backing_dev_info->congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5684 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5685 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5686 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5687 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5688 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5689 | if (rdev->raid_disk >= 0) |
| 5690 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5691 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5692 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5693 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5694 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5695 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5696 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5697 | if (mddev->ro) |
| 5698 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5699 | } else |
| 5700 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | /* |
| 5702 | * Free resources if final stop |
| 5703 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5704 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5706 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5707 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5708 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5709 | struct file *f = mddev->bitmap_info.file; |
| 5710 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5711 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5712 | spin_unlock(&mddev->lock); |
| 5713 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5714 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5715 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5717 | export_array(mddev); |
| 5718 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5719 | md_clean(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5720 | if (mddev->hold_active == UNTIL_STOP) |
| 5721 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5722 | } |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5723 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5724 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5725 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5726 | } |
| 5727 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5728 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5729 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5731 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5732 | int err; |
| 5733 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5734 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | |
| 5737 | printk(KERN_INFO "md: running: "); |
| 5738 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5739 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | char b[BDEVNAME_SIZE]; |
| 5741 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5742 | } |
| 5743 | printk("\n"); |
| 5744 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5745 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | if (err) { |
| 5747 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5748 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5749 | } |
| 5750 | } |
| 5751 | |
| 5752 | /* |
| 5753 | * lets try to run arrays based on all disks that have arrived |
| 5754 | * until now. (those are in pending_raid_disks) |
| 5755 | * |
| 5756 | * the method: pick the first pending disk, collect all disks with |
| 5757 | * the same UUID, remove all from the pending list and put them into |
| 5758 | * the 'same_array' list. Then order this list based on superblock |
| 5759 | * update time (freshest comes first), kick out 'old' disks and |
| 5760 | * compare superblocks. If everything's fine then run it. |
| 5761 | * |
| 5762 | * If "unit" is allocated, then bump its reference count |
| 5763 | */ |
| 5764 | static void autorun_devices(int part) |
| 5765 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5766 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5767 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5768 | char b[BDEVNAME_SIZE]; |
| 5769 | |
| 5770 | printk(KERN_INFO "md: autorun ...\n"); |
| 5771 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5772 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5773 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5774 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5775 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5776 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5777 | |
| 5778 | printk(KERN_INFO "md: considering %s ...\n", |
| 5779 | bdevname(rdev0->bdev,b)); |
| 5780 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5781 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5782 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5783 | printk(KERN_INFO "md: adding %s ...\n", |
| 5784 | bdevname(rdev->bdev,b)); |
| 5785 | list_move(&rdev->same_set, &candidates); |
| 5786 | } |
| 5787 | /* |
| 5788 | * now we have a set of devices, with all of them having |
| 5789 | * mostly sane superblocks. It's time to allocate the |
| 5790 | * mddev. |
| 5791 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5792 | if (part) { |
| 5793 | dev = MKDEV(mdp_major, |
| 5794 | rdev0->preferred_minor << MdpMinorShift); |
| 5795 | unit = MINOR(dev) >> MdpMinorShift; |
| 5796 | } else { |
| 5797 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5798 | unit = MINOR(dev); |
| 5799 | } |
| 5800 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5801 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5802 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5803 | break; |
| 5804 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | |
| 5806 | md_probe(dev, NULL, NULL); |
| 5807 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5808 | if (!mddev || !mddev->gendisk) { |
| 5809 | if (mddev) |
| 5810 | mddev_put(mddev); |
| 5811 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5812 | "md: cannot allocate memory for md drive.\n"); |
| 5813 | break; |
| 5814 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5815 | if (mddev_lock(mddev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5816 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5817 | mdname(mddev)); |
| 5818 | else if (mddev->raid_disks || mddev->major_version |
| 5819 | || !list_empty(&mddev->disks)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5820 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | "md: %s already running, cannot run %s\n", |
| 5822 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5823 | mddev_unlock(mddev); |
| 5824 | } else { |
| 5825 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5826 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5827 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | list_del_init(&rdev->same_set); |
| 5829 | if (bind_rdev_to_array(rdev, mddev)) |
| 5830 | export_rdev(rdev); |
| 5831 | } |
| 5832 | autorun_array(mddev); |
| 5833 | mddev_unlock(mddev); |
| 5834 | } |
| 5835 | /* on success, candidates will be empty, on error |
| 5836 | * it won't... |
| 5837 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5838 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5839 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5840 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5841 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5842 | mddev_put(mddev); |
| 5843 | } |
| 5844 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5845 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5846 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5847 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5848 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | { |
| 5850 | mdu_version_t ver; |
| 5851 | |
| 5852 | ver.major = MD_MAJOR_VERSION; |
| 5853 | ver.minor = MD_MINOR_VERSION; |
| 5854 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5855 | |
| 5856 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5857 | return -EFAULT; |
| 5858 | |
| 5859 | return 0; |
| 5860 | } |
| 5861 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5862 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | { |
| 5864 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5865 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5866 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5868 | nr = working = insync = failed = spare = 0; |
| 5869 | rcu_read_lock(); |
| 5870 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5871 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5872 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | failed++; |
| 5874 | else { |
| 5875 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5876 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5877 | insync++; |
Song Liu | b347af8 | 2016-08-11 17:14:45 -0700 | [diff] [blame] | 5878 | else if (test_bit(Journal, &rdev->flags)) |
| 5879 | /* TODO: add journal count to md_u.h */ |
| 5880 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5881 | else |
| 5882 | spare++; |
| 5883 | } |
| 5884 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5885 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5886 | |
| 5887 | info.major_version = mddev->major_version; |
| 5888 | info.minor_version = mddev->minor_version; |
| 5889 | info.patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 5890 | info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5891 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5892 | info.size = mddev->dev_sectors / 2; |
| 5893 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5894 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | info.nr_disks = nr; |
| 5896 | info.raid_disks = mddev->raid_disks; |
| 5897 | info.md_minor = mddev->md_minor; |
| 5898 | info.not_persistent= !mddev->persistent; |
| 5899 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 5900 | info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5901 | info.state = 0; |
| 5902 | if (mddev->in_sync) |
| 5903 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5904 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 5905 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 5906 | if (mddev_is_clustered(mddev)) |
| 5907 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5908 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | info.working_disks = working; |
| 5910 | info.failed_disks = failed; |
| 5911 | info.spare_disks = spare; |
| 5912 | |
| 5913 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5914 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5915 | |
| 5916 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5917 | return -EFAULT; |
| 5918 | |
| 5919 | return 0; |
| 5920 | } |
| 5921 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5922 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5923 | { |
| 5924 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5925 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5926 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5927 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5928 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5929 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5930 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5931 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5932 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5933 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5934 | /* bitmap enabled */ |
| 5935 | if (mddev->bitmap_info.file) { |
| 5936 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 5937 | sizeof(file->pathname)); |
| 5938 | if (IS_ERR(ptr)) |
| 5939 | err = PTR_ERR(ptr); |
| 5940 | else |
| 5941 | memmove(file->pathname, ptr, |
| 5942 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 5943 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5944 | spin_unlock(&mddev->lock); |
| 5945 | |
| 5946 | if (err == 0 && |
| 5947 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5948 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5949 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5950 | kfree(file); |
| 5951 | return err; |
| 5952 | } |
| 5953 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5954 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | { |
| 5956 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5957 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5958 | |
| 5959 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5960 | return -EFAULT; |
| 5961 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5962 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 5963 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5964 | if (rdev) { |
| 5965 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5966 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5967 | info.raid_disk = rdev->raid_disk; |
| 5968 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5969 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5970 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5971 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5973 | info.state |= (1<<MD_DISK_SYNC); |
| 5974 | } |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 5975 | if (test_bit(Journal, &rdev->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 5976 | info.state |= (1<<MD_DISK_JOURNAL); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5977 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5978 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5979 | } else { |
| 5980 | info.major = info.minor = 0; |
| 5981 | info.raid_disk = -1; |
| 5982 | info.state = (1<<MD_DISK_REMOVED); |
| 5983 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5984 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5985 | |
| 5986 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5987 | return -EFAULT; |
| 5988 | |
| 5989 | return 0; |
| 5990 | } |
| 5991 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5992 | 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] | 5993 | { |
| 5994 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5995 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5996 | dev_t dev = MKDEV(info->major,info->minor); |
| 5997 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5998 | if (mddev_is_clustered(mddev) && |
| 5999 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 6000 | pr_err("%s: Cannot add to clustered mddev.\n", |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6001 | mdname(mddev)); |
| 6002 | return -EINVAL; |
| 6003 | } |
| 6004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6005 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 6006 | return -EOVERFLOW; |
| 6007 | |
| 6008 | if (!mddev->raid_disks) { |
| 6009 | int err; |
| 6010 | /* expecting a device which has a superblock */ |
| 6011 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 6012 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6013 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | "md: md_import_device returned %ld\n", |
| 6015 | PTR_ERR(rdev)); |
| 6016 | return PTR_ERR(rdev); |
| 6017 | } |
| 6018 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6019 | struct md_rdev *rdev0 |
| 6020 | = list_entry(mddev->disks.next, |
| 6021 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6022 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6023 | .load_super(rdev, rdev0, mddev->minor_version); |
| 6024 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6025 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6026 | "md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6027 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | bdevname(rdev0->bdev,b2)); |
| 6029 | export_rdev(rdev); |
| 6030 | return -EINVAL; |
| 6031 | } |
| 6032 | } |
| 6033 | err = bind_rdev_to_array(rdev, mddev); |
| 6034 | if (err) |
| 6035 | export_rdev(rdev); |
| 6036 | return err; |
| 6037 | } |
| 6038 | |
| 6039 | /* |
| 6040 | * add_new_disk can be used once the array is assembled |
| 6041 | * to add "hot spares". They must already have a superblock |
| 6042 | * written |
| 6043 | */ |
| 6044 | if (mddev->pers) { |
| 6045 | int err; |
| 6046 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6047 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6048 | "%s: personality does not support diskops!\n", |
| 6049 | mdname(mddev)); |
| 6050 | return -EINVAL; |
| 6051 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 6052 | if (mddev->persistent) |
| 6053 | rdev = md_import_device(dev, mddev->major_version, |
| 6054 | mddev->minor_version); |
| 6055 | else |
| 6056 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6058 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | "md: md_import_device returned %ld\n", |
| 6060 | PTR_ERR(rdev)); |
| 6061 | return PTR_ERR(rdev); |
| 6062 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 6063 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6064 | if (!mddev->persistent) { |
| 6065 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6066 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6067 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6068 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 6069 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6070 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6071 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 6072 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6073 | } else |
| 6074 | super_types[mddev->major_version]. |
| 6075 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6076 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 6077 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6078 | /* This was a hot-add request, but events doesn't |
| 6079 | * match, so reject it. |
| 6080 | */ |
| 6081 | export_rdev(rdev); |
| 6082 | return -EINVAL; |
| 6083 | } |
| 6084 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6085 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6086 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6087 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6088 | else |
| 6089 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6090 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6091 | if (info->state & (1<<MD_DISK_JOURNAL)) { |
| 6092 | struct md_rdev *rdev2; |
| 6093 | bool has_journal = false; |
| 6094 | |
| 6095 | /* make sure no existing journal disk */ |
| 6096 | rdev_for_each(rdev2, mddev) { |
| 6097 | if (test_bit(Journal, &rdev2->flags)) { |
| 6098 | has_journal = true; |
| 6099 | break; |
| 6100 | } |
| 6101 | } |
| 6102 | if (has_journal) { |
| 6103 | export_rdev(rdev); |
| 6104 | return -EBUSY; |
| 6105 | } |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6106 | set_bit(Journal, &rdev->flags); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6107 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6108 | /* |
| 6109 | * check whether the device shows up in other nodes |
| 6110 | */ |
| 6111 | if (mddev_is_clustered(mddev)) { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6112 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6113 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6114 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6115 | /* --add initiated by this node */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6116 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6117 | if (err) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6118 | export_rdev(rdev); |
| 6119 | return err; |
| 6120 | } |
| 6121 | } |
| 6122 | } |
| 6123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6124 | rdev->raid_disk = -1; |
| 6125 | err = bind_rdev_to_array(rdev, mddev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6127 | if (err) |
| 6128 | export_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6129 | |
| 6130 | if (mddev_is_clustered(mddev)) { |
Guoqing Jiang | e566aef | 2016-08-12 13:42:34 +0800 | [diff] [blame] | 6131 | if (info->state & (1 << MD_DISK_CANDIDATE)) { |
| 6132 | if (!err) { |
| 6133 | err = md_cluster_ops->new_disk_ack(mddev, |
| 6134 | err == 0); |
| 6135 | if (err) |
| 6136 | md_kick_rdev_from_array(rdev); |
| 6137 | } |
| 6138 | } else { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6139 | if (err) |
| 6140 | md_cluster_ops->add_new_disk_cancel(mddev); |
| 6141 | else |
| 6142 | err = add_bound_rdev(rdev); |
| 6143 | } |
| 6144 | |
| 6145 | } else if (!err) |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 6146 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6148 | return err; |
| 6149 | } |
| 6150 | |
| 6151 | /* otherwise, add_new_disk is only allowed |
| 6152 | * for major_version==0 superblocks |
| 6153 | */ |
| 6154 | if (mddev->major_version != 0) { |
| 6155 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 6156 | mdname(mddev)); |
| 6157 | return -EINVAL; |
| 6158 | } |
| 6159 | |
| 6160 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 6161 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6162 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6164 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | "md: error, md_import_device() returned %ld\n", |
| 6166 | PTR_ERR(rdev)); |
| 6167 | return PTR_ERR(rdev); |
| 6168 | } |
| 6169 | rdev->desc_nr = info->number; |
| 6170 | if (info->raid_disk < mddev->raid_disks) |
| 6171 | rdev->raid_disk = info->raid_disk; |
| 6172 | else |
| 6173 | rdev->raid_disk = -1; |
| 6174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6175 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6176 | if (info->state & (1<<MD_DISK_SYNC)) |
| 6177 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6178 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6179 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6180 | set_bit(WriteMostly, &rdev->flags); |
| 6181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6182 | if (!mddev->persistent) { |
| 6183 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6184 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 6185 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6186 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6187 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6188 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6189 | err = bind_rdev_to_array(rdev, mddev); |
| 6190 | if (err) { |
| 6191 | export_rdev(rdev); |
| 6192 | return err; |
| 6193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6194 | } |
| 6195 | |
| 6196 | return 0; |
| 6197 | } |
| 6198 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6199 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6200 | { |
| 6201 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6202 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6203 | |
Yufen Yu | e51f4fc | 2018-05-04 18:08:10 +0800 | [diff] [blame] | 6204 | if (!mddev->pers) |
| 6205 | return -ENODEV; |
| 6206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6207 | rdev = find_rdev(mddev, dev); |
| 6208 | if (!rdev) |
| 6209 | return -ENXIO; |
| 6210 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6211 | if (rdev->raid_disk < 0) |
| 6212 | goto kick_rdev; |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6213 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6214 | clear_bit(Blocked, &rdev->flags); |
| 6215 | remove_and_add_spares(mddev, rdev); |
| 6216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6217 | if (rdev->raid_disk >= 0) |
| 6218 | goto busy; |
| 6219 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6220 | kick_rdev: |
Goldwyn Rodrigues | 54a8839 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6221 | if (mddev_is_clustered(mddev)) |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6222 | md_cluster_ops->remove_disk(mddev, rdev); |
| 6223 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6224 | md_kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6225 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6226 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6227 | |
| 6228 | return 0; |
| 6229 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 6230 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6231 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6232 | return -EBUSY; |
| 6233 | } |
| 6234 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6235 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6236 | { |
| 6237 | char b[BDEVNAME_SIZE]; |
| 6238 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6239 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6240 | |
| 6241 | if (!mddev->pers) |
| 6242 | return -ENODEV; |
| 6243 | |
| 6244 | if (mddev->major_version != 0) { |
| 6245 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 6246 | " version-0 superblocks.\n", |
| 6247 | mdname(mddev)); |
| 6248 | return -EINVAL; |
| 6249 | } |
| 6250 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6251 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6252 | "%s: personality does not support diskops!\n", |
| 6253 | mdname(mddev)); |
| 6254 | return -EINVAL; |
| 6255 | } |
| 6256 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6257 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6258 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6259 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6260 | "md: error, md_import_device() returned %ld\n", |
| 6261 | PTR_ERR(rdev)); |
| 6262 | return -EINVAL; |
| 6263 | } |
| 6264 | |
| 6265 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6266 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6267 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6268 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6269 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6270 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6271 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6272 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6273 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6274 | "md: can not hot-add faulty %s disk to %s!\n", |
| 6275 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6276 | err = -EINVAL; |
| 6277 | goto abort_export; |
| 6278 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6279 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6280 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6281 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 6282 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6283 | err = bind_rdev_to_array(rdev, mddev); |
| 6284 | if (err) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 6285 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6286 | |
| 6287 | /* |
| 6288 | * The rest should better be atomic, we can have disk failures |
| 6289 | * noticed in interrupt contexts ... |
| 6290 | */ |
| 6291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6292 | rdev->raid_disk = -1; |
| 6293 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6294 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6295 | /* |
| 6296 | * Kick recovery, maybe this spare has to be added to the |
| 6297 | * array immediately. |
| 6298 | */ |
| 6299 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6300 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6301 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6302 | return 0; |
| 6303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6304 | abort_export: |
| 6305 | export_rdev(rdev); |
| 6306 | return err; |
| 6307 | } |
| 6308 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6309 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6310 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6311 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6312 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6313 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 6314 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6315 | return -EBUSY; |
| 6316 | if (mddev->recovery || mddev->sync_thread) |
| 6317 | return -EBUSY; |
| 6318 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6319 | } |
| 6320 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6321 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6322 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6323 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6324 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6325 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 6326 | return -EEXIST; /* cannot add when bitmap is present */ |
| 6327 | f = fget(fd); |
| 6328 | |
| 6329 | if (f == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6330 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 6331 | mdname(mddev)); |
| 6332 | return -EBADF; |
| 6333 | } |
| 6334 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6335 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6336 | if (!S_ISREG(inode->i_mode)) { |
| 6337 | printk(KERN_ERR "%s: error: bitmap file must be a regular file\n", |
| 6338 | mdname(mddev)); |
| 6339 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6340 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6341 | printk(KERN_ERR "%s: error: bitmap file must open for write\n", |
| 6342 | mdname(mddev)); |
| 6343 | err = -EBADF; |
| 6344 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6345 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 6346 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6347 | err = -EBUSY; |
| 6348 | } |
| 6349 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6350 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6351 | return err; |
| 6352 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6353 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6354 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6355 | } else if (mddev->bitmap == NULL) |
| 6356 | return -ENOENT; /* cannot remove what isn't there */ |
| 6357 | err = 0; |
| 6358 | if (mddev->pers) { |
| 6359 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6360 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6361 | struct bitmap *bitmap; |
| 6362 | |
| 6363 | bitmap = bitmap_create(mddev, -1); |
| 6364 | if (!IS_ERR(bitmap)) { |
| 6365 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6366 | err = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6367 | } else |
| 6368 | err = PTR_ERR(bitmap); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6369 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6370 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6371 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6372 | fd = -1; /* make sure to put the file */ |
| 6373 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6374 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6375 | } |
| 6376 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6377 | struct file *f = mddev->bitmap_info.file; |
| 6378 | if (f) { |
| 6379 | spin_lock(&mddev->lock); |
| 6380 | mddev->bitmap_info.file = NULL; |
| 6381 | spin_unlock(&mddev->lock); |
| 6382 | fput(f); |
| 6383 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6384 | } |
| 6385 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6386 | return err; |
| 6387 | } |
| 6388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6389 | /* |
| 6390 | * set_array_info is used two different ways |
| 6391 | * The original usage is when creating a new array. |
| 6392 | * In this usage, raid_disks is > 0 and it together with |
| 6393 | * level, size, not_persistent,layout,chunksize determine the |
| 6394 | * shape of the array. |
| 6395 | * This will always create an array with a type-0.90.0 superblock. |
| 6396 | * The newer usage is when assembling an array. |
| 6397 | * In this case raid_disks will be 0, and the major_version field is |
| 6398 | * use to determine which style super-blocks are to be found on the devices. |
| 6399 | * The minor and patch _version numbers are also kept incase the |
| 6400 | * super_block handler wishes to interpret them. |
| 6401 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6402 | 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] | 6403 | { |
| 6404 | |
| 6405 | if (info->raid_disks == 0) { |
| 6406 | /* just setting version number for superblock loading */ |
| 6407 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6408 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6409 | super_types[info->major_version].name == NULL) { |
| 6410 | /* maybe try to auto-load a module? */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6411 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6412 | "md: superblock version %d not known\n", |
| 6413 | info->major_version); |
| 6414 | return -EINVAL; |
| 6415 | } |
| 6416 | mddev->major_version = info->major_version; |
| 6417 | mddev->minor_version = info->minor_version; |
| 6418 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6419 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6420 | /* ensure mddev_put doesn't delete this now that there |
| 6421 | * is some minimal configuration. |
| 6422 | */ |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6423 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6424 | return 0; |
| 6425 | } |
| 6426 | mddev->major_version = MD_MAJOR_VERSION; |
| 6427 | mddev->minor_version = MD_MINOR_VERSION; |
| 6428 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6429 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6430 | |
| 6431 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6432 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6433 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | mddev->raid_disks = info->raid_disks; |
| 6435 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6436 | * openned |
| 6437 | */ |
| 6438 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6439 | mddev->recovery_cp = MaxSector; |
| 6440 | else |
| 6441 | mddev->recovery_cp = 0; |
| 6442 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6443 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6444 | |
| 6445 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6446 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6447 | |
| 6448 | mddev->max_disks = MD_SB_DISKS; |
| 6449 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6450 | if (mddev->persistent) |
| 6451 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6452 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6454 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6455 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6456 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6457 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6458 | mddev->reshape_position = MaxSector; |
| 6459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6460 | /* |
| 6461 | * Generate a 128 bit UUID |
| 6462 | */ |
| 6463 | get_random_bytes(mddev->uuid, 16); |
| 6464 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6465 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6466 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6467 | mddev->new_layout = mddev->layout; |
| 6468 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6469 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6471 | return 0; |
| 6472 | } |
| 6473 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6474 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6475 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6476 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6477 | |
| 6478 | if (mddev->external_size) |
| 6479 | return; |
| 6480 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6481 | mddev->array_sectors = array_sectors; |
| 6482 | } |
| 6483 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6484 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6485 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6486 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6487 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6488 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6489 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6490 | |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 6491 | /* cluster raid doesn't support update size */ |
| 6492 | if (mddev_is_clustered(mddev)) |
| 6493 | return -EINVAL; |
| 6494 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6495 | if (mddev->pers->resize == NULL) |
| 6496 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6497 | /* The "num_sectors" is the number of sectors of each device that |
| 6498 | * is used. This can only make sense for arrays with redundancy. |
| 6499 | * linear and raid0 always use whatever space is available. We can only |
| 6500 | * consider changing this number if no resync or reconstruction is |
| 6501 | * 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] | 6502 | * 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] | 6503 | * of each device. If num_sectors is zero, we find the largest size |
| 6504 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6505 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6506 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6507 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6508 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6509 | if (mddev->ro) |
| 6510 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6511 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6512 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6513 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6514 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6515 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6516 | num_sectors = avail; |
| 6517 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6518 | return -ENOSPC; |
| 6519 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6520 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6521 | if (!rv) |
| 6522 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6523 | return rv; |
| 6524 | } |
| 6525 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6526 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6527 | { |
| 6528 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6529 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6530 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6531 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6532 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6533 | if (mddev->ro) |
| 6534 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6535 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6536 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6537 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6538 | if (mddev->sync_thread || |
| 6539 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6540 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6541 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6542 | |
| 6543 | rdev_for_each(rdev, mddev) { |
| 6544 | if (mddev->raid_disks < raid_disks && |
| 6545 | rdev->data_offset < rdev->new_data_offset) |
| 6546 | return -EINVAL; |
| 6547 | if (mddev->raid_disks > raid_disks && |
| 6548 | rdev->data_offset > rdev->new_data_offset) |
| 6549 | return -EINVAL; |
| 6550 | } |
| 6551 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6552 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6553 | if (mddev->delta_disks < 0) |
| 6554 | mddev->reshape_backwards = 1; |
| 6555 | else if (mddev->delta_disks > 0) |
| 6556 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6557 | |
| 6558 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6559 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6560 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6561 | mddev->reshape_backwards = 0; |
| 6562 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6563 | return rv; |
| 6564 | } |
| 6565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6566 | /* |
| 6567 | * update_array_info is used to change the configuration of an |
| 6568 | * on-line array. |
| 6569 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6570 | * fields in the info are checked against the array. |
| 6571 | * Any differences that cannot be handled will cause an error. |
| 6572 | * Normally, only one change can be managed at a time. |
| 6573 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6574 | 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] | 6575 | { |
| 6576 | int rv = 0; |
| 6577 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6578 | int state = 0; |
| 6579 | |
| 6580 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6581 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6582 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6583 | |
| 6584 | if (mddev->major_version != info->major_version || |
| 6585 | mddev->minor_version != info->minor_version || |
| 6586 | /* mddev->patch_version != info->patch_version || */ |
| 6587 | mddev->ctime != info->ctime || |
| 6588 | mddev->level != info->level || |
| 6589 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 6590 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6591 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6592 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6593 | ((state^info->state) & 0xfffffe00) |
| 6594 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6595 | return -EINVAL; |
| 6596 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6597 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6598 | cnt++; |
| 6599 | if (mddev->raid_disks != info->raid_disks) |
| 6600 | cnt++; |
| 6601 | if (mddev->layout != info->layout) |
| 6602 | cnt++; |
| 6603 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6604 | cnt++; |
| 6605 | if (cnt == 0) |
| 6606 | return 0; |
| 6607 | if (cnt > 1) |
| 6608 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6609 | |
| 6610 | if (mddev->layout != info->layout) { |
| 6611 | /* Change layout |
| 6612 | * we don't need to do anything at the md level, the |
| 6613 | * personality will take care of it all. |
| 6614 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6615 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6616 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6617 | else { |
| 6618 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6619 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6620 | if (rv) |
| 6621 | mddev->new_layout = mddev->layout; |
| 6622 | return rv; |
| 6623 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6624 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6625 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6626 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6627 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6628 | if (mddev->raid_disks != info->raid_disks) |
| 6629 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6630 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6631 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6632 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 6633 | rv = -EINVAL; |
| 6634 | goto err; |
| 6635 | } |
| 6636 | if (mddev->recovery || mddev->sync_thread) { |
| 6637 | rv = -EBUSY; |
| 6638 | goto err; |
| 6639 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6640 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6641 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6642 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6643 | if (mddev->bitmap) { |
| 6644 | rv = -EEXIST; |
| 6645 | goto err; |
| 6646 | } |
| 6647 | if (mddev->bitmap_info.default_offset == 0) { |
| 6648 | rv = -EINVAL; |
| 6649 | goto err; |
| 6650 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6651 | mddev->bitmap_info.offset = |
| 6652 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6653 | mddev->bitmap_info.space = |
| 6654 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6655 | mddev->pers->quiesce(mddev, 1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6656 | bitmap = bitmap_create(mddev, -1); |
| 6657 | if (!IS_ERR(bitmap)) { |
| 6658 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6659 | rv = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6660 | } else |
| 6661 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6662 | if (rv) |
| 6663 | bitmap_destroy(mddev); |
| 6664 | mddev->pers->quiesce(mddev, 0); |
| 6665 | } else { |
| 6666 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6667 | if (!mddev->bitmap) { |
| 6668 | rv = -ENOENT; |
| 6669 | goto err; |
| 6670 | } |
| 6671 | if (mddev->bitmap->storage.file) { |
| 6672 | rv = -EINVAL; |
| 6673 | goto err; |
| 6674 | } |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6675 | if (mddev->bitmap_info.nodes) { |
| 6676 | /* hold PW on all the bitmap lock */ |
| 6677 | if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) { |
| 6678 | printk("md: can't change bitmap to none since the" |
| 6679 | " array is in use by more than one node\n"); |
| 6680 | rv = -EPERM; |
| 6681 | md_cluster_ops->unlock_all_bitmaps(mddev); |
| 6682 | goto err; |
| 6683 | } |
| 6684 | |
| 6685 | mddev->bitmap_info.nodes = 0; |
| 6686 | md_cluster_ops->leave(mddev); |
| 6687 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6688 | mddev->pers->quiesce(mddev, 1); |
| 6689 | bitmap_destroy(mddev); |
| 6690 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6691 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6692 | } |
| 6693 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6694 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6695 | return rv; |
| 6696 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6697 | return rv; |
| 6698 | } |
| 6699 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6700 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6701 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6702 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6703 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6704 | |
| 6705 | if (mddev->pers == NULL) |
| 6706 | return -ENODEV; |
| 6707 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6708 | rcu_read_lock(); |
| 6709 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6710 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6711 | err = -ENODEV; |
| 6712 | else { |
| 6713 | md_error(mddev, rdev); |
| 6714 | if (!test_bit(Faulty, &rdev->flags)) |
| 6715 | err = -EBUSY; |
| 6716 | } |
| 6717 | rcu_read_unlock(); |
| 6718 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6719 | } |
| 6720 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6721 | /* |
| 6722 | * We have a problem here : there is no easy way to give a CHS |
| 6723 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6724 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6725 | * dosfs just mad... ;-) |
| 6726 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6727 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6728 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6729 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6730 | |
| 6731 | geo->heads = 2; |
| 6732 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6733 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6734 | return 0; |
| 6735 | } |
| 6736 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6737 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 6738 | { |
| 6739 | switch (cmd) { |
| 6740 | case ADD_NEW_DISK: |
| 6741 | case BLKROSET: |
| 6742 | case GET_ARRAY_INFO: |
| 6743 | case GET_BITMAP_FILE: |
| 6744 | case GET_DISK_INFO: |
| 6745 | case HOT_ADD_DISK: |
| 6746 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6747 | case RAID_AUTORUN: |
| 6748 | case RAID_VERSION: |
| 6749 | case RESTART_ARRAY_RW: |
| 6750 | case RUN_ARRAY: |
| 6751 | case SET_ARRAY_INFO: |
| 6752 | case SET_BITMAP_FILE: |
| 6753 | case SET_DISK_FAULTY: |
| 6754 | case STOP_ARRAY: |
| 6755 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6756 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6757 | return true; |
| 6758 | default: |
| 6759 | return false; |
| 6760 | } |
| 6761 | } |
| 6762 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6763 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6764 | unsigned int cmd, unsigned long arg) |
| 6765 | { |
| 6766 | int err = 0; |
| 6767 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6768 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6769 | int ro; |
NeilBrown | 7e78978 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 6770 | bool did_set_md_closing = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6771 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6772 | if (!md_ioctl_valid(cmd)) |
| 6773 | return -ENOTTY; |
| 6774 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6775 | switch (cmd) { |
| 6776 | case RAID_VERSION: |
| 6777 | case GET_ARRAY_INFO: |
| 6778 | case GET_DISK_INFO: |
| 6779 | break; |
| 6780 | default: |
| 6781 | if (!capable(CAP_SYS_ADMIN)) |
| 6782 | return -EACCES; |
| 6783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6784 | |
| 6785 | /* |
| 6786 | * Commands dealing with the RAID driver but not any |
| 6787 | * particular array: |
| 6788 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6789 | switch (cmd) { |
| 6790 | case RAID_VERSION: |
| 6791 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6792 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6794 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6795 | case RAID_AUTORUN: |
| 6796 | err = 0; |
| 6797 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6798 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6799 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6800 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6801 | } |
| 6802 | |
| 6803 | /* |
| 6804 | * Commands creating/starting a new array: |
| 6805 | */ |
| 6806 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6807 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6808 | |
| 6809 | if (!mddev) { |
| 6810 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6811 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6812 | } |
| 6813 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6814 | /* Some actions do not requires the mutex */ |
| 6815 | switch (cmd) { |
| 6816 | case GET_ARRAY_INFO: |
| 6817 | if (!mddev->raid_disks && !mddev->external) |
| 6818 | err = -ENODEV; |
| 6819 | else |
| 6820 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6821 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6822 | |
| 6823 | case GET_DISK_INFO: |
| 6824 | if (!mddev->raid_disks && !mddev->external) |
| 6825 | err = -ENODEV; |
| 6826 | else |
| 6827 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6828 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6829 | |
| 6830 | case SET_DISK_FAULTY: |
| 6831 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6832 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6833 | |
| 6834 | case GET_BITMAP_FILE: |
| 6835 | err = get_bitmap_file(mddev, argp); |
| 6836 | goto out; |
| 6837 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6838 | } |
| 6839 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 6840 | if (cmd == ADD_NEW_DISK) |
| 6841 | /* need to ensure md_delayed_delete() has completed */ |
| 6842 | flush_workqueue(md_misc_wq); |
| 6843 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 6844 | if (cmd == HOT_REMOVE_DISK) |
| 6845 | /* need to ensure recovery thread has run */ |
| 6846 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 6847 | !test_bit(MD_RECOVERY_NEEDED, |
Shaohua Li | 60a931c | 2016-12-08 15:48:18 -0800 | [diff] [blame] | 6848 | &mddev->recovery), |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 6849 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6850 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 6851 | /* Need to flush page cache, and ensure no-one else opens |
| 6852 | * and writes |
| 6853 | */ |
| 6854 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6855 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6856 | mutex_unlock(&mddev->open_mutex); |
| 6857 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6858 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6859 | } |
NeilBrown | 7e78978 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 6860 | WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags)); |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 6861 | set_bit(MD_CLOSING, &mddev->flags); |
NeilBrown | 7e78978 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 6862 | did_set_md_closing = true; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6863 | mutex_unlock(&mddev->open_mutex); |
| 6864 | sync_blockdev(bdev); |
| 6865 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6866 | err = mddev_lock(mddev); |
| 6867 | if (err) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6868 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6869 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6870 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6871 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6872 | } |
| 6873 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6874 | if (cmd == SET_ARRAY_INFO) { |
| 6875 | mdu_array_info_t info; |
| 6876 | if (!arg) |
| 6877 | memset(&info, 0, sizeof(info)); |
| 6878 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6879 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6880 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6881 | } |
| 6882 | if (mddev->pers) { |
| 6883 | err = update_array_info(mddev, &info); |
| 6884 | if (err) { |
| 6885 | printk(KERN_WARNING "md: couldn't update" |
| 6886 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6887 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6888 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6889 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6890 | } |
| 6891 | if (!list_empty(&mddev->disks)) { |
| 6892 | printk(KERN_WARNING |
| 6893 | "md: array %s already has disks!\n", |
| 6894 | mdname(mddev)); |
| 6895 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6896 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6897 | } |
| 6898 | if (mddev->raid_disks) { |
| 6899 | printk(KERN_WARNING |
| 6900 | "md: array %s already initialised!\n", |
| 6901 | mdname(mddev)); |
| 6902 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6903 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6904 | } |
| 6905 | err = set_array_info(mddev, &info); |
| 6906 | if (err) { |
| 6907 | printk(KERN_WARNING "md: couldn't set" |
| 6908 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6909 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6910 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6911 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6912 | } |
| 6913 | |
| 6914 | /* |
| 6915 | * Commands querying/configuring an existing array: |
| 6916 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6917 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6918 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6919 | if ((!mddev->raid_disks && !mddev->external) |
| 6920 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6921 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6922 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6923 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6924 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6925 | } |
| 6926 | |
| 6927 | /* |
| 6928 | * Commands even a read-only array can execute: |
| 6929 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6930 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6931 | case RESTART_ARRAY_RW: |
| 6932 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6933 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6934 | |
| 6935 | case STOP_ARRAY: |
| 6936 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6937 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6938 | |
| 6939 | case STOP_ARRAY_RO: |
| 6940 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6941 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6942 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6943 | case HOT_REMOVE_DISK: |
| 6944 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6945 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6946 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6947 | case ADD_NEW_DISK: |
| 6948 | /* We can support ADD_NEW_DISK on read-only arrays |
Wei Fang | 466ad29 | 2016-03-21 19:19:30 +0800 | [diff] [blame] | 6949 | * only if we are re-adding a preexisting device. |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6950 | * So require mddev->pers and MD_DISK_SYNC. |
| 6951 | */ |
| 6952 | if (mddev->pers) { |
| 6953 | mdu_disk_info_t info; |
| 6954 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6955 | err = -EFAULT; |
| 6956 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 6957 | /* Need to clear read-only for this */ |
| 6958 | break; |
| 6959 | else |
| 6960 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6961 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6962 | } |
| 6963 | break; |
| 6964 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6965 | case BLKROSET: |
| 6966 | if (get_user(ro, (int __user *)(arg))) { |
| 6967 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6968 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6969 | } |
| 6970 | err = -EINVAL; |
| 6971 | |
| 6972 | /* if the bdev is going readonly the value of mddev->ro |
| 6973 | * does not matter, no writes are coming |
| 6974 | */ |
| 6975 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6976 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6977 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6978 | /* are we are already prepared for writes? */ |
| 6979 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6980 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6981 | |
| 6982 | /* transitioning to readauto need only happen for |
| 6983 | * arrays that call md_write_start |
| 6984 | */ |
| 6985 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6986 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6987 | if (err == 0) { |
| 6988 | mddev->ro = 2; |
| 6989 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6990 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6991 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6992 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6993 | } |
| 6994 | |
| 6995 | /* |
| 6996 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6997 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6998 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 6999 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7000 | if (mddev->ro == 2) { |
| 7001 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7002 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7003 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7004 | /* mddev_unlock will wake thread */ |
| 7005 | /* If a device failed while we were read-only, we |
| 7006 | * need to make sure the metadata is updated now. |
| 7007 | */ |
| 7008 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
| 7009 | mddev_unlock(mddev); |
| 7010 | wait_event(mddev->sb_wait, |
| 7011 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && |
| 7012 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 7013 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7014 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7015 | } else { |
| 7016 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7017 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7018 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7019 | } |
| 7020 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7021 | switch (cmd) { |
| 7022 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7023 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7024 | mdu_disk_info_t info; |
| 7025 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7026 | err = -EFAULT; |
| 7027 | else |
| 7028 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7029 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7030 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7031 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 7032 | case CLUSTERED_DISK_NACK: |
| 7033 | if (mddev_is_clustered(mddev)) |
| 7034 | md_cluster_ops->new_disk_ack(mddev, false); |
| 7035 | else |
| 7036 | err = -EINVAL; |
| 7037 | goto unlock; |
| 7038 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7039 | case HOT_ADD_DISK: |
| 7040 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7041 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7042 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7043 | case RUN_ARRAY: |
| 7044 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7045 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7046 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7047 | case SET_BITMAP_FILE: |
| 7048 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7049 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7050 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7051 | default: |
| 7052 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7053 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7054 | } |
| 7055 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7056 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7057 | if (mddev->hold_active == UNTIL_IOCTL && |
| 7058 | err != -EINVAL) |
| 7059 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7060 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7061 | out: |
NeilBrown | 7e78978 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7062 | if(did_set_md_closing) |
| 7063 | clear_bit(MD_CLOSING, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7064 | return err; |
| 7065 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7066 | #ifdef CONFIG_COMPAT |
| 7067 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 7068 | unsigned int cmd, unsigned long arg) |
| 7069 | { |
| 7070 | switch (cmd) { |
| 7071 | case HOT_REMOVE_DISK: |
| 7072 | case HOT_ADD_DISK: |
| 7073 | case SET_DISK_FAULTY: |
| 7074 | case SET_BITMAP_FILE: |
| 7075 | /* These take in integer arg, do not convert */ |
| 7076 | break; |
| 7077 | default: |
| 7078 | arg = (unsigned long)compat_ptr(arg); |
| 7079 | break; |
| 7080 | } |
| 7081 | |
| 7082 | return md_ioctl(bdev, mode, cmd, arg); |
| 7083 | } |
| 7084 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7085 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7086 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7087 | { |
| 7088 | /* |
| 7089 | * Succeed if we can lock the mddev, which confirms that |
| 7090 | * it isn't being stopped right now. |
| 7091 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7092 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7093 | int err; |
| 7094 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 7095 | if (!mddev) |
| 7096 | return -ENODEV; |
| 7097 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7098 | if (mddev->gendisk != bdev->bd_disk) { |
| 7099 | /* we are racing with mddev_put which is discarding this |
| 7100 | * bd_disk. |
| 7101 | */ |
| 7102 | mddev_put(mddev); |
| 7103 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7104 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7105 | /* Then retry the open from the top */ |
| 7106 | return -ERESTARTSYS; |
| 7107 | } |
| 7108 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 7109 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7110 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7111 | goto out; |
| 7112 | |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7113 | if (test_bit(MD_CLOSING, &mddev->flags)) { |
| 7114 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 5cc85ef | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7115 | err = -ENODEV; |
| 7116 | goto out; |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7117 | } |
| 7118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7119 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7120 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7121 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7122 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7123 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7124 | out: |
NeilBrown | 5cc85ef | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7125 | if (err) |
| 7126 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | return err; |
| 7128 | } |
| 7129 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 7130 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7131 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7132 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7133 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 7134 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7135 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7136 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7137 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7138 | |
| 7139 | static int md_media_changed(struct gendisk *disk) |
| 7140 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7141 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7142 | |
| 7143 | return mddev->changed; |
| 7144 | } |
| 7145 | |
| 7146 | static int md_revalidate(struct gendisk *disk) |
| 7147 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7148 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7149 | |
| 7150 | mddev->changed = 0; |
| 7151 | return 0; |
| 7152 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 7153 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7154 | { |
| 7155 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7156 | .open = md_open, |
| 7157 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 7158 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7159 | #ifdef CONFIG_COMPAT |
| 7160 | .compat_ioctl = md_compat_ioctl, |
| 7161 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7162 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7163 | .media_changed = md_media_changed, |
| 7164 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7165 | }; |
| 7166 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7167 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7168 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7169 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7171 | /* |
| 7172 | * md_thread is a 'system-thread', it's priority should be very |
| 7173 | * high. We avoid resource deadlocks individually in each |
| 7174 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 7175 | * the very same RT priority as kswapd, thus we will never get |
| 7176 | * into a priority inversion deadlock. |
| 7177 | * |
| 7178 | * we definitely have to have equal or higher priority than |
| 7179 | * bdflush, otherwise bdflush will deadlock if there are too |
| 7180 | * many dirty RAID5 blocks. |
| 7181 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7182 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7183 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7184 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7185 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7186 | /* We need to wait INTERRUPTIBLE so that |
| 7187 | * we don't add to the load-average. |
| 7188 | * That means we need to be sure no signals are |
| 7189 | * pending |
| 7190 | */ |
| 7191 | if (signal_pending(current)) |
| 7192 | flush_signals(current); |
| 7193 | |
| 7194 | wait_event_interruptible_timeout |
| 7195 | (thread->wqueue, |
| 7196 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 7197 | || kthread_should_stop(), |
| 7198 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7199 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7200 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 7201 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7202 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7203 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7205 | return 0; |
| 7206 | } |
| 7207 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7208 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7209 | { |
| 7210 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7211 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7212 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 7213 | wake_up(&thread->wqueue); |
| 7214 | } |
| 7215 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7216 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7217 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7218 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7219 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7220 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7221 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7222 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7223 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7224 | if (!thread) |
| 7225 | return NULL; |
| 7226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7227 | init_waitqueue_head(&thread->wqueue); |
| 7228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7229 | thread->run = run; |
| 7230 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7231 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7232 | thread->tsk = kthread_run(md_thread, thread, |
| 7233 | "%s_%s", |
| 7234 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7235 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7236 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7237 | kfree(thread); |
| 7238 | return NULL; |
| 7239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7240 | return thread; |
| 7241 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7242 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7243 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7244 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7245 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7246 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7247 | if (!thread) |
| 7248 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7249 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7250 | /* Locking ensures that mddev_unlock does not wake_up a |
| 7251 | * non-existent thread |
| 7252 | */ |
| 7253 | spin_lock(&pers_lock); |
| 7254 | *threadp = NULL; |
| 7255 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7256 | |
| 7257 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7258 | kfree(thread); |
| 7259 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7260 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7261 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7262 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7263 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7264 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7265 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7266 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7267 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7268 | return; |
| 7269 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7270 | if (mddev->degraded) |
| 7271 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7272 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7273 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7274 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7275 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 7276 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7277 | queue_work(md_misc_wq, &mddev->event_work); |
Guoqing Jiang | bb9ef71 | 2015-12-28 10:46:38 +0800 | [diff] [blame] | 7278 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7279 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7280 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7281 | |
| 7282 | /* seq_file implementation /proc/mdstat */ |
| 7283 | |
| 7284 | static void status_unused(struct seq_file *seq) |
| 7285 | { |
| 7286 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7287 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7288 | |
| 7289 | seq_printf(seq, "unused devices: "); |
| 7290 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7291 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7292 | char b[BDEVNAME_SIZE]; |
| 7293 | i++; |
| 7294 | seq_printf(seq, "%s ", |
| 7295 | bdevname(rdev->bdev,b)); |
| 7296 | } |
| 7297 | if (!i) |
| 7298 | seq_printf(seq, "<none>"); |
| 7299 | |
| 7300 | seq_printf(seq, "\n"); |
| 7301 | } |
| 7302 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7303 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7304 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7305 | sector_t max_sectors, resync, res; |
Mariusz Tkaczyk | 4f73470 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 7306 | unsigned long dt, db = 0; |
| 7307 | sector_t rt, curr_mark_cnt, resync_mark_cnt; |
| 7308 | int scale, recovery_active; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7309 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7310 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7311 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 7312 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7313 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7314 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7315 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7316 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7317 | resync = mddev->curr_resync; |
| 7318 | if (resync <= 3) { |
| 7319 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7320 | /* Still cleaning up */ |
| 7321 | resync = max_sectors; |
| 7322 | } else |
| 7323 | resync -= atomic_read(&mddev->recovery_active); |
| 7324 | |
| 7325 | if (resync == 0) { |
| 7326 | if (mddev->recovery_cp < MaxSector) { |
| 7327 | seq_printf(seq, "\tresync=PENDING"); |
| 7328 | return 1; |
| 7329 | } |
| 7330 | return 0; |
| 7331 | } |
| 7332 | if (resync < 3) { |
| 7333 | seq_printf(seq, "\tresync=DELAYED"); |
| 7334 | return 1; |
| 7335 | } |
| 7336 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 7337 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7338 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7339 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7340 | * u32, as those are the requirements for sector_div. |
| 7341 | * Thus 'scale' must be at least 10 |
| 7342 | */ |
| 7343 | scale = 10; |
| 7344 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7345 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7346 | scale++; |
| 7347 | } |
| 7348 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7349 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7350 | |
| 7351 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7352 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7353 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7354 | seq_printf(seq, "["); |
| 7355 | for (i = 0; i < x; i++) |
| 7356 | seq_printf(seq, "="); |
| 7357 | seq_printf(seq, ">"); |
| 7358 | for (i = 0; i < y; i++) |
| 7359 | seq_printf(seq, "."); |
| 7360 | seq_printf(seq, "] "); |
| 7361 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7362 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7363 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 7364 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7365 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 7366 | "check" : |
| 7367 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 7368 | "resync" : "recovery"))), |
| 7369 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7370 | (unsigned long long) resync/2, |
| 7371 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7372 | |
| 7373 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7374 | * dt: time from mark until now |
| 7375 | * db: blocks written from mark until now |
| 7376 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7377 | * |
Mariusz Tkaczyk | 4f73470 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 7378 | * rt is a sector_t, which is always 64bit now. We are keeping |
| 7379 | * the original algorithm, but it is not really necessary. |
| 7380 | * |
| 7381 | * Original algorithm: |
| 7382 | * So we divide before multiply in case it is 32bit and close |
| 7383 | * to the limit. |
| 7384 | * We scale the divisor (db) by 32 to avoid losing precision |
| 7385 | * near the end of resync when the number of remaining sectors |
| 7386 | * is close to 'db'. |
| 7387 | * We then divide rt by 32 after multiplying by db to compensate. |
| 7388 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7389 | */ |
| 7390 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 7391 | if (!dt) dt++; |
Mariusz Tkaczyk | 4f73470 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 7392 | |
| 7393 | curr_mark_cnt = mddev->curr_mark_cnt; |
| 7394 | recovery_active = atomic_read(&mddev->recovery_active); |
| 7395 | resync_mark_cnt = mddev->resync_mark_cnt; |
| 7396 | |
| 7397 | if (curr_mark_cnt >= (recovery_active + resync_mark_cnt)) |
| 7398 | db = curr_mark_cnt - (recovery_active + resync_mark_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7399 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7400 | rt = max_sectors - resync; /* number of remaining sectors */ |
Mariusz Tkaczyk | 4f73470 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 7401 | rt = div64_u64(rt, db/32+1); |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7402 | rt *= dt; |
| 7403 | rt >>= 5; |
| 7404 | |
| 7405 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 7406 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7407 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7408 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7409 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7410 | } |
| 7411 | |
| 7412 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 7413 | { |
| 7414 | struct list_head *tmp; |
| 7415 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7416 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7417 | |
| 7418 | if (l >= 0x10000) |
| 7419 | return NULL; |
| 7420 | if (!l--) |
| 7421 | /* header */ |
| 7422 | return (void*)1; |
| 7423 | |
| 7424 | spin_lock(&all_mddevs_lock); |
| 7425 | list_for_each(tmp,&all_mddevs) |
| 7426 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7427 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7428 | mddev_get(mddev); |
| 7429 | spin_unlock(&all_mddevs_lock); |
| 7430 | return mddev; |
| 7431 | } |
| 7432 | spin_unlock(&all_mddevs_lock); |
| 7433 | if (!l--) |
| 7434 | return (void*)2;/* tail */ |
| 7435 | return NULL; |
| 7436 | } |
| 7437 | |
| 7438 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 7439 | { |
| 7440 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7441 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7443 | ++*pos; |
| 7444 | if (v == (void*)2) |
| 7445 | return NULL; |
| 7446 | |
| 7447 | spin_lock(&all_mddevs_lock); |
| 7448 | if (v == (void*)1) |
| 7449 | tmp = all_mddevs.next; |
| 7450 | else |
| 7451 | tmp = mddev->all_mddevs.next; |
| 7452 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7453 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7454 | else { |
| 7455 | next_mddev = (void*)2; |
| 7456 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7457 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7458 | spin_unlock(&all_mddevs_lock); |
| 7459 | |
| 7460 | if (v != (void*)1) |
| 7461 | mddev_put(mddev); |
| 7462 | return next_mddev; |
| 7463 | |
| 7464 | } |
| 7465 | |
| 7466 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 7467 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7468 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7469 | |
| 7470 | if (mddev && v != (void*)1 && v != (void*)2) |
| 7471 | mddev_put(mddev); |
| 7472 | } |
| 7473 | |
| 7474 | static int md_seq_show(struct seq_file *seq, void *v) |
| 7475 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7476 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7477 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7478 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7479 | |
| 7480 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7481 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7482 | seq_printf(seq, "Personalities : "); |
| 7483 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7484 | list_for_each_entry(pers, &pers_list, list) |
| 7485 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7486 | |
| 7487 | spin_unlock(&pers_lock); |
| 7488 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7489 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7490 | return 0; |
| 7491 | } |
| 7492 | if (v == (void*)2) { |
| 7493 | status_unused(seq); |
| 7494 | return 0; |
| 7495 | } |
| 7496 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7497 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7498 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7499 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7500 | mddev->pers ? "" : "in"); |
| 7501 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7502 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7503 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7504 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7505 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7506 | seq_printf(seq, " %s", mddev->pers->name); |
| 7507 | } |
| 7508 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7509 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7510 | rcu_read_lock(); |
| 7511 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7512 | char b[BDEVNAME_SIZE]; |
| 7513 | seq_printf(seq, " %s[%d]", |
| 7514 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7515 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7516 | seq_printf(seq, "(W)"); |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 7517 | if (test_bit(Journal, &rdev->flags)) |
| 7518 | seq_printf(seq, "(J)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7519 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7520 | seq_printf(seq, "(F)"); |
| 7521 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7522 | } |
| 7523 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7524 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7525 | if (test_bit(Replacement, &rdev->flags)) |
| 7526 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7527 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7528 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7529 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7530 | |
| 7531 | if (!list_empty(&mddev->disks)) { |
| 7532 | if (mddev->pers) |
| 7533 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7534 | (unsigned long long) |
| 7535 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7536 | else |
| 7537 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7538 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7539 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7540 | if (mddev->persistent) { |
| 7541 | if (mddev->major_version != 0 || |
| 7542 | mddev->minor_version != 90) { |
| 7543 | seq_printf(seq," super %d.%d", |
| 7544 | mddev->major_version, |
| 7545 | mddev->minor_version); |
| 7546 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7547 | } else if (mddev->external) |
| 7548 | seq_printf(seq, " super external:%s", |
| 7549 | mddev->metadata_type); |
| 7550 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7551 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7552 | |
| 7553 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7554 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7555 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7556 | if (mddev->pers->sync_request) { |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7557 | if (status_resync(seq, mddev)) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7558 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7559 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7560 | } else |
| 7561 | seq_printf(seq, "\n "); |
| 7562 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7563 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7564 | |
| 7565 | seq_printf(seq, "\n"); |
| 7566 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7567 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7569 | return 0; |
| 7570 | } |
| 7571 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7572 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7573 | .start = md_seq_start, |
| 7574 | .next = md_seq_next, |
| 7575 | .stop = md_seq_stop, |
| 7576 | .show = md_seq_show, |
| 7577 | }; |
| 7578 | |
| 7579 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7580 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7581 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7582 | int error; |
| 7583 | |
| 7584 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7585 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7586 | return error; |
| 7587 | |
| 7588 | seq = file->private_data; |
| 7589 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7590 | return error; |
| 7591 | } |
| 7592 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7593 | static int md_unloading; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7594 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7595 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7596 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7597 | int mask; |
| 7598 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7599 | if (md_unloading) |
kbuild test robot | 7d7e64f | 2014-12-03 16:07:59 +1100 | [diff] [blame] | 7600 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7601 | poll_wait(filp, &md_event_waiters, wait); |
| 7602 | |
| 7603 | /* always allow read */ |
| 7604 | mask = POLLIN | POLLRDNORM; |
| 7605 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7606 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7607 | mask |= POLLERR | POLLPRI; |
| 7608 | return mask; |
| 7609 | } |
| 7610 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7611 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7612 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7613 | .open = md_seq_open, |
| 7614 | .read = seq_read, |
| 7615 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7616 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7617 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7618 | }; |
| 7619 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7620 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7621 | { |
Chao Yu | 50bd377 | 2014-09-25 15:28:34 +0800 | [diff] [blame] | 7622 | printk(KERN_INFO "md: %s personality registered for level %d\n", |
| 7623 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7624 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7625 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7626 | spin_unlock(&pers_lock); |
| 7627 | return 0; |
| 7628 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7629 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7630 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7631 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7632 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7633 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7634 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7635 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7636 | spin_unlock(&pers_lock); |
| 7637 | return 0; |
| 7638 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7639 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7640 | |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7641 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
| 7642 | struct module *module) |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7643 | { |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7644 | int ret = 0; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7645 | spin_lock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7646 | if (md_cluster_ops != NULL) |
| 7647 | ret = -EALREADY; |
| 7648 | else { |
| 7649 | md_cluster_ops = ops; |
| 7650 | md_cluster_mod = module; |
| 7651 | } |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7652 | spin_unlock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7653 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7654 | } |
| 7655 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 7656 | |
| 7657 | int unregister_md_cluster_operations(void) |
| 7658 | { |
| 7659 | spin_lock(&pers_lock); |
| 7660 | md_cluster_ops = NULL; |
| 7661 | spin_unlock(&pers_lock); |
| 7662 | return 0; |
| 7663 | } |
| 7664 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 7665 | |
| 7666 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 7667 | { |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 7668 | if (!md_cluster_ops) |
| 7669 | request_module("md-cluster"); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7670 | spin_lock(&pers_lock); |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 7671 | /* ensure module won't be unloaded */ |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7672 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 7673 | pr_err("can't find md-cluster module or get it's reference.\n"); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7674 | spin_unlock(&pers_lock); |
| 7675 | return -ENOENT; |
| 7676 | } |
| 7677 | spin_unlock(&pers_lock); |
| 7678 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 7679 | return md_cluster_ops->join(mddev, nodes); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7680 | } |
| 7681 | |
| 7682 | void md_cluster_stop(struct mddev *mddev) |
| 7683 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 7684 | if (!md_cluster_ops) |
| 7685 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7686 | md_cluster_ops->leave(mddev); |
| 7687 | module_put(md_cluster_mod); |
| 7688 | } |
| 7689 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7690 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7691 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7692 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7693 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7694 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7695 | |
| 7696 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7697 | rcu_read_lock(); |
| 7698 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7699 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7700 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7701 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7702 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7703 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7704 | * as sync_io is counted when a request starts, and |
| 7705 | * disk_stats is counted when it completes. |
| 7706 | * So resync activity will cause curr_events to be smaller than |
| 7707 | * when there was no such activity. |
| 7708 | * non-sync IO will cause disk_stat to increase without |
| 7709 | * increasing sync_io so curr_events will (eventually) |
| 7710 | * be larger than it was before. Once it becomes |
| 7711 | * substantially larger, the test below will cause |
| 7712 | * the array to appear non-idle, and resync will slow |
| 7713 | * down. |
| 7714 | * If there is a lot of outstanding resync activity when |
| 7715 | * we set last_event to curr_events, then all that activity |
| 7716 | * completing might cause the array to appear non-idle |
| 7717 | * and resync will be slowed down even though there might |
| 7718 | * not have been non-resync activity. This will only |
| 7719 | * happen once though. 'last_events' will soon reflect |
| 7720 | * the state where there is little or no outstanding |
| 7721 | * resync requests, and further resync activity will |
| 7722 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7723 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7724 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7725 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7726 | rdev->last_events = curr_events; |
| 7727 | idle = 0; |
| 7728 | } |
| 7729 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7730 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7731 | return idle; |
| 7732 | } |
| 7733 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7734 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7735 | { |
| 7736 | /* another "blocks" (512byte) blocks have been synced */ |
| 7737 | atomic_sub(blocks, &mddev->recovery_active); |
| 7738 | wake_up(&mddev->recovery_wait); |
| 7739 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7740 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7741 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7742 | md_wakeup_thread(mddev->thread); |
| 7743 | // stop recovery, signal do_sync .... |
| 7744 | } |
| 7745 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7746 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7747 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7748 | /* md_write_start(mddev, bi) |
| 7749 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7750 | * in superblock) before writing, schedule a superblock update |
| 7751 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7752 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7753 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7754 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7755 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7756 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7757 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7758 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7759 | BUG_ON(mddev->ro == 1); |
| 7760 | if (mddev->ro == 2) { |
| 7761 | /* need to switch to read/write */ |
| 7762 | mddev->ro = 0; |
| 7763 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7764 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7765 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7766 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7767 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7768 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7769 | if (mddev->safemode == 1) |
| 7770 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7771 | if (mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7772 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7773 | if (mddev->in_sync) { |
| 7774 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7775 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7776 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7777 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7778 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7779 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7780 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7781 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7782 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7783 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7784 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7785 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7786 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7787 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7788 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7789 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7790 | { |
| 7791 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7792 | if (mddev->safemode == 2) |
| 7793 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7794 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7795 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7796 | } |
| 7797 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7798 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7799 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7800 | /* md_allow_write(mddev) |
| 7801 | * Calling this ensures that the array is marked 'active' so that writes |
| 7802 | * may proceed without blocking. It is important to call this before |
| 7803 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7804 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7805 | * |
Guoqing Jiang | abf3508 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 7806 | * In the ->external case MD_CHANGE_PENDING can not be cleared until mddev->lock |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7807 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7808 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7809 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7810 | { |
| 7811 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7812 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7813 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7814 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7815 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7816 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7817 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7818 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7819 | if (mddev->in_sync) { |
| 7820 | mddev->in_sync = 0; |
| 7821 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7822 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7823 | if (mddev->safemode_delay && |
| 7824 | mddev->safemode == 0) |
| 7825 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7826 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7827 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7828 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7829 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7830 | spin_unlock(&mddev->lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7831 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7832 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7833 | return -EAGAIN; |
| 7834 | else |
| 7835 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7836 | } |
| 7837 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7839 | #define SYNC_MARKS 10 |
| 7840 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7841 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7842 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7843 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7844 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7845 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7846 | unsigned int currspeed = 0, |
| 7847 | window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7848 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7849 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7850 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7851 | sector_t mark_cnt[SYNC_MARKS]; |
| 7852 | int last_mark,m; |
| 7853 | struct list_head *tmp; |
| 7854 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7855 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7856 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7857 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7858 | struct blk_plug plug; |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 7859 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7860 | |
| 7861 | /* just incase thread restarts... */ |
| 7862 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7863 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7864 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 7865 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7866 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7867 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7868 | |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 7869 | if (mddev_is_clustered(mddev)) { |
| 7870 | ret = md_cluster_ops->resync_start(mddev); |
| 7871 | if (ret) |
| 7872 | goto skip; |
| 7873 | |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 7874 | set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags); |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 7875 | if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 7876 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) || |
| 7877 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
| 7878 | && ((unsigned long long)mddev->curr_resync_completed |
| 7879 | < (unsigned long long)mddev->resync_max_sectors)) |
| 7880 | goto skip; |
| 7881 | } |
| 7882 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7883 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7884 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7885 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7886 | action = "check"; |
| 7887 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7888 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7889 | action = "repair"; |
| 7890 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7891 | desc = "resync"; |
| 7892 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7893 | desc = "reshape"; |
| 7894 | else |
| 7895 | desc = "recovery"; |
| 7896 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7897 | mddev->last_sync_action = action ?: desc; |
| 7898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7899 | /* we overload curr_resync somewhat here. |
| 7900 | * 0 == not engaged in resync at all |
| 7901 | * 2 == checking that there is no conflict with another sync |
| 7902 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7903 | * commense |
| 7904 | * other == active in resync - this many blocks |
| 7905 | * |
| 7906 | * Before starting a resync we must have set curr_resync to |
| 7907 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7908 | * less than ours. When we find one that is the same or higher |
| 7909 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7910 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7911 | * This will mean we have to start checking from the beginning again. |
| 7912 | * |
| 7913 | */ |
| 7914 | |
| 7915 | do { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 7916 | int mddev2_minor = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7917 | mddev->curr_resync = 2; |
| 7918 | |
| 7919 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7920 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7921 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7922 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7923 | if (mddev2 == mddev) |
| 7924 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7925 | if (!mddev->parallel_resync |
| 7926 | && mddev2->curr_resync |
| 7927 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7928 | DEFINE_WAIT(wq); |
| 7929 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7930 | /* arbitrarily yield */ |
| 7931 | mddev->curr_resync = 1; |
| 7932 | wake_up(&resync_wait); |
| 7933 | } |
| 7934 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7935 | /* no need to wait here, we can wait the next |
| 7936 | * time 'round when curr_resync == 2 |
| 7937 | */ |
| 7938 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7939 | /* We need to wait 'interruptible' so as not to |
| 7940 | * contribute to the load average, and not to |
| 7941 | * be caught by 'softlockup' |
| 7942 | */ |
| 7943 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7944 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7945 | mddev2->curr_resync >= mddev->curr_resync) { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 7946 | if (mddev2_minor != mddev2->md_minor) { |
| 7947 | mddev2_minor = mddev2->md_minor; |
| 7948 | printk(KERN_INFO "md: delaying %s of %s" |
| 7949 | " until %s has finished (they" |
| 7950 | " share one or more physical units)\n", |
| 7951 | desc, mdname(mddev), |
| 7952 | mdname(mddev2)); |
| 7953 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7954 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7955 | if (signal_pending(current)) |
| 7956 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7957 | schedule(); |
| 7958 | finish_wait(&resync_wait, &wq); |
| 7959 | goto try_again; |
| 7960 | } |
| 7961 | finish_wait(&resync_wait, &wq); |
| 7962 | } |
| 7963 | } |
| 7964 | } while (mddev->curr_resync < 2); |
| 7965 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7966 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7967 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7968 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7969 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7970 | */ |
| 7971 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 7972 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7973 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7974 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7975 | j = mddev->resync_min; |
| 7976 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7977 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7978 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7979 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7980 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7981 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7982 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7983 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7984 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7985 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7986 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7987 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 7988 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7989 | !test_bit(Faulty, &rdev->flags) && |
| 7990 | !test_bit(In_sync, &rdev->flags) && |
| 7991 | rdev->recovery_offset < j) |
| 7992 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7993 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 7994 | |
| 7995 | /* If there is a bitmap, we need to make sure all |
| 7996 | * writes that started before we added a spare |
| 7997 | * complete before we start doing a recovery. |
| 7998 | * Otherwise the write might complete and (via |
| 7999 | * bitmap_endwrite) set a bit in the bitmap after the |
| 8000 | * recovery has checked that bit and skipped that |
| 8001 | * region. |
| 8002 | */ |
| 8003 | if (mddev->bitmap) { |
| 8004 | mddev->pers->quiesce(mddev, 1); |
| 8005 | mddev->pers->quiesce(mddev, 0); |
| 8006 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8007 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8008 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8009 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 8010 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 8011 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 8012 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8013 | "(but not more than %d KB/sec) for %s.\n", |
| 8014 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8015 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8016 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8017 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8018 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8019 | for (m = 0; m < SYNC_MARKS; m++) { |
| 8020 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8021 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8022 | } |
| 8023 | last_mark = 0; |
| 8024 | mddev->resync_mark = mark[last_mark]; |
| 8025 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 8026 | |
| 8027 | /* |
| 8028 | * Tune reconstruction: |
| 8029 | */ |
| 8030 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 8031 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 8032 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8033 | |
| 8034 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8035 | last_check = 0; |
| 8036 | |
| 8037 | if (j>2) { |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8038 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8039 | "md: resuming %s of %s from checkpoint.\n", |
| 8040 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8041 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8042 | } else |
| 8043 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8044 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8045 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 8046 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8047 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8048 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8049 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8050 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8051 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8052 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8053 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8054 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8055 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8056 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 8057 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 8058 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8059 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8060 | (j - mddev->curr_resync_completed)*2 |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 8061 | >= mddev->resync_max - mddev->curr_resync_completed || |
| 8062 | mddev->curr_resync_completed > mddev->resync_max |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8063 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8064 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8065 | wait_event(mddev->recovery_wait, |
| 8066 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8067 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8068 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 8069 | j > mddev->recovery_cp) |
| 8070 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8071 | update_time = jiffies; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 8072 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8073 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8074 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8075 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8076 | while (j >= mddev->resync_max && |
| 8077 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8078 | /* As this condition is controlled by user-space, |
| 8079 | * we can block indefinitely, so use '_interruptible' |
| 8080 | * to avoid triggering warnings. |
| 8081 | */ |
| 8082 | flush_signals(current); /* just in case */ |
| 8083 | wait_event_interruptible(mddev->recovery_wait, |
| 8084 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8085 | || test_bit(MD_RECOVERY_INTR, |
| 8086 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8087 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8088 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8089 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8090 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8091 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8092 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8093 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8094 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8095 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8096 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8097 | |
| 8098 | if (!skipped) { /* actual IO requested */ |
| 8099 | io_sectors += sectors; |
| 8100 | atomic_add(sectors, &mddev->recovery_active); |
| 8101 | } |
| 8102 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8103 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8104 | break; |
| 8105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8106 | j += sectors; |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8107 | if (j > max_sectors) |
| 8108 | /* when skipping, extra large numbers can be returned. */ |
| 8109 | j = max_sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8110 | if (j > 2) |
| 8111 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8112 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8113 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8114 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8115 | * visible in /proc/mdstat |
| 8116 | */ |
| 8117 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8118 | |
| 8119 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8120 | continue; |
| 8121 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8122 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8123 | repeat: |
| 8124 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 8125 | /* step marks */ |
| 8126 | int next = (last_mark+1) % SYNC_MARKS; |
| 8127 | |
| 8128 | mddev->resync_mark = mark[next]; |
| 8129 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 8130 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8131 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8132 | last_mark = next; |
| 8133 | } |
| 8134 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8135 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8136 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8137 | |
| 8138 | /* |
| 8139 | * this loop exits only if either when we are slower than |
| 8140 | * the 'hard' speed limit, or the system was IO-idle for |
| 8141 | * a jiffy. |
| 8142 | * the system might be non-idle CPU-wise, but we only care |
| 8143 | * about not overloading the IO subsystem. (things like an |
| 8144 | * e2fsck being done on the RAID array should execute fast) |
| 8145 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8146 | cond_resched(); |
| 8147 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8148 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 8149 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8150 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8151 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 8152 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8153 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8154 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8155 | goto repeat; |
| 8156 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8157 | if (!is_mddev_idle(mddev, 0)) { |
| 8158 | /* |
| 8159 | * Give other IO more of a chance. |
| 8160 | * The faster the devices, the less we wait. |
| 8161 | */ |
| 8162 | wait_event(mddev->recovery_wait, |
| 8163 | !atomic_read(&mddev->recovery_active)); |
| 8164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8165 | } |
| 8166 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8167 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
| 8168 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 8169 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8170 | /* |
| 8171 | * this also signals 'finished resyncing' to md_stop |
| 8172 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8173 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8174 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 8175 | |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8176 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8177 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 8178 | mddev->curr_resync > 3) { |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8179 | mddev->curr_resync_completed = mddev->curr_resync; |
| 8180 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 8181 | } |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8182 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8183 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8184 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 8185 | mddev->curr_resync > 3) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8186 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 8187 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8188 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 8189 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8190 | "md: checkpointing %s of %s.\n", |
| 8191 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8192 | if (test_bit(MD_RECOVERY_ERROR, |
| 8193 | &mddev->recovery)) |
| 8194 | mddev->recovery_cp = |
| 8195 | mddev->curr_resync_completed; |
| 8196 | else |
| 8197 | mddev->recovery_cp = |
| 8198 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8199 | } |
| 8200 | } else |
| 8201 | mddev->recovery_cp = MaxSector; |
| 8202 | } else { |
| 8203 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8204 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8205 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8206 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8207 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 8208 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8209 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8210 | !test_bit(Faulty, &rdev->flags) && |
| 8211 | !test_bit(In_sync, &rdev->flags) && |
| 8212 | rdev->recovery_offset < mddev->curr_resync) |
| 8213 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8214 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8216 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 8217 | skip: |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 8218 | /* set CHANGE_PENDING here since maybe another update is needed, |
| 8219 | * so other nodes are informed. It should be harmless for normal |
| 8220 | * raid */ |
| 8221 | set_mask_bits(&mddev->flags, 0, |
| 8222 | BIT(MD_CHANGE_PENDING) | BIT(MD_CHANGE_DEVS)); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8223 | |
BingJing Chang | 547f11f | 2018-02-22 13:34:46 +0800 | [diff] [blame] | 8224 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8225 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8226 | mddev->delta_disks > 0 && |
| 8227 | mddev->pers->finish_reshape && |
| 8228 | mddev->pers->size && |
| 8229 | mddev->queue) { |
| 8230 | mddev_lock_nointr(mddev); |
| 8231 | md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0)); |
| 8232 | mddev_unlock(mddev); |
| 8233 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 8234 | revalidate_disk(mddev->gendisk); |
| 8235 | } |
| 8236 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8237 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 8238 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8239 | /* We completed so min/max setting can be forgotten if used. */ |
| 8240 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8241 | mddev->resync_min = 0; |
| 8242 | mddev->resync_max = MaxSector; |
| 8243 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8244 | mddev->resync_min = mddev->curr_resync_completed; |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8245 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8246 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8247 | spin_unlock(&mddev->lock); |
| 8248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8249 | wake_up(&resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8250 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 8251 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8252 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8253 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8254 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8255 | static int remove_and_add_spares(struct mddev *mddev, |
| 8256 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8257 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8258 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8259 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8260 | int removed = 0; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8261 | bool remove_some = false; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8262 | |
NeilBrown | eb2593f | 2018-02-03 09:19:30 +1100 | [diff] [blame] | 8263 | if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 8264 | /* Mustn't remove devices when resync thread is running */ |
| 8265 | return 0; |
| 8266 | |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8267 | rdev_for_each(rdev, mddev) { |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8268 | if ((this == NULL || rdev == this) && |
| 8269 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8270 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8271 | test_bit(Faulty, &rdev->flags) && |
| 8272 | atomic_read(&rdev->nr_pending)==0) { |
| 8273 | /* Faulty non-Blocked devices with nr_pending == 0 |
| 8274 | * never get nr_pending incremented, |
| 8275 | * never get Faulty cleared, and never get Blocked set. |
| 8276 | * So we can synchronize_rcu now rather than once per device |
| 8277 | */ |
| 8278 | remove_some = true; |
| 8279 | set_bit(RemoveSynchronized, &rdev->flags); |
| 8280 | } |
| 8281 | } |
| 8282 | |
| 8283 | if (remove_some) |
| 8284 | synchronize_rcu(); |
| 8285 | rdev_for_each(rdev, mddev) { |
| 8286 | if ((this == NULL || rdev == this) && |
| 8287 | rdev->raid_disk >= 0 && |
| 8288 | !test_bit(Blocked, &rdev->flags) && |
| 8289 | ((test_bit(RemoveSynchronized, &rdev->flags) || |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8290 | (!test_bit(In_sync, &rdev->flags) && |
| 8291 | !test_bit(Journal, &rdev->flags))) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8292 | atomic_read(&rdev->nr_pending)==0)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8293 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8294 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 8295 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | 4866848 | 2018-04-26 14:46:29 +1000 | [diff] [blame] | 8296 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8297 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8298 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8299 | } |
| 8300 | } |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 8301 | if (remove_some && test_bit(RemoveSynchronized, &rdev->flags)) |
| 8302 | clear_bit(RemoveSynchronized, &rdev->flags); |
| 8303 | } |
| 8304 | |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 8305 | if (removed && mddev->kobj.sd) |
| 8306 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8307 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8308 | if (this && removed) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8309 | goto no_add; |
| 8310 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8311 | rdev_for_each(rdev, mddev) { |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8312 | if (this && this != rdev) |
| 8313 | continue; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 8314 | if (test_bit(Candidate, &rdev->flags)) |
| 8315 | continue; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8316 | if (rdev->raid_disk >= 0 && |
| 8317 | !test_bit(In_sync, &rdev->flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8318 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8319 | !test_bit(Faulty, &rdev->flags)) |
| 8320 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8321 | if (rdev->raid_disk >= 0) |
| 8322 | continue; |
| 8323 | if (test_bit(Faulty, &rdev->flags)) |
| 8324 | continue; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8325 | if (!test_bit(Journal, &rdev->flags)) { |
| 8326 | if (mddev->ro && |
| 8327 | ! (rdev->saved_raid_disk >= 0 && |
| 8328 | !test_bit(Bitmap_sync, &rdev->flags))) |
| 8329 | continue; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8330 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8331 | rdev->recovery_offset = 0; |
| 8332 | } |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8333 | if (mddev->pers-> |
| 8334 | hot_add_disk(mddev, rdev) == 0) { |
| 8335 | if (sysfs_link_rdev(mddev, rdev)) |
| 8336 | /* failure here is OK */; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8337 | if (!test_bit(Journal, &rdev->flags)) |
| 8338 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8339 | md_new_event(mddev); |
| 8340 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8341 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8342 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8343 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 8344 | if (removed) |
| 8345 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8346 | return spares; |
| 8347 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 8348 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8349 | static void md_start_sync(struct work_struct *ws) |
| 8350 | { |
| 8351 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8352 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8353 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 8354 | mddev, |
| 8355 | "resync"); |
| 8356 | if (!mddev->sync_thread) { |
Guoqing Jiang | 207efcd | 2016-08-12 13:42:40 +0800 | [diff] [blame] | 8357 | printk(KERN_ERR "%s: could not start resync thread...\n", |
| 8358 | mdname(mddev)); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8359 | /* leave the spares where they are, it shouldn't hurt */ |
| 8360 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8361 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8362 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8363 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 8364 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8365 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8366 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8367 | &mddev->recovery)) |
| 8368 | if (mddev->sysfs_action) |
| 8369 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8370 | } else |
| 8371 | md_wakeup_thread(mddev->sync_thread); |
| 8372 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8373 | md_new_event(mddev); |
| 8374 | } |
| 8375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8376 | /* |
| 8377 | * This routine is regularly called by all per-raid-array threads to |
| 8378 | * deal with generic issues like resync and super-block update. |
| 8379 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 8380 | * need this as they never do any recovery or update the superblock. |
| 8381 | * |
| 8382 | * It does not do any resync itself, but rather "forks" off other threads |
| 8383 | * to do that as needed. |
| 8384 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 8385 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8386 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8387 | * and wakeups up this thread which will reap the thread and finish up. |
| 8388 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 8389 | * |
| 8390 | * The overall approach is: |
| 8391 | * 1/ if the superblock needs updating, update it. |
| 8392 | * 2/ If a recovery thread is running, don't do anything else. |
| 8393 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 8394 | * 4/ If there are any faulty devices, remove them. |
| 8395 | * 5/ If array is degraded, try to add spares devices |
| 8396 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 8397 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8398 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8399 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 8400 | if (mddev->suspended) |
| 8401 | return; |
| 8402 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 8403 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 8404 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8405 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8406 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8407 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8408 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 8409 | mdname(mddev)); |
| 8410 | mddev->safemode = 2; |
| 8411 | } |
| 8412 | flush_signals(current); |
| 8413 | } |
| 8414 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8415 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 8416 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8417 | if ( ! ( |
NeilBrown | 142d44c | 2013-11-28 10:34:18 +1100 | [diff] [blame] | 8418 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8419 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8420 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
Guoqing Jiang | 15858fa | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 8421 | test_bit(MD_RELOAD_SB, &mddev->flags) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8422 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8423 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 8424 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8425 | )) |
| 8426 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8427 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 8428 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8429 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8430 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8431 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 8432 | struct md_rdev *rdev; |
| 8433 | if (!mddev->external && mddev->in_sync) |
| 8434 | /* 'Blocked' flag not needed as failed devices |
| 8435 | * will be recorded if array switched to read/write. |
| 8436 | * Leaving it set will prevent the device |
| 8437 | * from being removed. |
| 8438 | */ |
| 8439 | rdev_for_each(rdev, mddev) |
| 8440 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8441 | /* On a read-only array we can: |
| 8442 | * - remove failed devices |
| 8443 | * - add already-in_sync devices if the array itself |
| 8444 | * is in-sync. |
| 8445 | * As we only add devices that are already in-sync, |
| 8446 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8447 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8448 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8449 | /* There is no thread, but we need to call |
| 8450 | * ->spare_active and clear saved_raid_disk |
| 8451 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 8452 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8453 | md_reap_sync_thread(mddev); |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 8454 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8455 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Shaohua Li | d4929ad | 2015-09-18 10:20:12 -0700 | [diff] [blame] | 8456 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8457 | goto unlock; |
| 8458 | } |
| 8459 | |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 8460 | if (mddev_is_clustered(mddev)) { |
| 8461 | struct md_rdev *rdev; |
| 8462 | /* kick the device if another node issued a |
| 8463 | * remove disk. |
| 8464 | */ |
| 8465 | rdev_for_each(rdev, mddev) { |
| 8466 | if (test_and_clear_bit(ClusterRemove, &rdev->flags) && |
| 8467 | rdev->raid_disk < 0) |
| 8468 | md_kick_rdev_from_array(rdev); |
| 8469 | } |
Guoqing Jiang | 15858fa | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 8470 | |
| 8471 | if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags)) |
| 8472 | md_reload_sb(mddev, mddev->good_device_nr); |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 8473 | } |
| 8474 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8475 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8476 | int did_change = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8477 | spin_lock(&mddev->lock); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8478 | if (mddev->safemode && |
| 8479 | !atomic_read(&mddev->writes_pending) && |
| 8480 | !mddev->in_sync && |
| 8481 | mddev->recovery_cp == MaxSector) { |
| 8482 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8483 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 8484 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8485 | } |
| 8486 | if (mddev->safemode == 1) |
| 8487 | mddev->safemode = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8488 | spin_unlock(&mddev->lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8489 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8490 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8491 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8492 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 8493 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 8494 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8496 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 8497 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 8498 | /* resync/recovery still happening */ |
| 8499 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8500 | goto unlock; |
| 8501 | } |
| 8502 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8503 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8504 | goto unlock; |
| 8505 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8506 | /* Set RUNNING before clearing NEEDED to avoid |
| 8507 | * any transients in the value of "sync_action". |
| 8508 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8509 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8510 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8511 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8512 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8513 | /* Clear some bits that don't mean anything, but |
| 8514 | * might be left set |
| 8515 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8516 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 8517 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8518 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 8519 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 8520 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8521 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8522 | /* no recovery is running. |
| 8523 | * remove any failed drives, then |
| 8524 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8525 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8526 | * the personality to fail the re-add. |
| 8527 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8528 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8529 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8530 | if (mddev->pers->check_reshape == NULL || |
| 8531 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8532 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8533 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8534 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8535 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8536 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8537 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8538 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 8539 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8540 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8541 | } else if (mddev->recovery_cp < MaxSector) { |
| 8542 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8543 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8544 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 8545 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8546 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8548 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 8549 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 8550 | /* We are adding a device or devices to an array |
| 8551 | * which has the bitmap stored on all devices. |
| 8552 | * So make sure all bitmap pages get written |
| 8553 | */ |
| 8554 | bitmap_write_all(mddev->bitmap); |
| 8555 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8556 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 8557 | queue_work(md_misc_wq, &mddev->del_work); |
| 8558 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8559 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8560 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8561 | if (!mddev->sync_thread) { |
| 8562 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8563 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8564 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8565 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 8566 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8567 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8568 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8569 | unlock: |
| 8570 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8571 | mddev_unlock(mddev); |
| 8572 | } |
| 8573 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8574 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8575 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8576 | void md_reap_sync_thread(struct mddev *mddev) |
| 8577 | { |
| 8578 | struct md_rdev *rdev; |
| 8579 | |
| 8580 | /* resync has finished, collect result */ |
| 8581 | md_unregister_thread(&mddev->sync_thread); |
| 8582 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
Guoqing Jiang | f14b1b3 | 2019-07-24 11:09:21 +0200 | [diff] [blame] | 8583 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
| 8584 | mddev->degraded != mddev->raid_disks) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8585 | /* success...*/ |
| 8586 | /* activate any spares */ |
| 8587 | if (mddev->pers->spare_active(mddev)) { |
| 8588 | sysfs_notify(&mddev->kobj, NULL, |
| 8589 | "degraded"); |
| 8590 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 8591 | } |
| 8592 | } |
| 8593 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8594 | mddev->pers->finish_reshape) |
| 8595 | mddev->pers->finish_reshape(mddev); |
| 8596 | |
| 8597 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8598 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8599 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8600 | if (!mddev->degraded) |
| 8601 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8602 | rdev->saved_raid_disk = -1; |
| 8603 | |
| 8604 | md_update_sb(mddev, 1); |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 8605 | /* MD_CHANGE_PENDING should be cleared by md_update_sb, so we can |
| 8606 | * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by |
| 8607 | * clustered raid */ |
| 8608 | if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags)) |
| 8609 | md_cluster_ops->resync_finish(mddev); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8610 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 8611 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8612 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8613 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8614 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8615 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8616 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8617 | /* flag recovery needed just to double check */ |
| 8618 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8619 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8620 | md_new_event(mddev); |
| 8621 | if (mddev->event_work.func) |
| 8622 | queue_work(md_misc_wq, &mddev->event_work); |
| 8623 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8624 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8625 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8626 | 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] | 8627 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8628 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8629 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8630 | !test_bit(Blocked, &rdev->flags) && |
| 8631 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8632 | msecs_to_jiffies(5000)); |
| 8633 | rdev_dec_pending(rdev, mddev); |
| 8634 | } |
| 8635 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 8636 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8637 | void md_finish_reshape(struct mddev *mddev) |
| 8638 | { |
| 8639 | /* called be personality module when reshape completes. */ |
| 8640 | struct md_rdev *rdev; |
| 8641 | |
| 8642 | rdev_for_each(rdev, mddev) { |
| 8643 | if (rdev->data_offset > rdev->new_data_offset) |
| 8644 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 8645 | else |
| 8646 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 8647 | rdev->data_offset = rdev->new_data_offset; |
| 8648 | } |
| 8649 | } |
| 8650 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8651 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8652 | /* Bad block management */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8653 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8654 | /* Returns 1 on success, 0 on failure */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8655 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8656 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8657 | { |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 8658 | struct mddev *mddev = rdev->mddev; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8659 | int rv; |
| 8660 | if (is_new) |
| 8661 | s += rdev->new_data_offset; |
| 8662 | else |
| 8663 | s += rdev->data_offset; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8664 | rv = badblocks_set(&rdev->badblocks, s, sectors, 0); |
| 8665 | if (rv == 0) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8666 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8667 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 8668 | set_mask_bits(&mddev->flags, 0, |
| 8669 | BIT(MD_CHANGE_CLEAN) | BIT(MD_CHANGE_PENDING)); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8670 | md_wakeup_thread(rdev->mddev->thread); |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8671 | return 1; |
| 8672 | } else |
| 8673 | return 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8674 | } |
| 8675 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8676 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8677 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8678 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8679 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8680 | if (is_new) |
| 8681 | s += rdev->new_data_offset; |
| 8682 | else |
| 8683 | s += rdev->data_offset; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8684 | return badblocks_clear(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8685 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8686 | } |
| 8687 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8688 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8689 | static int md_notify_reboot(struct notifier_block *this, |
| 8690 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8691 | { |
| 8692 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8693 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8694 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8695 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8696 | for_each_mddev(mddev, tmp) { |
| 8697 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8698 | if (mddev->pers) |
| 8699 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 8700 | if (mddev->persistent) |
| 8701 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8702 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8703 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8704 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8705 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8706 | /* |
| 8707 | * certain more exotic SCSI devices are known to be |
| 8708 | * volatile wrt too early system reboots. While the |
| 8709 | * right place to handle this issue is the given |
| 8710 | * driver, we do want to have a safe RAID driver ... |
| 8711 | */ |
| 8712 | if (need_delay) |
| 8713 | mdelay(1000*1); |
| 8714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8715 | return NOTIFY_DONE; |
| 8716 | } |
| 8717 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8718 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8719 | .notifier_call = md_notify_reboot, |
| 8720 | .next = NULL, |
| 8721 | .priority = INT_MAX, /* before any real devices */ |
| 8722 | }; |
| 8723 | |
| 8724 | static void md_geninit(void) |
| 8725 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8726 | 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] | 8727 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8728 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8729 | } |
| 8730 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8731 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8732 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8733 | int ret = -ENOMEM; |
| 8734 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8735 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8736 | if (!md_wq) |
| 8737 | goto err_wq; |
| 8738 | |
| 8739 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8740 | if (!md_misc_wq) |
| 8741 | goto err_misc_wq; |
| 8742 | |
| 8743 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8744 | goto err_md; |
| 8745 | |
| 8746 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8747 | goto err_mdp; |
| 8748 | mdp_major = ret; |
| 8749 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8750 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8751 | md_probe, NULL, NULL); |
| 8752 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8753 | md_probe, NULL, NULL); |
| 8754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8755 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8756 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8757 | |
| 8758 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8759 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8760 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8761 | err_mdp: |
| 8762 | unregister_blkdev(MD_MAJOR, "md"); |
| 8763 | err_md: |
| 8764 | destroy_workqueue(md_misc_wq); |
| 8765 | err_misc_wq: |
| 8766 | destroy_workqueue(md_wq); |
| 8767 | err_wq: |
| 8768 | return ret; |
| 8769 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8770 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8771 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8772 | { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8773 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 8774 | struct md_rdev *rdev2; |
| 8775 | int role, ret; |
| 8776 | char b[BDEVNAME_SIZE]; |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8777 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8778 | /* Check for change of roles in the active devices */ |
| 8779 | rdev_for_each(rdev2, mddev) { |
| 8780 | if (test_bit(Faulty, &rdev2->flags)) |
| 8781 | continue; |
| 8782 | |
| 8783 | /* Check if the roles changed */ |
| 8784 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 8785 | |
| 8786 | if (test_bit(Candidate, &rdev2->flags)) { |
| 8787 | if (role == 0xfffe) { |
| 8788 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); |
| 8789 | md_kick_rdev_from_array(rdev2); |
| 8790 | continue; |
| 8791 | } |
| 8792 | else |
| 8793 | clear_bit(Candidate, &rdev2->flags); |
| 8794 | } |
| 8795 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8796 | if (role != rdev2->raid_disk) { |
| 8797 | /* got activated */ |
| 8798 | if (rdev2->raid_disk == -1 && role != 0xffff) { |
| 8799 | rdev2->saved_raid_disk = role; |
| 8800 | ret = remove_and_add_spares(mddev, rdev2); |
| 8801 | pr_info("Activated spare: %s\n", |
| 8802 | bdevname(rdev2->bdev,b)); |
Guoqing Jiang | a578183 | 2016-05-02 11:33:14 -0400 | [diff] [blame] | 8803 | /* wakeup mddev->thread here, so array could |
| 8804 | * perform resync with the new activated disk */ |
| 8805 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8806 | md_wakeup_thread(mddev->thread); |
| 8807 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8808 | } |
| 8809 | /* device faulty |
| 8810 | * We just want to do the minimum to mark the disk |
| 8811 | * as faulty. The recovery is performed by the |
| 8812 | * one who initiated the error. |
| 8813 | */ |
| 8814 | if ((role == 0xfffe) || (role == 0xfffd)) { |
| 8815 | md_error(mddev, rdev2); |
| 8816 | clear_bit(Blocked, &rdev2->flags); |
| 8817 | } |
| 8818 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8819 | } |
| 8820 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 8821 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) |
| 8822 | update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8823 | |
| 8824 | /* Finally set the event to be up to date */ |
| 8825 | mddev->events = le64_to_cpu(sb->events); |
| 8826 | } |
| 8827 | |
| 8828 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) |
| 8829 | { |
| 8830 | int err; |
| 8831 | struct page *swapout = rdev->sb_page; |
| 8832 | struct mdp_superblock_1 *sb; |
| 8833 | |
| 8834 | /* Store the sb page of the rdev in the swapout temporary |
| 8835 | * variable in case we err in the future |
| 8836 | */ |
| 8837 | rdev->sb_page = NULL; |
| 8838 | alloc_disk_sb(rdev); |
| 8839 | ClearPageUptodate(rdev->sb_page); |
| 8840 | rdev->sb_loaded = 0; |
| 8841 | err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version); |
| 8842 | |
| 8843 | if (err < 0) { |
| 8844 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", |
| 8845 | __func__, __LINE__, rdev->desc_nr, err); |
| 8846 | put_page(rdev->sb_page); |
| 8847 | rdev->sb_page = swapout; |
| 8848 | rdev->sb_loaded = 1; |
| 8849 | return err; |
| 8850 | } |
| 8851 | |
| 8852 | sb = page_address(rdev->sb_page); |
| 8853 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET |
| 8854 | * is not set |
| 8855 | */ |
| 8856 | |
| 8857 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) |
| 8858 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 8859 | |
| 8860 | /* The other node finished recovery, call spare_active to set |
| 8861 | * device In_sync and mddev->degraded |
| 8862 | */ |
| 8863 | if (rdev->recovery_offset == MaxSector && |
| 8864 | !test_bit(In_sync, &rdev->flags) && |
| 8865 | mddev->pers->spare_active(mddev)) |
| 8866 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
| 8867 | |
| 8868 | put_page(swapout); |
| 8869 | return 0; |
| 8870 | } |
| 8871 | |
| 8872 | void md_reload_sb(struct mddev *mddev, int nr) |
| 8873 | { |
| 8874 | struct md_rdev *rdev; |
| 8875 | int err; |
| 8876 | |
| 8877 | /* Find the rdev */ |
| 8878 | rdev_for_each_rcu(rdev, mddev) { |
| 8879 | if (rdev->desc_nr == nr) |
| 8880 | break; |
| 8881 | } |
| 8882 | |
| 8883 | if (!rdev || rdev->desc_nr != nr) { |
| 8884 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); |
| 8885 | return; |
| 8886 | } |
| 8887 | |
| 8888 | err = read_rdev(mddev, rdev); |
| 8889 | if (err < 0) |
| 8890 | return; |
| 8891 | |
| 8892 | check_sb_changes(mddev, rdev); |
| 8893 | |
| 8894 | /* Read all rdev's to update recovery_offset */ |
| 8895 | rdev_for_each_rcu(rdev, mddev) |
| 8896 | read_rdev(mddev, rdev); |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8897 | } |
| 8898 | EXPORT_SYMBOL(md_reload_sb); |
| 8899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8900 | #ifndef MODULE |
| 8901 | |
| 8902 | /* |
| 8903 | * Searches all registered partitions for autorun RAID arrays |
| 8904 | * at boot time. |
| 8905 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8906 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 8907 | static DEFINE_MUTEX(detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8908 | static LIST_HEAD(all_detected_devices); |
| 8909 | struct detected_devices_node { |
| 8910 | struct list_head list; |
| 8911 | dev_t dev; |
| 8912 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8913 | |
| 8914 | void md_autodetect_dev(dev_t dev) |
| 8915 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8916 | struct detected_devices_node *node_detected_dev; |
| 8917 | |
| 8918 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8919 | if (node_detected_dev) { |
| 8920 | node_detected_dev->dev = dev; |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 8921 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8922 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 8923 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8924 | } else { |
| 8925 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8926 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8927 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8928 | } |
| 8929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8930 | static void autostart_arrays(int part) |
| 8931 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8932 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8933 | struct detected_devices_node *node_detected_dev; |
| 8934 | dev_t dev; |
| 8935 | int i_scanned, i_passed; |
| 8936 | |
| 8937 | i_scanned = 0; |
| 8938 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8939 | |
| 8940 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8941 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 8942 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8943 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8944 | i_scanned++; |
| 8945 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8946 | struct detected_devices_node, list); |
| 8947 | list_del(&node_detected_dev->list); |
| 8948 | dev = node_detected_dev->dev; |
| 8949 | kfree(node_detected_dev); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 8950 | mutex_unlock(&detected_devices_mutex); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8951 | rdev = md_import_device(dev,0, 90); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 8952 | mutex_lock(&detected_devices_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8953 | if (IS_ERR(rdev)) |
| 8954 | continue; |
| 8955 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8956 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8957 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8958 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8959 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8960 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8961 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8962 | } |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 8963 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8964 | |
| 8965 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8966 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8967 | |
| 8968 | autorun_devices(part); |
| 8969 | } |
| 8970 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8971 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8972 | |
| 8973 | static __exit void md_exit(void) |
| 8974 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8975 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8976 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8977 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8978 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8979 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8980 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8981 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8982 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8983 | unregister_blkdev(mdp_major, "mdp"); |
| 8984 | unregister_reboot_notifier(&md_notifier); |
| 8985 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8986 | |
| 8987 | /* We cannot unload the modules while some process is |
| 8988 | * waiting for us in select() or poll() - wake them up |
| 8989 | */ |
| 8990 | md_unloading = 1; |
| 8991 | while (waitqueue_active(&md_event_waiters)) { |
| 8992 | /* not safe to leave yet */ |
| 8993 | wake_up(&md_event_waiters); |
| 8994 | msleep(delay); |
| 8995 | delay += delay; |
| 8996 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8997 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8998 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8999 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9000 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9001 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9002 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9003 | destroy_workqueue(md_misc_wq); |
| 9004 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9005 | } |
| 9006 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 9007 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9008 | module_exit(md_exit) |
| 9009 | |
Kees Cook | 24da2c8 | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 9010 | static int get_ro(char *buffer, const struct kernel_param *kp) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9011 | { |
| 9012 | return sprintf(buffer, "%d", start_readonly); |
| 9013 | } |
Kees Cook | 24da2c8 | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 9014 | static int set_ro(const char *val, const struct kernel_param *kp) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9015 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 9016 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9017 | } |
| 9018 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 9019 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 9020 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9021 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9023 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 9024 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 9025 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 9026 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |