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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Al Viro | ff01bb48 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 39 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 40 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 41 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 42 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 43 | #include <linux/hdreg.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/random.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 46 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 47 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 49 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 50 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/raid/md_p.h> |
| 52 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 56 | #include "md-cluster.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 59 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 62 | /* pers_list is a list of registered personalities protected |
| 63 | * by pers_lock. |
| 64 | * pers_lock does extra service to protect accesses to |
| 65 | * mddev->thread when the mutex cannot be held. |
| 66 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 67 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static DEFINE_SPINLOCK(pers_lock); |
| 69 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 70 | struct md_cluster_operations *md_cluster_ops; |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame] | 71 | EXPORT_SYMBOL(md_cluster_ops); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 72 | struct module *md_cluster_mod; |
| 73 | EXPORT_SYMBOL(md_cluster_mod); |
| 74 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 75 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 76 | static struct workqueue_struct *md_wq; |
| 77 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 78 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 79 | static int remove_and_add_spares(struct mddev *mddev, |
| 80 | struct md_rdev *this); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 81 | static void mddev_detach(struct mddev *mddev); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 84 | * Default number of read corrections we'll attempt on an rdev |
| 85 | * before ejecting it from the array. We divide the read error |
| 86 | * count by 2 for every hour elapsed between read errors. |
| 87 | */ |
| 88 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 89 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 91 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 92 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 93 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 95 | * speed limit - in case reconstruction slows down your system despite |
| 96 | * idle IO detection. |
| 97 | * |
| 98 | * 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] | 99 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | */ |
| 101 | |
| 102 | static int sysctl_speed_limit_min = 1000; |
| 103 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 104 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 105 | { |
| 106 | return mddev->sync_speed_min ? |
| 107 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 108 | } |
| 109 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 110 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 111 | { |
| 112 | return mddev->sync_speed_max ? |
| 113 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | static struct ctl_table_header *raid_table_header; |
| 117 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 118 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | .procname = "speed_limit_min", |
| 121 | .data = &sysctl_speed_limit_min, |
| 122 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 123 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 124 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | }, |
| 126 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | .procname = "speed_limit_max", |
| 128 | .data = &sysctl_speed_limit_max, |
| 129 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 130 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 131 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 133 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 136 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | .procname = "raid", |
| 139 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 140 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | .child = raid_table, |
| 142 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 143 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 146 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | .procname = "dev", |
| 149 | .maxlen = 0, |
| 150 | .mode = 0555, |
| 151 | .child = raid_dir_table, |
| 152 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 153 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 156 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 158 | static int start_readonly; |
| 159 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 160 | /* bio_clone_mddev |
| 161 | * like bio_clone, but with a local bio set |
| 162 | */ |
| 163 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 164 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 165 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 166 | { |
| 167 | struct bio *b; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 168 | |
| 169 | if (!mddev || !mddev->bio_set) |
| 170 | return bio_alloc(gfp_mask, nr_iovecs); |
| 171 | |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 172 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 173 | if (!b) |
| 174 | return NULL; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 175 | return b; |
| 176 | } |
| 177 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 178 | |
| 179 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 180 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 181 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 182 | if (!mddev || !mddev->bio_set) |
| 183 | return bio_clone(bio, gfp_mask); |
| 184 | |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 185 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 186 | } |
| 187 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 190 | * We have a system wide 'event count' that is incremented |
| 191 | * on any 'interesting' event, and readers of /proc/mdstat |
| 192 | * can use 'poll' or 'select' to find out when the event |
| 193 | * count increases. |
| 194 | * |
| 195 | * Events are: |
| 196 | * start array, stop array, error, add device, remove device, |
| 197 | * start build, activate spare |
| 198 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 199 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 200 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 201 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 202 | { |
| 203 | atomic_inc(&md_event_count); |
| 204 | wake_up(&md_event_waiters); |
| 205 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 206 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 207 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 208 | /* Alternate version that can be called from interrupts |
| 209 | * when calling sysfs_notify isn't needed. |
| 210 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 211 | static void md_new_event_inintr(struct mddev *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 212 | { |
| 213 | atomic_inc(&md_event_count); |
| 214 | wake_up(&md_event_waiters); |
| 215 | } |
| 216 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 217 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | * Enables to iterate over all existing md arrays |
| 219 | * all_mddevs_lock protects this list. |
| 220 | */ |
| 221 | static LIST_HEAD(all_mddevs); |
| 222 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | /* |
| 225 | * iterates through all used mddevs in the system. |
| 226 | * We take care to grab the all_mddevs_lock whenever navigating |
| 227 | * the list, and to always hold a refcount when unlocked. |
| 228 | * Any code which breaks out of this loop while own |
| 229 | * a reference to the current mddev and must mddev_put it. |
| 230 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 231 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | \ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 233 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 234 | _tmp = all_mddevs.next; \ |
| 235 | _mddev = NULL;}); \ |
| 236 | ({ if (_tmp != &all_mddevs) \ |
| 237 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 239 | if (_mddev) mddev_put(_mddev); \ |
| 240 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 241 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 243 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | ) |
| 245 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 246 | /* Rather than calling directly into the personality make_request function, |
| 247 | * IO requests come here first so that we can check if the device is |
| 248 | * being suspended pending a reconfiguration. |
| 249 | * We hold a refcount over the call to ->make_request. By the time that |
| 250 | * call has finished, the bio has been linked into some internal structure |
| 251 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 252 | */ |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 253 | 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] | 254 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 255 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 256 | struct mddev *mddev = q->queuedata; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 257 | unsigned int sectors; |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 258 | int cpu; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 259 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 260 | blk_queue_split(q, &bio, q->bio_split); |
| 261 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 262 | if (mddev == NULL || mddev->pers == NULL |
| 263 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 264 | bio_io_error(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 265 | return BLK_QC_T_NONE; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 266 | } |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 267 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 268 | if (bio_sectors(bio) != 0) |
| 269 | bio->bi_error = -EROFS; |
| 270 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 271 | return BLK_QC_T_NONE; |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 272 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 273 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 274 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 275 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 276 | DEFINE_WAIT(__wait); |
| 277 | for (;;) { |
| 278 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 279 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 280 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 281 | break; |
| 282 | rcu_read_unlock(); |
| 283 | schedule(); |
| 284 | rcu_read_lock(); |
| 285 | } |
| 286 | finish_wait(&mddev->sb_wait, &__wait); |
| 287 | } |
| 288 | atomic_inc(&mddev->active_io); |
| 289 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 290 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 291 | /* |
| 292 | * save the sectors now since our bio can |
| 293 | * go away inside make_request |
| 294 | */ |
| 295 | sectors = bio_sectors(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 296 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 297 | |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 298 | cpu = part_stat_lock(); |
| 299 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 300 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
| 301 | part_stat_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 302 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 303 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 304 | wake_up(&mddev->sb_wait); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 305 | |
| 306 | return BLK_QC_T_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 309 | /* mddev_suspend makes sure no new requests are submitted |
| 310 | * to the device, and that any requests that have been submitted |
| 311 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 312 | * Once mddev_detach() is called and completes, the module will be |
| 313 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 314 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 315 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 316 | { |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 317 | if (mddev->suspended++) |
| 318 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 319 | synchronize_rcu(); |
| 320 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 321 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 322 | |
| 323 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 324 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 325 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 326 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 327 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 328 | { |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 329 | if (--mddev->suspended) |
| 330 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 331 | wake_up(&mddev->sb_wait); |
| 332 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 333 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 334 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 335 | md_wakeup_thread(mddev->thread); |
| 336 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 337 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 338 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 339 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 340 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 341 | { |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 342 | struct md_personality *pers = mddev->pers; |
| 343 | int ret = 0; |
| 344 | |
| 345 | rcu_read_lock(); |
| 346 | if (mddev->suspended) |
| 347 | ret = 1; |
| 348 | else if (pers && pers->congested) |
| 349 | ret = pers->congested(mddev, bits); |
| 350 | rcu_read_unlock(); |
| 351 | return ret; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 352 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 353 | EXPORT_SYMBOL_GPL(mddev_congested); |
| 354 | static int md_congested(void *data, int bits) |
| 355 | { |
| 356 | struct mddev *mddev = data; |
| 357 | return mddev_congested(mddev, bits); |
| 358 | } |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 359 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 360 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 361 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 362 | */ |
| 363 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 364 | static void md_end_flush(struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 365 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 366 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 367 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 368 | |
| 369 | rdev_dec_pending(rdev, mddev); |
| 370 | |
| 371 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 372 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 373 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 374 | } |
| 375 | bio_put(bio); |
| 376 | } |
| 377 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 378 | static void md_submit_flush_data(struct work_struct *ws); |
| 379 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 380 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 381 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 382 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 383 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 384 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 385 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 386 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 387 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 388 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 389 | if (rdev->raid_disk >= 0 && |
| 390 | !test_bit(Faulty, &rdev->flags)) { |
| 391 | /* Take two references, one is dropped |
| 392 | * when request finishes, one after |
| 393 | * we reclaim rcu_read_lock |
| 394 | */ |
| 395 | struct bio *bi; |
| 396 | atomic_inc(&rdev->nr_pending); |
| 397 | atomic_inc(&rdev->nr_pending); |
| 398 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 399 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 400 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 401 | bi->bi_private = rdev; |
| 402 | bi->bi_bdev = rdev->bdev; |
| 403 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 404 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 405 | rcu_read_lock(); |
| 406 | rdev_dec_pending(rdev, mddev); |
| 407 | } |
| 408 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 409 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 410 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 411 | } |
| 412 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 413 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 414 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 415 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 416 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 417 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 418 | if (bio->bi_iter.bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 419 | /* an empty barrier - all done */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 420 | bio_endio(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 421 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 422 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 423 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 424 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 425 | |
| 426 | mddev->flush_bio = NULL; |
| 427 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 428 | } |
| 429 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 430 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 431 | { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 432 | spin_lock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 433 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 434 | !mddev->flush_bio, |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 435 | mddev->lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 436 | mddev->flush_bio = bio; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 437 | spin_unlock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 438 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 439 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 440 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 441 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 442 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 443 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 444 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 445 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 446 | struct mddev *mddev = cb->data; |
| 447 | md_wakeup_thread(mddev->thread); |
| 448 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 449 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 450 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 451 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 452 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
| 454 | atomic_inc(&mddev->active); |
| 455 | return mddev; |
| 456 | } |
| 457 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 458 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 459 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 460 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 462 | struct bio_set *bs = NULL; |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 465 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 466 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 467 | mddev->ctime == 0 && !mddev->hold_active) { |
| 468 | /* Array is not configured at all, and not held active, |
| 469 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 470 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 471 | bs = mddev->bio_set; |
| 472 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 473 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 474 | /* We did a probe so need to clean up. Call |
| 475 | * queue_work inside the spinlock so that |
| 476 | * flush_workqueue() after mddev_find will |
| 477 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 478 | */ |
| 479 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 480 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 481 | } else |
| 482 | kfree(mddev); |
| 483 | } |
| 484 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 485 | if (bs) |
| 486 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 489 | static void md_safemode_timeout(unsigned long data); |
| 490 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 491 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 492 | { |
| 493 | mutex_init(&mddev->open_mutex); |
| 494 | mutex_init(&mddev->reconfig_mutex); |
| 495 | mutex_init(&mddev->bitmap_info.mutex); |
| 496 | INIT_LIST_HEAD(&mddev->disks); |
| 497 | INIT_LIST_HEAD(&mddev->all_mddevs); |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 498 | setup_timer(&mddev->safemode_timer, md_safemode_timeout, |
| 499 | (unsigned long) mddev); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 500 | atomic_set(&mddev->active, 1); |
| 501 | atomic_set(&mddev->openers, 0); |
| 502 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 503 | spin_lock_init(&mddev->lock); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 504 | atomic_set(&mddev->flush_pending, 0); |
| 505 | init_waitqueue_head(&mddev->sb_wait); |
| 506 | init_waitqueue_head(&mddev->recovery_wait); |
| 507 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 508 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 509 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 510 | mddev->resync_min = 0; |
| 511 | mddev->resync_max = MaxSector; |
| 512 | mddev->level = LEVEL_NONE; |
| 513 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 514 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 515 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 516 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 518 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 520 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 521 | unit &= ~((1<<MdpMinorShift)-1); |
| 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | retry: |
| 524 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 526 | if (unit) { |
| 527 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 528 | if (mddev->unit == unit) { |
| 529 | mddev_get(mddev); |
| 530 | spin_unlock(&all_mddevs_lock); |
| 531 | kfree(new); |
| 532 | return mddev; |
| 533 | } |
| 534 | |
| 535 | if (new) { |
| 536 | list_add(&new->all_mddevs, &all_mddevs); |
| 537 | spin_unlock(&all_mddevs_lock); |
| 538 | new->hold_active = UNTIL_IOCTL; |
| 539 | return new; |
| 540 | } |
| 541 | } else if (new) { |
| 542 | /* find an unused unit number */ |
| 543 | static int next_minor = 512; |
| 544 | int start = next_minor; |
| 545 | int is_free = 0; |
| 546 | int dev = 0; |
| 547 | while (!is_free) { |
| 548 | dev = MKDEV(MD_MAJOR, next_minor); |
| 549 | next_minor++; |
| 550 | if (next_minor > MINORMASK) |
| 551 | next_minor = 0; |
| 552 | if (next_minor == start) { |
| 553 | /* Oh dear, all in use. */ |
| 554 | spin_unlock(&all_mddevs_lock); |
| 555 | kfree(new); |
| 556 | return NULL; |
| 557 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 558 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 559 | is_free = 1; |
| 560 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 561 | if (mddev->unit == dev) { |
| 562 | is_free = 0; |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | new->unit = dev; |
| 567 | new->md_minor = MINOR(dev); |
| 568 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | list_add(&new->all_mddevs, &all_mddevs); |
| 570 | spin_unlock(&all_mddevs_lock); |
| 571 | return new; |
| 572 | } |
| 573 | spin_unlock(&all_mddevs_lock); |
| 574 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 575 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | if (!new) |
| 577 | return NULL; |
| 578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | new->unit = unit; |
| 580 | if (MAJOR(unit) == MD_MAJOR) |
| 581 | new->md_minor = MINOR(unit); |
| 582 | else |
| 583 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 584 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 585 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | goto retry; |
| 588 | } |
| 589 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 590 | static struct attribute_group md_redundancy_group; |
| 591 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 592 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 594 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 595 | /* These cannot be removed under reconfig_mutex as |
| 596 | * an access to the files will try to take reconfig_mutex |
| 597 | * while holding the file unremovable, which leads to |
| 598 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 599 | * So hold set sysfs_active while the remove in happeing, |
| 600 | * and anything else which might set ->to_remove or my |
| 601 | * otherwise change the sysfs namespace will fail with |
| 602 | * -EBUSY if sysfs_active is still set. |
| 603 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 604 | * test it under the same mutex to ensure its correct value |
| 605 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 606 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 607 | struct attribute_group *to_remove = mddev->to_remove; |
| 608 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 609 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 610 | mutex_unlock(&mddev->reconfig_mutex); |
| 611 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 612 | if (mddev->kobj.sd) { |
| 613 | if (to_remove != &md_redundancy_group) |
| 614 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 615 | if (mddev->pers == NULL || |
| 616 | mddev->pers->sync_request == NULL) { |
| 617 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 618 | if (mddev->sysfs_action) |
| 619 | sysfs_put(mddev->sysfs_action); |
| 620 | mddev->sysfs_action = NULL; |
| 621 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 622 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 623 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 624 | } else |
| 625 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 627 | /* As we've dropped the mutex we need a spinlock to |
| 628 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 629 | */ |
| 630 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 631 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 632 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 634 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 636 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 637 | { |
| 638 | struct md_rdev *rdev; |
| 639 | |
| 640 | rdev_for_each_rcu(rdev, mddev) |
| 641 | if (rdev->desc_nr == nr) |
| 642 | return rdev; |
| 643 | |
| 644 | return NULL; |
| 645 | } |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 646 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 647 | |
| 648 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 650 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 652 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | if (rdev->bdev->bd_dev == dev) |
| 654 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | return NULL; |
| 657 | } |
| 658 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 659 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 660 | { |
| 661 | struct md_rdev *rdev; |
| 662 | |
| 663 | rdev_for_each_rcu(rdev, mddev) |
| 664 | if (rdev->bdev->bd_dev == dev) |
| 665 | return rdev; |
| 666 | |
| 667 | return NULL; |
| 668 | } |
| 669 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 670 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 671 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 672 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 673 | list_for_each_entry(pers, &pers_list, list) { |
| 674 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 675 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 676 | if (strcmp(pers->name, clevel)==0) |
| 677 | return pers; |
| 678 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 679 | return NULL; |
| 680 | } |
| 681 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 682 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 683 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 685 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 686 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } |
| 688 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 689 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 692 | if (!rdev->sb_page) { |
| 693 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 694 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 700 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | { |
| 702 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 703 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | rdev->sb_loaded = 0; |
| 705 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 706 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 707 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 709 | if (rdev->bb_page) { |
| 710 | put_page(rdev->bb_page); |
| 711 | rdev->bb_page = NULL; |
| 712 | } |
NeilBrown | 4fa2f32 | 2012-05-22 13:55:01 +1000 | [diff] [blame] | 713 | kfree(rdev->badblocks.page); |
| 714 | rdev->badblocks.page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 716 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 718 | static void super_written(struct bio *bio) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 719 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 720 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 721 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 722 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 723 | if (bio->bi_error) { |
| 724 | printk("md: super_written gets error=%d\n", bio->bi_error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 725 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 726 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 727 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 728 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 729 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 730 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 731 | } |
| 732 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 733 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 734 | sector_t sector, int size, struct page *page) |
| 735 | { |
| 736 | /* write first size bytes of page to sector of rdev |
| 737 | * Increment mddev->pending_writes before returning |
| 738 | * and decrement it on completion, waking up sb_wait |
| 739 | * if zero is reached. |
| 740 | * If an error occurred, call md_error |
| 741 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 742 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 743 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 744 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 745 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 746 | bio_add_page(bio, page, size, 0); |
| 747 | bio->bi_private = rdev; |
| 748 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 749 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 750 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 751 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 752 | } |
| 753 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 754 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 755 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 756 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 757 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 758 | } |
| 759 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 760 | int sync_page_io(struct md_rdev *rdev, sector_t sector, int size, |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 761 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 763 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | int ret; |
| 765 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 766 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 767 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 768 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 769 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 770 | else if (rdev->mddev->reshape_position != MaxSector && |
| 771 | (rdev->mddev->reshape_backwards == |
| 772 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 773 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 774 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 775 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | bio_add_page(bio, page, size, 0); |
Kent Overstreet | c170bbb | 2013-11-24 16:32:22 -0700 | [diff] [blame] | 777 | submit_bio_wait(rw, 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 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 792 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, 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 | */ |
| 1029 | if (rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1030 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1032 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1033 | /* "this cannot possibly happen" ... */ |
| 1034 | ret = -EINVAL; |
| 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | abort: |
| 1037 | return ret; |
| 1038 | } |
| 1039 | |
| 1040 | /* |
| 1041 | * validate_super for 0.90.0 |
| 1042 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1043 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | { |
| 1045 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1046 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1047 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1049 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1050 | clear_bit(Faulty, &rdev->flags); |
| 1051 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1052 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1053 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1054 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | if (mddev->raid_disks == 0) { |
| 1056 | mddev->major_version = 0; |
| 1057 | mddev->minor_version = sb->minor_version; |
| 1058 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1059 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1060 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | mddev->ctime = sb->ctime; |
| 1062 | mddev->utime = sb->utime; |
| 1063 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1064 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | mddev->layout = sb->layout; |
| 1066 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1067 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1068 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1069 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1070 | mddev->bitmap_info.space = 0; |
| 1071 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1072 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1073 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1074 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1076 | if (mddev->minor_version >= 91) { |
| 1077 | mddev->reshape_position = sb->reshape_position; |
| 1078 | mddev->delta_disks = sb->delta_disks; |
| 1079 | mddev->new_level = sb->new_level; |
| 1080 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1081 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1082 | if (mddev->delta_disks < 0) |
| 1083 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1084 | } else { |
| 1085 | mddev->reshape_position = MaxSector; |
| 1086 | mddev->delta_disks = 0; |
| 1087 | mddev->new_level = mddev->level; |
| 1088 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1089 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1090 | } |
| 1091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1093 | mddev->recovery_cp = MaxSector; |
| 1094 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1095 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | sb->events_lo == sb->cp_events_lo) { |
| 1097 | mddev->recovery_cp = sb->recovery_cp; |
| 1098 | } else |
| 1099 | mddev->recovery_cp = 0; |
| 1100 | } |
| 1101 | |
| 1102 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1103 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1104 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1105 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1106 | |
| 1107 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1108 | |
| 1109 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1110 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1111 | mddev->bitmap_info.offset = |
| 1112 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1113 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1114 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1115 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1116 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1117 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1118 | /* Insist on good event counter while assembling, except |
| 1119 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1121 | if (sb->disks[rdev->desc_nr].state & ( |
| 1122 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1123 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1124 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1125 | } else if (mddev->bitmap) { |
| 1126 | /* if adding to array with a bitmap, then we can accept an |
| 1127 | * older device ... but not too old. |
| 1128 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1129 | if (ev1 < mddev->bitmap->events_cleared) |
| 1130 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1131 | if (ev1 < mddev->events) |
| 1132 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1133 | } else { |
| 1134 | if (ev1 < mddev->events) |
| 1135 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1136 | return 0; |
| 1137 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | desc = sb->disks + rdev->desc_nr; |
| 1141 | |
| 1142 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1143 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1144 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1145 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1146 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1148 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1149 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1150 | /* active but not in sync implies recovery up to |
| 1151 | * reshape position. We don't know exactly where |
| 1152 | * that is, so set to zero for now */ |
| 1153 | if (mddev->minor_version >= 91) { |
| 1154 | rdev->recovery_offset = 0; |
| 1155 | rdev->raid_disk = desc->raid_disk; |
| 1156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1158 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1159 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1160 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1161 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | /* |
| 1166 | * sync_super for 0.90.0 |
| 1167 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1168 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | { |
| 1170 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1171 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | /* make rdev->sb match mddev data.. |
| 1175 | * |
| 1176 | * 1/ zero out disks |
| 1177 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1178 | * 3/ any empty disks < next_spare become removed |
| 1179 | * |
| 1180 | * disks[0] gets initialised to REMOVED because |
| 1181 | * we cannot be sure from other fields if it has |
| 1182 | * been initialised or not. |
| 1183 | */ |
| 1184 | int i; |
| 1185 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1186 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1187 | rdev->sb_size = MD_SB_BYTES; |
| 1188 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1189 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
| 1191 | memset(sb, 0, sizeof(*sb)); |
| 1192 | |
| 1193 | sb->md_magic = MD_SB_MAGIC; |
| 1194 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | sb->patch_version = mddev->patch_version; |
| 1196 | sb->gvalid_words = 0; /* ignored */ |
| 1197 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1198 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1199 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1200 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1201 | |
| 1202 | sb->ctime = mddev->ctime; |
| 1203 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1204 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | sb->raid_disks = mddev->raid_disks; |
| 1206 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1207 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | sb->utime = mddev->utime; |
| 1209 | sb->state = 0; |
| 1210 | sb->events_hi = (mddev->events>>32); |
| 1211 | sb->events_lo = (u32)mddev->events; |
| 1212 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1213 | if (mddev->reshape_position == MaxSector) |
| 1214 | sb->minor_version = 90; |
| 1215 | else { |
| 1216 | sb->minor_version = 91; |
| 1217 | sb->reshape_position = mddev->reshape_position; |
| 1218 | sb->new_level = mddev->new_level; |
| 1219 | sb->delta_disks = mddev->delta_disks; |
| 1220 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1221 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1222 | } |
| 1223 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | if (mddev->in_sync) |
| 1225 | { |
| 1226 | sb->recovery_cp = mddev->recovery_cp; |
| 1227 | sb->cp_events_hi = (mddev->events>>32); |
| 1228 | sb->cp_events_lo = (u32)mddev->events; |
| 1229 | if (mddev->recovery_cp == MaxSector) |
| 1230 | sb->state = (1<< MD_SB_CLEAN); |
| 1231 | } else |
| 1232 | sb->recovery_cp = 0; |
| 1233 | |
| 1234 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1235 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1237 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1238 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1241 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1243 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1244 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1245 | |
| 1246 | if (rdev2->raid_disk >= 0 && |
| 1247 | sb->minor_version >= 91) |
| 1248 | /* we have nowhere to store the recovery_offset, |
| 1249 | * but if it is not below the reshape_position, |
| 1250 | * we can piggy-back on that. |
| 1251 | */ |
| 1252 | is_active = 1; |
| 1253 | if (rdev2->raid_disk < 0 || |
| 1254 | test_bit(Faulty, &rdev2->flags)) |
| 1255 | is_active = 0; |
| 1256 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1257 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1259 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1260 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | d = &sb->disks[rdev2->desc_nr]; |
| 1262 | nr_disks++; |
| 1263 | d->number = rdev2->desc_nr; |
| 1264 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1265 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1266 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | d->raid_disk = rdev2->raid_disk; |
| 1268 | else |
| 1269 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1270 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1272 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1274 | if (test_bit(In_sync, &rdev2->flags)) |
| 1275 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | active++; |
| 1277 | working++; |
| 1278 | } else { |
| 1279 | d->state = 0; |
| 1280 | spare++; |
| 1281 | working++; |
| 1282 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1283 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1284 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1287 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1288 | mdp_disk_t *d = &sb->disks[i]; |
| 1289 | if (d->state == 0 && d->number == 0) { |
| 1290 | d->number = i; |
| 1291 | d->raid_disk = i; |
| 1292 | d->state = (1<<MD_DISK_REMOVED); |
| 1293 | d->state |= (1<<MD_DISK_FAULTY); |
| 1294 | failed++; |
| 1295 | } |
| 1296 | } |
| 1297 | sb->nr_disks = nr_disks; |
| 1298 | sb->active_disks = active; |
| 1299 | sb->working_disks = working; |
| 1300 | sb->failed_disks = failed; |
| 1301 | sb->spare_disks = spare; |
| 1302 | |
| 1303 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1304 | sb->sb_csum = calc_sb_csum(sb); |
| 1305 | } |
| 1306 | |
| 1307 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1308 | * rdev_size_change for 0.90.0 |
| 1309 | */ |
| 1310 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1311 | 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] | 1312 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1313 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1314 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1315 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1316 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1317 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1318 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1319 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1320 | /* Limit to 4TB as metadata cannot record more than that. |
| 1321 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1322 | */ |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1323 | if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1324 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1325 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1326 | rdev->sb_page); |
| 1327 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1328 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1329 | } |
| 1330 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1331 | static int |
| 1332 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1333 | { |
| 1334 | /* non-zero offset changes not possible with v0.90 */ |
| 1335 | return new_offset == 0; |
| 1336 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1337 | |
| 1338 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | * version 1 superblock |
| 1340 | */ |
| 1341 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1342 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1344 | __le32 disk_csum; |
| 1345 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | unsigned long long newcsum; |
| 1347 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1348 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | |
| 1350 | disk_csum = sb->sb_csum; |
| 1351 | sb->sb_csum = 0; |
| 1352 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1353 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | newcsum += le32_to_cpu(*isuper++); |
| 1355 | |
| 1356 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1357 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | |
| 1359 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1360 | sb->sb_csum = disk_csum; |
| 1361 | return cpu_to_le32(csum); |
| 1362 | } |
| 1363 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1364 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1365 | int acknowledged); |
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, |
| 1478 | rdev->bb_page, READ, true)) |
| 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; |
| 1490 | if (md_set_badblocks(&rdev->badblocks, |
| 1491 | sector, count, 1) == 0) |
| 1492 | return -EINVAL; |
| 1493 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1494 | } else if (sb->bblog_offset != 0) |
| 1495 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1496 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1497 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1498 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1499 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1501 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | |
| 1503 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1504 | sb->level != refsb->level || |
| 1505 | sb->layout != refsb->layout || |
| 1506 | sb->chunksize != refsb->chunksize) { |
| 1507 | printk(KERN_WARNING "md: %s has strangely different" |
| 1508 | " superblock to %s\n", |
| 1509 | bdevname(rdev->bdev,b), |
| 1510 | bdevname(refdev->bdev,b2)); |
| 1511 | return -EINVAL; |
| 1512 | } |
| 1513 | ev1 = le64_to_cpu(sb->events); |
| 1514 | ev2 = le64_to_cpu(refsb->events); |
| 1515 | |
| 1516 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1517 | ret = 1; |
| 1518 | else |
| 1519 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1521 | if (minor_version) { |
| 1522 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1523 | sectors -= rdev->data_offset; |
| 1524 | } else |
| 1525 | sectors = rdev->sb_start; |
| 1526 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1528 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1529 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | } |
| 1531 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1532 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1534 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1535 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1537 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1538 | clear_bit(Faulty, &rdev->flags); |
| 1539 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1540 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1541 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | if (mddev->raid_disks == 0) { |
| 1544 | mddev->major_version = 1; |
| 1545 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1546 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1547 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1549 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1550 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1551 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | mddev->layout = le32_to_cpu(sb->layout); |
| 1553 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1554 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1555 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1556 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1557 | mddev->bitmap_info.space = 0; |
| 1558 | /* Default location for bitmap is 1K after superblock |
| 1559 | * using 3K - total of 4K |
| 1560 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1561 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1562 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1563 | mddev->reshape_backwards = 0; |
| 1564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1566 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1567 | |
| 1568 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1569 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1570 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1571 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1572 | mddev->bitmap_info.offset = |
| 1573 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1574 | /* Metadata doesn't record how much space is available. |
| 1575 | * For 1.0, we assume we can use up to the superblock |
| 1576 | * if before, else to 4K beyond superblock. |
| 1577 | * For others, assume no change is possible. |
| 1578 | */ |
| 1579 | if (mddev->minor_version > 0) |
| 1580 | mddev->bitmap_info.space = 0; |
| 1581 | else if (mddev->bitmap_info.offset > 0) |
| 1582 | mddev->bitmap_info.space = |
| 1583 | 8 - mddev->bitmap_info.offset; |
| 1584 | else |
| 1585 | mddev->bitmap_info.space = |
| 1586 | -mddev->bitmap_info.offset; |
| 1587 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1588 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1589 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1590 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1591 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1592 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1593 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1594 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1595 | if (mddev->delta_disks < 0 || |
| 1596 | (mddev->delta_disks == 0 && |
| 1597 | (le32_to_cpu(sb->feature_map) |
| 1598 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1599 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1600 | } else { |
| 1601 | mddev->reshape_position = MaxSector; |
| 1602 | mddev->delta_disks = 0; |
| 1603 | mddev->new_level = mddev->level; |
| 1604 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1605 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1606 | } |
| 1607 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1608 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1609 | /* Insist of good event counter while assembling, except for |
| 1610 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1612 | if (rdev->desc_nr >= 0 && |
| 1613 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
Song Liu | a3dfbda | 2015-10-08 21:54:11 -0700 | [diff] [blame] | 1614 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1615 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1616 | if (ev1 < mddev->events) |
| 1617 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1618 | } else if (mddev->bitmap) { |
| 1619 | /* If adding to array with a bitmap, then we can accept an |
| 1620 | * older device, but not too old. |
| 1621 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1622 | if (ev1 < mddev->bitmap->events_cleared) |
| 1623 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1624 | if (ev1 < mddev->events) |
| 1625 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1626 | } else { |
| 1627 | if (ev1 < mddev->events) |
| 1628 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1629 | return 0; |
| 1630 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1632 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1633 | if (rdev->desc_nr < 0 || |
| 1634 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1635 | role = MD_DISK_ROLE_SPARE; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1636 | rdev->desc_nr = -1; |
| 1637 | } else |
| 1638 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | switch(role) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1640 | case MD_DISK_ROLE_SPARE: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | break; |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1642 | case MD_DISK_ROLE_FAULTY: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1643 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | break; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1645 | case MD_DISK_ROLE_JOURNAL: /* journal device */ |
| 1646 | if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) { |
| 1647 | /* journal device without journal feature */ |
| 1648 | printk(KERN_WARNING |
| 1649 | "md: journal device provided without journal feature, ignoring the device\n"); |
| 1650 | return -EINVAL; |
| 1651 | } |
| 1652 | set_bit(Journal, &rdev->flags); |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1653 | rdev->journal_tail = le64_to_cpu(sb->journal_tail); |
Shaohua Li | bd18f64 | 2015-09-02 13:49:50 -0700 | [diff] [blame] | 1654 | if (mddev->recovery_cp == MaxSector) |
| 1655 | set_bit(MD_JOURNAL_CLEAN, &mddev->flags); |
Shaohua Li | 9b15603 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 1656 | rdev->raid_disk = 0; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1657 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1659 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1660 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1661 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1662 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1663 | if (!(le32_to_cpu(sb->feature_map) & |
| 1664 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1665 | rdev->saved_raid_disk = -1; |
| 1666 | } else |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1667 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | rdev->raid_disk = role; |
| 1669 | break; |
| 1670 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1671 | if (sb->devflags & WriteMostly1) |
| 1672 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1673 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1674 | set_bit(Replacement, &rdev->flags); |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1675 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) |
| 1676 | set_bit(MD_HAS_JOURNAL, &mddev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1677 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1678 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | return 0; |
| 1681 | } |
| 1682 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1683 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | { |
| 1685 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1686 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | int max_dev, i; |
| 1688 | /* make rdev->sb match mddev and rdev data. */ |
| 1689 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1690 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | |
| 1692 | sb->feature_map = 0; |
| 1693 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1694 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1696 | |
| 1697 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1698 | sb->events = cpu_to_le64(mddev->events); |
| 1699 | if (mddev->in_sync) |
| 1700 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
Shaohua Li | bd18f64 | 2015-09-02 13:49:50 -0700 | [diff] [blame] | 1701 | else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags)) |
| 1702 | sb->resync_offset = cpu_to_le64(MaxSector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | else |
| 1704 | sb->resync_offset = cpu_to_le64(0); |
| 1705 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1706 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1707 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1708 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1709 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1710 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1711 | sb->level = cpu_to_le32(mddev->level); |
| 1712 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1713 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1714 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1715 | sb->devflags |= WriteMostly1; |
| 1716 | else |
| 1717 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1718 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1719 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1720 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1721 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1722 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1723 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1724 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1725 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 1726 | if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1727 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1728 | sb->feature_map |= |
| 1729 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1730 | sb->recovery_offset = |
| 1731 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1732 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 1733 | sb->feature_map |= |
| 1734 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1735 | } |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1736 | /* Note: recovery_offset and journal_tail share space */ |
| 1737 | if (test_bit(Journal, &rdev->flags)) |
| 1738 | sb->journal_tail = cpu_to_le64(rdev->journal_tail); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1739 | if (test_bit(Replacement, &rdev->flags)) |
| 1740 | sb->feature_map |= |
| 1741 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1742 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1743 | if (mddev->reshape_position != MaxSector) { |
| 1744 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1745 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1746 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1747 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1748 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1749 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1750 | if (mddev->delta_disks == 0 && |
| 1751 | mddev->reshape_backwards) |
| 1752 | sb->feature_map |
| 1753 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1754 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1755 | sb->feature_map |
| 1756 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1757 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1758 | - rdev->data_offset)); |
| 1759 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1760 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1761 | |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 1762 | if (mddev_is_clustered(mddev)) |
| 1763 | sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED); |
| 1764 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1765 | if (rdev->badblocks.count == 0) |
| 1766 | /* Nothing to do for bad blocks*/ ; |
| 1767 | else if (sb->bblog_offset == 0) |
| 1768 | /* Cannot record bad blocks on this device */ |
| 1769 | md_error(mddev, rdev); |
| 1770 | else { |
| 1771 | struct badblocks *bb = &rdev->badblocks; |
| 1772 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1773 | u64 *p = bb->page; |
| 1774 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1775 | if (bb->changed) { |
| 1776 | unsigned seq; |
| 1777 | |
| 1778 | retry: |
| 1779 | seq = read_seqbegin(&bb->lock); |
| 1780 | |
| 1781 | memset(bbp, 0xff, PAGE_SIZE); |
| 1782 | |
| 1783 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1784 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1785 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1786 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1787 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1788 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1789 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1790 | if (read_seqretry(&bb->lock, seq)) |
| 1791 | goto retry; |
| 1792 | |
| 1793 | bb->sector = (rdev->sb_start + |
| 1794 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1795 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1796 | } |
| 1797 | } |
| 1798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1800 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | if (rdev2->desc_nr+1 > max_dev) |
| 1802 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1803 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1804 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1805 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1806 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1807 | rdev->sb_size = max_dev * 2 + 256; |
| 1808 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1809 | if (rdev->sb_size & bmask) |
| 1810 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1811 | } else |
| 1812 | max_dev = le32_to_cpu(sb->max_dev); |
| 1813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | for (i=0; i<max_dev;i++) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1815 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1816 | |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1817 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) |
| 1818 | sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1820 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1822 | if (test_bit(Faulty, &rdev2->flags)) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1823 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1824 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1826 | else if (test_bit(Journal, &rdev2->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1827 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1828 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1829 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | else |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1831 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1835 | } |
| 1836 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1837 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1838 | 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] | 1839 | { |
| 1840 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1841 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1842 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1843 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1844 | if (rdev->data_offset != rdev->new_data_offset) |
| 1845 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1846 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1847 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1848 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1849 | max_sectors -= rdev->data_offset; |
| 1850 | if (!num_sectors || num_sectors > max_sectors) |
| 1851 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1852 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1853 | /* minor version 0 with bitmap we can't move */ |
| 1854 | return 0; |
| 1855 | } else { |
| 1856 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1857 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1858 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1859 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1860 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1861 | if (!num_sectors || num_sectors > max_sectors) |
| 1862 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1863 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1864 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1865 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1866 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1867 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1868 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1869 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1870 | rdev->sb_page); |
| 1871 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1872 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1873 | |
| 1874 | } |
| 1875 | |
| 1876 | static int |
| 1877 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1878 | unsigned long long new_offset) |
| 1879 | { |
| 1880 | /* All necessary checks on new >= old have been done */ |
| 1881 | struct bitmap *bitmap; |
| 1882 | if (new_offset >= rdev->data_offset) |
| 1883 | return 1; |
| 1884 | |
| 1885 | /* with 1.0 metadata, there is no metadata to tread on |
| 1886 | * so we can always move back */ |
| 1887 | if (rdev->mddev->minor_version == 0) |
| 1888 | return 1; |
| 1889 | |
| 1890 | /* otherwise we must be sure not to step on |
| 1891 | * any metadata, so stay: |
| 1892 | * 36K beyond start of superblock |
| 1893 | * beyond end of badblocks |
| 1894 | * beyond write-intent bitmap |
| 1895 | */ |
| 1896 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1897 | return 0; |
| 1898 | bitmap = rdev->mddev->bitmap; |
| 1899 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1900 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1901 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1902 | return 0; |
| 1903 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1904 | return 0; |
| 1905 | |
| 1906 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1907 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1909 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | [0] = { |
| 1911 | .name = "0.90.0", |
| 1912 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1913 | .load_super = super_90_load, |
| 1914 | .validate_super = super_90_validate, |
| 1915 | .sync_super = super_90_sync, |
| 1916 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1917 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | }, |
| 1919 | [1] = { |
| 1920 | .name = "md-1", |
| 1921 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1922 | .load_super = super_1_load, |
| 1923 | .validate_super = super_1_validate, |
| 1924 | .sync_super = super_1_sync, |
| 1925 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1926 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | }, |
| 1928 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1930 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1931 | { |
| 1932 | if (mddev->sync_super) { |
| 1933 | mddev->sync_super(mddev, rdev); |
| 1934 | return; |
| 1935 | } |
| 1936 | |
| 1937 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1938 | |
| 1939 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1940 | } |
| 1941 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1942 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1944 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1946 | rcu_read_lock(); |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 1947 | rdev_for_each_rcu(rdev, mddev1) { |
| 1948 | if (test_bit(Faulty, &rdev->flags) || |
| 1949 | test_bit(Journal, &rdev->flags) || |
| 1950 | rdev->raid_disk == -1) |
| 1951 | continue; |
| 1952 | rdev_for_each_rcu(rdev2, mddev2) { |
| 1953 | if (test_bit(Faulty, &rdev2->flags) || |
| 1954 | test_bit(Journal, &rdev2->flags) || |
| 1955 | rdev2->raid_disk == -1) |
| 1956 | continue; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1957 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1958 | rdev2->bdev->bd_contains) { |
| 1959 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1960 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1961 | } |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 1962 | } |
| 1963 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1964 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | return 0; |
| 1966 | } |
| 1967 | |
| 1968 | static LIST_HEAD(pending_raid_disks); |
| 1969 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1970 | /* |
| 1971 | * Try to register data integrity profile for an mddev |
| 1972 | * |
| 1973 | * This is called when an array is started and after a disk has been kicked |
| 1974 | * from the array. It only succeeds if all working and active component devices |
| 1975 | * are integrity capable with matching profiles. |
| 1976 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1977 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1978 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1979 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1980 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1981 | if (list_empty(&mddev->disks)) |
| 1982 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1983 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1984 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1985 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1986 | /* skip spares and non-functional disks */ |
| 1987 | if (test_bit(Faulty, &rdev->flags)) |
| 1988 | continue; |
| 1989 | if (rdev->raid_disk < 0) |
| 1990 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1991 | if (!reference) { |
| 1992 | /* Use the first rdev as the reference */ |
| 1993 | reference = rdev; |
| 1994 | continue; |
| 1995 | } |
| 1996 | /* does this rdev's profile match the reference profile? */ |
| 1997 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1998 | rdev->bdev->bd_disk) < 0) |
| 1999 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2000 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2001 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2002 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2003 | /* |
| 2004 | * All component devices are integrity capable and have matching |
| 2005 | * profiles, register the common profile for the md device. |
| 2006 | */ |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 2007 | blk_integrity_register(mddev->gendisk, |
| 2008 | bdev_get_integrity(reference->bdev)); |
| 2009 | |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2010 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 2011 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 2012 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 2013 | mdname(mddev)); |
| 2014 | return -EINVAL; |
| 2015 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2016 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2017 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2018 | EXPORT_SYMBOL(md_integrity_register); |
| 2019 | |
| 2020 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2021 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2022 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2023 | struct blk_integrity *bi_rdev; |
| 2024 | struct blk_integrity *bi_mddev; |
| 2025 | |
| 2026 | if (!mddev->gendisk) |
| 2027 | return; |
| 2028 | |
| 2029 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2030 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2031 | |
| 2032 | if (!bi_mddev) /* nothing to do */ |
| 2033 | return; |
| 2034 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2035 | return; |
| 2036 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2037 | rdev->bdev->bd_disk) >= 0) |
| 2038 | return; |
Dan Williams | c7bfced | 2015-10-21 13:20:02 -0400 | [diff] [blame] | 2039 | WARN_ON_ONCE(!mddev->suspended); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2040 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2041 | blk_integrity_unregister(mddev->gendisk); |
| 2042 | } |
| 2043 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2044 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2045 | 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] | 2046 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2047 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2048 | struct kobject *ko; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2049 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2051 | /* prevent duplicates */ |
| 2052 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2053 | return -EEXIST; |
| 2054 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2055 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2056 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2057 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2058 | if (mddev->pers) { |
| 2059 | /* Cannot change size, so fail |
| 2060 | * If mddev->level <= 0, then we don't care |
| 2061 | * about aligning sizes (e.g. linear) |
| 2062 | */ |
| 2063 | if (mddev->level > 0) |
| 2064 | return -ENOSPC; |
| 2065 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2066 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | |
| 2069 | /* Verify rdev->desc_nr is unique. |
| 2070 | * If it is -1, assign a free number, else |
| 2071 | * check number is not in use |
| 2072 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2073 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | if (rdev->desc_nr < 0) { |
| 2075 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2076 | if (mddev->pers) |
| 2077 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2078 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | choice++; |
| 2080 | rdev->desc_nr = choice; |
| 2081 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2082 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2083 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2085 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2087 | rcu_read_unlock(); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2088 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2089 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2090 | mdname(mddev), mddev->max_disks); |
| 2091 | return -EBUSY; |
| 2092 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2093 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2094 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2097 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2098 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2099 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2100 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2101 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2102 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2103 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2104 | /* failure here is OK */; |
| 2105 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2106 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2107 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2108 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2109 | |
| 2110 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2111 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2114 | |
| 2115 | fail: |
| 2116 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2117 | b, mdname(mddev)); |
| 2118 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | } |
| 2120 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2121 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2122 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2123 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2124 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2125 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2126 | } |
| 2127 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2128 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | { |
| 2130 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2131 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2132 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2133 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2135 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2136 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2137 | sysfs_put(rdev->sysfs_state); |
| 2138 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2139 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2140 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2141 | * writing to 'remove' to "dev/state". We also need |
| 2142 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2143 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2144 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2145 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2146 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2147 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | * prevent the device from being mounted, repartitioned or |
| 2152 | * otherwise reused by a RAID array (or any other kernel |
| 2153 | * subsystem), by bd_claiming the device. |
| 2154 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2155 | 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] | 2156 | { |
| 2157 | int err = 0; |
| 2158 | struct block_device *bdev; |
| 2159 | char b[BDEVNAME_SIZE]; |
| 2160 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2161 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2162 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | if (IS_ERR(bdev)) { |
| 2164 | printk(KERN_ERR "md: could not open %s.\n", |
| 2165 | __bdevname(dev, b)); |
| 2166 | return PTR_ERR(bdev); |
| 2167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | rdev->bdev = bdev; |
| 2169 | return err; |
| 2170 | } |
| 2171 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2172 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | { |
| 2174 | struct block_device *bdev = rdev->bdev; |
| 2175 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2176 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | void md_autodetect_dev(dev_t dev); |
| 2180 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2181 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | { |
| 2183 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2186 | bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2187 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2189 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2190 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | #endif |
| 2192 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2193 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | } |
| 2195 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2196 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | { |
| 2198 | unbind_rdev_from_array(rdev); |
| 2199 | export_rdev(rdev); |
| 2200 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2201 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2203 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2205 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2207 | while (!list_empty(&mddev->disks)) { |
| 2208 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2209 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2210 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | mddev->raid_disks = 0; |
| 2213 | mddev->major_version = 0; |
| 2214 | } |
| 2215 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2216 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2218 | /* Update each superblock (in-memory image), but |
| 2219 | * if we are allowed to, skip spares which already |
| 2220 | * have the right event counter, or have one earlier |
| 2221 | * (which would mean they aren't being marked as dirty |
| 2222 | * with the rest of the array) |
| 2223 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2224 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2225 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2226 | if (rdev->sb_events == mddev->events || |
| 2227 | (nospares && |
| 2228 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2229 | rdev->sb_events+1 == mddev->events)) { |
| 2230 | /* Don't update this superblock */ |
| 2231 | rdev->sb_loaded = 2; |
| 2232 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2233 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2234 | rdev->sb_loaded = 1; |
| 2235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | } |
| 2237 | } |
| 2238 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2239 | static bool does_sb_need_changing(struct mddev *mddev) |
| 2240 | { |
| 2241 | struct md_rdev *rdev; |
| 2242 | struct mdp_superblock_1 *sb; |
| 2243 | int role; |
| 2244 | |
| 2245 | /* Find a good rdev */ |
| 2246 | rdev_for_each(rdev, mddev) |
| 2247 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) |
| 2248 | break; |
| 2249 | |
| 2250 | /* No good device found. */ |
| 2251 | if (!rdev) |
| 2252 | return false; |
| 2253 | |
| 2254 | sb = page_address(rdev->sb_page); |
| 2255 | /* Check if a device has become faulty or a spare become active */ |
| 2256 | rdev_for_each(rdev, mddev) { |
| 2257 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 2258 | /* Device activated? */ |
| 2259 | if (role == 0xffff && rdev->raid_disk >=0 && |
| 2260 | !test_bit(Faulty, &rdev->flags)) |
| 2261 | return true; |
| 2262 | /* Device turned faulty? */ |
| 2263 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) |
| 2264 | return true; |
| 2265 | } |
| 2266 | |
| 2267 | /* Check if any mddev parameters have changed */ |
| 2268 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || |
| 2269 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2270 | (mddev->layout != le64_to_cpu(sb->layout)) || |
| 2271 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || |
| 2272 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) |
| 2273 | return true; |
| 2274 | |
| 2275 | return false; |
| 2276 | } |
| 2277 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2278 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2280 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2281 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2282 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2283 | int any_badblocks_changed = 0; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2284 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2286 | if (mddev->ro) { |
| 2287 | if (force_change) |
| 2288 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2289 | return; |
| 2290 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2291 | |
| 2292 | if (mddev_is_clustered(mddev)) { |
| 2293 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2294 | force_change = 1; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2295 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2296 | /* Has someone else has updated the sb */ |
| 2297 | if (!does_sb_need_changing(mddev)) { |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2298 | if (ret == 0) |
| 2299 | md_cluster_ops->metadata_update_cancel(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2300 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2301 | return; |
| 2302 | } |
| 2303 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2305 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2306 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2307 | if (rdev->raid_disk >= 0 && |
| 2308 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2309 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2310 | !test_bit(In_sync, &rdev->flags) && |
| 2311 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2312 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2313 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2314 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2315 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2316 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2317 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2318 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2319 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2320 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2321 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2322 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2323 | md_ack_all_badblocks(&rdev->badblocks); |
| 2324 | md_error(mddev, rdev); |
| 2325 | } |
| 2326 | clear_bit(Blocked, &rdev->flags); |
| 2327 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2328 | wake_up(&rdev->blocked_wait); |
| 2329 | } |
| 2330 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2331 | wake_up(&mddev->sb_wait); |
| 2332 | return; |
| 2333 | } |
| 2334 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2335 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2336 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2337 | mddev->utime = get_seconds(); |
| 2338 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2339 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2340 | force_change = 1; |
| 2341 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2342 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2343 | * though if events isn't the right even/odd, we will have to do |
| 2344 | * spares after all |
| 2345 | */ |
| 2346 | nospares = 1; |
| 2347 | if (force_change) |
| 2348 | nospares = 0; |
| 2349 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2350 | /* If the array is degraded, then skipping spares is both |
| 2351 | * dangerous and fairly pointless. |
| 2352 | * Dangerous because a device that was removed from the array |
| 2353 | * might have a event_count that still looks up-to-date, |
| 2354 | * so it can be re-added without a resync. |
| 2355 | * Pointless because if there are any spares to skip, |
| 2356 | * then a recovery will happen and soon that array won't |
| 2357 | * be degraded any more and the spare can go back to sleep then. |
| 2358 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2359 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2360 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2361 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2362 | |
| 2363 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2364 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2365 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2366 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2367 | && mddev->can_decrease_events |
| 2368 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2369 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2370 | mddev->can_decrease_events = 0; |
| 2371 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2372 | /* otherwise we have to go forward and ... */ |
| 2373 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2374 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2375 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2377 | /* |
| 2378 | * This 64-bit counter should never wrap. |
| 2379 | * Either we are in around ~1 trillion A.C., assuming |
| 2380 | * 1 reboot per second, or we have a bug... |
| 2381 | */ |
| 2382 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2383 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2384 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2385 | if (rdev->badblocks.changed) |
| 2386 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2387 | if (test_bit(Faulty, &rdev->flags)) |
| 2388 | set_bit(FaultRecorded, &rdev->flags); |
| 2389 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2390 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2391 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2392 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2394 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2395 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2397 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2398 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2400 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2401 | if (rdev->sb_loaded != 1) |
| 2402 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2404 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2405 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2406 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2407 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2408 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2409 | bdevname(rdev->bdev, b), |
| 2410 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2411 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2412 | if (rdev->badblocks.size) { |
| 2413 | md_super_write(mddev, rdev, |
| 2414 | rdev->badblocks.sector, |
| 2415 | rdev->badblocks.size << 9, |
| 2416 | rdev->bb_page); |
| 2417 | rdev->badblocks.size = 0; |
| 2418 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2419 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2420 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2421 | pr_debug("md: %s (skipping faulty)\n", |
| 2422 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2423 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2424 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | /* only need to write one superblock... */ |
| 2426 | break; |
| 2427 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2428 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2429 | /* 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] | 2430 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2431 | spin_lock(&mddev->lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2432 | if (mddev->in_sync != sync_req || |
| 2433 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2434 | /* have to write it out again */ |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2435 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2436 | goto repeat; |
| 2437 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2438 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2439 | spin_unlock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2440 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2441 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2442 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2443 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2444 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2445 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2446 | clear_bit(Blocked, &rdev->flags); |
| 2447 | |
| 2448 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2449 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2450 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2451 | wake_up(&rdev->blocked_wait); |
| 2452 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2453 | |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2454 | if (mddev_is_clustered(mddev) && ret == 0) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2455 | md_cluster_ops->metadata_update_finish(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2457 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2459 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2460 | { |
| 2461 | struct mddev *mddev = rdev->mddev; |
| 2462 | int err = 0; |
| 2463 | |
| 2464 | if (!mddev->pers->hot_remove_disk) { |
| 2465 | /* If there is hot_add_disk but no hot_remove_disk |
| 2466 | * then added disks for geometry changes, |
| 2467 | * and should be added immediately. |
| 2468 | */ |
| 2469 | super_types[mddev->major_version]. |
| 2470 | validate_super(mddev, rdev); |
| 2471 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 2472 | if (err) { |
| 2473 | unbind_rdev_from_array(rdev); |
| 2474 | export_rdev(rdev); |
| 2475 | return err; |
| 2476 | } |
| 2477 | } |
| 2478 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2479 | |
| 2480 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2481 | if (mddev->degraded) |
| 2482 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2483 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2484 | md_new_event(mddev); |
| 2485 | md_wakeup_thread(mddev->thread); |
| 2486 | return 0; |
| 2487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2489 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2490 | * We want to accept with case. For this we use cmd_match. |
| 2491 | */ |
| 2492 | static int cmd_match(const char *cmd, const char *str) |
| 2493 | { |
| 2494 | /* See if cmd, written into a sysfs file, matches |
| 2495 | * str. They must either be the same, or cmd can |
| 2496 | * have a trailing newline |
| 2497 | */ |
| 2498 | while (*cmd && *str && *cmd == *str) { |
| 2499 | cmd++; |
| 2500 | str++; |
| 2501 | } |
| 2502 | if (*cmd == '\n') |
| 2503 | cmd++; |
| 2504 | if (*str || *cmd) |
| 2505 | return 0; |
| 2506 | return 1; |
| 2507 | } |
| 2508 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2509 | struct rdev_sysfs_entry { |
| 2510 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2511 | ssize_t (*show)(struct md_rdev *, char *); |
| 2512 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2513 | }; |
| 2514 | |
| 2515 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2516 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2517 | { |
| 2518 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2519 | size_t len = 0; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2520 | unsigned long flags = ACCESS_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2521 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2522 | if (test_bit(Faulty, &flags) || |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2523 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2524 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2525 | sep = ","; |
| 2526 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2527 | if (test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2528 | len += sprintf(page+len, "%sin_sync",sep); |
| 2529 | sep = ","; |
| 2530 | } |
Shaohua Li | ac6096e | 2015-10-04 09:20:11 -0700 | [diff] [blame] | 2531 | if (test_bit(Journal, &flags)) { |
| 2532 | len += sprintf(page+len, "%sjournal",sep); |
| 2533 | sep = ","; |
| 2534 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2535 | if (test_bit(WriteMostly, &flags)) { |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2536 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2537 | sep = ","; |
| 2538 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2539 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2540 | (rdev->badblocks.unacked_exist |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2541 | && !test_bit(Faulty, &flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2542 | len += sprintf(page+len, "%sblocked", sep); |
| 2543 | sep = ","; |
| 2544 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2545 | if (!test_bit(Faulty, &flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2546 | !test_bit(Journal, &flags) && |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2547 | !test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2548 | len += sprintf(page+len, "%sspare", sep); |
| 2549 | sep = ","; |
| 2550 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2551 | if (test_bit(WriteErrorSeen, &flags)) { |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2552 | len += sprintf(page+len, "%swrite_error", sep); |
| 2553 | sep = ","; |
| 2554 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2555 | if (test_bit(WantReplacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2556 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2557 | sep = ","; |
| 2558 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2559 | if (test_bit(Replacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2560 | len += sprintf(page+len, "%sreplacement", sep); |
| 2561 | sep = ","; |
| 2562 | } |
| 2563 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2564 | return len+sprintf(page+len, "\n"); |
| 2565 | } |
| 2566 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2567 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2568 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2569 | { |
| 2570 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2571 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2572 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2573 | * writemostly - sets write_mostly |
| 2574 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2575 | * blocked - sets the Blocked flags |
| 2576 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2577 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2578 | * -insync - clear Insync for a device with a slot assigned, |
| 2579 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2580 | * write_error - sets WriteErrorSeen |
| 2581 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2582 | */ |
| 2583 | int err = -EINVAL; |
| 2584 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2585 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2586 | if (test_bit(Faulty, &rdev->flags)) |
| 2587 | err = 0; |
| 2588 | else |
| 2589 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2590 | } else if (cmd_match(buf, "remove")) { |
| 2591 | if (rdev->raid_disk >= 0) |
| 2592 | err = -EBUSY; |
| 2593 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2594 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2595 | err = 0; |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 2596 | if (mddev_is_clustered(mddev)) |
| 2597 | err = md_cluster_ops->remove_disk(mddev, rdev); |
| 2598 | |
| 2599 | if (err == 0) { |
| 2600 | md_kick_rdev_from_array(rdev); |
| 2601 | if (mddev->pers) |
| 2602 | md_update_sb(mddev, 1); |
| 2603 | md_new_event(mddev); |
| 2604 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2605 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2606 | } else if (cmd_match(buf, "writemostly")) { |
| 2607 | set_bit(WriteMostly, &rdev->flags); |
| 2608 | err = 0; |
| 2609 | } else if (cmd_match(buf, "-writemostly")) { |
| 2610 | clear_bit(WriteMostly, &rdev->flags); |
| 2611 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2612 | } else if (cmd_match(buf, "blocked")) { |
| 2613 | set_bit(Blocked, &rdev->flags); |
| 2614 | err = 0; |
| 2615 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2616 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2617 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2618 | /* metadata handler doesn't understand badblocks, |
| 2619 | * so we need to fail the device |
| 2620 | */ |
| 2621 | md_error(rdev->mddev, rdev); |
| 2622 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2623 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2624 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2625 | wake_up(&rdev->blocked_wait); |
| 2626 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2627 | md_wakeup_thread(rdev->mddev->thread); |
| 2628 | |
| 2629 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2630 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2631 | set_bit(In_sync, &rdev->flags); |
| 2632 | err = 0; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2633 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 && |
| 2634 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 2635 | if (rdev->mddev->pers == NULL) { |
| 2636 | clear_bit(In_sync, &rdev->flags); |
| 2637 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2638 | rdev->raid_disk = -1; |
| 2639 | err = 0; |
| 2640 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2641 | } else if (cmd_match(buf, "write_error")) { |
| 2642 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2643 | err = 0; |
| 2644 | } else if (cmd_match(buf, "-write_error")) { |
| 2645 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2646 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2647 | } else if (cmd_match(buf, "want_replacement")) { |
| 2648 | /* Any non-spare device that is not a replacement can |
| 2649 | * become want_replacement at any time, but we then need to |
| 2650 | * check if recovery is needed. |
| 2651 | */ |
| 2652 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2653 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2654 | !test_bit(Replacement, &rdev->flags)) |
| 2655 | set_bit(WantReplacement, &rdev->flags); |
| 2656 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2657 | md_wakeup_thread(rdev->mddev->thread); |
| 2658 | err = 0; |
| 2659 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2660 | /* Clearing 'want_replacement' is always allowed. |
| 2661 | * Once replacements starts it is too late though. |
| 2662 | */ |
| 2663 | err = 0; |
| 2664 | clear_bit(WantReplacement, &rdev->flags); |
| 2665 | } else if (cmd_match(buf, "replacement")) { |
| 2666 | /* Can only set a device as a replacement when array has not |
| 2667 | * yet been started. Once running, replacement is automatic |
| 2668 | * from spares, or by assigning 'slot'. |
| 2669 | */ |
| 2670 | if (rdev->mddev->pers) |
| 2671 | err = -EBUSY; |
| 2672 | else { |
| 2673 | set_bit(Replacement, &rdev->flags); |
| 2674 | err = 0; |
| 2675 | } |
| 2676 | } else if (cmd_match(buf, "-replacement")) { |
| 2677 | /* Similarly, can only clear Replacement before start */ |
| 2678 | if (rdev->mddev->pers) |
| 2679 | err = -EBUSY; |
| 2680 | else { |
| 2681 | clear_bit(Replacement, &rdev->flags); |
| 2682 | err = 0; |
| 2683 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2684 | } else if (cmd_match(buf, "re-add")) { |
| 2685 | if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 2686 | /* clear_bit is performed _after_ all the devices |
| 2687 | * have their local Faulty bit cleared. If any writes |
| 2688 | * happen in the meantime in the local node, they |
| 2689 | * will land in the local bitmap, which will be synced |
| 2690 | * by this node eventually |
| 2691 | */ |
| 2692 | if (!mddev_is_clustered(rdev->mddev) || |
| 2693 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 2694 | clear_bit(Faulty, &rdev->flags); |
| 2695 | err = add_bound_rdev(rdev); |
| 2696 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2697 | } else |
| 2698 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2699 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2700 | if (!err) |
| 2701 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2702 | return err ? err : len; |
| 2703 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2704 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 2705 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2706 | |
| 2707 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2708 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2709 | { |
| 2710 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2711 | } |
| 2712 | |
| 2713 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2714 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2715 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2716 | unsigned int n; |
| 2717 | int rv; |
| 2718 | |
| 2719 | rv = kstrtouint(buf, 10, &n); |
| 2720 | if (rv < 0) |
| 2721 | return rv; |
| 2722 | atomic_set(&rdev->corrected_errors, n); |
| 2723 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2724 | } |
| 2725 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2726 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2727 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2728 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2729 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2730 | { |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2731 | if (test_bit(Journal, &rdev->flags)) |
| 2732 | return sprintf(page, "journal\n"); |
| 2733 | else if (rdev->raid_disk < 0) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2734 | return sprintf(page, "none\n"); |
| 2735 | else |
| 2736 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2737 | } |
| 2738 | |
| 2739 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2740 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2741 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2742 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2743 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2744 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2745 | if (test_bit(Journal, &rdev->flags)) |
| 2746 | return -EBUSY; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2747 | if (strncmp(buf, "none", 4)==0) |
| 2748 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2749 | else { |
| 2750 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 2751 | if (err < 0) |
| 2752 | return err; |
| 2753 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2754 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2755 | /* Setting 'slot' on an active array requires also |
| 2756 | * updating the 'rd%d' link, and communicating |
| 2757 | * with the personality with ->hot_*_disk. |
| 2758 | * For now we only support removing |
| 2759 | * failed/spare devices. This normally happens automatically, |
| 2760 | * but not when the metadata is externally managed. |
| 2761 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2762 | if (rdev->raid_disk == -1) |
| 2763 | return -EEXIST; |
| 2764 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2765 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2766 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2767 | clear_bit(Blocked, &rdev->flags); |
| 2768 | remove_and_add_spares(rdev->mddev, rdev); |
| 2769 | if (rdev->raid_disk >= 0) |
| 2770 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2771 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2772 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2773 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2774 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2775 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2776 | */ |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2777 | int err; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2778 | |
| 2779 | if (rdev->raid_disk != -1) |
| 2780 | return -EBUSY; |
| 2781 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2782 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2783 | return -EBUSY; |
| 2784 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2785 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2786 | return -EINVAL; |
| 2787 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2788 | if (slot >= rdev->mddev->raid_disks && |
| 2789 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2790 | return -ENOSPC; |
| 2791 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2792 | rdev->raid_disk = slot; |
| 2793 | if (test_bit(In_sync, &rdev->flags)) |
| 2794 | rdev->saved_raid_disk = slot; |
| 2795 | else |
| 2796 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2797 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 2798 | clear_bit(Bitmap_sync, &rdev->flags); |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2799 | err = rdev->mddev->pers-> |
| 2800 | hot_add_disk(rdev->mddev, rdev); |
| 2801 | if (err) { |
| 2802 | rdev->raid_disk = -1; |
| 2803 | return err; |
| 2804 | } else |
| 2805 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2806 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
| 2807 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2808 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2809 | } else { |
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) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2812 | return -ENOSPC; |
| 2813 | rdev->raid_disk = slot; |
| 2814 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2815 | clear_bit(Faulty, &rdev->flags); |
| 2816 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2817 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2818 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2819 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2820 | return len; |
| 2821 | } |
| 2822 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2823 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2824 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2825 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2826 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2827 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2828 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2829 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2830 | } |
| 2831 | |
| 2832 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2833 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2834 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2835 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2836 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2837 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2838 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2839 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2840 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2841 | /* Must set offset before size, so overlap checks |
| 2842 | * can be sane */ |
| 2843 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2844 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2845 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2846 | return len; |
| 2847 | } |
| 2848 | |
| 2849 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2850 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2851 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2852 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2853 | { |
| 2854 | return sprintf(page, "%llu\n", |
| 2855 | (unsigned long long)rdev->new_data_offset); |
| 2856 | } |
| 2857 | |
| 2858 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2859 | const char *buf, size_t len) |
| 2860 | { |
| 2861 | unsigned long long new_offset; |
| 2862 | struct mddev *mddev = rdev->mddev; |
| 2863 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2864 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2865 | return -EINVAL; |
| 2866 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 2867 | if (mddev->sync_thread || |
| 2868 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2869 | return -EBUSY; |
| 2870 | if (new_offset == rdev->data_offset) |
| 2871 | /* reset is always permitted */ |
| 2872 | ; |
| 2873 | else if (new_offset > rdev->data_offset) { |
| 2874 | /* must not push array size beyond rdev_sectors */ |
| 2875 | if (new_offset - rdev->data_offset |
| 2876 | + mddev->dev_sectors > rdev->sectors) |
| 2877 | return -E2BIG; |
| 2878 | } |
| 2879 | /* Metadata worries about other space details. */ |
| 2880 | |
| 2881 | /* decreasing the offset is inconsistent with a backwards |
| 2882 | * reshape. |
| 2883 | */ |
| 2884 | if (new_offset < rdev->data_offset && |
| 2885 | mddev->reshape_backwards) |
| 2886 | return -EINVAL; |
| 2887 | /* Increasing offset is inconsistent with forwards |
| 2888 | * reshape. reshape_direction should be set to |
| 2889 | * 'backwards' first. |
| 2890 | */ |
| 2891 | if (new_offset > rdev->data_offset && |
| 2892 | !mddev->reshape_backwards) |
| 2893 | return -EINVAL; |
| 2894 | |
| 2895 | if (mddev->pers && mddev->persistent && |
| 2896 | !super_types[mddev->major_version] |
| 2897 | .allow_new_offset(rdev, new_offset)) |
| 2898 | return -E2BIG; |
| 2899 | rdev->new_data_offset = new_offset; |
| 2900 | if (new_offset > rdev->data_offset) |
| 2901 | mddev->reshape_backwards = 1; |
| 2902 | else if (new_offset < rdev->data_offset) |
| 2903 | mddev->reshape_backwards = 0; |
| 2904 | |
| 2905 | return len; |
| 2906 | } |
| 2907 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2908 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2909 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2910 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2911 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2912 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2913 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2914 | } |
| 2915 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2916 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2917 | { |
| 2918 | /* check if two start/length pairs overlap */ |
| 2919 | if (s1+l1 <= s2) |
| 2920 | return 0; |
| 2921 | if (s2+l2 <= s1) |
| 2922 | return 0; |
| 2923 | return 1; |
| 2924 | } |
| 2925 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2926 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2927 | { |
| 2928 | unsigned long long blocks; |
| 2929 | sector_t new; |
| 2930 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2931 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2932 | return -EINVAL; |
| 2933 | |
| 2934 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2935 | return -EINVAL; /* sector conversion overflow */ |
| 2936 | |
| 2937 | new = blocks * 2; |
| 2938 | if (new != blocks * 2) |
| 2939 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2940 | |
| 2941 | *sectors = new; |
| 2942 | return 0; |
| 2943 | } |
| 2944 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2945 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2946 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2947 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2948 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2949 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2950 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2951 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2952 | if (test_bit(Journal, &rdev->flags)) |
| 2953 | return -EBUSY; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2954 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2955 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2956 | if (rdev->data_offset != rdev->new_data_offset) |
| 2957 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2958 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2959 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2960 | sectors = super_types[my_mddev->major_version]. |
| 2961 | rdev_size_change(rdev, sectors); |
| 2962 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2963 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2964 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2965 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2966 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 2967 | if (!my_mddev->pers->resize) |
| 2968 | /* Cannot change size for RAID0 or Linear etc */ |
| 2969 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2970 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2971 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2972 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2973 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2974 | rdev->sectors = sectors; |
| 2975 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2976 | /* Need to check that all other rdevs with the same |
| 2977 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 2978 | * the rdev lists safely. |
| 2979 | * This check does not provide a hard guarantee, it |
| 2980 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2981 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2982 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2983 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2984 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2985 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2986 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2987 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2988 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2989 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2990 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2991 | if (rdev->bdev == rdev2->bdev && |
| 2992 | rdev != rdev2 && |
| 2993 | overlaps(rdev->data_offset, rdev->sectors, |
| 2994 | rdev2->data_offset, |
| 2995 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2996 | overlap = 1; |
| 2997 | break; |
| 2998 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2999 | if (overlap) { |
| 3000 | mddev_put(mddev); |
| 3001 | break; |
| 3002 | } |
| 3003 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3004 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3005 | if (overlap) { |
| 3006 | /* Someone else could have slipped in a size |
| 3007 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3008 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3009 | * safe, and trust userspace not to race with |
| 3010 | * itself |
| 3011 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3012 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3013 | return -EBUSY; |
| 3014 | } |
| 3015 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3016 | return len; |
| 3017 | } |
| 3018 | |
| 3019 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3020 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3021 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3022 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3023 | { |
| 3024 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3025 | |
| 3026 | if (test_bit(In_sync, &rdev->flags) || |
| 3027 | recovery_start == MaxSector) |
| 3028 | return sprintf(page, "none\n"); |
| 3029 | |
| 3030 | return sprintf(page, "%llu\n", recovery_start); |
| 3031 | } |
| 3032 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3033 | 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] | 3034 | { |
| 3035 | unsigned long long recovery_start; |
| 3036 | |
| 3037 | if (cmd_match(buf, "none")) |
| 3038 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3039 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3040 | return -EINVAL; |
| 3041 | |
| 3042 | if (rdev->mddev->pers && |
| 3043 | rdev->raid_disk >= 0) |
| 3044 | return -EBUSY; |
| 3045 | |
| 3046 | rdev->recovery_offset = recovery_start; |
| 3047 | if (recovery_start == MaxSector) |
| 3048 | set_bit(In_sync, &rdev->flags); |
| 3049 | else |
| 3050 | clear_bit(In_sync, &rdev->flags); |
| 3051 | return len; |
| 3052 | } |
| 3053 | |
| 3054 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3055 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3056 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3057 | static ssize_t |
| 3058 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 3059 | static ssize_t |
| 3060 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 3061 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3062 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3063 | { |
| 3064 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3065 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3066 | 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] | 3067 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3068 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3069 | /* Maybe that ack was all we needed */ |
| 3070 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3071 | wake_up(&rdev->blocked_wait); |
| 3072 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3073 | } |
| 3074 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3075 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3076 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3077 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3078 | { |
| 3079 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3080 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3081 | 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] | 3082 | { |
| 3083 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3084 | } |
| 3085 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3086 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3087 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3088 | static struct attribute *rdev_default_attrs[] = { |
| 3089 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3090 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3091 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3092 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3093 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3094 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3095 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3096 | &rdev_bad_blocks.attr, |
| 3097 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3098 | NULL, |
| 3099 | }; |
| 3100 | static ssize_t |
| 3101 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3102 | { |
| 3103 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3104 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3105 | |
| 3106 | if (!entry->show) |
| 3107 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3108 | if (!rdev->mddev) |
| 3109 | return -EBUSY; |
| 3110 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | static ssize_t |
| 3114 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3115 | const char *page, size_t length) |
| 3116 | { |
| 3117 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3118 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3119 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3120 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3121 | |
| 3122 | if (!entry->store) |
| 3123 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3124 | if (!capable(CAP_SYS_ADMIN)) |
| 3125 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3126 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3127 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3128 | if (rdev->mddev == NULL) |
| 3129 | rv = -EBUSY; |
| 3130 | else |
| 3131 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3132 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3133 | } |
| 3134 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | static void rdev_free(struct kobject *ko) |
| 3138 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3139 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3140 | kfree(rdev); |
| 3141 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3142 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3143 | .show = rdev_attr_show, |
| 3144 | .store = rdev_attr_store, |
| 3145 | }; |
| 3146 | static struct kobj_type rdev_ktype = { |
| 3147 | .release = rdev_free, |
| 3148 | .sysfs_ops = &rdev_sysfs_ops, |
| 3149 | .default_attrs = rdev_default_attrs, |
| 3150 | }; |
| 3151 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3152 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3153 | { |
| 3154 | rdev->desc_nr = -1; |
| 3155 | rdev->saved_raid_disk = -1; |
| 3156 | rdev->raid_disk = -1; |
| 3157 | rdev->flags = 0; |
| 3158 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3159 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3160 | rdev->sb_events = 0; |
| 3161 | rdev->last_read_error.tv_sec = 0; |
| 3162 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3163 | rdev->sb_loaded = 0; |
| 3164 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3165 | atomic_set(&rdev->nr_pending, 0); |
| 3166 | atomic_set(&rdev->read_errors, 0); |
| 3167 | atomic_set(&rdev->corrected_errors, 0); |
| 3168 | |
| 3169 | INIT_LIST_HEAD(&rdev->same_set); |
| 3170 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3171 | |
| 3172 | /* Add space to store bad block list. |
| 3173 | * This reserves the space even on arrays where it cannot |
| 3174 | * be used - I wonder if that matters |
| 3175 | */ |
| 3176 | rdev->badblocks.count = 0; |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 3177 | rdev->badblocks.shift = -1; /* disabled until explicitly enabled */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3178 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3179 | seqlock_init(&rdev->badblocks.lock); |
| 3180 | if (rdev->badblocks.page == NULL) |
| 3181 | return -ENOMEM; |
| 3182 | |
| 3183 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3184 | } |
| 3185 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3186 | /* |
| 3187 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3188 | * |
| 3189 | * mark the device faulty if: |
| 3190 | * |
| 3191 | * - the device is nonexistent (zero size) |
| 3192 | * - the device has no valid superblock |
| 3193 | * |
| 3194 | * a faulty rdev _never_ has rdev->sb set. |
| 3195 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3196 | 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] | 3197 | { |
| 3198 | char b[BDEVNAME_SIZE]; |
| 3199 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3200 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | sector_t size; |
| 3202 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3203 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3204 | if (!rdev) { |
| 3205 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3206 | return ERR_PTR(-ENOMEM); |
| 3207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3209 | err = md_rdev_init(rdev); |
| 3210 | if (err) |
| 3211 | goto abort_free; |
| 3212 | err = alloc_disk_sb(rdev); |
| 3213 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | goto abort_free; |
| 3215 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3216 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | if (err) |
| 3218 | goto abort_free; |
| 3219 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3220 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3221 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3222 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | if (!size) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3224 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3225 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3226 | bdevname(rdev->bdev,b)); |
| 3227 | err = -EINVAL; |
| 3228 | goto abort_free; |
| 3229 | } |
| 3230 | |
| 3231 | if (super_format >= 0) { |
| 3232 | err = super_types[super_format]. |
| 3233 | load_super(rdev, NULL, super_minor); |
| 3234 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3235 | printk(KERN_WARNING |
| 3236 | "md: %s does not have a valid v%d.%d " |
| 3237 | "superblock, not importing!\n", |
| 3238 | bdevname(rdev->bdev,b), |
| 3239 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3240 | goto abort_free; |
| 3241 | } |
| 3242 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3243 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | "md: could not read %s's sb, not importing!\n", |
| 3245 | bdevname(rdev->bdev,b)); |
| 3246 | goto abort_free; |
| 3247 | } |
| 3248 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3250 | return rdev; |
| 3251 | |
| 3252 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3253 | if (rdev->bdev) |
| 3254 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3255 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3256 | kfree(rdev); |
| 3257 | return ERR_PTR(err); |
| 3258 | } |
| 3259 | |
| 3260 | /* |
| 3261 | * Check a full RAID array for plausibility |
| 3262 | */ |
| 3263 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3264 | static void analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | { |
| 3266 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3267 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3268 | char b[BDEVNAME_SIZE]; |
| 3269 | |
| 3270 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3271 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | switch (super_types[mddev->major_version]. |
| 3273 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3274 | case 1: |
| 3275 | freshest = rdev; |
| 3276 | break; |
| 3277 | case 0: |
| 3278 | break; |
| 3279 | default: |
| 3280 | printk( KERN_ERR \ |
| 3281 | "md: fatal superblock inconsistency in %s" |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3282 | " -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3284 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | } |
| 3286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | super_types[mddev->major_version]. |
| 3288 | validate_super(mddev, freshest); |
| 3289 | |
| 3290 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3291 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3292 | if (mddev->max_disks && |
| 3293 | (rdev->desc_nr >= mddev->max_disks || |
| 3294 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3295 | printk(KERN_WARNING |
| 3296 | "md: %s: %s: only %d devices permitted\n", |
| 3297 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3298 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3299 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3300 | continue; |
| 3301 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3302 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | if (super_types[mddev->major_version]. |
| 3304 | validate_super(mddev, rdev)) { |
| 3305 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3306 | " from array!\n", |
| 3307 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3308 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | continue; |
| 3310 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3313 | rdev->desc_nr = i++; |
| 3314 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3315 | set_bit(In_sync, &rdev->flags); |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3316 | } else if (rdev->raid_disk >= |
| 3317 | (mddev->raid_disks - min(0, mddev->delta_disks)) && |
| 3318 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3319 | rdev->raid_disk = -1; |
| 3320 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3321 | } |
| 3322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3323 | } |
| 3324 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3325 | /* Read a fixed-point number. |
| 3326 | * Numbers in sysfs attributes should be in "standard" units where |
| 3327 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3328 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3329 | * milliseconds or jiffies. |
| 3330 | * This function takes a decimal number with a possible fractional |
| 3331 | * component, and produces an integer which is the result of |
| 3332 | * multiplying that number by 10^'scale'. |
| 3333 | * all without any floating-point arithmetic. |
| 3334 | */ |
| 3335 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3336 | { |
| 3337 | unsigned long result = 0; |
| 3338 | long decimals = -1; |
| 3339 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3340 | if (*cp == '.') |
| 3341 | decimals = 0; |
| 3342 | else if (decimals < scale) { |
| 3343 | unsigned int value; |
| 3344 | value = *cp - '0'; |
| 3345 | result = result * 10 + value; |
| 3346 | if (decimals >= 0) |
| 3347 | decimals++; |
| 3348 | } |
| 3349 | cp++; |
| 3350 | } |
| 3351 | if (*cp == '\n') |
| 3352 | cp++; |
| 3353 | if (*cp) |
| 3354 | return -EINVAL; |
| 3355 | if (decimals < 0) |
| 3356 | decimals = 0; |
| 3357 | while (decimals < scale) { |
| 3358 | result *= 10; |
| 3359 | decimals ++; |
| 3360 | } |
| 3361 | *res = result; |
| 3362 | return 0; |
| 3363 | } |
| 3364 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3365 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3366 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3367 | { |
| 3368 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3369 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3370 | } |
| 3371 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3372 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3373 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3374 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3375 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3376 | if (mddev_is_clustered(mddev)) { |
| 3377 | pr_info("md: Safemode is disabled for clustered mode\n"); |
| 3378 | return -EINVAL; |
| 3379 | } |
| 3380 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3381 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3382 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3383 | if (msec == 0) |
| 3384 | mddev->safemode_delay = 0; |
| 3385 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3386 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3387 | unsigned long new_delay = (msec*HZ)/1000; |
| 3388 | |
| 3389 | if (new_delay == 0) |
| 3390 | new_delay = 1; |
| 3391 | mddev->safemode_delay = new_delay; |
| 3392 | if (new_delay < old_delay || old_delay == 0) |
| 3393 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3394 | } |
| 3395 | return len; |
| 3396 | } |
| 3397 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3398 | __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] | 3399 | |
| 3400 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3401 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3402 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3403 | struct md_personality *p; |
| 3404 | int ret; |
| 3405 | spin_lock(&mddev->lock); |
| 3406 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3407 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3408 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3409 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3410 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3411 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3412 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3413 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3414 | ret = 0; |
| 3415 | spin_unlock(&mddev->lock); |
| 3416 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3417 | } |
| 3418 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3419 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3420 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3421 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3422 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3423 | ssize_t rv; |
| 3424 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3425 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3426 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3427 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3428 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3429 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3430 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3431 | return -EINVAL; |
| 3432 | |
| 3433 | rv = mddev_lock(mddev); |
| 3434 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3435 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3436 | |
| 3437 | if (mddev->pers == NULL) { |
| 3438 | strncpy(mddev->clevel, buf, slen); |
| 3439 | if (mddev->clevel[slen-1] == '\n') |
| 3440 | slen--; |
| 3441 | mddev->clevel[slen] = 0; |
| 3442 | mddev->level = LEVEL_NONE; |
| 3443 | rv = len; |
| 3444 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3445 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3446 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3447 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3448 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3449 | |
| 3450 | /* request to change the personality. Need to ensure: |
| 3451 | * - array is not engaged in resync/recovery/reshape |
| 3452 | * - old personality can be suspended |
| 3453 | * - new personality will access other array. |
| 3454 | */ |
| 3455 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3456 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3457 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3458 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3459 | mddev->reshape_position != MaxSector || |
| 3460 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3461 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3462 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3463 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3464 | if (!mddev->pers->quiesce) { |
| 3465 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3466 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3467 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3471 | strncpy(clevel, buf, slen); |
| 3472 | if (clevel[slen-1] == '\n') |
| 3473 | slen--; |
| 3474 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3475 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3476 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3477 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3478 | if (request_module("md-%s", clevel) != 0) |
| 3479 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3480 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3481 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3482 | if (!pers || !try_module_get(pers->owner)) { |
| 3483 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3484 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3485 | rv = -EINVAL; |
| 3486 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3487 | } |
| 3488 | spin_unlock(&pers_lock); |
| 3489 | |
| 3490 | if (pers == mddev->pers) { |
| 3491 | /* Nothing to do! */ |
| 3492 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3493 | rv = len; |
| 3494 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3495 | } |
| 3496 | if (!pers->takeover) { |
| 3497 | module_put(pers->owner); |
| 3498 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3499 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3500 | rv = -EINVAL; |
| 3501 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3502 | } |
| 3503 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3504 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3505 | rdev->new_raid_disk = rdev->raid_disk; |
| 3506 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3507 | /* ->takeover must set new_* and/or delta_disks |
| 3508 | * if it succeeds, and may set them when it fails. |
| 3509 | */ |
| 3510 | priv = pers->takeover(mddev); |
| 3511 | if (IS_ERR(priv)) { |
| 3512 | mddev->new_level = mddev->level; |
| 3513 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3514 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3515 | mddev->raid_disks -= mddev->delta_disks; |
| 3516 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3517 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3518 | module_put(pers->owner); |
| 3519 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3520 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3521 | rv = PTR_ERR(priv); |
| 3522 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3523 | } |
| 3524 | |
| 3525 | /* Looks like we have a winner */ |
| 3526 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 3527 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3528 | |
| 3529 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3530 | oldpers = mddev->pers; |
| 3531 | oldpriv = mddev->private; |
| 3532 | mddev->pers = pers; |
| 3533 | mddev->private = priv; |
| 3534 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3535 | mddev->level = mddev->new_level; |
| 3536 | mddev->layout = mddev->new_layout; |
| 3537 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 3538 | mddev->delta_disks = 0; |
| 3539 | mddev->reshape_backwards = 0; |
| 3540 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3541 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3542 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3543 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3544 | mddev->external) { |
| 3545 | /* We are converting from a no-redundancy array |
| 3546 | * to a redundancy array and metadata is managed |
| 3547 | * externally so we need to be sure that writes |
| 3548 | * won't block due to a need to transition |
| 3549 | * clean->dirty |
| 3550 | * until external management is started. |
| 3551 | */ |
| 3552 | mddev->in_sync = 0; |
| 3553 | mddev->safemode_delay = 0; |
| 3554 | mddev->safemode = 0; |
| 3555 | } |
| 3556 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3557 | oldpers->free(mddev, oldpriv); |
| 3558 | |
| 3559 | if (oldpers->sync_request == NULL && |
| 3560 | pers->sync_request != NULL) { |
| 3561 | /* need to add the md_redundancy_group */ |
| 3562 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3563 | printk(KERN_WARNING |
| 3564 | "md: cannot register extra attributes for %s\n", |
| 3565 | mdname(mddev)); |
| 3566 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3567 | } |
| 3568 | if (oldpers->sync_request != NULL && |
| 3569 | pers->sync_request == NULL) { |
| 3570 | /* need to remove the md_redundancy_group */ |
| 3571 | if (mddev->to_remove == NULL) |
| 3572 | mddev->to_remove = &md_redundancy_group; |
| 3573 | } |
| 3574 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3575 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3576 | if (rdev->raid_disk < 0) |
| 3577 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3578 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3579 | rdev->new_raid_disk = -1; |
| 3580 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3581 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3582 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3583 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3584 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3585 | if (rdev->raid_disk < 0) |
| 3586 | continue; |
| 3587 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3588 | continue; |
| 3589 | rdev->raid_disk = rdev->new_raid_disk; |
| 3590 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3591 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3592 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3593 | if (sysfs_link_rdev(mddev, rdev)) |
| 3594 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3595 | " for %s after level change\n", |
| 3596 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3597 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3598 | } |
| 3599 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3600 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3601 | /* this is now an array without redundancy, so |
| 3602 | * it must always be in_sync |
| 3603 | */ |
| 3604 | mddev->in_sync = 1; |
| 3605 | del_timer_sync(&mddev->safemode_timer); |
| 3606 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 3607 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3608 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3609 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3610 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 3611 | if (!mddev->thread) |
| 3612 | md_update_sb(mddev, 1); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3613 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3614 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3615 | rv = len; |
| 3616 | out_unlock: |
| 3617 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3618 | return rv; |
| 3619 | } |
| 3620 | |
| 3621 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3622 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3623 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3624 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3625 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3626 | { |
| 3627 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3628 | if (mddev->reshape_position != MaxSector && |
| 3629 | mddev->layout != mddev->new_layout) |
| 3630 | return sprintf(page, "%d (%d)\n", |
| 3631 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3632 | return sprintf(page, "%d\n", mddev->layout); |
| 3633 | } |
| 3634 | |
| 3635 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3636 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3637 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3638 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3639 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3640 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3641 | err = kstrtouint(buf, 10, &n); |
| 3642 | if (err < 0) |
| 3643 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3644 | err = mddev_lock(mddev); |
| 3645 | if (err) |
| 3646 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3647 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3648 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3649 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3650 | err = -EBUSY; |
| 3651 | else if (mddev->ro) |
| 3652 | err = -EROFS; |
| 3653 | else { |
| 3654 | mddev->new_layout = n; |
| 3655 | err = mddev->pers->check_reshape(mddev); |
| 3656 | if (err) |
| 3657 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3658 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3659 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3660 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3661 | if (mddev->reshape_position == MaxSector) |
| 3662 | mddev->layout = n; |
| 3663 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3664 | mddev_unlock(mddev); |
| 3665 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3666 | } |
| 3667 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3668 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3669 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3670 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3671 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3672 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3673 | if (mddev->raid_disks == 0) |
| 3674 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3675 | if (mddev->reshape_position != MaxSector && |
| 3676 | mddev->delta_disks != 0) |
| 3677 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3678 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3679 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3680 | } |
| 3681 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3682 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3683 | |
| 3684 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3685 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3686 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3687 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3688 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3689 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3690 | err = kstrtouint(buf, 10, &n); |
| 3691 | if (err < 0) |
| 3692 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3693 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3694 | err = mddev_lock(mddev); |
| 3695 | if (err) |
| 3696 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3697 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3698 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3699 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3700 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3701 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3702 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3703 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3704 | rdev_for_each(rdev, mddev) { |
| 3705 | if (olddisks < n && |
| 3706 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3707 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3708 | if (olddisks > n && |
| 3709 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3710 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3711 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3712 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3713 | mddev->delta_disks = n - olddisks; |
| 3714 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3715 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3716 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3717 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3718 | out_unlock: |
| 3719 | mddev_unlock(mddev); |
| 3720 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3721 | } |
| 3722 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3723 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3724 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3725 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3726 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3727 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3728 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3729 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3730 | return sprintf(page, "%d (%d)\n", |
| 3731 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3732 | mddev->chunk_sectors << 9); |
| 3733 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3734 | } |
| 3735 | |
| 3736 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3737 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3738 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3739 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3740 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3741 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3742 | err = kstrtoul(buf, 10, &n); |
| 3743 | if (err < 0) |
| 3744 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3745 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3746 | err = mddev_lock(mddev); |
| 3747 | if (err) |
| 3748 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3749 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3750 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3751 | err = -EBUSY; |
| 3752 | else if (mddev->ro) |
| 3753 | err = -EROFS; |
| 3754 | else { |
| 3755 | mddev->new_chunk_sectors = n >> 9; |
| 3756 | err = mddev->pers->check_reshape(mddev); |
| 3757 | if (err) |
| 3758 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3759 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3760 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3761 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3762 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3763 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3764 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3765 | mddev_unlock(mddev); |
| 3766 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3767 | } |
| 3768 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3769 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3770 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3771 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3772 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3773 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3774 | if (mddev->recovery_cp == MaxSector) |
| 3775 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3776 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3777 | } |
| 3778 | |
| 3779 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3780 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3781 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3782 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3783 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3784 | |
| 3785 | if (cmd_match(buf, "none")) |
| 3786 | n = MaxSector; |
| 3787 | else { |
| 3788 | err = kstrtoull(buf, 10, &n); |
| 3789 | if (err < 0) |
| 3790 | return err; |
| 3791 | if (n != (sector_t)n) |
| 3792 | return -EINVAL; |
| 3793 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3794 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3795 | err = mddev_lock(mddev); |
| 3796 | if (err) |
| 3797 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3798 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3799 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3800 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3801 | if (!err) { |
| 3802 | mddev->recovery_cp = n; |
| 3803 | if (mddev->pers) |
| 3804 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3805 | } |
| 3806 | mddev_unlock(mddev); |
| 3807 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3808 | } |
| 3809 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3810 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 3811 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3812 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3813 | /* |
| 3814 | * The array state can be: |
| 3815 | * |
| 3816 | * clear |
| 3817 | * No devices, no size, no level |
| 3818 | * Equivalent to STOP_ARRAY ioctl |
| 3819 | * inactive |
| 3820 | * May have some settings, but array is not active |
| 3821 | * all IO results in error |
| 3822 | * When written, doesn't tear down array, but just stops it |
| 3823 | * suspended (not supported yet) |
| 3824 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3825 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3826 | * readonly |
| 3827 | * no resync can happen. no superblocks get written. |
| 3828 | * write requests fail |
| 3829 | * read-auto |
| 3830 | * like readonly, but behaves like 'clean' on a write request. |
| 3831 | * |
| 3832 | * clean - no pending writes, but otherwise active. |
| 3833 | * When written to inactive array, starts without resync |
| 3834 | * If a write request arrives then |
| 3835 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3836 | * if not known, block and switch to write-pending |
| 3837 | * If written to an active array that has pending writes, then fails. |
| 3838 | * active |
| 3839 | * fully active: IO and resync can be happening. |
| 3840 | * When written to inactive array, starts with resync |
| 3841 | * |
| 3842 | * write-pending |
| 3843 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3844 | * |
| 3845 | * active-idle |
| 3846 | * like active, but no writes have been seen for a while (100msec). |
| 3847 | * |
| 3848 | */ |
| 3849 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3850 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3851 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3852 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3853 | "write-pending", "active-idle", NULL }; |
| 3854 | |
| 3855 | static int match_word(const char *word, char **list) |
| 3856 | { |
| 3857 | int n; |
| 3858 | for (n=0; list[n]; n++) |
| 3859 | if (cmd_match(word, list[n])) |
| 3860 | break; |
| 3861 | return n; |
| 3862 | } |
| 3863 | |
| 3864 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3865 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3866 | { |
| 3867 | enum array_state st = inactive; |
| 3868 | |
| 3869 | if (mddev->pers) |
| 3870 | switch(mddev->ro) { |
| 3871 | case 1: |
| 3872 | st = readonly; |
| 3873 | break; |
| 3874 | case 2: |
| 3875 | st = read_auto; |
| 3876 | break; |
| 3877 | case 0: |
| 3878 | if (mddev->in_sync) |
| 3879 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3880 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3881 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3882 | else if (mddev->safemode) |
| 3883 | st = active_idle; |
| 3884 | else |
| 3885 | st = active; |
| 3886 | } |
| 3887 | else { |
| 3888 | if (list_empty(&mddev->disks) && |
| 3889 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3890 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3891 | st = clear; |
| 3892 | else |
| 3893 | st = inactive; |
| 3894 | } |
| 3895 | return sprintf(page, "%s\n", array_states[st]); |
| 3896 | } |
| 3897 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3898 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 3899 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 3900 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3901 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3902 | |
| 3903 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3904 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3905 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3906 | int err; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3907 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3908 | |
| 3909 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 3910 | /* don't take reconfig_mutex when toggling between |
| 3911 | * clean and active |
| 3912 | */ |
| 3913 | spin_lock(&mddev->lock); |
| 3914 | if (st == active) { |
| 3915 | restart_array(mddev); |
| 3916 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 3917 | wake_up(&mddev->sb_wait); |
| 3918 | err = 0; |
| 3919 | } else /* st == clean */ { |
| 3920 | restart_array(mddev); |
| 3921 | if (atomic_read(&mddev->writes_pending) == 0) { |
| 3922 | if (mddev->in_sync == 0) { |
| 3923 | mddev->in_sync = 1; |
| 3924 | if (mddev->safemode == 1) |
| 3925 | mddev->safemode = 0; |
| 3926 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3927 | } |
| 3928 | err = 0; |
| 3929 | } else |
| 3930 | err = -EBUSY; |
| 3931 | } |
| 3932 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 3933 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3934 | } |
| 3935 | err = mddev_lock(mddev); |
| 3936 | if (err) |
| 3937 | return err; |
| 3938 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3939 | switch(st) { |
| 3940 | case bad_word: |
| 3941 | break; |
| 3942 | case clear: |
| 3943 | /* stopping an active array */ |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3944 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3945 | break; |
| 3946 | case inactive: |
| 3947 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3948 | if (mddev->pers) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3949 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3950 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3951 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3952 | break; |
| 3953 | case suspended: |
| 3954 | break; /* not supported yet */ |
| 3955 | case readonly: |
| 3956 | if (mddev->pers) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3957 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3958 | else { |
| 3959 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3960 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3961 | err = do_md_run(mddev); |
| 3962 | } |
| 3963 | break; |
| 3964 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3965 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3966 | if (mddev->ro == 0) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3967 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3968 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3969 | err = restart_array(mddev); |
| 3970 | if (err == 0) { |
| 3971 | mddev->ro = 2; |
| 3972 | set_disk_ro(mddev->gendisk, 0); |
| 3973 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3974 | } else { |
| 3975 | mddev->ro = 2; |
| 3976 | err = do_md_run(mddev); |
| 3977 | } |
| 3978 | break; |
| 3979 | case clean: |
| 3980 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 3981 | err = restart_array(mddev); |
| 3982 | if (err) |
| 3983 | break; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3984 | spin_lock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3985 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3986 | if (mddev->in_sync == 0) { |
| 3987 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3988 | if (mddev->safemode == 1) |
| 3989 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3990 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3991 | } |
| 3992 | err = 0; |
| 3993 | } else |
| 3994 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3995 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3996 | } else |
| 3997 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3998 | break; |
| 3999 | case active: |
| 4000 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4001 | err = restart_array(mddev); |
| 4002 | if (err) |
| 4003 | break; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 4004 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4005 | wake_up(&mddev->sb_wait); |
| 4006 | err = 0; |
| 4007 | } else { |
| 4008 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4009 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4010 | err = do_md_run(mddev); |
| 4011 | } |
| 4012 | break; |
| 4013 | case write_pending: |
| 4014 | case active_idle: |
| 4015 | /* these cannot be set */ |
| 4016 | break; |
| 4017 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4018 | |
| 4019 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 4020 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4021 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4022 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4023 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4024 | mddev_unlock(mddev); |
| 4025 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4026 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4027 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4028 | __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] | 4029 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4030 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4031 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4032 | return sprintf(page, "%d\n", |
| 4033 | atomic_read(&mddev->max_corr_read_errors)); |
| 4034 | } |
| 4035 | |
| 4036 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4037 | 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] | 4038 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4039 | unsigned int n; |
| 4040 | int rv; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4041 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4042 | rv = kstrtouint(buf, 10, &n); |
| 4043 | if (rv < 0) |
| 4044 | return rv; |
| 4045 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4046 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4047 | } |
| 4048 | |
| 4049 | static struct md_sysfs_entry max_corr_read_errors = |
| 4050 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4051 | max_corrected_read_errors_store); |
| 4052 | |
| 4053 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4054 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4055 | { |
| 4056 | return -EINVAL; |
| 4057 | } |
| 4058 | |
| 4059 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4060 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4061 | { |
| 4062 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4063 | /* The new device is added to the array. |
| 4064 | * If the array has a persistent superblock, we read the |
| 4065 | * superblock to initialise info and check validity. |
| 4066 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4067 | * which mainly checks size. |
| 4068 | */ |
| 4069 | char *e; |
| 4070 | int major = simple_strtoul(buf, &e, 10); |
| 4071 | int minor; |
| 4072 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4073 | struct md_rdev *rdev; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4074 | int err; |
| 4075 | |
| 4076 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4077 | return -EINVAL; |
| 4078 | minor = simple_strtoul(e+1, &e, 10); |
| 4079 | if (*e && *e != '\n') |
| 4080 | return -EINVAL; |
| 4081 | dev = MKDEV(major, minor); |
| 4082 | if (major != MAJOR(dev) || |
| 4083 | minor != MINOR(dev)) |
| 4084 | return -EOVERFLOW; |
| 4085 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4086 | flush_workqueue(md_misc_wq); |
| 4087 | |
| 4088 | err = mddev_lock(mddev); |
| 4089 | if (err) |
| 4090 | return err; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4091 | if (mddev->persistent) { |
| 4092 | rdev = md_import_device(dev, mddev->major_version, |
| 4093 | mddev->minor_version); |
| 4094 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4095 | struct md_rdev *rdev0 |
| 4096 | = list_entry(mddev->disks.next, |
| 4097 | struct md_rdev, same_set); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4098 | err = super_types[mddev->major_version] |
| 4099 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4100 | if (err < 0) |
| 4101 | goto out; |
| 4102 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4103 | } else if (mddev->external) |
| 4104 | rdev = md_import_device(dev, -2, -1); |
| 4105 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4106 | rdev = md_import_device(dev, -1, -1); |
| 4107 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4108 | if (IS_ERR(rdev)) { |
| 4109 | mddev_unlock(mddev); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4110 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4111 | } |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4112 | err = bind_rdev_to_array(rdev, mddev); |
| 4113 | out: |
| 4114 | if (err) |
| 4115 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4116 | mddev_unlock(mddev); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4117 | return err ? err : len; |
| 4118 | } |
| 4119 | |
| 4120 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4121 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4122 | |
| 4123 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4124 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4125 | { |
| 4126 | char *end; |
| 4127 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4128 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4129 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4130 | err = mddev_lock(mddev); |
| 4131 | if (err) |
| 4132 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4133 | if (!mddev->bitmap) |
| 4134 | goto out; |
| 4135 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4136 | while (*buf) { |
| 4137 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4138 | if (buf == end) break; |
| 4139 | if (*end == '-') { /* range */ |
| 4140 | buf = end + 1; |
| 4141 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4142 | if (buf == end) break; |
| 4143 | } |
| 4144 | if (*end && !isspace(*end)) break; |
| 4145 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4146 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4147 | } |
| 4148 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4149 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4150 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4151 | return len; |
| 4152 | } |
| 4153 | |
| 4154 | static struct md_sysfs_entry md_bitmap = |
| 4155 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4156 | |
| 4157 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4158 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4159 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4160 | return sprintf(page, "%llu\n", |
| 4161 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4162 | } |
| 4163 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4164 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4165 | |
| 4166 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4167 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4168 | { |
| 4169 | /* If array is inactive, we can reduce the component size, but |
| 4170 | * not increase it (except from 0). |
| 4171 | * If array is active, we can try an on-line resize |
| 4172 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4173 | sector_t sectors; |
| 4174 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4175 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4176 | if (err < 0) |
| 4177 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4178 | err = mddev_lock(mddev); |
| 4179 | if (err) |
| 4180 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4181 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4182 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4183 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4184 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4185 | if (mddev->dev_sectors == 0 || |
| 4186 | mddev->dev_sectors > sectors) |
| 4187 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4188 | else |
| 4189 | err = -ENOSPC; |
| 4190 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4191 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4192 | return err ? err : len; |
| 4193 | } |
| 4194 | |
| 4195 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4196 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4197 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4198 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4199 | * This is one of |
| 4200 | * 'none' for arrays with no metadata (good luck...) |
| 4201 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4202 | * or N.M for internally known formats |
| 4203 | */ |
| 4204 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4205 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4206 | { |
| 4207 | if (mddev->persistent) |
| 4208 | return sprintf(page, "%d.%d\n", |
| 4209 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4210 | else if (mddev->external) |
| 4211 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4212 | else |
| 4213 | return sprintf(page, "none\n"); |
| 4214 | } |
| 4215 | |
| 4216 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4217 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4218 | { |
| 4219 | int major, minor; |
| 4220 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4221 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4222 | /* Changing the details of 'external' metadata is |
| 4223 | * always permitted. Otherwise there must be |
| 4224 | * no devices attached to the array. |
| 4225 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4226 | |
| 4227 | err = mddev_lock(mddev); |
| 4228 | if (err) |
| 4229 | return err; |
| 4230 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4231 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4232 | ; |
| 4233 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4234 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4235 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4236 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4237 | if (cmd_match(buf, "none")) { |
| 4238 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4239 | mddev->external = 0; |
| 4240 | mddev->major_version = 0; |
| 4241 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4242 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4243 | } |
| 4244 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4245 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4246 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4247 | namelen = sizeof(mddev->metadata_type)-1; |
| 4248 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4249 | mddev->metadata_type[namelen] = 0; |
| 4250 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4251 | mddev->metadata_type[--namelen] = 0; |
| 4252 | mddev->persistent = 0; |
| 4253 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4254 | mddev->major_version = 0; |
| 4255 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4256 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4257 | } |
| 4258 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4259 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4260 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4261 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4262 | buf = e+1; |
| 4263 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4264 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4265 | goto out_unlock; |
| 4266 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4267 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4268 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4269 | mddev->major_version = major; |
| 4270 | mddev->minor_version = minor; |
| 4271 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4272 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4273 | err = 0; |
| 4274 | out_unlock: |
| 4275 | mddev_unlock(mddev); |
| 4276 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4277 | } |
| 4278 | |
| 4279 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4280 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4281 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4282 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4283 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4284 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4285 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4286 | unsigned long recovery = mddev->recovery; |
| 4287 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4288 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4289 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4290 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4291 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4292 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4293 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4294 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4295 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4296 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4297 | type = "check"; |
| 4298 | else |
| 4299 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4300 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4301 | type = "recover"; |
NeilBrown | 985ca97 | 2015-07-06 12:26:57 +1000 | [diff] [blame] | 4302 | else if (mddev->reshape_position != MaxSector) |
| 4303 | type = "reshape"; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4304 | } |
| 4305 | return sprintf(page, "%s\n", type); |
| 4306 | } |
| 4307 | |
| 4308 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4309 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4310 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4311 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4312 | return -EINVAL; |
| 4313 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4314 | |
| 4315 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4316 | if (cmd_match(page, "frozen")) |
| 4317 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4318 | else |
| 4319 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4320 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4321 | mddev_lock(mddev) == 0) { |
| 4322 | flush_workqueue(md_misc_wq); |
| 4323 | if (mddev->sync_thread) { |
| 4324 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4325 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4326 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4327 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4328 | } |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame^] | 4329 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4330 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4331 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4332 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4333 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4334 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4335 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4336 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4337 | int err; |
| 4338 | if (mddev->pers->start_reshape == NULL) |
| 4339 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4340 | err = mddev_lock(mddev); |
| 4341 | if (!err) { |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame^] | 4342 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4343 | err = -EBUSY; |
| 4344 | else { |
| 4345 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4346 | err = mddev->pers->start_reshape(mddev); |
| 4347 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4348 | mddev_unlock(mddev); |
| 4349 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4350 | if (err) |
| 4351 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4352 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4353 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4354 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4355 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4356 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4357 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4358 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4359 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4360 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4361 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4362 | if (mddev->ro == 2) { |
| 4363 | /* A write to sync_action is enough to justify |
| 4364 | * canceling read-auto mode |
| 4365 | */ |
| 4366 | mddev->ro = 0; |
| 4367 | md_wakeup_thread(mddev->sync_thread); |
| 4368 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4369 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4370 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4371 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4372 | return len; |
| 4373 | } |
| 4374 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4375 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4376 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4377 | |
| 4378 | static ssize_t |
| 4379 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4380 | { |
| 4381 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4382 | } |
| 4383 | |
| 4384 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4385 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4386 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4387 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4388 | { |
| 4389 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4390 | (unsigned long long) |
| 4391 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4392 | } |
| 4393 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4394 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4395 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4396 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4397 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4398 | { |
| 4399 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4400 | mddev->sync_speed_min ? "local": "system"); |
| 4401 | } |
| 4402 | |
| 4403 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4404 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4405 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4406 | unsigned int min; |
| 4407 | int rv; |
| 4408 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4409 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4410 | min = 0; |
| 4411 | } else { |
| 4412 | rv = kstrtouint(buf, 10, &min); |
| 4413 | if (rv < 0) |
| 4414 | return rv; |
| 4415 | if (min == 0) |
| 4416 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4417 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4418 | mddev->sync_speed_min = min; |
| 4419 | return len; |
| 4420 | } |
| 4421 | |
| 4422 | static struct md_sysfs_entry md_sync_min = |
| 4423 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4424 | |
| 4425 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4426 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4427 | { |
| 4428 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4429 | mddev->sync_speed_max ? "local": "system"); |
| 4430 | } |
| 4431 | |
| 4432 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4433 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4434 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4435 | unsigned int max; |
| 4436 | int rv; |
| 4437 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4438 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4439 | max = 0; |
| 4440 | } else { |
| 4441 | rv = kstrtouint(buf, 10, &max); |
| 4442 | if (rv < 0) |
| 4443 | return rv; |
| 4444 | if (max == 0) |
| 4445 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4446 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4447 | mddev->sync_speed_max = max; |
| 4448 | return len; |
| 4449 | } |
| 4450 | |
| 4451 | static struct md_sysfs_entry md_sync_max = |
| 4452 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4453 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4454 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4455 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4456 | { |
| 4457 | return sprintf(page, "%d\n", mddev->degraded); |
| 4458 | } |
| 4459 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4460 | |
| 4461 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4462 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4463 | { |
| 4464 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4465 | } |
| 4466 | |
| 4467 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4468 | 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] | 4469 | { |
| 4470 | long n; |
| 4471 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4472 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4473 | return -EINVAL; |
| 4474 | |
| 4475 | if (n != 0 && n != 1) |
| 4476 | return -EINVAL; |
| 4477 | |
| 4478 | mddev->parallel_resync = n; |
| 4479 | |
| 4480 | if (mddev->sync_thread) |
| 4481 | wake_up(&resync_wait); |
| 4482 | |
| 4483 | return len; |
| 4484 | } |
| 4485 | |
| 4486 | /* force parallel resync, even with shared block devices */ |
| 4487 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4488 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4489 | sync_force_parallel_show, sync_force_parallel_store); |
| 4490 | |
| 4491 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4492 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4493 | { |
| 4494 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4495 | if (mddev->curr_resync == 0) |
| 4496 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4497 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4498 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4499 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4500 | db = resync - mddev->resync_mark_cnt; |
| 4501 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4502 | } |
| 4503 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4504 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4505 | |
| 4506 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4507 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4508 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4509 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4510 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4511 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4512 | return sprintf(page, "none\n"); |
| 4513 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4514 | if (mddev->curr_resync == 1 || |
| 4515 | mddev->curr_resync == 2) |
| 4516 | return sprintf(page, "delayed\n"); |
| 4517 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4518 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4519 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4520 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4521 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4522 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4523 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4524 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4525 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4526 | } |
| 4527 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4528 | static struct md_sysfs_entry md_sync_completed = |
| 4529 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4530 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4531 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4532 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4533 | { |
| 4534 | return sprintf(page, "%llu\n", |
| 4535 | (unsigned long long)mddev->resync_min); |
| 4536 | } |
| 4537 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4538 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4539 | { |
| 4540 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4541 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4542 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4543 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4544 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4545 | |
| 4546 | spin_lock(&mddev->lock); |
| 4547 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4548 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4549 | goto out_unlock; |
| 4550 | |
| 4551 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4552 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4553 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4554 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 4555 | /* Round down to multiple of 4K for safety */ |
| 4556 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4557 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4558 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4559 | out_unlock: |
| 4560 | spin_unlock(&mddev->lock); |
| 4561 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | static struct md_sysfs_entry md_min_sync = |
| 4565 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4566 | |
| 4567 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4568 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4569 | { |
| 4570 | if (mddev->resync_max == MaxSector) |
| 4571 | return sprintf(page, "max\n"); |
| 4572 | else |
| 4573 | return sprintf(page, "%llu\n", |
| 4574 | (unsigned long long)mddev->resync_max); |
| 4575 | } |
| 4576 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4577 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4578 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4579 | int err; |
| 4580 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4581 | if (strncmp(buf, "max", 3) == 0) |
| 4582 | mddev->resync_max = MaxSector; |
| 4583 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4584 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4585 | int chunk; |
| 4586 | |
| 4587 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4588 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4589 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4590 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4591 | goto out_unlock; |
| 4592 | |
| 4593 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4594 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4595 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4596 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4597 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4598 | |
| 4599 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4600 | chunk = mddev->chunk_sectors; |
| 4601 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4602 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4603 | |
| 4604 | err = -EINVAL; |
| 4605 | if (sector_div(temp, chunk)) |
| 4606 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4607 | } |
| 4608 | mddev->resync_max = max; |
| 4609 | } |
| 4610 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4611 | err = 0; |
| 4612 | out_unlock: |
| 4613 | spin_unlock(&mddev->lock); |
| 4614 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | static struct md_sysfs_entry md_max_sync = |
| 4618 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4619 | |
| 4620 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4621 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4622 | { |
| 4623 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4624 | } |
| 4625 | |
| 4626 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4627 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4628 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4629 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4630 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4631 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4632 | err = kstrtoull(buf, 10, &new); |
| 4633 | if (err < 0) |
| 4634 | return err; |
| 4635 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4636 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4637 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4638 | err = mddev_lock(mddev); |
| 4639 | if (err) |
| 4640 | return err; |
| 4641 | err = -EINVAL; |
| 4642 | if (mddev->pers == NULL || |
| 4643 | mddev->pers->quiesce == NULL) |
| 4644 | goto unlock; |
| 4645 | old = mddev->suspend_lo; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4646 | mddev->suspend_lo = new; |
| 4647 | if (new >= old) |
| 4648 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4649 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4650 | else { |
| 4651 | /* Expanding suspended region - need to wait */ |
| 4652 | mddev->pers->quiesce(mddev, 1); |
| 4653 | mddev->pers->quiesce(mddev, 0); |
| 4654 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4655 | err = 0; |
| 4656 | unlock: |
| 4657 | mddev_unlock(mddev); |
| 4658 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4659 | } |
| 4660 | static struct md_sysfs_entry md_suspend_lo = |
| 4661 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4662 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4663 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4664 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4665 | { |
| 4666 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4667 | } |
| 4668 | |
| 4669 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4670 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4671 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4672 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4673 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4674 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4675 | err = kstrtoull(buf, 10, &new); |
| 4676 | if (err < 0) |
| 4677 | return err; |
| 4678 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4679 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4680 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4681 | err = mddev_lock(mddev); |
| 4682 | if (err) |
| 4683 | return err; |
| 4684 | err = -EINVAL; |
| 4685 | if (mddev->pers == NULL || |
| 4686 | mddev->pers->quiesce == NULL) |
| 4687 | goto unlock; |
| 4688 | old = mddev->suspend_hi; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4689 | mddev->suspend_hi = new; |
| 4690 | if (new <= old) |
| 4691 | /* Shrinking suspended region */ |
| 4692 | mddev->pers->quiesce(mddev, 2); |
| 4693 | else { |
| 4694 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4695 | mddev->pers->quiesce(mddev, 1); |
| 4696 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4697 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4698 | err = 0; |
| 4699 | unlock: |
| 4700 | mddev_unlock(mddev); |
| 4701 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4702 | } |
| 4703 | static struct md_sysfs_entry md_suspend_hi = |
| 4704 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4705 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4706 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4707 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4708 | { |
| 4709 | if (mddev->reshape_position != MaxSector) |
| 4710 | return sprintf(page, "%llu\n", |
| 4711 | (unsigned long long)mddev->reshape_position); |
| 4712 | strcpy(page, "none\n"); |
| 4713 | return 5; |
| 4714 | } |
| 4715 | |
| 4716 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4717 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4718 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4719 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4720 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4721 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4722 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4723 | err = kstrtoull(buf, 10, &new); |
| 4724 | if (err < 0) |
| 4725 | return err; |
| 4726 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4727 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4728 | err = mddev_lock(mddev); |
| 4729 | if (err) |
| 4730 | return err; |
| 4731 | err = -EBUSY; |
| 4732 | if (mddev->pers) |
| 4733 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4734 | mddev->reshape_position = new; |
| 4735 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4736 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4737 | mddev->new_level = mddev->level; |
| 4738 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4739 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4740 | rdev_for_each(rdev, mddev) |
| 4741 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4742 | err = 0; |
| 4743 | unlock: |
| 4744 | mddev_unlock(mddev); |
| 4745 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | static struct md_sysfs_entry md_reshape_position = |
| 4749 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4750 | reshape_position_store); |
| 4751 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4752 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4753 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4754 | { |
| 4755 | return sprintf(page, "%s\n", |
| 4756 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4757 | } |
| 4758 | |
| 4759 | static ssize_t |
| 4760 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4761 | { |
| 4762 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4763 | int err; |
| 4764 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4765 | if (cmd_match(buf, "forwards")) |
| 4766 | backwards = 0; |
| 4767 | else if (cmd_match(buf, "backwards")) |
| 4768 | backwards = 1; |
| 4769 | else |
| 4770 | return -EINVAL; |
| 4771 | if (mddev->reshape_backwards == backwards) |
| 4772 | return len; |
| 4773 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4774 | err = mddev_lock(mddev); |
| 4775 | if (err) |
| 4776 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4777 | /* check if we are allowed to change */ |
| 4778 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4779 | err = -EBUSY; |
| 4780 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4781 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4782 | err = -EINVAL; |
| 4783 | else |
| 4784 | mddev->reshape_backwards = backwards; |
| 4785 | mddev_unlock(mddev); |
| 4786 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4787 | } |
| 4788 | |
| 4789 | static struct md_sysfs_entry md_reshape_direction = |
| 4790 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4791 | reshape_direction_store); |
| 4792 | |
| 4793 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4794 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4795 | { |
| 4796 | if (mddev->external_size) |
| 4797 | return sprintf(page, "%llu\n", |
| 4798 | (unsigned long long)mddev->array_sectors/2); |
| 4799 | else |
| 4800 | return sprintf(page, "default\n"); |
| 4801 | } |
| 4802 | |
| 4803 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4804 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4805 | { |
| 4806 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4807 | int err; |
| 4808 | |
| 4809 | err = mddev_lock(mddev); |
| 4810 | if (err) |
| 4811 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4812 | |
| 4813 | if (strncmp(buf, "default", 7) == 0) { |
| 4814 | if (mddev->pers) |
| 4815 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4816 | else |
| 4817 | sectors = mddev->array_sectors; |
| 4818 | |
| 4819 | mddev->external_size = 0; |
| 4820 | } else { |
| 4821 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4822 | err = -EINVAL; |
| 4823 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 4824 | err = -E2BIG; |
| 4825 | else |
| 4826 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4827 | } |
| 4828 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4829 | if (!err) { |
| 4830 | mddev->array_sectors = sectors; |
| 4831 | if (mddev->pers) { |
| 4832 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4833 | revalidate_disk(mddev->gendisk); |
| 4834 | } |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4835 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4836 | mddev_unlock(mddev); |
| 4837 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4838 | } |
| 4839 | |
| 4840 | static struct md_sysfs_entry md_array_size = |
| 4841 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4842 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4843 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4844 | static struct attribute *md_default_attrs[] = { |
| 4845 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4846 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4847 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4848 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4849 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4850 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4851 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4852 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4853 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4854 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4855 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4856 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4857 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4858 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4859 | NULL, |
| 4860 | }; |
| 4861 | |
| 4862 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4863 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4864 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4865 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4866 | &md_sync_min.attr, |
| 4867 | &md_sync_max.attr, |
| 4868 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4869 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4870 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4871 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4872 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4873 | &md_suspend_lo.attr, |
| 4874 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4875 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4876 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4877 | NULL, |
| 4878 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4879 | static struct attribute_group md_redundancy_group = { |
| 4880 | .name = NULL, |
| 4881 | .attrs = md_redundancy_attrs, |
| 4882 | }; |
| 4883 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4884 | static ssize_t |
| 4885 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4886 | { |
| 4887 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4888 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4889 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4890 | |
| 4891 | if (!entry->show) |
| 4892 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4893 | spin_lock(&all_mddevs_lock); |
| 4894 | if (list_empty(&mddev->all_mddevs)) { |
| 4895 | spin_unlock(&all_mddevs_lock); |
| 4896 | return -EBUSY; |
| 4897 | } |
| 4898 | mddev_get(mddev); |
| 4899 | spin_unlock(&all_mddevs_lock); |
| 4900 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4901 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4902 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4903 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | static ssize_t |
| 4907 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4908 | const char *page, size_t length) |
| 4909 | { |
| 4910 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4911 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4912 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4913 | |
| 4914 | if (!entry->store) |
| 4915 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4916 | if (!capable(CAP_SYS_ADMIN)) |
| 4917 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4918 | spin_lock(&all_mddevs_lock); |
| 4919 | if (list_empty(&mddev->all_mddevs)) { |
| 4920 | spin_unlock(&all_mddevs_lock); |
| 4921 | return -EBUSY; |
| 4922 | } |
| 4923 | mddev_get(mddev); |
| 4924 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4925 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4926 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4927 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4928 | } |
| 4929 | |
| 4930 | static void md_free(struct kobject *ko) |
| 4931 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4932 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4933 | |
| 4934 | if (mddev->sysfs_state) |
| 4935 | sysfs_put(mddev->sysfs_state); |
| 4936 | |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 4937 | if (mddev->queue) |
| 4938 | blk_cleanup_queue(mddev->queue); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4939 | if (mddev->gendisk) { |
| 4940 | del_gendisk(mddev->gendisk); |
| 4941 | put_disk(mddev->gendisk); |
| 4942 | } |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4943 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4944 | kfree(mddev); |
| 4945 | } |
| 4946 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4947 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4948 | .show = md_attr_show, |
| 4949 | .store = md_attr_store, |
| 4950 | }; |
| 4951 | static struct kobj_type md_ktype = { |
| 4952 | .release = md_free, |
| 4953 | .sysfs_ops = &md_sysfs_ops, |
| 4954 | .default_attrs = md_default_attrs, |
| 4955 | }; |
| 4956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | int mdp_major = 0; |
| 4958 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4959 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4960 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4961 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4962 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4963 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4964 | kobject_del(&mddev->kobj); |
| 4965 | kobject_put(&mddev->kobj); |
| 4966 | } |
| 4967 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4968 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4970 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4971 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4972 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4973 | int partitioned; |
| 4974 | int shift; |
| 4975 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4976 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | |
| 4978 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4979 | return -ENODEV; |
| 4980 | |
| 4981 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4982 | shift = partitioned ? MdpMinorShift : 0; |
| 4983 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4984 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4985 | /* wait for any previous instance of this device to be |
| 4986 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4987 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4988 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4989 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4990 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4991 | error = -EEXIST; |
| 4992 | if (mddev->gendisk) |
| 4993 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4994 | |
| 4995 | if (name) { |
| 4996 | /* Need to ensure that 'name' is not a duplicate. |
| 4997 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4998 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4999 | spin_lock(&all_mddevs_lock); |
| 5000 | |
| 5001 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 5002 | if (mddev2->gendisk && |
| 5003 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 5004 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5005 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5006 | } |
| 5007 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5009 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5010 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5011 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5012 | if (!mddev->queue) |
| 5013 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5014 | mddev->queue->queuedata = mddev; |
| 5015 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5016 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 5017 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | disk = alloc_disk(1 << shift); |
| 5020 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5021 | blk_cleanup_queue(mddev->queue); |
| 5022 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5023 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5024 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5025 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5026 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5027 | if (name) |
| 5028 | strcpy(disk->disk_name, name); |
| 5029 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5031 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5032 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | disk->fops = &md_fops; |
| 5034 | disk->private_data = mddev; |
| 5035 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5036 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5037 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5038 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5039 | * remove it now. |
| 5040 | */ |
| 5041 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5042 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5043 | /* As soon as we call add_disk(), another thread could get |
| 5044 | * through to md_open, so make sure it doesn't get too far |
| 5045 | */ |
| 5046 | mutex_lock(&mddev->open_mutex); |
| 5047 | add_disk(disk); |
| 5048 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 5049 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 5050 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5051 | if (error) { |
| 5052 | /* This isn't possible, but as kobject_init_and_add is marked |
| 5053 | * __must_check, we must do something with the result |
| 5054 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5055 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 5056 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5057 | error = 0; |
| 5058 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5059 | if (mddev->kobj.sd && |
| 5060 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5061 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5062 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5063 | abort: |
| 5064 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5065 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5066 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5067 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5068 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5069 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5070 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5071 | } |
| 5072 | |
| 5073 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 5074 | { |
| 5075 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | return NULL; |
| 5077 | } |
| 5078 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5079 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 5080 | { |
| 5081 | /* val must be "md_*" where * is not all digits. |
| 5082 | * We allocate an array with a large free minor number, and |
| 5083 | * set the name to val. val must not already be an active name. |
| 5084 | */ |
| 5085 | int len = strlen(val); |
| 5086 | char buf[DISK_NAME_LEN]; |
| 5087 | |
| 5088 | while (len && val[len-1] == '\n') |
| 5089 | len--; |
| 5090 | if (len >= DISK_NAME_LEN) |
| 5091 | return -E2BIG; |
| 5092 | strlcpy(buf, val, len+1); |
| 5093 | if (strncmp(buf, "md_", 3) != 0) |
| 5094 | return -EINVAL; |
| 5095 | return md_alloc(0, buf); |
| 5096 | } |
| 5097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5098 | static void md_safemode_timeout(unsigned long data) |
| 5099 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5100 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5101 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5102 | if (!atomic_read(&mddev->writes_pending)) { |
| 5103 | mddev->safemode = 1; |
| 5104 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5105 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | md_wakeup_thread(mddev->thread); |
| 5108 | } |
| 5109 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5110 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5112 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5114 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5115 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5116 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5118 | if (list_empty(&mddev->disks)) |
| 5119 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | |
| 5122 | if (mddev->pers) |
| 5123 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5124 | /* Cannot run until previous stop completes properly */ |
| 5125 | if (mddev->sysfs_active) |
| 5126 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | /* |
| 5129 | * Analyze all RAID superblock(s) |
| 5130 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5131 | if (!mddev->raid_disks) { |
| 5132 | if (!mddev->persistent) |
| 5133 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5134 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5137 | if (mddev->level != LEVEL_NONE) |
| 5138 | request_module("md-level-%d", mddev->level); |
| 5139 | else if (mddev->clevel[0]) |
| 5140 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | |
| 5142 | /* |
| 5143 | * Drop all container device buffers, from now on |
| 5144 | * the only valid external interface is through the md |
| 5145 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5146 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5147 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5148 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | continue; |
| 5150 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5151 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5152 | |
| 5153 | /* perform some consistency tests on the device. |
| 5154 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5155 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5156 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5157 | if (rdev->meta_bdev) { |
| 5158 | /* Nothing to check */; |
| 5159 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5160 | if (mddev->dev_sectors && |
| 5161 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5162 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5163 | printk("md: %s: data overlaps metadata\n", |
| 5164 | mdname(mddev)); |
| 5165 | return -EINVAL; |
| 5166 | } |
| 5167 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5168 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5169 | > rdev->data_offset) { |
| 5170 | printk("md: %s: metadata overlaps data\n", |
| 5171 | mdname(mddev)); |
| 5172 | return -EINVAL; |
| 5173 | } |
| 5174 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5175 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | } |
| 5177 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5178 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 5179 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5182 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5183 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5185 | if (mddev->level != LEVEL_NONE) |
| 5186 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5187 | mddev->level); |
| 5188 | else |
| 5189 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5190 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | return -EINVAL; |
| 5192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5193 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5194 | if (mddev->level != pers->level) { |
| 5195 | mddev->level = pers->level; |
| 5196 | mddev->new_level = pers->level; |
| 5197 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5198 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5200 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5201 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5202 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5203 | module_put(pers->owner); |
| 5204 | return -EINVAL; |
| 5205 | } |
| 5206 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5207 | if (pers->sync_request) { |
| 5208 | /* Warn if this is a potentially silly |
| 5209 | * configuration. |
| 5210 | */ |
| 5211 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5212 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5213 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5214 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5215 | rdev_for_each(rdev, mddev) |
| 5216 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5217 | if (rdev < rdev2 && |
| 5218 | rdev->bdev->bd_contains == |
| 5219 | rdev2->bdev->bd_contains) { |
| 5220 | printk(KERN_WARNING |
| 5221 | "%s: WARNING: %s appears to be" |
| 5222 | " on the same physical disk as" |
| 5223 | " %s.\n", |
| 5224 | mdname(mddev), |
| 5225 | bdevname(rdev->bdev,b), |
| 5226 | bdevname(rdev2->bdev,b2)); |
| 5227 | warned = 1; |
| 5228 | } |
| 5229 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5230 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5231 | if (warned) |
| 5232 | printk(KERN_WARNING |
| 5233 | "True protection against single-disk" |
| 5234 | " failure might be compromised.\n"); |
| 5235 | } |
| 5236 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5237 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5238 | /* may be over-ridden by personality */ |
| 5239 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5240 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5241 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5243 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5244 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5245 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5246 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5247 | if (err) |
| 5248 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5249 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5250 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5251 | " but 'external_size' not in effect?\n", __func__); |
| 5252 | printk(KERN_ERR |
| 5253 | "md: invalid array_size %llu > default size %llu\n", |
| 5254 | (unsigned long long)mddev->array_sectors / 2, |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5255 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5256 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5257 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5258 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5259 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5260 | struct bitmap *bitmap; |
| 5261 | |
| 5262 | bitmap = bitmap_create(mddev, -1); |
| 5263 | if (IS_ERR(bitmap)) { |
| 5264 | err = PTR_ERR(bitmap); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5265 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5266 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5267 | } else |
| 5268 | mddev->bitmap = bitmap; |
| 5269 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5271 | if (err) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5272 | mddev_detach(mddev); |
NeilBrown | 0c35bd4 | 2015-03-13 11:51:18 +1100 | [diff] [blame] | 5273 | if (mddev->private) |
| 5274 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5275 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5276 | module_put(pers->owner); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5277 | bitmap_destroy(mddev); |
| 5278 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5280 | if (mddev->queue) { |
| 5281 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5282 | mddev->queue->backing_dev_info.congested_fn = md_congested; |
| 5283 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5284 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5285 | if (mddev->kobj.sd && |
| 5286 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5287 | printk(KERN_WARNING |
| 5288 | "md: cannot register extra attributes for %s\n", |
| 5289 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5290 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5291 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5292 | mddev->ro = 0; |
| 5293 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5294 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5295 | atomic_set(&mddev->max_corr_read_errors, |
| 5296 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | mddev->safemode = 0; |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5298 | if (mddev_is_clustered(mddev)) |
| 5299 | mddev->safemode_delay = 0; |
| 5300 | else |
| 5301 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5303 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5304 | spin_lock(&mddev->lock); |
| 5305 | mddev->pers = pers; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5306 | mddev->ready = 1; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5307 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5308 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5309 | if (rdev->raid_disk >= 0) |
| 5310 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5311 | /* failure here is OK */; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5312 | |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 5313 | if (mddev->degraded && !mddev->ro) |
| 5314 | /* This ensures that recovering status is reported immediately |
| 5315 | * via sysfs - until a lack of spares is confirmed. |
| 5316 | */ |
| 5317 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5319 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5320 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5321 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5323 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5324 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5325 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5326 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | return 0; |
| 5328 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5329 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5330 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5331 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5332 | { |
| 5333 | int err; |
| 5334 | |
| 5335 | err = md_run(mddev); |
| 5336 | if (err) |
| 5337 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5338 | err = bitmap_load(mddev); |
| 5339 | if (err) { |
| 5340 | bitmap_destroy(mddev); |
| 5341 | goto out; |
| 5342 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5343 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5344 | if (mddev_is_clustered(mddev)) |
| 5345 | md_allow_write(mddev); |
| 5346 | |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5347 | md_wakeup_thread(mddev->thread); |
| 5348 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5349 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5350 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5351 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5352 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5353 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5354 | out: |
| 5355 | return err; |
| 5356 | } |
| 5357 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5358 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5359 | { |
| 5360 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5361 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5362 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5363 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5364 | return -ENXIO; |
| 5365 | if (!mddev->pers) |
| 5366 | return -EINVAL; |
| 5367 | if (!mddev->ro) |
| 5368 | return -EBUSY; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 5369 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 5370 | struct md_rdev *rdev; |
| 5371 | bool has_journal = false; |
| 5372 | |
| 5373 | rcu_read_lock(); |
| 5374 | rdev_for_each_rcu(rdev, mddev) { |
| 5375 | if (test_bit(Journal, &rdev->flags) && |
| 5376 | !test_bit(Faulty, &rdev->flags)) { |
| 5377 | has_journal = true; |
| 5378 | break; |
| 5379 | } |
| 5380 | } |
| 5381 | rcu_read_unlock(); |
| 5382 | |
| 5383 | /* Don't restart rw with journal missing/faulty */ |
| 5384 | if (!has_journal) |
| 5385 | return -EINVAL; |
| 5386 | } |
| 5387 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5388 | mddev->safemode = 0; |
| 5389 | mddev->ro = 0; |
| 5390 | set_disk_ro(disk, 0); |
| 5391 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5392 | mdname(mddev)); |
| 5393 | /* Kick recovery or resync if necessary */ |
| 5394 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5395 | md_wakeup_thread(mddev->thread); |
| 5396 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5397 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5398 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | } |
| 5400 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5401 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5402 | { |
| 5403 | mddev->array_sectors = 0; |
| 5404 | mddev->external_size = 0; |
| 5405 | mddev->dev_sectors = 0; |
| 5406 | mddev->raid_disks = 0; |
| 5407 | mddev->recovery_cp = 0; |
| 5408 | mddev->resync_min = 0; |
| 5409 | mddev->resync_max = MaxSector; |
| 5410 | mddev->reshape_position = MaxSector; |
| 5411 | mddev->external = 0; |
| 5412 | mddev->persistent = 0; |
| 5413 | mddev->level = LEVEL_NONE; |
| 5414 | mddev->clevel[0] = 0; |
| 5415 | mddev->flags = 0; |
| 5416 | mddev->ro = 0; |
| 5417 | mddev->metadata_type[0] = 0; |
| 5418 | mddev->chunk_sectors = 0; |
| 5419 | mddev->ctime = mddev->utime = 0; |
| 5420 | mddev->layout = 0; |
| 5421 | mddev->max_disks = 0; |
| 5422 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5423 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5424 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5425 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5426 | mddev->new_level = LEVEL_NONE; |
| 5427 | mddev->new_layout = 0; |
| 5428 | mddev->new_chunk_sectors = 0; |
| 5429 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5430 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5431 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5432 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5433 | mddev->recovery = 0; |
| 5434 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5435 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5436 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5437 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5438 | mddev->private = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5439 | mddev->bitmap_info.offset = 0; |
| 5440 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5441 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5442 | mddev->bitmap_info.chunksize = 0; |
| 5443 | mddev->bitmap_info.daemon_sleep = 0; |
| 5444 | mddev->bitmap_info.max_write_behind = 0; |
| 5445 | } |
| 5446 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5447 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5448 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 5449 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5450 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5451 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5452 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5453 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5454 | } |
| 5455 | |
| 5456 | del_timer_sync(&mddev->safemode_timer); |
| 5457 | |
| 5458 | bitmap_flush(mddev); |
| 5459 | md_super_wait(mddev); |
| 5460 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5461 | if (mddev->ro == 0 && |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5462 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
| 5463 | (mddev->flags & MD_UPDATE_SB_FLAGS))) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5464 | /* mark array as shutdown cleanly */ |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5465 | if (!mddev_is_clustered(mddev)) |
| 5466 | mddev->in_sync = 1; |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5467 | md_update_sb(mddev, 1); |
| 5468 | } |
| 5469 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5470 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5471 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5472 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 5473 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5474 | __md_stop_writes(mddev); |
| 5475 | mddev_unlock(mddev); |
| 5476 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5477 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5478 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5479 | static void mddev_detach(struct mddev *mddev) |
| 5480 | { |
| 5481 | struct bitmap *bitmap = mddev->bitmap; |
| 5482 | /* wait for behind writes to complete */ |
| 5483 | if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { |
| 5484 | printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n", |
| 5485 | mdname(mddev)); |
| 5486 | /* need to kick something here to make sure I/O goes? */ |
| 5487 | wait_event(bitmap->behind_wait, |
| 5488 | atomic_read(&bitmap->behind_writes) == 0); |
| 5489 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5490 | if (mddev->pers && mddev->pers->quiesce) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5491 | mddev->pers->quiesce(mddev, 1); |
| 5492 | mddev->pers->quiesce(mddev, 0); |
| 5493 | } |
| 5494 | md_unregister_thread(&mddev->thread); |
| 5495 | if (mddev->queue) |
| 5496 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 5497 | } |
| 5498 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5499 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5500 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5501 | struct md_personality *pers = mddev->pers; |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5502 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 5503 | /* Ensure ->event_work is done */ |
| 5504 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5505 | spin_lock(&mddev->lock); |
| 5506 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5507 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5508 | spin_unlock(&mddev->lock); |
| 5509 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5510 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5511 | if (pers->sync_request && mddev->to_remove == NULL) |
| 5512 | mddev->to_remove = &md_redundancy_group; |
| 5513 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5514 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5515 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5516 | |
| 5517 | void md_stop(struct mddev *mddev) |
| 5518 | { |
| 5519 | /* stop the array and free an attached data structures. |
| 5520 | * This is called from dm-raid |
| 5521 | */ |
| 5522 | __md_stop(mddev); |
| 5523 | bitmap_destroy(mddev); |
| 5524 | if (mddev->bio_set) |
| 5525 | bioset_free(mddev->bio_set); |
| 5526 | } |
| 5527 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5528 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5529 | |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5530 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5531 | { |
| 5532 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5533 | int did_freeze = 0; |
| 5534 | |
| 5535 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5536 | did_freeze = 1; |
| 5537 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5538 | md_wakeup_thread(mddev->thread); |
| 5539 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5540 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5541 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5542 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5543 | /* Thread might be blocked waiting for metadata update |
| 5544 | * which will now never happen */ |
| 5545 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5546 | |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5547 | if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
| 5548 | return -EBUSY; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5549 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5550 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 5551 | &mddev->recovery)); |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5552 | wait_event(mddev->sb_wait, |
| 5553 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5554 | mddev_lock_nointr(mddev); |
| 5555 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5556 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5557 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5558 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5559 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5560 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5561 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5562 | if (did_freeze) { |
| 5563 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5564 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5565 | md_wakeup_thread(mddev->thread); |
| 5566 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5567 | err = -EBUSY; |
| 5568 | goto out; |
| 5569 | } |
| 5570 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5571 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5572 | |
| 5573 | err = -ENXIO; |
| 5574 | if (mddev->ro==1) |
| 5575 | goto out; |
| 5576 | mddev->ro = 1; |
| 5577 | set_disk_ro(mddev->gendisk, 1); |
| 5578 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5579 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5580 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5581 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5582 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5583 | } |
| 5584 | out: |
| 5585 | mutex_unlock(&mddev->open_mutex); |
| 5586 | return err; |
| 5587 | } |
| 5588 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5589 | /* mode: |
| 5590 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5591 | * 2 - stop but do not disassemble array |
| 5592 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5593 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5594 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5595 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5596 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5597 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5598 | int did_freeze = 0; |
| 5599 | |
| 5600 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5601 | did_freeze = 1; |
| 5602 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5603 | md_wakeup_thread(mddev->thread); |
| 5604 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5605 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5606 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5607 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5608 | /* Thread might be blocked waiting for metadata update |
| 5609 | * which will now never happen */ |
| 5610 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5611 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5612 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5613 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 5614 | !test_bit(MD_RECOVERY_RUNNING, |
| 5615 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5616 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5617 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5618 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5619 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5620 | mddev->sysfs_active || |
| 5621 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5622 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5623 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5624 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5625 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5626 | if (did_freeze) { |
| 5627 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5628 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5629 | md_wakeup_thread(mddev->thread); |
| 5630 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 5631 | return -EBUSY; |
| 5632 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5633 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5634 | if (mddev->ro) |
| 5635 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5636 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5637 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5638 | __md_stop(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5639 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5640 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5641 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5642 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5643 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5644 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5645 | if (rdev->raid_disk >= 0) |
| 5646 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5647 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5648 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5649 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5650 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5651 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5652 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5653 | if (mddev->ro) |
| 5654 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5655 | } else |
| 5656 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5657 | /* |
| 5658 | * Free resources if final stop |
| 5659 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5660 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5661 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5662 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5663 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5664 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5665 | struct file *f = mddev->bitmap_info.file; |
| 5666 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5667 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5668 | spin_unlock(&mddev->lock); |
| 5669 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5670 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5671 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5673 | export_array(mddev); |
| 5674 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5675 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5676 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5677 | if (mddev->hold_active == UNTIL_STOP) |
| 5678 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5679 | } |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5680 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5681 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5682 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5683 | } |
| 5684 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5685 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5686 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5688 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5689 | int err; |
| 5690 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5691 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5693 | |
| 5694 | printk(KERN_INFO "md: running: "); |
| 5695 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5696 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | char b[BDEVNAME_SIZE]; |
| 5698 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5699 | } |
| 5700 | printk("\n"); |
| 5701 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5702 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5703 | if (err) { |
| 5704 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5705 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | } |
| 5707 | } |
| 5708 | |
| 5709 | /* |
| 5710 | * lets try to run arrays based on all disks that have arrived |
| 5711 | * until now. (those are in pending_raid_disks) |
| 5712 | * |
| 5713 | * the method: pick the first pending disk, collect all disks with |
| 5714 | * the same UUID, remove all from the pending list and put them into |
| 5715 | * the 'same_array' list. Then order this list based on superblock |
| 5716 | * update time (freshest comes first), kick out 'old' disks and |
| 5717 | * compare superblocks. If everything's fine then run it. |
| 5718 | * |
| 5719 | * If "unit" is allocated, then bump its reference count |
| 5720 | */ |
| 5721 | static void autorun_devices(int part) |
| 5722 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5723 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5724 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5725 | char b[BDEVNAME_SIZE]; |
| 5726 | |
| 5727 | printk(KERN_INFO "md: autorun ...\n"); |
| 5728 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5729 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5731 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5732 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5733 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5734 | |
| 5735 | printk(KERN_INFO "md: considering %s ...\n", |
| 5736 | bdevname(rdev0->bdev,b)); |
| 5737 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5738 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5739 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5740 | printk(KERN_INFO "md: adding %s ...\n", |
| 5741 | bdevname(rdev->bdev,b)); |
| 5742 | list_move(&rdev->same_set, &candidates); |
| 5743 | } |
| 5744 | /* |
| 5745 | * now we have a set of devices, with all of them having |
| 5746 | * mostly sane superblocks. It's time to allocate the |
| 5747 | * mddev. |
| 5748 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5749 | if (part) { |
| 5750 | dev = MKDEV(mdp_major, |
| 5751 | rdev0->preferred_minor << MdpMinorShift); |
| 5752 | unit = MINOR(dev) >> MdpMinorShift; |
| 5753 | } else { |
| 5754 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5755 | unit = MINOR(dev); |
| 5756 | } |
| 5757 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5759 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5760 | break; |
| 5761 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | |
| 5763 | md_probe(dev, NULL, NULL); |
| 5764 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5765 | if (!mddev || !mddev->gendisk) { |
| 5766 | if (mddev) |
| 5767 | mddev_put(mddev); |
| 5768 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5769 | "md: cannot allocate memory for md drive.\n"); |
| 5770 | break; |
| 5771 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5772 | if (mddev_lock(mddev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5773 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5774 | mdname(mddev)); |
| 5775 | else if (mddev->raid_disks || mddev->major_version |
| 5776 | || !list_empty(&mddev->disks)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5777 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5778 | "md: %s already running, cannot run %s\n", |
| 5779 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5780 | mddev_unlock(mddev); |
| 5781 | } else { |
| 5782 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5783 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5784 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5785 | list_del_init(&rdev->same_set); |
| 5786 | if (bind_rdev_to_array(rdev, mddev)) |
| 5787 | export_rdev(rdev); |
| 5788 | } |
| 5789 | autorun_array(mddev); |
| 5790 | mddev_unlock(mddev); |
| 5791 | } |
| 5792 | /* on success, candidates will be empty, on error |
| 5793 | * it won't... |
| 5794 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5795 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5796 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5798 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5799 | mddev_put(mddev); |
| 5800 | } |
| 5801 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5802 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5803 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5804 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5805 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5806 | { |
| 5807 | mdu_version_t ver; |
| 5808 | |
| 5809 | ver.major = MD_MAJOR_VERSION; |
| 5810 | ver.minor = MD_MINOR_VERSION; |
| 5811 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5812 | |
| 5813 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5814 | return -EFAULT; |
| 5815 | |
| 5816 | return 0; |
| 5817 | } |
| 5818 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5819 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | { |
| 5821 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5822 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5823 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5825 | nr = working = insync = failed = spare = 0; |
| 5826 | rcu_read_lock(); |
| 5827 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5829 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5830 | failed++; |
| 5831 | else { |
| 5832 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5833 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5834 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5835 | else |
| 5836 | spare++; |
| 5837 | } |
| 5838 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5839 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5840 | |
| 5841 | info.major_version = mddev->major_version; |
| 5842 | info.minor_version = mddev->minor_version; |
| 5843 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5844 | info.ctime = mddev->ctime; |
| 5845 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5846 | info.size = mddev->dev_sectors / 2; |
| 5847 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5848 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | info.nr_disks = nr; |
| 5850 | info.raid_disks = mddev->raid_disks; |
| 5851 | info.md_minor = mddev->md_minor; |
| 5852 | info.not_persistent= !mddev->persistent; |
| 5853 | |
| 5854 | info.utime = mddev->utime; |
| 5855 | info.state = 0; |
| 5856 | if (mddev->in_sync) |
| 5857 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5858 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 5859 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 5860 | if (mddev_is_clustered(mddev)) |
| 5861 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5862 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | info.working_disks = working; |
| 5864 | info.failed_disks = failed; |
| 5865 | info.spare_disks = spare; |
| 5866 | |
| 5867 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5868 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5869 | |
| 5870 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5871 | return -EFAULT; |
| 5872 | |
| 5873 | return 0; |
| 5874 | } |
| 5875 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5876 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5877 | { |
| 5878 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5879 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5880 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5881 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5882 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5883 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5884 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5885 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5886 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5887 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5888 | /* bitmap enabled */ |
| 5889 | if (mddev->bitmap_info.file) { |
| 5890 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 5891 | sizeof(file->pathname)); |
| 5892 | if (IS_ERR(ptr)) |
| 5893 | err = PTR_ERR(ptr); |
| 5894 | else |
| 5895 | memmove(file->pathname, ptr, |
| 5896 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 5897 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5898 | spin_unlock(&mddev->lock); |
| 5899 | |
| 5900 | if (err == 0 && |
| 5901 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5902 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5903 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5904 | kfree(file); |
| 5905 | return err; |
| 5906 | } |
| 5907 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5908 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | { |
| 5910 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5911 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | |
| 5913 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5914 | return -EFAULT; |
| 5915 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5916 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 5917 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | if (rdev) { |
| 5919 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5920 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5921 | info.raid_disk = rdev->raid_disk; |
| 5922 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5923 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5925 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5926 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5927 | info.state |= (1<<MD_DISK_SYNC); |
| 5928 | } |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 5929 | if (test_bit(Journal, &rdev->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 5930 | info.state |= (1<<MD_DISK_JOURNAL); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5931 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5932 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | } else { |
| 5934 | info.major = info.minor = 0; |
| 5935 | info.raid_disk = -1; |
| 5936 | info.state = (1<<MD_DISK_REMOVED); |
| 5937 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5938 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | |
| 5940 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5941 | return -EFAULT; |
| 5942 | |
| 5943 | return 0; |
| 5944 | } |
| 5945 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5946 | 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] | 5947 | { |
| 5948 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5949 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | dev_t dev = MKDEV(info->major,info->minor); |
| 5951 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5952 | if (mddev_is_clustered(mddev) && |
| 5953 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 5954 | pr_err("%s: Cannot add to clustered mddev.\n", |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5955 | mdname(mddev)); |
| 5956 | return -EINVAL; |
| 5957 | } |
| 5958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5959 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5960 | return -EOVERFLOW; |
| 5961 | |
| 5962 | if (!mddev->raid_disks) { |
| 5963 | int err; |
| 5964 | /* expecting a device which has a superblock */ |
| 5965 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5966 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5967 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | "md: md_import_device returned %ld\n", |
| 5969 | PTR_ERR(rdev)); |
| 5970 | return PTR_ERR(rdev); |
| 5971 | } |
| 5972 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5973 | struct md_rdev *rdev0 |
| 5974 | = list_entry(mddev->disks.next, |
| 5975 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5976 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5977 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5978 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5979 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5980 | "md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5981 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5982 | bdevname(rdev0->bdev,b2)); |
| 5983 | export_rdev(rdev); |
| 5984 | return -EINVAL; |
| 5985 | } |
| 5986 | } |
| 5987 | err = bind_rdev_to_array(rdev, mddev); |
| 5988 | if (err) |
| 5989 | export_rdev(rdev); |
| 5990 | return err; |
| 5991 | } |
| 5992 | |
| 5993 | /* |
| 5994 | * add_new_disk can be used once the array is assembled |
| 5995 | * to add "hot spares". They must already have a superblock |
| 5996 | * written |
| 5997 | */ |
| 5998 | if (mddev->pers) { |
| 5999 | int err; |
| 6000 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6001 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6002 | "%s: personality does not support diskops!\n", |
| 6003 | mdname(mddev)); |
| 6004 | return -EINVAL; |
| 6005 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 6006 | if (mddev->persistent) |
| 6007 | rdev = md_import_device(dev, mddev->major_version, |
| 6008 | mddev->minor_version); |
| 6009 | else |
| 6010 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6011 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6012 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6013 | "md: md_import_device returned %ld\n", |
| 6014 | PTR_ERR(rdev)); |
| 6015 | return PTR_ERR(rdev); |
| 6016 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 6017 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6018 | if (!mddev->persistent) { |
| 6019 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6020 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6021 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6022 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 6023 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6024 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6025 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 6026 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6027 | } else |
| 6028 | super_types[mddev->major_version]. |
| 6029 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6030 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 6031 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6032 | /* This was a hot-add request, but events doesn't |
| 6033 | * match, so reject it. |
| 6034 | */ |
| 6035 | export_rdev(rdev); |
| 6036 | return -EINVAL; |
| 6037 | } |
| 6038 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6039 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6040 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6041 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6042 | else |
| 6043 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6044 | |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6045 | if (info->state & (1<<MD_DISK_JOURNAL)) |
| 6046 | set_bit(Journal, &rdev->flags); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6047 | /* |
| 6048 | * check whether the device shows up in other nodes |
| 6049 | */ |
| 6050 | if (mddev_is_clustered(mddev)) { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6051 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6052 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6053 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6054 | /* --add initiated by this node */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6055 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6056 | if (err) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6057 | export_rdev(rdev); |
| 6058 | return err; |
| 6059 | } |
| 6060 | } |
| 6061 | } |
| 6062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | rdev->raid_disk = -1; |
| 6064 | err = bind_rdev_to_array(rdev, mddev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | if (err) |
| 6067 | export_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6068 | |
| 6069 | if (mddev_is_clustered(mddev)) { |
| 6070 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
| 6071 | md_cluster_ops->new_disk_ack(mddev, (err == 0)); |
| 6072 | else { |
| 6073 | if (err) |
| 6074 | md_cluster_ops->add_new_disk_cancel(mddev); |
| 6075 | else |
| 6076 | err = add_bound_rdev(rdev); |
| 6077 | } |
| 6078 | |
| 6079 | } else if (!err) |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 6080 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | return err; |
| 6083 | } |
| 6084 | |
| 6085 | /* otherwise, add_new_disk is only allowed |
| 6086 | * for major_version==0 superblocks |
| 6087 | */ |
| 6088 | if (mddev->major_version != 0) { |
| 6089 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 6090 | mdname(mddev)); |
| 6091 | return -EINVAL; |
| 6092 | } |
| 6093 | |
| 6094 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 6095 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6096 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6098 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | "md: error, md_import_device() returned %ld\n", |
| 6100 | PTR_ERR(rdev)); |
| 6101 | return PTR_ERR(rdev); |
| 6102 | } |
| 6103 | rdev->desc_nr = info->number; |
| 6104 | if (info->raid_disk < mddev->raid_disks) |
| 6105 | rdev->raid_disk = info->raid_disk; |
| 6106 | else |
| 6107 | rdev->raid_disk = -1; |
| 6108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6109 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6110 | if (info->state & (1<<MD_DISK_SYNC)) |
| 6111 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6112 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6113 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6114 | set_bit(WriteMostly, &rdev->flags); |
| 6115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | if (!mddev->persistent) { |
| 6117 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6118 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 6119 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6120 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6121 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6122 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6123 | err = bind_rdev_to_array(rdev, mddev); |
| 6124 | if (err) { |
| 6125 | export_rdev(rdev); |
| 6126 | return err; |
| 6127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | } |
| 6129 | |
| 6130 | return 0; |
| 6131 | } |
| 6132 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6133 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6134 | { |
| 6135 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6136 | struct md_rdev *rdev; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 6137 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | rdev = find_rdev(mddev, dev); |
| 6140 | if (!rdev) |
| 6141 | return -ENXIO; |
| 6142 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6143 | if (mddev_is_clustered(mddev)) |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 6144 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6145 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6146 | if (rdev->raid_disk < 0) |
| 6147 | goto kick_rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6148 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6149 | clear_bit(Blocked, &rdev->flags); |
| 6150 | remove_and_add_spares(mddev, rdev); |
| 6151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6152 | if (rdev->raid_disk >= 0) |
| 6153 | goto busy; |
| 6154 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6155 | kick_rdev: |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 6156 | if (mddev_is_clustered(mddev) && ret == 0) |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6157 | md_cluster_ops->remove_disk(mddev, rdev); |
| 6158 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6159 | md_kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6160 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6161 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6162 | |
| 6163 | return 0; |
| 6164 | busy: |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 6165 | if (mddev_is_clustered(mddev) && ret == 0) |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6166 | md_cluster_ops->metadata_update_cancel(mddev); |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6167 | |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 6168 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6169 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6170 | return -EBUSY; |
| 6171 | } |
| 6172 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6173 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6174 | { |
| 6175 | char b[BDEVNAME_SIZE]; |
| 6176 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6177 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6178 | |
| 6179 | if (!mddev->pers) |
| 6180 | return -ENODEV; |
| 6181 | |
| 6182 | if (mddev->major_version != 0) { |
| 6183 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 6184 | " version-0 superblocks.\n", |
| 6185 | mdname(mddev)); |
| 6186 | return -EINVAL; |
| 6187 | } |
| 6188 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6189 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6190 | "%s: personality does not support diskops!\n", |
| 6191 | mdname(mddev)); |
| 6192 | return -EINVAL; |
| 6193 | } |
| 6194 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6195 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6196 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6197 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6198 | "md: error, md_import_device() returned %ld\n", |
| 6199 | PTR_ERR(rdev)); |
| 6200 | return -EINVAL; |
| 6201 | } |
| 6202 | |
| 6203 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6204 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6205 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6206 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6207 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6208 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6209 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6210 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6211 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6212 | "md: can not hot-add faulty %s disk to %s!\n", |
| 6213 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6214 | err = -EINVAL; |
| 6215 | goto abort_export; |
| 6216 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6217 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6218 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6219 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 6220 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6221 | err = bind_rdev_to_array(rdev, mddev); |
| 6222 | if (err) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 6223 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6224 | |
| 6225 | /* |
| 6226 | * The rest should better be atomic, we can have disk failures |
| 6227 | * noticed in interrupt contexts ... |
| 6228 | */ |
| 6229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6230 | rdev->raid_disk = -1; |
| 6231 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6232 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6233 | /* |
| 6234 | * Kick recovery, maybe this spare has to be added to the |
| 6235 | * array immediately. |
| 6236 | */ |
| 6237 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6238 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6239 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6240 | return 0; |
| 6241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6242 | abort_export: |
| 6243 | export_rdev(rdev); |
| 6244 | return err; |
| 6245 | } |
| 6246 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6247 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6248 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6249 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6250 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6251 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 6252 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6253 | return -EBUSY; |
| 6254 | if (mddev->recovery || mddev->sync_thread) |
| 6255 | return -EBUSY; |
| 6256 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6257 | } |
| 6258 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6259 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6260 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6261 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6262 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6263 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 6264 | return -EEXIST; /* cannot add when bitmap is present */ |
| 6265 | f = fget(fd); |
| 6266 | |
| 6267 | if (f == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6268 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 6269 | mdname(mddev)); |
| 6270 | return -EBADF; |
| 6271 | } |
| 6272 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6273 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6274 | if (!S_ISREG(inode->i_mode)) { |
| 6275 | printk(KERN_ERR "%s: error: bitmap file must be a regular file\n", |
| 6276 | mdname(mddev)); |
| 6277 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6278 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6279 | printk(KERN_ERR "%s: error: bitmap file must open for write\n", |
| 6280 | mdname(mddev)); |
| 6281 | err = -EBADF; |
| 6282 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6283 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 6284 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6285 | err = -EBUSY; |
| 6286 | } |
| 6287 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6288 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6289 | return err; |
| 6290 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6291 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6292 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6293 | } else if (mddev->bitmap == NULL) |
| 6294 | return -ENOENT; /* cannot remove what isn't there */ |
| 6295 | err = 0; |
| 6296 | if (mddev->pers) { |
| 6297 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6298 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6299 | struct bitmap *bitmap; |
| 6300 | |
| 6301 | bitmap = bitmap_create(mddev, -1); |
| 6302 | if (!IS_ERR(bitmap)) { |
| 6303 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6304 | err = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6305 | } else |
| 6306 | err = PTR_ERR(bitmap); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6307 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6308 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6309 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6310 | fd = -1; /* make sure to put the file */ |
| 6311 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6312 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6313 | } |
| 6314 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6315 | struct file *f = mddev->bitmap_info.file; |
| 6316 | if (f) { |
| 6317 | spin_lock(&mddev->lock); |
| 6318 | mddev->bitmap_info.file = NULL; |
| 6319 | spin_unlock(&mddev->lock); |
| 6320 | fput(f); |
| 6321 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6322 | } |
| 6323 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6324 | return err; |
| 6325 | } |
| 6326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6327 | /* |
| 6328 | * set_array_info is used two different ways |
| 6329 | * The original usage is when creating a new array. |
| 6330 | * In this usage, raid_disks is > 0 and it together with |
| 6331 | * level, size, not_persistent,layout,chunksize determine the |
| 6332 | * shape of the array. |
| 6333 | * This will always create an array with a type-0.90.0 superblock. |
| 6334 | * The newer usage is when assembling an array. |
| 6335 | * In this case raid_disks will be 0, and the major_version field is |
| 6336 | * use to determine which style super-blocks are to be found on the devices. |
| 6337 | * The minor and patch _version numbers are also kept incase the |
| 6338 | * super_block handler wishes to interpret them. |
| 6339 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6340 | 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] | 6341 | { |
| 6342 | |
| 6343 | if (info->raid_disks == 0) { |
| 6344 | /* just setting version number for superblock loading */ |
| 6345 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6346 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6347 | super_types[info->major_version].name == NULL) { |
| 6348 | /* maybe try to auto-load a module? */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6349 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6350 | "md: superblock version %d not known\n", |
| 6351 | info->major_version); |
| 6352 | return -EINVAL; |
| 6353 | } |
| 6354 | mddev->major_version = info->major_version; |
| 6355 | mddev->minor_version = info->minor_version; |
| 6356 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6357 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6358 | /* ensure mddev_put doesn't delete this now that there |
| 6359 | * is some minimal configuration. |
| 6360 | */ |
| 6361 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6362 | return 0; |
| 6363 | } |
| 6364 | mddev->major_version = MD_MAJOR_VERSION; |
| 6365 | mddev->minor_version = MD_MINOR_VERSION; |
| 6366 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 6367 | mddev->ctime = get_seconds(); |
| 6368 | |
| 6369 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6370 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6371 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6372 | mddev->raid_disks = info->raid_disks; |
| 6373 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6374 | * openned |
| 6375 | */ |
| 6376 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6377 | mddev->recovery_cp = MaxSector; |
| 6378 | else |
| 6379 | mddev->recovery_cp = 0; |
| 6380 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6381 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6382 | |
| 6383 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6384 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6385 | |
| 6386 | mddev->max_disks = MD_SB_DISKS; |
| 6387 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6388 | if (mddev->persistent) |
| 6389 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6390 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6391 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6392 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6393 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6394 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6395 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6396 | mddev->reshape_position = MaxSector; |
| 6397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | /* |
| 6399 | * Generate a 128 bit UUID |
| 6400 | */ |
| 6401 | get_random_bytes(mddev->uuid, 16); |
| 6402 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6403 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6404 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6405 | mddev->new_layout = mddev->layout; |
| 6406 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6407 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6409 | return 0; |
| 6410 | } |
| 6411 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6412 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6413 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6414 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6415 | |
| 6416 | if (mddev->external_size) |
| 6417 | return; |
| 6418 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6419 | mddev->array_sectors = array_sectors; |
| 6420 | } |
| 6421 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6422 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6423 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6424 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6425 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6426 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6427 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6428 | |
| 6429 | if (mddev->pers->resize == NULL) |
| 6430 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6431 | /* The "num_sectors" is the number of sectors of each device that |
| 6432 | * is used. This can only make sense for arrays with redundancy. |
| 6433 | * linear and raid0 always use whatever space is available. We can only |
| 6434 | * consider changing this number if no resync or reconstruction is |
| 6435 | * 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] | 6436 | * 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] | 6437 | * of each device. If num_sectors is zero, we find the largest size |
| 6438 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6439 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6440 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6441 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6442 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6443 | if (mddev->ro) |
| 6444 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6445 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6446 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6447 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6448 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6449 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6450 | num_sectors = avail; |
| 6451 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6452 | return -ENOSPC; |
| 6453 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6454 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6455 | if (!rv) |
| 6456 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6457 | return rv; |
| 6458 | } |
| 6459 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6460 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6461 | { |
| 6462 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6463 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6464 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6465 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6466 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6467 | if (mddev->ro) |
| 6468 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6469 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6470 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6471 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6472 | if (mddev->sync_thread || |
| 6473 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6474 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6475 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6476 | |
| 6477 | rdev_for_each(rdev, mddev) { |
| 6478 | if (mddev->raid_disks < raid_disks && |
| 6479 | rdev->data_offset < rdev->new_data_offset) |
| 6480 | return -EINVAL; |
| 6481 | if (mddev->raid_disks > raid_disks && |
| 6482 | rdev->data_offset > rdev->new_data_offset) |
| 6483 | return -EINVAL; |
| 6484 | } |
| 6485 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6486 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6487 | if (mddev->delta_disks < 0) |
| 6488 | mddev->reshape_backwards = 1; |
| 6489 | else if (mddev->delta_disks > 0) |
| 6490 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6491 | |
| 6492 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6493 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6494 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6495 | mddev->reshape_backwards = 0; |
| 6496 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6497 | return rv; |
| 6498 | } |
| 6499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | /* |
| 6501 | * update_array_info is used to change the configuration of an |
| 6502 | * on-line array. |
| 6503 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6504 | * fields in the info are checked against the array. |
| 6505 | * Any differences that cannot be handled will cause an error. |
| 6506 | * Normally, only one change can be managed at a time. |
| 6507 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6508 | 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] | 6509 | { |
| 6510 | int rv = 0; |
| 6511 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6512 | int state = 0; |
| 6513 | |
| 6514 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6515 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6516 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6517 | |
| 6518 | if (mddev->major_version != info->major_version || |
| 6519 | mddev->minor_version != info->minor_version || |
| 6520 | /* mddev->patch_version != info->patch_version || */ |
| 6521 | mddev->ctime != info->ctime || |
| 6522 | mddev->level != info->level || |
| 6523 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 6524 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6525 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6526 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6527 | ((state^info->state) & 0xfffffe00) |
| 6528 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6529 | return -EINVAL; |
| 6530 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6531 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6532 | cnt++; |
| 6533 | if (mddev->raid_disks != info->raid_disks) |
| 6534 | cnt++; |
| 6535 | if (mddev->layout != info->layout) |
| 6536 | cnt++; |
| 6537 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6538 | cnt++; |
| 6539 | if (cnt == 0) |
| 6540 | return 0; |
| 6541 | if (cnt > 1) |
| 6542 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6543 | |
| 6544 | if (mddev->layout != info->layout) { |
| 6545 | /* Change layout |
| 6546 | * we don't need to do anything at the md level, the |
| 6547 | * personality will take care of it all. |
| 6548 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6549 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6550 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6551 | else { |
| 6552 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6553 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6554 | if (rv) |
| 6555 | mddev->new_layout = mddev->layout; |
| 6556 | return rv; |
| 6557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6558 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6559 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6560 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6562 | if (mddev->raid_disks != info->raid_disks) |
| 6563 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6564 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6565 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6566 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 6567 | rv = -EINVAL; |
| 6568 | goto err; |
| 6569 | } |
| 6570 | if (mddev->recovery || mddev->sync_thread) { |
| 6571 | rv = -EBUSY; |
| 6572 | goto err; |
| 6573 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6574 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6575 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6576 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6577 | if (mddev->bitmap) { |
| 6578 | rv = -EEXIST; |
| 6579 | goto err; |
| 6580 | } |
| 6581 | if (mddev->bitmap_info.default_offset == 0) { |
| 6582 | rv = -EINVAL; |
| 6583 | goto err; |
| 6584 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6585 | mddev->bitmap_info.offset = |
| 6586 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6587 | mddev->bitmap_info.space = |
| 6588 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6589 | mddev->pers->quiesce(mddev, 1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6590 | bitmap = bitmap_create(mddev, -1); |
| 6591 | if (!IS_ERR(bitmap)) { |
| 6592 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6593 | rv = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6594 | } else |
| 6595 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6596 | if (rv) |
| 6597 | bitmap_destroy(mddev); |
| 6598 | mddev->pers->quiesce(mddev, 0); |
| 6599 | } else { |
| 6600 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6601 | if (!mddev->bitmap) { |
| 6602 | rv = -ENOENT; |
| 6603 | goto err; |
| 6604 | } |
| 6605 | if (mddev->bitmap->storage.file) { |
| 6606 | rv = -EINVAL; |
| 6607 | goto err; |
| 6608 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6609 | mddev->pers->quiesce(mddev, 1); |
| 6610 | bitmap_destroy(mddev); |
| 6611 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6612 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6613 | } |
| 6614 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6615 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6616 | return rv; |
| 6617 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6618 | return rv; |
| 6619 | } |
| 6620 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6621 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6622 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6623 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6624 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6625 | |
| 6626 | if (mddev->pers == NULL) |
| 6627 | return -ENODEV; |
| 6628 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6629 | rcu_read_lock(); |
| 6630 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6631 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6632 | err = -ENODEV; |
| 6633 | else { |
| 6634 | md_error(mddev, rdev); |
| 6635 | if (!test_bit(Faulty, &rdev->flags)) |
| 6636 | err = -EBUSY; |
| 6637 | } |
| 6638 | rcu_read_unlock(); |
| 6639 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6640 | } |
| 6641 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6642 | /* |
| 6643 | * We have a problem here : there is no easy way to give a CHS |
| 6644 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6645 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6646 | * dosfs just mad... ;-) |
| 6647 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6648 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6649 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6650 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6651 | |
| 6652 | geo->heads = 2; |
| 6653 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6654 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6655 | return 0; |
| 6656 | } |
| 6657 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6658 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 6659 | { |
| 6660 | switch (cmd) { |
| 6661 | case ADD_NEW_DISK: |
| 6662 | case BLKROSET: |
| 6663 | case GET_ARRAY_INFO: |
| 6664 | case GET_BITMAP_FILE: |
| 6665 | case GET_DISK_INFO: |
| 6666 | case HOT_ADD_DISK: |
| 6667 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6668 | case RAID_AUTORUN: |
| 6669 | case RAID_VERSION: |
| 6670 | case RESTART_ARRAY_RW: |
| 6671 | case RUN_ARRAY: |
| 6672 | case SET_ARRAY_INFO: |
| 6673 | case SET_BITMAP_FILE: |
| 6674 | case SET_DISK_FAULTY: |
| 6675 | case STOP_ARRAY: |
| 6676 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6677 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6678 | return true; |
| 6679 | default: |
| 6680 | return false; |
| 6681 | } |
| 6682 | } |
| 6683 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6684 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6685 | unsigned int cmd, unsigned long arg) |
| 6686 | { |
| 6687 | int err = 0; |
| 6688 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6689 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6690 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6691 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6692 | if (!md_ioctl_valid(cmd)) |
| 6693 | return -ENOTTY; |
| 6694 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6695 | switch (cmd) { |
| 6696 | case RAID_VERSION: |
| 6697 | case GET_ARRAY_INFO: |
| 6698 | case GET_DISK_INFO: |
| 6699 | break; |
| 6700 | default: |
| 6701 | if (!capable(CAP_SYS_ADMIN)) |
| 6702 | return -EACCES; |
| 6703 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6704 | |
| 6705 | /* |
| 6706 | * Commands dealing with the RAID driver but not any |
| 6707 | * particular array: |
| 6708 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6709 | switch (cmd) { |
| 6710 | case RAID_VERSION: |
| 6711 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6712 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6713 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6714 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6715 | case RAID_AUTORUN: |
| 6716 | err = 0; |
| 6717 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6718 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6719 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6720 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6721 | } |
| 6722 | |
| 6723 | /* |
| 6724 | * Commands creating/starting a new array: |
| 6725 | */ |
| 6726 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6727 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6728 | |
| 6729 | if (!mddev) { |
| 6730 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6731 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6732 | } |
| 6733 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6734 | /* Some actions do not requires the mutex */ |
| 6735 | switch (cmd) { |
| 6736 | case GET_ARRAY_INFO: |
| 6737 | if (!mddev->raid_disks && !mddev->external) |
| 6738 | err = -ENODEV; |
| 6739 | else |
| 6740 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6741 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6742 | |
| 6743 | case GET_DISK_INFO: |
| 6744 | if (!mddev->raid_disks && !mddev->external) |
| 6745 | err = -ENODEV; |
| 6746 | else |
| 6747 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6748 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6749 | |
| 6750 | case SET_DISK_FAULTY: |
| 6751 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6752 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6753 | |
| 6754 | case GET_BITMAP_FILE: |
| 6755 | err = get_bitmap_file(mddev, argp); |
| 6756 | goto out; |
| 6757 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6758 | } |
| 6759 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 6760 | if (cmd == ADD_NEW_DISK) |
| 6761 | /* need to ensure md_delayed_delete() has completed */ |
| 6762 | flush_workqueue(md_misc_wq); |
| 6763 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 6764 | if (cmd == HOT_REMOVE_DISK) |
| 6765 | /* need to ensure recovery thread has run */ |
| 6766 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 6767 | !test_bit(MD_RECOVERY_NEEDED, |
| 6768 | &mddev->flags), |
| 6769 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6770 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 6771 | /* Need to flush page cache, and ensure no-one else opens |
| 6772 | * and writes |
| 6773 | */ |
| 6774 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6775 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6776 | mutex_unlock(&mddev->open_mutex); |
| 6777 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6778 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6779 | } |
| 6780 | set_bit(MD_STILL_CLOSED, &mddev->flags); |
| 6781 | mutex_unlock(&mddev->open_mutex); |
| 6782 | sync_blockdev(bdev); |
| 6783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6784 | err = mddev_lock(mddev); |
| 6785 | if (err) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6786 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6787 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6788 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6789 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6790 | } |
| 6791 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6792 | if (cmd == SET_ARRAY_INFO) { |
| 6793 | mdu_array_info_t info; |
| 6794 | if (!arg) |
| 6795 | memset(&info, 0, sizeof(info)); |
| 6796 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6797 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6798 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6799 | } |
| 6800 | if (mddev->pers) { |
| 6801 | err = update_array_info(mddev, &info); |
| 6802 | if (err) { |
| 6803 | printk(KERN_WARNING "md: couldn't update" |
| 6804 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6805 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6806 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6807 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6808 | } |
| 6809 | if (!list_empty(&mddev->disks)) { |
| 6810 | printk(KERN_WARNING |
| 6811 | "md: array %s already has disks!\n", |
| 6812 | mdname(mddev)); |
| 6813 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6814 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6815 | } |
| 6816 | if (mddev->raid_disks) { |
| 6817 | printk(KERN_WARNING |
| 6818 | "md: array %s already initialised!\n", |
| 6819 | mdname(mddev)); |
| 6820 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6821 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6822 | } |
| 6823 | err = set_array_info(mddev, &info); |
| 6824 | if (err) { |
| 6825 | printk(KERN_WARNING "md: couldn't set" |
| 6826 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6827 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6828 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6829 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6830 | } |
| 6831 | |
| 6832 | /* |
| 6833 | * Commands querying/configuring an existing array: |
| 6834 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6835 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6836 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6837 | if ((!mddev->raid_disks && !mddev->external) |
| 6838 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6839 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6840 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6841 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6842 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6843 | } |
| 6844 | |
| 6845 | /* |
| 6846 | * Commands even a read-only array can execute: |
| 6847 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6848 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6849 | case RESTART_ARRAY_RW: |
| 6850 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6851 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6852 | |
| 6853 | case STOP_ARRAY: |
| 6854 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6855 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6856 | |
| 6857 | case STOP_ARRAY_RO: |
| 6858 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6859 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6860 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6861 | case HOT_REMOVE_DISK: |
| 6862 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6863 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6864 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6865 | case ADD_NEW_DISK: |
| 6866 | /* We can support ADD_NEW_DISK on read-only arrays |
| 6867 | * on if we are re-adding a preexisting device. |
| 6868 | * So require mddev->pers and MD_DISK_SYNC. |
| 6869 | */ |
| 6870 | if (mddev->pers) { |
| 6871 | mdu_disk_info_t info; |
| 6872 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6873 | err = -EFAULT; |
| 6874 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 6875 | /* Need to clear read-only for this */ |
| 6876 | break; |
| 6877 | else |
| 6878 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6879 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6880 | } |
| 6881 | break; |
| 6882 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6883 | case BLKROSET: |
| 6884 | if (get_user(ro, (int __user *)(arg))) { |
| 6885 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6886 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6887 | } |
| 6888 | err = -EINVAL; |
| 6889 | |
| 6890 | /* if the bdev is going readonly the value of mddev->ro |
| 6891 | * does not matter, no writes are coming |
| 6892 | */ |
| 6893 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6894 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6895 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6896 | /* are we are already prepared for writes? */ |
| 6897 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6898 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6899 | |
| 6900 | /* transitioning to readauto need only happen for |
| 6901 | * arrays that call md_write_start |
| 6902 | */ |
| 6903 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6904 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6905 | if (err == 0) { |
| 6906 | mddev->ro = 2; |
| 6907 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6908 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6909 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6910 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6911 | } |
| 6912 | |
| 6913 | /* |
| 6914 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6915 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6916 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 6917 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6918 | if (mddev->ro == 2) { |
| 6919 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6920 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6921 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6922 | /* mddev_unlock will wake thread */ |
| 6923 | /* If a device failed while we were read-only, we |
| 6924 | * need to make sure the metadata is updated now. |
| 6925 | */ |
| 6926 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
| 6927 | mddev_unlock(mddev); |
| 6928 | wait_event(mddev->sb_wait, |
| 6929 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && |
| 6930 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6931 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6932 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6933 | } else { |
| 6934 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6935 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6936 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6937 | } |
| 6938 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6939 | switch (cmd) { |
| 6940 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6941 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6942 | mdu_disk_info_t info; |
| 6943 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6944 | err = -EFAULT; |
| 6945 | else |
| 6946 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6947 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6948 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6949 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6950 | case CLUSTERED_DISK_NACK: |
| 6951 | if (mddev_is_clustered(mddev)) |
| 6952 | md_cluster_ops->new_disk_ack(mddev, false); |
| 6953 | else |
| 6954 | err = -EINVAL; |
| 6955 | goto unlock; |
| 6956 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6957 | case HOT_ADD_DISK: |
| 6958 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6959 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6960 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6961 | case RUN_ARRAY: |
| 6962 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6963 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6964 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6965 | case SET_BITMAP_FILE: |
| 6966 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6967 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6968 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6969 | default: |
| 6970 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6971 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6972 | } |
| 6973 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6974 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6975 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6976 | err != -EINVAL) |
| 6977 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6979 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6980 | return err; |
| 6981 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6982 | #ifdef CONFIG_COMPAT |
| 6983 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6984 | unsigned int cmd, unsigned long arg) |
| 6985 | { |
| 6986 | switch (cmd) { |
| 6987 | case HOT_REMOVE_DISK: |
| 6988 | case HOT_ADD_DISK: |
| 6989 | case SET_DISK_FAULTY: |
| 6990 | case SET_BITMAP_FILE: |
| 6991 | /* These take in integer arg, do not convert */ |
| 6992 | break; |
| 6993 | default: |
| 6994 | arg = (unsigned long)compat_ptr(arg); |
| 6995 | break; |
| 6996 | } |
| 6997 | |
| 6998 | return md_ioctl(bdev, mode, cmd, arg); |
| 6999 | } |
| 7000 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7001 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7002 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7003 | { |
| 7004 | /* |
| 7005 | * Succeed if we can lock the mddev, which confirms that |
| 7006 | * it isn't being stopped right now. |
| 7007 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7008 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7009 | int err; |
| 7010 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 7011 | if (!mddev) |
| 7012 | return -ENODEV; |
| 7013 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7014 | if (mddev->gendisk != bdev->bd_disk) { |
| 7015 | /* we are racing with mddev_put which is discarding this |
| 7016 | * bd_disk. |
| 7017 | */ |
| 7018 | mddev_put(mddev); |
| 7019 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7020 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7021 | /* Then retry the open from the top */ |
| 7022 | return -ERESTARTSYS; |
| 7023 | } |
| 7024 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 7025 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7026 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7027 | goto out; |
| 7028 | |
| 7029 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7030 | atomic_inc(&mddev->openers); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7031 | clear_bit(MD_STILL_CLOSED, &mddev->flags); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7032 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7033 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7034 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7035 | out: |
| 7036 | return err; |
| 7037 | } |
| 7038 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 7039 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7040 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7041 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7042 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 7043 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7044 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7045 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7046 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7047 | |
| 7048 | static int md_media_changed(struct gendisk *disk) |
| 7049 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7050 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7051 | |
| 7052 | return mddev->changed; |
| 7053 | } |
| 7054 | |
| 7055 | static int md_revalidate(struct gendisk *disk) |
| 7056 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7057 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7058 | |
| 7059 | mddev->changed = 0; |
| 7060 | return 0; |
| 7061 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 7062 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7063 | { |
| 7064 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7065 | .open = md_open, |
| 7066 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 7067 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7068 | #ifdef CONFIG_COMPAT |
| 7069 | .compat_ioctl = md_compat_ioctl, |
| 7070 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7071 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7072 | .media_changed = md_media_changed, |
| 7073 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7074 | }; |
| 7075 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7076 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7077 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7078 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7079 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7080 | /* |
| 7081 | * md_thread is a 'system-thread', it's priority should be very |
| 7082 | * high. We avoid resource deadlocks individually in each |
| 7083 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 7084 | * the very same RT priority as kswapd, thus we will never get |
| 7085 | * into a priority inversion deadlock. |
| 7086 | * |
| 7087 | * we definitely have to have equal or higher priority than |
| 7088 | * bdflush, otherwise bdflush will deadlock if there are too |
| 7089 | * many dirty RAID5 blocks. |
| 7090 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7091 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7092 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7093 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7094 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7095 | /* We need to wait INTERRUPTIBLE so that |
| 7096 | * we don't add to the load-average. |
| 7097 | * That means we need to be sure no signals are |
| 7098 | * pending |
| 7099 | */ |
| 7100 | if (signal_pending(current)) |
| 7101 | flush_signals(current); |
| 7102 | |
| 7103 | wait_event_interruptible_timeout |
| 7104 | (thread->wqueue, |
| 7105 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 7106 | || kthread_should_stop(), |
| 7107 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7108 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7109 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 7110 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7111 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7112 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7114 | return 0; |
| 7115 | } |
| 7116 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7117 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7118 | { |
| 7119 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7120 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7121 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 7122 | wake_up(&thread->wqueue); |
| 7123 | } |
| 7124 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7125 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7126 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7127 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7128 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7129 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7130 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7131 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7132 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7133 | if (!thread) |
| 7134 | return NULL; |
| 7135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7136 | init_waitqueue_head(&thread->wqueue); |
| 7137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7138 | thread->run = run; |
| 7139 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7140 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7141 | thread->tsk = kthread_run(md_thread, thread, |
| 7142 | "%s_%s", |
| 7143 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7144 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7145 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7146 | kfree(thread); |
| 7147 | return NULL; |
| 7148 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7149 | return thread; |
| 7150 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7151 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7152 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7153 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7154 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7155 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7156 | if (!thread) |
| 7157 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7158 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7159 | /* Locking ensures that mddev_unlock does not wake_up a |
| 7160 | * non-existent thread |
| 7161 | */ |
| 7162 | spin_lock(&pers_lock); |
| 7163 | *threadp = NULL; |
| 7164 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7165 | |
| 7166 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7167 | kfree(thread); |
| 7168 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7169 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7170 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7171 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7172 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7173 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7174 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7175 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7176 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7177 | return; |
| 7178 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7179 | if (mddev->degraded) |
| 7180 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7181 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7182 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7183 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7184 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 7185 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7186 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 7187 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7188 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7189 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7190 | |
| 7191 | /* seq_file implementation /proc/mdstat */ |
| 7192 | |
| 7193 | static void status_unused(struct seq_file *seq) |
| 7194 | { |
| 7195 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7196 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7197 | |
| 7198 | seq_printf(seq, "unused devices: "); |
| 7199 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7200 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7201 | char b[BDEVNAME_SIZE]; |
| 7202 | i++; |
| 7203 | seq_printf(seq, "%s ", |
| 7204 | bdevname(rdev->bdev,b)); |
| 7205 | } |
| 7206 | if (!i) |
| 7207 | seq_printf(seq, "<none>"); |
| 7208 | |
| 7209 | seq_printf(seq, "\n"); |
| 7210 | } |
| 7211 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7212 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7213 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7214 | sector_t max_sectors, resync, res; |
| 7215 | unsigned long dt, db; |
| 7216 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7217 | int scale; |
| 7218 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7219 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7220 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 7221 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7222 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7223 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7224 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7225 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7226 | resync = mddev->curr_resync; |
| 7227 | if (resync <= 3) { |
| 7228 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7229 | /* Still cleaning up */ |
| 7230 | resync = max_sectors; |
| 7231 | } else |
| 7232 | resync -= atomic_read(&mddev->recovery_active); |
| 7233 | |
| 7234 | if (resync == 0) { |
| 7235 | if (mddev->recovery_cp < MaxSector) { |
| 7236 | seq_printf(seq, "\tresync=PENDING"); |
| 7237 | return 1; |
| 7238 | } |
| 7239 | return 0; |
| 7240 | } |
| 7241 | if (resync < 3) { |
| 7242 | seq_printf(seq, "\tresync=DELAYED"); |
| 7243 | return 1; |
| 7244 | } |
| 7245 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 7246 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7247 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7248 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7249 | * u32, as those are the requirements for sector_div. |
| 7250 | * Thus 'scale' must be at least 10 |
| 7251 | */ |
| 7252 | scale = 10; |
| 7253 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7254 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7255 | scale++; |
| 7256 | } |
| 7257 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7258 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7259 | |
| 7260 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7261 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7262 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7263 | seq_printf(seq, "["); |
| 7264 | for (i = 0; i < x; i++) |
| 7265 | seq_printf(seq, "="); |
| 7266 | seq_printf(seq, ">"); |
| 7267 | for (i = 0; i < y; i++) |
| 7268 | seq_printf(seq, "."); |
| 7269 | seq_printf(seq, "] "); |
| 7270 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7271 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7272 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 7273 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7274 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 7275 | "check" : |
| 7276 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 7277 | "resync" : "recovery"))), |
| 7278 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7279 | (unsigned long long) resync/2, |
| 7280 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7281 | |
| 7282 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7283 | * dt: time from mark until now |
| 7284 | * db: blocks written from mark until now |
| 7285 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7286 | * |
| 7287 | * rt is a sector_t, so could be 32bit or 64bit. |
| 7288 | * So we divide before multiply in case it is 32bit and close |
| 7289 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 7290 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7291 | * near the end of resync when the number of remaining sectors |
| 7292 | * is close to 'db'. |
| 7293 | * We then divide rt by 32 after multiplying by db to compensate. |
| 7294 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7295 | */ |
| 7296 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 7297 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7298 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 7299 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7300 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7301 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 7302 | sector_div(rt, db/32+1); |
| 7303 | rt *= dt; |
| 7304 | rt >>= 5; |
| 7305 | |
| 7306 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 7307 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7308 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7309 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7310 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7311 | } |
| 7312 | |
| 7313 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 7314 | { |
| 7315 | struct list_head *tmp; |
| 7316 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7317 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7318 | |
| 7319 | if (l >= 0x10000) |
| 7320 | return NULL; |
| 7321 | if (!l--) |
| 7322 | /* header */ |
| 7323 | return (void*)1; |
| 7324 | |
| 7325 | spin_lock(&all_mddevs_lock); |
| 7326 | list_for_each(tmp,&all_mddevs) |
| 7327 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7328 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7329 | mddev_get(mddev); |
| 7330 | spin_unlock(&all_mddevs_lock); |
| 7331 | return mddev; |
| 7332 | } |
| 7333 | spin_unlock(&all_mddevs_lock); |
| 7334 | if (!l--) |
| 7335 | return (void*)2;/* tail */ |
| 7336 | return NULL; |
| 7337 | } |
| 7338 | |
| 7339 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 7340 | { |
| 7341 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7342 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7344 | ++*pos; |
| 7345 | if (v == (void*)2) |
| 7346 | return NULL; |
| 7347 | |
| 7348 | spin_lock(&all_mddevs_lock); |
| 7349 | if (v == (void*)1) |
| 7350 | tmp = all_mddevs.next; |
| 7351 | else |
| 7352 | tmp = mddev->all_mddevs.next; |
| 7353 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7354 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7355 | else { |
| 7356 | next_mddev = (void*)2; |
| 7357 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7359 | spin_unlock(&all_mddevs_lock); |
| 7360 | |
| 7361 | if (v != (void*)1) |
| 7362 | mddev_put(mddev); |
| 7363 | return next_mddev; |
| 7364 | |
| 7365 | } |
| 7366 | |
| 7367 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 7368 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7369 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7370 | |
| 7371 | if (mddev && v != (void*)1 && v != (void*)2) |
| 7372 | mddev_put(mddev); |
| 7373 | } |
| 7374 | |
| 7375 | static int md_seq_show(struct seq_file *seq, void *v) |
| 7376 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7377 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7378 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7379 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7380 | |
| 7381 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7382 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7383 | seq_printf(seq, "Personalities : "); |
| 7384 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7385 | list_for_each_entry(pers, &pers_list, list) |
| 7386 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7387 | |
| 7388 | spin_unlock(&pers_lock); |
| 7389 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7390 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7391 | return 0; |
| 7392 | } |
| 7393 | if (v == (void*)2) { |
| 7394 | status_unused(seq); |
| 7395 | return 0; |
| 7396 | } |
| 7397 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7398 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7399 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7400 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7401 | mddev->pers ? "" : "in"); |
| 7402 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7403 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7404 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7405 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7406 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7407 | seq_printf(seq, " %s", mddev->pers->name); |
| 7408 | } |
| 7409 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7410 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7411 | rcu_read_lock(); |
| 7412 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7413 | char b[BDEVNAME_SIZE]; |
| 7414 | seq_printf(seq, " %s[%d]", |
| 7415 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7416 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7417 | seq_printf(seq, "(W)"); |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 7418 | if (test_bit(Journal, &rdev->flags)) |
| 7419 | seq_printf(seq, "(J)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7420 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7421 | seq_printf(seq, "(F)"); |
| 7422 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7423 | } |
| 7424 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7425 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7426 | if (test_bit(Replacement, &rdev->flags)) |
| 7427 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7428 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7429 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7430 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7431 | |
| 7432 | if (!list_empty(&mddev->disks)) { |
| 7433 | if (mddev->pers) |
| 7434 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7435 | (unsigned long long) |
| 7436 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7437 | else |
| 7438 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7439 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7440 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7441 | if (mddev->persistent) { |
| 7442 | if (mddev->major_version != 0 || |
| 7443 | mddev->minor_version != 90) { |
| 7444 | seq_printf(seq," super %d.%d", |
| 7445 | mddev->major_version, |
| 7446 | mddev->minor_version); |
| 7447 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7448 | } else if (mddev->external) |
| 7449 | seq_printf(seq, " super external:%s", |
| 7450 | mddev->metadata_type); |
| 7451 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7452 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7453 | |
| 7454 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7455 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7456 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7457 | if (mddev->pers->sync_request) { |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7458 | if (status_resync(seq, mddev)) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7459 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7460 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7461 | } else |
| 7462 | seq_printf(seq, "\n "); |
| 7463 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7464 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7465 | |
| 7466 | seq_printf(seq, "\n"); |
| 7467 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7468 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7470 | return 0; |
| 7471 | } |
| 7472 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7473 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7474 | .start = md_seq_start, |
| 7475 | .next = md_seq_next, |
| 7476 | .stop = md_seq_stop, |
| 7477 | .show = md_seq_show, |
| 7478 | }; |
| 7479 | |
| 7480 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7481 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7482 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7483 | int error; |
| 7484 | |
| 7485 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7486 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7487 | return error; |
| 7488 | |
| 7489 | seq = file->private_data; |
| 7490 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7491 | return error; |
| 7492 | } |
| 7493 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7494 | static int md_unloading; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7495 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7496 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7497 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7498 | int mask; |
| 7499 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7500 | if (md_unloading) |
kbuild test robot | 7d7e64f | 2014-12-03 16:07:59 +1100 | [diff] [blame] | 7501 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7502 | poll_wait(filp, &md_event_waiters, wait); |
| 7503 | |
| 7504 | /* always allow read */ |
| 7505 | mask = POLLIN | POLLRDNORM; |
| 7506 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7507 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7508 | mask |= POLLERR | POLLPRI; |
| 7509 | return mask; |
| 7510 | } |
| 7511 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7512 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7513 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7514 | .open = md_seq_open, |
| 7515 | .read = seq_read, |
| 7516 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7517 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7518 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7519 | }; |
| 7520 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7521 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7522 | { |
Chao Yu | 50bd377 | 2014-09-25 15:28:34 +0800 | [diff] [blame] | 7523 | printk(KERN_INFO "md: %s personality registered for level %d\n", |
| 7524 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7525 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7526 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7527 | spin_unlock(&pers_lock); |
| 7528 | return 0; |
| 7529 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7530 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7531 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7532 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7533 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7534 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7535 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7536 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7537 | spin_unlock(&pers_lock); |
| 7538 | return 0; |
| 7539 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7540 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7541 | |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7542 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
| 7543 | struct module *module) |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7544 | { |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7545 | int ret = 0; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7546 | spin_lock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7547 | if (md_cluster_ops != NULL) |
| 7548 | ret = -EALREADY; |
| 7549 | else { |
| 7550 | md_cluster_ops = ops; |
| 7551 | md_cluster_mod = module; |
| 7552 | } |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7553 | spin_unlock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7554 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7555 | } |
| 7556 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 7557 | |
| 7558 | int unregister_md_cluster_operations(void) |
| 7559 | { |
| 7560 | spin_lock(&pers_lock); |
| 7561 | md_cluster_ops = NULL; |
| 7562 | spin_unlock(&pers_lock); |
| 7563 | return 0; |
| 7564 | } |
| 7565 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 7566 | |
| 7567 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 7568 | { |
| 7569 | int err; |
| 7570 | |
| 7571 | err = request_module("md-cluster"); |
| 7572 | if (err) { |
| 7573 | pr_err("md-cluster module not found.\n"); |
Goldwyn Rodrigues | b0c26a7 | 2015-07-22 12:09:15 -0500 | [diff] [blame] | 7574 | return -ENOENT; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7575 | } |
| 7576 | |
| 7577 | spin_lock(&pers_lock); |
| 7578 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
| 7579 | spin_unlock(&pers_lock); |
| 7580 | return -ENOENT; |
| 7581 | } |
| 7582 | spin_unlock(&pers_lock); |
| 7583 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 7584 | return md_cluster_ops->join(mddev, nodes); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7585 | } |
| 7586 | |
| 7587 | void md_cluster_stop(struct mddev *mddev) |
| 7588 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 7589 | if (!md_cluster_ops) |
| 7590 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7591 | md_cluster_ops->leave(mddev); |
| 7592 | module_put(md_cluster_mod); |
| 7593 | } |
| 7594 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7595 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7596 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7597 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7598 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7599 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7600 | |
| 7601 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7602 | rcu_read_lock(); |
| 7603 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7604 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7605 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7606 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7607 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7608 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7609 | * as sync_io is counted when a request starts, and |
| 7610 | * disk_stats is counted when it completes. |
| 7611 | * So resync activity will cause curr_events to be smaller than |
| 7612 | * when there was no such activity. |
| 7613 | * non-sync IO will cause disk_stat to increase without |
| 7614 | * increasing sync_io so curr_events will (eventually) |
| 7615 | * be larger than it was before. Once it becomes |
| 7616 | * substantially larger, the test below will cause |
| 7617 | * the array to appear non-idle, and resync will slow |
| 7618 | * down. |
| 7619 | * If there is a lot of outstanding resync activity when |
| 7620 | * we set last_event to curr_events, then all that activity |
| 7621 | * completing might cause the array to appear non-idle |
| 7622 | * and resync will be slowed down even though there might |
| 7623 | * not have been non-resync activity. This will only |
| 7624 | * happen once though. 'last_events' will soon reflect |
| 7625 | * the state where there is little or no outstanding |
| 7626 | * resync requests, and further resync activity will |
| 7627 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7628 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7629 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7630 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7631 | rdev->last_events = curr_events; |
| 7632 | idle = 0; |
| 7633 | } |
| 7634 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7635 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7636 | return idle; |
| 7637 | } |
| 7638 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7639 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7640 | { |
| 7641 | /* another "blocks" (512byte) blocks have been synced */ |
| 7642 | atomic_sub(blocks, &mddev->recovery_active); |
| 7643 | wake_up(&mddev->recovery_wait); |
| 7644 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7645 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7646 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7647 | md_wakeup_thread(mddev->thread); |
| 7648 | // stop recovery, signal do_sync .... |
| 7649 | } |
| 7650 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7651 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7652 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7653 | /* md_write_start(mddev, bi) |
| 7654 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7655 | * in superblock) before writing, schedule a superblock update |
| 7656 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7657 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7658 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7659 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7660 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7661 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7662 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7663 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7664 | BUG_ON(mddev->ro == 1); |
| 7665 | if (mddev->ro == 2) { |
| 7666 | /* need to switch to read/write */ |
| 7667 | mddev->ro = 0; |
| 7668 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7669 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7670 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7671 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7672 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7673 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7674 | if (mddev->safemode == 1) |
| 7675 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7676 | if (mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7677 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7678 | if (mddev->in_sync) { |
| 7679 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7680 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7681 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7682 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7683 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7684 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7685 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7686 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7687 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7688 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7689 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7690 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7691 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7692 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7693 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7694 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7695 | { |
| 7696 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7697 | if (mddev->safemode == 2) |
| 7698 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7699 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7700 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7701 | } |
| 7702 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7703 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7704 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7705 | /* md_allow_write(mddev) |
| 7706 | * Calling this ensures that the array is marked 'active' so that writes |
| 7707 | * may proceed without blocking. It is important to call this before |
| 7708 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7709 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7710 | * |
| 7711 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7712 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7713 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7714 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7715 | { |
| 7716 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7717 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7718 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7719 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7720 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7721 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7722 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7723 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7724 | if (mddev->in_sync) { |
| 7725 | mddev->in_sync = 0; |
| 7726 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7727 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7728 | if (mddev->safemode_delay && |
| 7729 | mddev->safemode == 0) |
| 7730 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7731 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7732 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7733 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7734 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7735 | spin_unlock(&mddev->lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7736 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7737 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7738 | return -EAGAIN; |
| 7739 | else |
| 7740 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7741 | } |
| 7742 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7744 | #define SYNC_MARKS 10 |
| 7745 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7746 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7747 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7748 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7749 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7750 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7751 | unsigned int currspeed = 0, |
| 7752 | window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7753 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7754 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7755 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7756 | sector_t mark_cnt[SYNC_MARKS]; |
| 7757 | int last_mark,m; |
| 7758 | struct list_head *tmp; |
| 7759 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7760 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7761 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7762 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7763 | struct blk_plug plug; |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 7764 | bool cluster_resync_finished = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7765 | |
| 7766 | /* just incase thread restarts... */ |
| 7767 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7768 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7769 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 7770 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7771 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7773 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7774 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7775 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7776 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7777 | action = "check"; |
| 7778 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7779 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7780 | action = "repair"; |
| 7781 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7782 | desc = "resync"; |
| 7783 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7784 | desc = "reshape"; |
| 7785 | else |
| 7786 | desc = "recovery"; |
| 7787 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7788 | mddev->last_sync_action = action ?: desc; |
| 7789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7790 | /* we overload curr_resync somewhat here. |
| 7791 | * 0 == not engaged in resync at all |
| 7792 | * 2 == checking that there is no conflict with another sync |
| 7793 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7794 | * commense |
| 7795 | * other == active in resync - this many blocks |
| 7796 | * |
| 7797 | * Before starting a resync we must have set curr_resync to |
| 7798 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7799 | * less than ours. When we find one that is the same or higher |
| 7800 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7801 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7802 | * This will mean we have to start checking from the beginning again. |
| 7803 | * |
| 7804 | */ |
| 7805 | |
| 7806 | do { |
| 7807 | mddev->curr_resync = 2; |
| 7808 | |
| 7809 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7810 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7811 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7812 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7813 | if (mddev2 == mddev) |
| 7814 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7815 | if (!mddev->parallel_resync |
| 7816 | && mddev2->curr_resync |
| 7817 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7818 | DEFINE_WAIT(wq); |
| 7819 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7820 | /* arbitrarily yield */ |
| 7821 | mddev->curr_resync = 1; |
| 7822 | wake_up(&resync_wait); |
| 7823 | } |
| 7824 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7825 | /* no need to wait here, we can wait the next |
| 7826 | * time 'round when curr_resync == 2 |
| 7827 | */ |
| 7828 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7829 | /* We need to wait 'interruptible' so as not to |
| 7830 | * contribute to the load average, and not to |
| 7831 | * be caught by 'softlockup' |
| 7832 | */ |
| 7833 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7834 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7835 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7836 | printk(KERN_INFO "md: delaying %s of %s" |
| 7837 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7838 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7839 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7840 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7841 | if (signal_pending(current)) |
| 7842 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7843 | schedule(); |
| 7844 | finish_wait(&resync_wait, &wq); |
| 7845 | goto try_again; |
| 7846 | } |
| 7847 | finish_wait(&resync_wait, &wq); |
| 7848 | } |
| 7849 | } |
| 7850 | } while (mddev->curr_resync < 2); |
| 7851 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7852 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7853 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7854 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7855 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7856 | */ |
| 7857 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 7858 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7859 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7860 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7861 | j = mddev->resync_min; |
| 7862 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7863 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7864 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7865 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7866 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7867 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7868 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7869 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7870 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7871 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7872 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7873 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 7874 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7875 | !test_bit(Faulty, &rdev->flags) && |
| 7876 | !test_bit(In_sync, &rdev->flags) && |
| 7877 | rdev->recovery_offset < j) |
| 7878 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7879 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 7880 | |
| 7881 | /* If there is a bitmap, we need to make sure all |
| 7882 | * writes that started before we added a spare |
| 7883 | * complete before we start doing a recovery. |
| 7884 | * Otherwise the write might complete and (via |
| 7885 | * bitmap_endwrite) set a bit in the bitmap after the |
| 7886 | * recovery has checked that bit and skipped that |
| 7887 | * region. |
| 7888 | */ |
| 7889 | if (mddev->bitmap) { |
| 7890 | mddev->pers->quiesce(mddev, 1); |
| 7891 | mddev->pers->quiesce(mddev, 0); |
| 7892 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7893 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7894 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7895 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7896 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7897 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7898 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7899 | "(but not more than %d KB/sec) for %s.\n", |
| 7900 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7901 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7902 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7903 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7904 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7905 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7906 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7907 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7908 | } |
| 7909 | last_mark = 0; |
| 7910 | mddev->resync_mark = mark[last_mark]; |
| 7911 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7912 | |
| 7913 | /* |
| 7914 | * Tune reconstruction: |
| 7915 | */ |
| 7916 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7917 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7918 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7919 | |
| 7920 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7921 | last_check = 0; |
| 7922 | |
| 7923 | if (j>2) { |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7924 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7925 | "md: resuming %s of %s from checkpoint.\n", |
| 7926 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7927 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7928 | } else |
| 7929 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7930 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7931 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 7932 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7933 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7934 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7935 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7936 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7937 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7938 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7939 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7940 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7941 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7942 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7943 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7944 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7945 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7946 | (j - mddev->curr_resync_completed)*2 |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 7947 | >= mddev->resync_max - mddev->curr_resync_completed || |
| 7948 | mddev->curr_resync_completed > mddev->resync_max |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7949 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7950 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7951 | wait_event(mddev->recovery_wait, |
| 7952 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7953 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7954 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 7955 | j > mddev->recovery_cp) |
| 7956 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7957 | update_time = jiffies; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7958 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7959 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7960 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7961 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7962 | while (j >= mddev->resync_max && |
| 7963 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7964 | /* As this condition is controlled by user-space, |
| 7965 | * we can block indefinitely, so use '_interruptible' |
| 7966 | * to avoid triggering warnings. |
| 7967 | */ |
| 7968 | flush_signals(current); /* just in case */ |
| 7969 | wait_event_interruptible(mddev->recovery_wait, |
| 7970 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7971 | || test_bit(MD_RECOVERY_INTR, |
| 7972 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7973 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7974 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7975 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7976 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7977 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 7978 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7979 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7980 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7981 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7982 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7983 | |
| 7984 | if (!skipped) { /* actual IO requested */ |
| 7985 | io_sectors += sectors; |
| 7986 | atomic_add(sectors, &mddev->recovery_active); |
| 7987 | } |
| 7988 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7989 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7990 | break; |
| 7991 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7992 | j += sectors; |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 7993 | if (j > max_sectors) |
| 7994 | /* when skipping, extra large numbers can be returned. */ |
| 7995 | j = max_sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7996 | if (j > 2) |
| 7997 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7998 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7999 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8000 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8001 | * visible in /proc/mdstat |
| 8002 | */ |
| 8003 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8004 | |
| 8005 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8006 | continue; |
| 8007 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8008 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8009 | repeat: |
| 8010 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 8011 | /* step marks */ |
| 8012 | int next = (last_mark+1) % SYNC_MARKS; |
| 8013 | |
| 8014 | mddev->resync_mark = mark[next]; |
| 8015 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 8016 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8017 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8018 | last_mark = next; |
| 8019 | } |
| 8020 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8021 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8022 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8023 | |
| 8024 | /* |
| 8025 | * this loop exits only if either when we are slower than |
| 8026 | * the 'hard' speed limit, or the system was IO-idle for |
| 8027 | * a jiffy. |
| 8028 | * the system might be non-idle CPU-wise, but we only care |
| 8029 | * about not overloading the IO subsystem. (things like an |
| 8030 | * e2fsck being done on the RAID array should execute fast) |
| 8031 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8032 | cond_resched(); |
| 8033 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8034 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 8035 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8036 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8037 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 8038 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8039 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8040 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8041 | goto repeat; |
| 8042 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8043 | if (!is_mddev_idle(mddev, 0)) { |
| 8044 | /* |
| 8045 | * Give other IO more of a chance. |
| 8046 | * The faster the devices, the less we wait. |
| 8047 | */ |
| 8048 | wait_event(mddev->recovery_wait, |
| 8049 | !atomic_read(&mddev->recovery_active)); |
| 8050 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8051 | } |
| 8052 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8053 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
| 8054 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 8055 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8056 | /* |
| 8057 | * this also signals 'finished resyncing' to md_stop |
| 8058 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8059 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8060 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 8061 | |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8062 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8063 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8064 | mddev->curr_resync > 2) { |
| 8065 | mddev->curr_resync_completed = mddev->curr_resync; |
| 8066 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 8067 | } |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8068 | /* tell personality and other nodes that we are finished */ |
| 8069 | if (mddev_is_clustered(mddev)) { |
| 8070 | md_cluster_ops->resync_finish(mddev); |
| 8071 | cluster_resync_finished = true; |
| 8072 | } |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8073 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8074 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8075 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8076 | mddev->curr_resync > 2) { |
| 8077 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 8078 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8079 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 8080 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8081 | "md: checkpointing %s of %s.\n", |
| 8082 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8083 | if (test_bit(MD_RECOVERY_ERROR, |
| 8084 | &mddev->recovery)) |
| 8085 | mddev->recovery_cp = |
| 8086 | mddev->curr_resync_completed; |
| 8087 | else |
| 8088 | mddev->recovery_cp = |
| 8089 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8090 | } |
| 8091 | } else |
| 8092 | mddev->recovery_cp = MaxSector; |
| 8093 | } else { |
| 8094 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8095 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8096 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8097 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8098 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 8099 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8100 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8101 | !test_bit(Faulty, &rdev->flags) && |
| 8102 | !test_bit(In_sync, &rdev->flags) && |
| 8103 | rdev->recovery_offset < mddev->curr_resync) |
| 8104 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8105 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8107 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 8108 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 8109 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8110 | |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8111 | if (mddev_is_clustered(mddev) && |
| 8112 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8113 | !cluster_resync_finished) |
| 8114 | md_cluster_ops->resync_finish(mddev); |
| 8115 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8116 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 8117 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8118 | /* We completed so min/max setting can be forgotten if used. */ |
| 8119 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8120 | mddev->resync_min = 0; |
| 8121 | mddev->resync_max = MaxSector; |
| 8122 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8123 | mddev->resync_min = mddev->curr_resync_completed; |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8124 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8125 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8126 | spin_unlock(&mddev->lock); |
| 8127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8128 | wake_up(&resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8129 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 8130 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8131 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8132 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8133 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8134 | static int remove_and_add_spares(struct mddev *mddev, |
| 8135 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8136 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8137 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8138 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8139 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8140 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8141 | rdev_for_each(rdev, mddev) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8142 | if ((this == NULL || rdev == this) && |
| 8143 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8144 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8145 | (test_bit(Faulty, &rdev->flags) || |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8146 | (!test_bit(In_sync, &rdev->flags) && |
| 8147 | !test_bit(Journal, &rdev->flags))) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8148 | atomic_read(&rdev->nr_pending)==0) { |
| 8149 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8150 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 8151 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8152 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8153 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8154 | } |
| 8155 | } |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 8156 | if (removed && mddev->kobj.sd) |
| 8157 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8158 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8159 | if (this && removed) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8160 | goto no_add; |
| 8161 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8162 | rdev_for_each(rdev, mddev) { |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8163 | if (this && this != rdev) |
| 8164 | continue; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 8165 | if (test_bit(Candidate, &rdev->flags)) |
| 8166 | continue; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8167 | if (rdev->raid_disk >= 0 && |
| 8168 | !test_bit(In_sync, &rdev->flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8169 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8170 | !test_bit(Faulty, &rdev->flags)) |
| 8171 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8172 | if (rdev->raid_disk >= 0) |
| 8173 | continue; |
| 8174 | if (test_bit(Faulty, &rdev->flags)) |
| 8175 | continue; |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 8176 | if (test_bit(Journal, &rdev->flags)) |
| 8177 | continue; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8178 | if (mddev->ro && |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8179 | ! (rdev->saved_raid_disk >= 0 && |
| 8180 | !test_bit(Bitmap_sync, &rdev->flags))) |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8181 | continue; |
| 8182 | |
NeilBrown | d01552a | 2015-10-31 11:00:56 +1100 | [diff] [blame] | 8183 | rdev->recovery_offset = 0; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8184 | if (mddev->pers-> |
| 8185 | hot_add_disk(mddev, rdev) == 0) { |
| 8186 | if (sysfs_link_rdev(mddev, rdev)) |
| 8187 | /* failure here is OK */; |
| 8188 | spares++; |
| 8189 | md_new_event(mddev); |
| 8190 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8191 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8192 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8193 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 8194 | if (removed) |
| 8195 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8196 | return spares; |
| 8197 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 8198 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8199 | static void md_start_sync(struct work_struct *ws) |
| 8200 | { |
| 8201 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8202 | int ret = 0; |
| 8203 | |
| 8204 | if (mddev_is_clustered(mddev)) { |
| 8205 | ret = md_cluster_ops->resync_start(mddev); |
| 8206 | if (ret) { |
| 8207 | mddev->sync_thread = NULL; |
| 8208 | goto out; |
| 8209 | } |
| 8210 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8211 | |
| 8212 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 8213 | mddev, |
| 8214 | "resync"); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8215 | out: |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8216 | if (!mddev->sync_thread) { |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8217 | if (!(mddev_is_clustered(mddev) && ret == -EAGAIN)) |
| 8218 | printk(KERN_ERR "%s: could not start resync" |
| 8219 | " thread...\n", |
| 8220 | mdname(mddev)); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8221 | /* leave the spares where they are, it shouldn't hurt */ |
| 8222 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8223 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8224 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8225 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 8226 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8227 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8228 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8229 | &mddev->recovery)) |
| 8230 | if (mddev->sysfs_action) |
| 8231 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8232 | } else |
| 8233 | md_wakeup_thread(mddev->sync_thread); |
| 8234 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8235 | md_new_event(mddev); |
| 8236 | } |
| 8237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8238 | /* |
| 8239 | * This routine is regularly called by all per-raid-array threads to |
| 8240 | * deal with generic issues like resync and super-block update. |
| 8241 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 8242 | * need this as they never do any recovery or update the superblock. |
| 8243 | * |
| 8244 | * It does not do any resync itself, but rather "forks" off other threads |
| 8245 | * to do that as needed. |
| 8246 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 8247 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8248 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8249 | * and wakeups up this thread which will reap the thread and finish up. |
| 8250 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 8251 | * |
| 8252 | * The overall approach is: |
| 8253 | * 1/ if the superblock needs updating, update it. |
| 8254 | * 2/ If a recovery thread is running, don't do anything else. |
| 8255 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 8256 | * 4/ If there are any faulty devices, remove them. |
| 8257 | * 5/ If array is degraded, try to add spares devices |
| 8258 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 8259 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8260 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8261 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 8262 | if (mddev->suspended) |
| 8263 | return; |
| 8264 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 8265 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 8266 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8267 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8268 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8269 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8270 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 8271 | mdname(mddev)); |
| 8272 | mddev->safemode = 2; |
| 8273 | } |
| 8274 | flush_signals(current); |
| 8275 | } |
| 8276 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8277 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 8278 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8279 | if ( ! ( |
NeilBrown | 142d44c | 2013-11-28 10:34:18 +1100 | [diff] [blame] | 8280 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8281 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8282 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8283 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8284 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 8285 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8286 | )) |
| 8287 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8288 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 8289 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8290 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8291 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8292 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 8293 | struct md_rdev *rdev; |
| 8294 | if (!mddev->external && mddev->in_sync) |
| 8295 | /* 'Blocked' flag not needed as failed devices |
| 8296 | * will be recorded if array switched to read/write. |
| 8297 | * Leaving it set will prevent the device |
| 8298 | * from being removed. |
| 8299 | */ |
| 8300 | rdev_for_each(rdev, mddev) |
| 8301 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8302 | /* On a read-only array we can: |
| 8303 | * - remove failed devices |
| 8304 | * - add already-in_sync devices if the array itself |
| 8305 | * is in-sync. |
| 8306 | * As we only add devices that are already in-sync, |
| 8307 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8308 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8309 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8310 | /* There is no thread, but we need to call |
| 8311 | * ->spare_active and clear saved_raid_disk |
| 8312 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 8313 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8314 | md_reap_sync_thread(mddev); |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 8315 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8316 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Shaohua Li | d4929ad | 2015-09-18 10:20:12 -0700 | [diff] [blame] | 8317 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8318 | goto unlock; |
| 8319 | } |
| 8320 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8321 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8322 | int did_change = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8323 | spin_lock(&mddev->lock); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8324 | if (mddev->safemode && |
| 8325 | !atomic_read(&mddev->writes_pending) && |
| 8326 | !mddev->in_sync && |
| 8327 | mddev->recovery_cp == MaxSector) { |
| 8328 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8329 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 8330 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8331 | } |
| 8332 | if (mddev->safemode == 1) |
| 8333 | mddev->safemode = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8334 | spin_unlock(&mddev->lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8335 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8336 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8337 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8338 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 8339 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 8340 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8342 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 8343 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 8344 | /* resync/recovery still happening */ |
| 8345 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8346 | goto unlock; |
| 8347 | } |
| 8348 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8349 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8350 | goto unlock; |
| 8351 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8352 | /* Set RUNNING before clearing NEEDED to avoid |
| 8353 | * any transients in the value of "sync_action". |
| 8354 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8355 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8356 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8357 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8358 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8359 | /* Clear some bits that don't mean anything, but |
| 8360 | * might be left set |
| 8361 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8362 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 8363 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8364 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 8365 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 8366 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8367 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8368 | /* no recovery is running. |
| 8369 | * remove any failed drives, then |
| 8370 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8371 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8372 | * the personality to fail the re-add. |
| 8373 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8374 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8375 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8376 | if (mddev->pers->check_reshape == NULL || |
| 8377 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8378 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8379 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8380 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8381 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8382 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8383 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8384 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 8385 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8386 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8387 | } else if (mddev->recovery_cp < MaxSector) { |
| 8388 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8389 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8390 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 8391 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8392 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8394 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 8395 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 8396 | /* We are adding a device or devices to an array |
| 8397 | * which has the bitmap stored on all devices. |
| 8398 | * So make sure all bitmap pages get written |
| 8399 | */ |
| 8400 | bitmap_write_all(mddev->bitmap); |
| 8401 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8402 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 8403 | queue_work(md_misc_wq, &mddev->del_work); |
| 8404 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8405 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8406 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8407 | if (!mddev->sync_thread) { |
| 8408 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8409 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8410 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8411 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 8412 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8413 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8414 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8415 | unlock: |
| 8416 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8417 | mddev_unlock(mddev); |
| 8418 | } |
| 8419 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8420 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8421 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8422 | void md_reap_sync_thread(struct mddev *mddev) |
| 8423 | { |
| 8424 | struct md_rdev *rdev; |
| 8425 | |
| 8426 | /* resync has finished, collect result */ |
| 8427 | md_unregister_thread(&mddev->sync_thread); |
| 8428 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8429 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 8430 | /* success...*/ |
| 8431 | /* activate any spares */ |
| 8432 | if (mddev->pers->spare_active(mddev)) { |
| 8433 | sysfs_notify(&mddev->kobj, NULL, |
| 8434 | "degraded"); |
| 8435 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 8436 | } |
| 8437 | } |
| 8438 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8439 | mddev->pers->finish_reshape) |
| 8440 | mddev->pers->finish_reshape(mddev); |
| 8441 | |
| 8442 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8443 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8444 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8445 | if (!mddev->degraded) |
| 8446 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8447 | rdev->saved_raid_disk = -1; |
| 8448 | |
| 8449 | md_update_sb(mddev, 1); |
| 8450 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 8451 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8452 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8453 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8454 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8455 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8456 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8457 | /* flag recovery needed just to double check */ |
| 8458 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8459 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8460 | md_new_event(mddev); |
| 8461 | if (mddev->event_work.func) |
| 8462 | queue_work(md_misc_wq, &mddev->event_work); |
| 8463 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8464 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8465 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8466 | 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] | 8467 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8468 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8469 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8470 | !test_bit(Blocked, &rdev->flags) && |
| 8471 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8472 | msecs_to_jiffies(5000)); |
| 8473 | rdev_dec_pending(rdev, mddev); |
| 8474 | } |
| 8475 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 8476 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8477 | void md_finish_reshape(struct mddev *mddev) |
| 8478 | { |
| 8479 | /* called be personality module when reshape completes. */ |
| 8480 | struct md_rdev *rdev; |
| 8481 | |
| 8482 | rdev_for_each(rdev, mddev) { |
| 8483 | if (rdev->data_offset > rdev->new_data_offset) |
| 8484 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 8485 | else |
| 8486 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 8487 | rdev->data_offset = rdev->new_data_offset; |
| 8488 | } |
| 8489 | } |
| 8490 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8491 | |
| 8492 | /* Bad block management. |
| 8493 | * We can record which blocks on each device are 'bad' and so just |
| 8494 | * fail those blocks, or that stripe, rather than the whole device. |
| 8495 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 8496 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 8497 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 8498 | * A 'shift' can be set so that larger blocks are tracked and |
| 8499 | * consequently larger devices can be covered. |
| 8500 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 8501 | * |
| 8502 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 8503 | * might need to retry if it is very unlucky. |
| 8504 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 8505 | * so we use the write_seqlock_irq variant. |
| 8506 | * |
| 8507 | * When looking for a bad block we specify a range and want to |
| 8508 | * know if any block in the range is bad. So we binary-search |
| 8509 | * to the last range that starts at-or-before the given endpoint, |
| 8510 | * (or "before the sector after the target range") |
| 8511 | * then see if it ends after the given start. |
| 8512 | * We return |
| 8513 | * 0 if there are no known bad blocks in the range |
| 8514 | * 1 if there are known bad block which are all acknowledged |
| 8515 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 8516 | * plus the start/length of the first bad section we overlap. |
| 8517 | */ |
| 8518 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 8519 | sector_t *first_bad, int *bad_sectors) |
| 8520 | { |
| 8521 | int hi; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8522 | int lo; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8523 | u64 *p = bb->page; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8524 | int rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8525 | sector_t target = s + sectors; |
| 8526 | unsigned seq; |
| 8527 | |
| 8528 | if (bb->shift > 0) { |
| 8529 | /* round the start down, and the end up */ |
| 8530 | s >>= bb->shift; |
| 8531 | target += (1<<bb->shift) - 1; |
| 8532 | target >>= bb->shift; |
| 8533 | sectors = target - s; |
| 8534 | } |
| 8535 | /* 'target' is now the first block after the bad range */ |
| 8536 | |
| 8537 | retry: |
| 8538 | seq = read_seqbegin(&bb->lock); |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8539 | lo = 0; |
| 8540 | rv = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8541 | hi = bb->count; |
| 8542 | |
| 8543 | /* Binary search between lo and hi for 'target' |
| 8544 | * i.e. for the last range that starts before 'target' |
| 8545 | */ |
| 8546 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 8547 | * are known not to be the last range before target. |
| 8548 | * VARIANT: hi-lo is the number of possible |
| 8549 | * ranges, and decreases until it reaches 1 |
| 8550 | */ |
| 8551 | while (hi - lo > 1) { |
| 8552 | int mid = (lo + hi) / 2; |
| 8553 | sector_t a = BB_OFFSET(p[mid]); |
| 8554 | if (a < target) |
| 8555 | /* This could still be the one, earlier ranges |
| 8556 | * could not. */ |
| 8557 | lo = mid; |
| 8558 | else |
| 8559 | /* This and later ranges are definitely out. */ |
| 8560 | hi = mid; |
| 8561 | } |
| 8562 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 8563 | if (hi > lo) { |
| 8564 | /* need to check all range that end after 's' to see if |
| 8565 | * any are unacknowledged. |
| 8566 | */ |
| 8567 | while (lo >= 0 && |
| 8568 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8569 | if (BB_OFFSET(p[lo]) < target) { |
| 8570 | /* starts before the end, and finishes after |
| 8571 | * the start, so they must overlap |
| 8572 | */ |
| 8573 | if (rv != -1 && BB_ACK(p[lo])) |
| 8574 | rv = 1; |
| 8575 | else |
| 8576 | rv = -1; |
| 8577 | *first_bad = BB_OFFSET(p[lo]); |
| 8578 | *bad_sectors = BB_LEN(p[lo]); |
| 8579 | } |
| 8580 | lo--; |
| 8581 | } |
| 8582 | } |
| 8583 | |
| 8584 | if (read_seqretry(&bb->lock, seq)) |
| 8585 | goto retry; |
| 8586 | |
| 8587 | return rv; |
| 8588 | } |
| 8589 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 8590 | |
| 8591 | /* |
| 8592 | * Add a range of bad blocks to the table. |
| 8593 | * This might extend the table, or might contract it |
| 8594 | * if two adjacent ranges can be merged. |
| 8595 | * We binary-search to find the 'insertion' point, then |
| 8596 | * decide how best to handle it. |
| 8597 | */ |
| 8598 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 8599 | int acknowledged) |
| 8600 | { |
| 8601 | u64 *p; |
| 8602 | int lo, hi; |
| 8603 | int rv = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8604 | unsigned long flags; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8605 | |
| 8606 | if (bb->shift < 0) |
| 8607 | /* badblocks are disabled */ |
| 8608 | return 0; |
| 8609 | |
| 8610 | if (bb->shift) { |
| 8611 | /* round the start down, and the end up */ |
| 8612 | sector_t next = s + sectors; |
| 8613 | s >>= bb->shift; |
| 8614 | next += (1<<bb->shift) - 1; |
| 8615 | next >>= bb->shift; |
| 8616 | sectors = next - s; |
| 8617 | } |
| 8618 | |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8619 | write_seqlock_irqsave(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8620 | |
| 8621 | p = bb->page; |
| 8622 | lo = 0; |
| 8623 | hi = bb->count; |
| 8624 | /* Find the last range that starts at-or-before 's' */ |
| 8625 | while (hi - lo > 1) { |
| 8626 | int mid = (lo + hi) / 2; |
| 8627 | sector_t a = BB_OFFSET(p[mid]); |
| 8628 | if (a <= s) |
| 8629 | lo = mid; |
| 8630 | else |
| 8631 | hi = mid; |
| 8632 | } |
| 8633 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 8634 | hi = lo; |
| 8635 | |
| 8636 | if (hi > lo) { |
| 8637 | /* we found a range that might merge with the start |
| 8638 | * of our new range |
| 8639 | */ |
| 8640 | sector_t a = BB_OFFSET(p[lo]); |
| 8641 | sector_t e = a + BB_LEN(p[lo]); |
| 8642 | int ack = BB_ACK(p[lo]); |
| 8643 | if (e >= s) { |
| 8644 | /* Yes, we can merge with a previous range */ |
| 8645 | if (s == a && s + sectors >= e) |
| 8646 | /* new range covers old */ |
| 8647 | ack = acknowledged; |
| 8648 | else |
| 8649 | ack = ack && acknowledged; |
| 8650 | |
| 8651 | if (e < s + sectors) |
| 8652 | e = s + sectors; |
| 8653 | if (e - a <= BB_MAX_LEN) { |
| 8654 | p[lo] = BB_MAKE(a, e-a, ack); |
| 8655 | s = e; |
| 8656 | } else { |
| 8657 | /* does not all fit in one range, |
| 8658 | * make p[lo] maximal |
| 8659 | */ |
| 8660 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8661 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8662 | s = a + BB_MAX_LEN; |
| 8663 | } |
| 8664 | sectors = e - s; |
| 8665 | } |
| 8666 | } |
| 8667 | if (sectors && hi < bb->count) { |
| 8668 | /* 'hi' points to the first range that starts after 's'. |
| 8669 | * Maybe we can merge with the start of that range */ |
| 8670 | sector_t a = BB_OFFSET(p[hi]); |
| 8671 | sector_t e = a + BB_LEN(p[hi]); |
| 8672 | int ack = BB_ACK(p[hi]); |
| 8673 | if (a <= s + sectors) { |
| 8674 | /* merging is possible */ |
| 8675 | if (e <= s + sectors) { |
| 8676 | /* full overlap */ |
| 8677 | e = s + sectors; |
| 8678 | ack = acknowledged; |
| 8679 | } else |
| 8680 | ack = ack && acknowledged; |
| 8681 | |
| 8682 | a = s; |
| 8683 | if (e - a <= BB_MAX_LEN) { |
| 8684 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8685 | s = e; |
| 8686 | } else { |
| 8687 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8688 | s = a + BB_MAX_LEN; |
| 8689 | } |
| 8690 | sectors = e - s; |
| 8691 | lo = hi; |
| 8692 | hi++; |
| 8693 | } |
| 8694 | } |
| 8695 | if (sectors == 0 && hi < bb->count) { |
| 8696 | /* we might be able to combine lo and hi */ |
| 8697 | /* Note: 's' is at the end of 'lo' */ |
| 8698 | sector_t a = BB_OFFSET(p[hi]); |
| 8699 | int lolen = BB_LEN(p[lo]); |
| 8700 | int hilen = BB_LEN(p[hi]); |
| 8701 | int newlen = lolen + hilen - (s - a); |
| 8702 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8703 | /* yes, we can combine them */ |
| 8704 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8705 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8706 | memmove(p + hi, p + hi + 1, |
| 8707 | (bb->count - hi - 1) * 8); |
| 8708 | bb->count--; |
| 8709 | } |
| 8710 | } |
| 8711 | while (sectors) { |
| 8712 | /* didn't merge (it all). |
| 8713 | * Need to add a range just before 'hi' */ |
| 8714 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8715 | /* No room for more */ |
| 8716 | rv = 0; |
| 8717 | break; |
| 8718 | } else { |
| 8719 | int this_sectors = sectors; |
| 8720 | memmove(p + hi + 1, p + hi, |
| 8721 | (bb->count - hi) * 8); |
| 8722 | bb->count++; |
| 8723 | |
| 8724 | if (this_sectors > BB_MAX_LEN) |
| 8725 | this_sectors = BB_MAX_LEN; |
| 8726 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8727 | sectors -= this_sectors; |
| 8728 | s += this_sectors; |
| 8729 | } |
| 8730 | } |
| 8731 | |
| 8732 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8733 | if (!acknowledged) |
| 8734 | bb->unacked_exist = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8735 | write_sequnlock_irqrestore(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8736 | |
| 8737 | return rv; |
| 8738 | } |
| 8739 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8740 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8741 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8742 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8743 | int rv; |
| 8744 | if (is_new) |
| 8745 | s += rdev->new_data_offset; |
| 8746 | else |
| 8747 | s += rdev->data_offset; |
| 8748 | rv = md_set_badblocks(&rdev->badblocks, |
| 8749 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8750 | if (rv) { |
| 8751 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8752 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8753 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
NeilBrown | 55ce74d | 2015-08-14 11:11:10 +1000 | [diff] [blame] | 8754 | set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8755 | md_wakeup_thread(rdev->mddev->thread); |
| 8756 | } |
| 8757 | return rv; |
| 8758 | } |
| 8759 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8760 | |
| 8761 | /* |
| 8762 | * Remove a range of bad blocks from the table. |
| 8763 | * This may involve extending the table if we spilt a region, |
| 8764 | * but it must not fail. So if the table becomes full, we just |
| 8765 | * drop the remove request. |
| 8766 | */ |
| 8767 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8768 | { |
| 8769 | u64 *p; |
| 8770 | int lo, hi; |
| 8771 | sector_t target = s + sectors; |
| 8772 | int rv = 0; |
| 8773 | |
| 8774 | if (bb->shift > 0) { |
| 8775 | /* When clearing we round the start up and the end down. |
| 8776 | * This should not matter as the shift should align with |
| 8777 | * the block size and no rounding should ever be needed. |
| 8778 | * However it is better the think a block is bad when it |
| 8779 | * isn't than to think a block is not bad when it is. |
| 8780 | */ |
| 8781 | s += (1<<bb->shift) - 1; |
| 8782 | s >>= bb->shift; |
| 8783 | target >>= bb->shift; |
| 8784 | sectors = target - s; |
| 8785 | } |
| 8786 | |
| 8787 | write_seqlock_irq(&bb->lock); |
| 8788 | |
| 8789 | p = bb->page; |
| 8790 | lo = 0; |
| 8791 | hi = bb->count; |
| 8792 | /* Find the last range that starts before 'target' */ |
| 8793 | while (hi - lo > 1) { |
| 8794 | int mid = (lo + hi) / 2; |
| 8795 | sector_t a = BB_OFFSET(p[mid]); |
| 8796 | if (a < target) |
| 8797 | lo = mid; |
| 8798 | else |
| 8799 | hi = mid; |
| 8800 | } |
| 8801 | if (hi > lo) { |
| 8802 | /* p[lo] is the last range that could overlap the |
| 8803 | * current range. Earlier ranges could also overlap, |
| 8804 | * but only this one can overlap the end of the range. |
| 8805 | */ |
| 8806 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8807 | /* Partial overlap, leave the tail of this range */ |
| 8808 | int ack = BB_ACK(p[lo]); |
| 8809 | sector_t a = BB_OFFSET(p[lo]); |
| 8810 | sector_t end = a + BB_LEN(p[lo]); |
| 8811 | |
| 8812 | if (a < s) { |
| 8813 | /* we need to split this range */ |
| 8814 | if (bb->count >= MD_MAX_BADBLOCKS) { |
NeilBrown | 8b32bf5 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 8815 | rv = -ENOSPC; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8816 | goto out; |
| 8817 | } |
| 8818 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8819 | bb->count++; |
| 8820 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8821 | lo++; |
| 8822 | } |
| 8823 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8824 | /* there is no longer an overlap */ |
| 8825 | hi = lo; |
| 8826 | lo--; |
| 8827 | } |
| 8828 | while (lo >= 0 && |
| 8829 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8830 | /* This range does overlap */ |
| 8831 | if (BB_OFFSET(p[lo]) < s) { |
| 8832 | /* Keep the early parts of this range. */ |
| 8833 | int ack = BB_ACK(p[lo]); |
| 8834 | sector_t start = BB_OFFSET(p[lo]); |
| 8835 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8836 | /* now low doesn't overlap, so.. */ |
| 8837 | break; |
| 8838 | } |
| 8839 | lo--; |
| 8840 | } |
| 8841 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8842 | * anything between needs to be discarded |
| 8843 | */ |
| 8844 | if (hi - lo > 1) { |
| 8845 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8846 | bb->count -= (hi - lo - 1); |
| 8847 | } |
| 8848 | } |
| 8849 | |
| 8850 | bb->changed = 1; |
| 8851 | out: |
| 8852 | write_sequnlock_irq(&bb->lock); |
| 8853 | return rv; |
| 8854 | } |
| 8855 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8856 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8857 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8858 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8859 | if (is_new) |
| 8860 | s += rdev->new_data_offset; |
| 8861 | else |
| 8862 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8863 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8864 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8865 | } |
| 8866 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8867 | |
| 8868 | /* |
| 8869 | * Acknowledge all bad blocks in a list. |
| 8870 | * This only succeeds if ->changed is clear. It is used by |
| 8871 | * in-kernel metadata updates |
| 8872 | */ |
| 8873 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8874 | { |
| 8875 | if (bb->page == NULL || bb->changed) |
| 8876 | /* no point even trying */ |
| 8877 | return; |
| 8878 | write_seqlock_irq(&bb->lock); |
| 8879 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8880 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8881 | u64 *p = bb->page; |
| 8882 | int i; |
| 8883 | for (i = 0; i < bb->count ; i++) { |
| 8884 | if (!BB_ACK(p[i])) { |
| 8885 | sector_t start = BB_OFFSET(p[i]); |
| 8886 | int len = BB_LEN(p[i]); |
| 8887 | p[i] = BB_MAKE(start, len, 1); |
| 8888 | } |
| 8889 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8890 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8891 | } |
| 8892 | write_sequnlock_irq(&bb->lock); |
| 8893 | } |
| 8894 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8895 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8896 | /* sysfs access to bad-blocks list. |
| 8897 | * We present two files. |
| 8898 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8899 | * are recorded as bad. The list is truncated to fit within |
| 8900 | * the one-page limit of sysfs. |
| 8901 | * Writing "sector length" to this file adds an acknowledged |
| 8902 | * bad block list. |
| 8903 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8904 | * been acknowledged. Writing to this file adds bad blocks |
| 8905 | * without acknowledging them. This is largely for testing. |
| 8906 | */ |
| 8907 | |
| 8908 | static ssize_t |
| 8909 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8910 | { |
| 8911 | size_t len; |
| 8912 | int i; |
| 8913 | u64 *p = bb->page; |
| 8914 | unsigned seq; |
| 8915 | |
| 8916 | if (bb->shift < 0) |
| 8917 | return 0; |
| 8918 | |
| 8919 | retry: |
| 8920 | seq = read_seqbegin(&bb->lock); |
| 8921 | |
| 8922 | len = 0; |
| 8923 | i = 0; |
| 8924 | |
| 8925 | while (len < PAGE_SIZE && i < bb->count) { |
| 8926 | sector_t s = BB_OFFSET(p[i]); |
| 8927 | unsigned int length = BB_LEN(p[i]); |
| 8928 | int ack = BB_ACK(p[i]); |
| 8929 | i++; |
| 8930 | |
| 8931 | if (unack && ack) |
| 8932 | continue; |
| 8933 | |
| 8934 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8935 | (unsigned long long)s << bb->shift, |
| 8936 | length << bb->shift); |
| 8937 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8938 | if (unack && len == 0) |
| 8939 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8940 | |
| 8941 | if (read_seqretry(&bb->lock, seq)) |
| 8942 | goto retry; |
| 8943 | |
| 8944 | return len; |
| 8945 | } |
| 8946 | |
| 8947 | #define DO_DEBUG 1 |
| 8948 | |
| 8949 | static ssize_t |
| 8950 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8951 | { |
| 8952 | unsigned long long sector; |
| 8953 | int length; |
| 8954 | char newline; |
| 8955 | #ifdef DO_DEBUG |
| 8956 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8957 | * Normally only a successful write may clear a badblock |
| 8958 | */ |
| 8959 | int clear = 0; |
| 8960 | if (page[0] == '-') { |
| 8961 | clear = 1; |
| 8962 | page++; |
| 8963 | } |
| 8964 | #endif /* DO_DEBUG */ |
| 8965 | |
| 8966 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8967 | case 3: |
| 8968 | if (newline != '\n') |
| 8969 | return -EINVAL; |
| 8970 | case 2: |
| 8971 | if (length <= 0) |
| 8972 | return -EINVAL; |
| 8973 | break; |
| 8974 | default: |
| 8975 | return -EINVAL; |
| 8976 | } |
| 8977 | |
| 8978 | #ifdef DO_DEBUG |
| 8979 | if (clear) { |
| 8980 | md_clear_badblocks(bb, sector, length); |
| 8981 | return len; |
| 8982 | } |
| 8983 | #endif /* DO_DEBUG */ |
| 8984 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8985 | return len; |
| 8986 | else |
| 8987 | return -ENOSPC; |
| 8988 | } |
| 8989 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8990 | static int md_notify_reboot(struct notifier_block *this, |
| 8991 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8992 | { |
| 8993 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8994 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8995 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8996 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8997 | for_each_mddev(mddev, tmp) { |
| 8998 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8999 | if (mddev->pers) |
| 9000 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 9001 | if (mddev->persistent) |
| 9002 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9003 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 9004 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9005 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9006 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9007 | /* |
| 9008 | * certain more exotic SCSI devices are known to be |
| 9009 | * volatile wrt too early system reboots. While the |
| 9010 | * right place to handle this issue is the given |
| 9011 | * driver, we do want to have a safe RAID driver ... |
| 9012 | */ |
| 9013 | if (need_delay) |
| 9014 | mdelay(1000*1); |
| 9015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9016 | return NOTIFY_DONE; |
| 9017 | } |
| 9018 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9019 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9020 | .notifier_call = md_notify_reboot, |
| 9021 | .next = NULL, |
| 9022 | .priority = INT_MAX, /* before any real devices */ |
| 9023 | }; |
| 9024 | |
| 9025 | static void md_geninit(void) |
| 9026 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 9027 | 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] | 9028 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 9029 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9030 | } |
| 9031 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9032 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9033 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9034 | int ret = -ENOMEM; |
| 9035 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 9036 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9037 | if (!md_wq) |
| 9038 | goto err_wq; |
| 9039 | |
| 9040 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 9041 | if (!md_misc_wq) |
| 9042 | goto err_misc_wq; |
| 9043 | |
| 9044 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 9045 | goto err_md; |
| 9046 | |
| 9047 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 9048 | goto err_mdp; |
| 9049 | mdp_major = ret; |
| 9050 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 9051 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 9052 | md_probe, NULL, NULL); |
| 9053 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9054 | md_probe, NULL, NULL); |
| 9055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9056 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 9057 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9058 | |
| 9059 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 9060 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9061 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9062 | err_mdp: |
| 9063 | unregister_blkdev(MD_MAJOR, "md"); |
| 9064 | err_md: |
| 9065 | destroy_workqueue(md_misc_wq); |
| 9066 | err_misc_wq: |
| 9067 | destroy_workqueue(md_wq); |
| 9068 | err_wq: |
| 9069 | return ret; |
| 9070 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9071 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9072 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9073 | { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9074 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 9075 | struct md_rdev *rdev2; |
| 9076 | int role, ret; |
| 9077 | char b[BDEVNAME_SIZE]; |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9078 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9079 | /* Check for change of roles in the active devices */ |
| 9080 | rdev_for_each(rdev2, mddev) { |
| 9081 | if (test_bit(Faulty, &rdev2->flags)) |
| 9082 | continue; |
| 9083 | |
| 9084 | /* Check if the roles changed */ |
| 9085 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 9086 | |
| 9087 | if (test_bit(Candidate, &rdev2->flags)) { |
| 9088 | if (role == 0xfffe) { |
| 9089 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); |
| 9090 | md_kick_rdev_from_array(rdev2); |
| 9091 | continue; |
| 9092 | } |
| 9093 | else |
| 9094 | clear_bit(Candidate, &rdev2->flags); |
| 9095 | } |
| 9096 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9097 | if (role != rdev2->raid_disk) { |
| 9098 | /* got activated */ |
| 9099 | if (rdev2->raid_disk == -1 && role != 0xffff) { |
| 9100 | rdev2->saved_raid_disk = role; |
| 9101 | ret = remove_and_add_spares(mddev, rdev2); |
| 9102 | pr_info("Activated spare: %s\n", |
| 9103 | bdevname(rdev2->bdev,b)); |
| 9104 | continue; |
| 9105 | } |
| 9106 | /* device faulty |
| 9107 | * We just want to do the minimum to mark the disk |
| 9108 | * as faulty. The recovery is performed by the |
| 9109 | * one who initiated the error. |
| 9110 | */ |
| 9111 | if ((role == 0xfffe) || (role == 0xfffd)) { |
| 9112 | md_error(mddev, rdev2); |
| 9113 | clear_bit(Blocked, &rdev2->flags); |
| 9114 | } |
| 9115 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9116 | } |
| 9117 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 9118 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) |
| 9119 | update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9120 | |
| 9121 | /* Finally set the event to be up to date */ |
| 9122 | mddev->events = le64_to_cpu(sb->events); |
| 9123 | } |
| 9124 | |
| 9125 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) |
| 9126 | { |
| 9127 | int err; |
| 9128 | struct page *swapout = rdev->sb_page; |
| 9129 | struct mdp_superblock_1 *sb; |
| 9130 | |
| 9131 | /* Store the sb page of the rdev in the swapout temporary |
| 9132 | * variable in case we err in the future |
| 9133 | */ |
| 9134 | rdev->sb_page = NULL; |
| 9135 | alloc_disk_sb(rdev); |
| 9136 | ClearPageUptodate(rdev->sb_page); |
| 9137 | rdev->sb_loaded = 0; |
| 9138 | err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version); |
| 9139 | |
| 9140 | if (err < 0) { |
| 9141 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", |
| 9142 | __func__, __LINE__, rdev->desc_nr, err); |
| 9143 | put_page(rdev->sb_page); |
| 9144 | rdev->sb_page = swapout; |
| 9145 | rdev->sb_loaded = 1; |
| 9146 | return err; |
| 9147 | } |
| 9148 | |
| 9149 | sb = page_address(rdev->sb_page); |
| 9150 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET |
| 9151 | * is not set |
| 9152 | */ |
| 9153 | |
| 9154 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) |
| 9155 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 9156 | |
| 9157 | /* The other node finished recovery, call spare_active to set |
| 9158 | * device In_sync and mddev->degraded |
| 9159 | */ |
| 9160 | if (rdev->recovery_offset == MaxSector && |
| 9161 | !test_bit(In_sync, &rdev->flags) && |
| 9162 | mddev->pers->spare_active(mddev)) |
| 9163 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
| 9164 | |
| 9165 | put_page(swapout); |
| 9166 | return 0; |
| 9167 | } |
| 9168 | |
| 9169 | void md_reload_sb(struct mddev *mddev, int nr) |
| 9170 | { |
| 9171 | struct md_rdev *rdev; |
| 9172 | int err; |
| 9173 | |
| 9174 | /* Find the rdev */ |
| 9175 | rdev_for_each_rcu(rdev, mddev) { |
| 9176 | if (rdev->desc_nr == nr) |
| 9177 | break; |
| 9178 | } |
| 9179 | |
| 9180 | if (!rdev || rdev->desc_nr != nr) { |
| 9181 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); |
| 9182 | return; |
| 9183 | } |
| 9184 | |
| 9185 | err = read_rdev(mddev, rdev); |
| 9186 | if (err < 0) |
| 9187 | return; |
| 9188 | |
| 9189 | check_sb_changes(mddev, rdev); |
| 9190 | |
| 9191 | /* Read all rdev's to update recovery_offset */ |
| 9192 | rdev_for_each_rcu(rdev, mddev) |
| 9193 | read_rdev(mddev, rdev); |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9194 | } |
| 9195 | EXPORT_SYMBOL(md_reload_sb); |
| 9196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9197 | #ifndef MODULE |
| 9198 | |
| 9199 | /* |
| 9200 | * Searches all registered partitions for autorun RAID arrays |
| 9201 | * at boot time. |
| 9202 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9203 | |
| 9204 | static LIST_HEAD(all_detected_devices); |
| 9205 | struct detected_devices_node { |
| 9206 | struct list_head list; |
| 9207 | dev_t dev; |
| 9208 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9209 | |
| 9210 | void md_autodetect_dev(dev_t dev) |
| 9211 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9212 | struct detected_devices_node *node_detected_dev; |
| 9213 | |
| 9214 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 9215 | if (node_detected_dev) { |
| 9216 | node_detected_dev->dev = dev; |
| 9217 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 9218 | } else { |
| 9219 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 9220 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 9221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9222 | } |
| 9223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9224 | static void autostart_arrays(int part) |
| 9225 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9226 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9227 | struct detected_devices_node *node_detected_dev; |
| 9228 | dev_t dev; |
| 9229 | int i_scanned, i_passed; |
| 9230 | |
| 9231 | i_scanned = 0; |
| 9232 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9233 | |
| 9234 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 9235 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9236 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 9237 | i_scanned++; |
| 9238 | node_detected_dev = list_entry(all_detected_devices.next, |
| 9239 | struct detected_devices_node, list); |
| 9240 | list_del(&node_detected_dev->list); |
| 9241 | dev = node_detected_dev->dev; |
| 9242 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 9243 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9244 | if (IS_ERR(rdev)) |
| 9245 | continue; |
| 9246 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9247 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9248 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9249 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 9250 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9251 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9252 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9253 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9254 | |
| 9255 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 9256 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9257 | |
| 9258 | autorun_devices(part); |
| 9259 | } |
| 9260 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 9261 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9262 | |
| 9263 | static __exit void md_exit(void) |
| 9264 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9265 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9266 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9267 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 9268 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 9269 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 9270 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9271 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 9272 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9273 | unregister_blkdev(mdp_major, "mdp"); |
| 9274 | unregister_reboot_notifier(&md_notifier); |
| 9275 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9276 | |
| 9277 | /* We cannot unload the modules while some process is |
| 9278 | * waiting for us in select() or poll() - wake them up |
| 9279 | */ |
| 9280 | md_unloading = 1; |
| 9281 | while (waitqueue_active(&md_event_waiters)) { |
| 9282 | /* not safe to leave yet */ |
| 9283 | wake_up(&md_event_waiters); |
| 9284 | msleep(delay); |
| 9285 | delay += delay; |
| 9286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9287 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9288 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 9289 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9290 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9291 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9292 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9293 | destroy_workqueue(md_misc_wq); |
| 9294 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9295 | } |
| 9296 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 9297 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9298 | module_exit(md_exit) |
| 9299 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9300 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 9301 | { |
| 9302 | return sprintf(buffer, "%d", start_readonly); |
| 9303 | } |
| 9304 | static int set_ro(const char *val, struct kernel_param *kp) |
| 9305 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 9306 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9307 | } |
| 9308 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 9309 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 9310 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9311 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9313 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 9314 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 9315 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 9316 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |