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 | ff01bb4 | 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 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 253 | static void 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); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 265 | return; |
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); |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 271 | return; |
| 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 307 | /* mddev_suspend makes sure no new requests are submitted |
| 308 | * to the device, and that any requests that have been submitted |
| 309 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 310 | * Once mddev_detach() is called and completes, the module will be |
| 311 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 312 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 313 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 314 | { |
| 315 | BUG_ON(mddev->suspended); |
| 316 | mddev->suspended = 1; |
| 317 | synchronize_rcu(); |
| 318 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 319 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 320 | |
| 321 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 322 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 323 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 324 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 325 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 326 | { |
| 327 | mddev->suspended = 0; |
| 328 | wake_up(&mddev->sb_wait); |
| 329 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 330 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 331 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 332 | md_wakeup_thread(mddev->thread); |
| 333 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 334 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 335 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 336 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 337 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 338 | { |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 339 | struct md_personality *pers = mddev->pers; |
| 340 | int ret = 0; |
| 341 | |
| 342 | rcu_read_lock(); |
| 343 | if (mddev->suspended) |
| 344 | ret = 1; |
| 345 | else if (pers && pers->congested) |
| 346 | ret = pers->congested(mddev, bits); |
| 347 | rcu_read_unlock(); |
| 348 | return ret; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 349 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 350 | EXPORT_SYMBOL_GPL(mddev_congested); |
| 351 | static int md_congested(void *data, int bits) |
| 352 | { |
| 353 | struct mddev *mddev = data; |
| 354 | return mddev_congested(mddev, bits); |
| 355 | } |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 356 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 357 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 358 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 359 | */ |
| 360 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 361 | static void md_end_flush(struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 362 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 363 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 364 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 365 | |
| 366 | rdev_dec_pending(rdev, mddev); |
| 367 | |
| 368 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 369 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 370 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 371 | } |
| 372 | bio_put(bio); |
| 373 | } |
| 374 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 375 | static void md_submit_flush_data(struct work_struct *ws); |
| 376 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 377 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 378 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 379 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 380 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 381 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 382 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 383 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 384 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 385 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 386 | if (rdev->raid_disk >= 0 && |
| 387 | !test_bit(Faulty, &rdev->flags)) { |
| 388 | /* Take two references, one is dropped |
| 389 | * when request finishes, one after |
| 390 | * we reclaim rcu_read_lock |
| 391 | */ |
| 392 | struct bio *bi; |
| 393 | atomic_inc(&rdev->nr_pending); |
| 394 | atomic_inc(&rdev->nr_pending); |
| 395 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 396 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 397 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 398 | bi->bi_private = rdev; |
| 399 | bi->bi_bdev = rdev->bdev; |
| 400 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 401 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 402 | rcu_read_lock(); |
| 403 | rdev_dec_pending(rdev, mddev); |
| 404 | } |
| 405 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 406 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 407 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 408 | } |
| 409 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 410 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 411 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 412 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 413 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 414 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 415 | if (bio->bi_iter.bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 416 | /* an empty barrier - all done */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 417 | bio_endio(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 418 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 419 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 420 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 421 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 422 | |
| 423 | mddev->flush_bio = NULL; |
| 424 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 425 | } |
| 426 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 427 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 428 | { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 429 | spin_lock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 430 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 431 | !mddev->flush_bio, |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 432 | mddev->lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 433 | mddev->flush_bio = bio; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 434 | spin_unlock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 435 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 436 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 437 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 438 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 439 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 440 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 441 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 442 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 443 | struct mddev *mddev = cb->data; |
| 444 | md_wakeup_thread(mddev->thread); |
| 445 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 446 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 447 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 448 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 449 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | { |
| 451 | atomic_inc(&mddev->active); |
| 452 | return mddev; |
| 453 | } |
| 454 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 455 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 456 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 457 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 459 | struct bio_set *bs = NULL; |
| 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 462 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 463 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 464 | mddev->ctime == 0 && !mddev->hold_active) { |
| 465 | /* Array is not configured at all, and not held active, |
| 466 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 467 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 468 | bs = mddev->bio_set; |
| 469 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 470 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 471 | /* We did a probe so need to clean up. Call |
| 472 | * queue_work inside the spinlock so that |
| 473 | * flush_workqueue() after mddev_find will |
| 474 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 475 | */ |
| 476 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 477 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 478 | } else |
| 479 | kfree(mddev); |
| 480 | } |
| 481 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 482 | if (bs) |
| 483 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 486 | static void md_safemode_timeout(unsigned long data); |
| 487 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 488 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 489 | { |
| 490 | mutex_init(&mddev->open_mutex); |
| 491 | mutex_init(&mddev->reconfig_mutex); |
| 492 | mutex_init(&mddev->bitmap_info.mutex); |
| 493 | INIT_LIST_HEAD(&mddev->disks); |
| 494 | INIT_LIST_HEAD(&mddev->all_mddevs); |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 495 | setup_timer(&mddev->safemode_timer, md_safemode_timeout, |
| 496 | (unsigned long) mddev); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 497 | atomic_set(&mddev->active, 1); |
| 498 | atomic_set(&mddev->openers, 0); |
| 499 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 500 | spin_lock_init(&mddev->lock); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 501 | atomic_set(&mddev->flush_pending, 0); |
| 502 | init_waitqueue_head(&mddev->sb_wait); |
| 503 | init_waitqueue_head(&mddev->recovery_wait); |
| 504 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 505 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 506 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 507 | mddev->resync_min = 0; |
| 508 | mddev->resync_max = MaxSector; |
| 509 | mddev->level = LEVEL_NONE; |
| 510 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 511 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 512 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 513 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 515 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 517 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 518 | unit &= ~((1<<MdpMinorShift)-1); |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | retry: |
| 521 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 523 | if (unit) { |
| 524 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 525 | if (mddev->unit == unit) { |
| 526 | mddev_get(mddev); |
| 527 | spin_unlock(&all_mddevs_lock); |
| 528 | kfree(new); |
| 529 | return mddev; |
| 530 | } |
| 531 | |
| 532 | if (new) { |
| 533 | list_add(&new->all_mddevs, &all_mddevs); |
| 534 | spin_unlock(&all_mddevs_lock); |
| 535 | new->hold_active = UNTIL_IOCTL; |
| 536 | return new; |
| 537 | } |
| 538 | } else if (new) { |
| 539 | /* find an unused unit number */ |
| 540 | static int next_minor = 512; |
| 541 | int start = next_minor; |
| 542 | int is_free = 0; |
| 543 | int dev = 0; |
| 544 | while (!is_free) { |
| 545 | dev = MKDEV(MD_MAJOR, next_minor); |
| 546 | next_minor++; |
| 547 | if (next_minor > MINORMASK) |
| 548 | next_minor = 0; |
| 549 | if (next_minor == start) { |
| 550 | /* Oh dear, all in use. */ |
| 551 | spin_unlock(&all_mddevs_lock); |
| 552 | kfree(new); |
| 553 | return NULL; |
| 554 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 555 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 556 | is_free = 1; |
| 557 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 558 | if (mddev->unit == dev) { |
| 559 | is_free = 0; |
| 560 | break; |
| 561 | } |
| 562 | } |
| 563 | new->unit = dev; |
| 564 | new->md_minor = MINOR(dev); |
| 565 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | list_add(&new->all_mddevs, &all_mddevs); |
| 567 | spin_unlock(&all_mddevs_lock); |
| 568 | return new; |
| 569 | } |
| 570 | spin_unlock(&all_mddevs_lock); |
| 571 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 572 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | if (!new) |
| 574 | return NULL; |
| 575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | new->unit = unit; |
| 577 | if (MAJOR(unit) == MD_MAJOR) |
| 578 | new->md_minor = MINOR(unit); |
| 579 | else |
| 580 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 581 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 582 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | goto retry; |
| 585 | } |
| 586 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 587 | static struct attribute_group md_redundancy_group; |
| 588 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 589 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 591 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 592 | /* These cannot be removed under reconfig_mutex as |
| 593 | * an access to the files will try to take reconfig_mutex |
| 594 | * while holding the file unremovable, which leads to |
| 595 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 596 | * So hold set sysfs_active while the remove in happeing, |
| 597 | * and anything else which might set ->to_remove or my |
| 598 | * otherwise change the sysfs namespace will fail with |
| 599 | * -EBUSY if sysfs_active is still set. |
| 600 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 601 | * test it under the same mutex to ensure its correct value |
| 602 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 603 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 604 | struct attribute_group *to_remove = mddev->to_remove; |
| 605 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 606 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 607 | mutex_unlock(&mddev->reconfig_mutex); |
| 608 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 609 | if (mddev->kobj.sd) { |
| 610 | if (to_remove != &md_redundancy_group) |
| 611 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 612 | if (mddev->pers == NULL || |
| 613 | mddev->pers->sync_request == NULL) { |
| 614 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 615 | if (mddev->sysfs_action) |
| 616 | sysfs_put(mddev->sysfs_action); |
| 617 | mddev->sysfs_action = NULL; |
| 618 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 619 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 620 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 621 | } else |
| 622 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 624 | /* As we've dropped the mutex we need a spinlock to |
| 625 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 626 | */ |
| 627 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 628 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 629 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 631 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 633 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 634 | { |
| 635 | struct md_rdev *rdev; |
| 636 | |
| 637 | rdev_for_each_rcu(rdev, mddev) |
| 638 | if (rdev->desc_nr == nr) |
| 639 | return rdev; |
| 640 | |
| 641 | return NULL; |
| 642 | } |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 643 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 644 | |
| 645 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 647 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 649 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | if (rdev->bdev->bd_dev == dev) |
| 651 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | return NULL; |
| 654 | } |
| 655 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 656 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 657 | { |
| 658 | struct md_rdev *rdev; |
| 659 | |
| 660 | rdev_for_each_rcu(rdev, mddev) |
| 661 | if (rdev->bdev->bd_dev == dev) |
| 662 | return rdev; |
| 663 | |
| 664 | return NULL; |
| 665 | } |
| 666 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 667 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 668 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 669 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 670 | list_for_each_entry(pers, &pers_list, list) { |
| 671 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 672 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 673 | if (strcmp(pers->name, clevel)==0) |
| 674 | return pers; |
| 675 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 676 | return NULL; |
| 677 | } |
| 678 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 679 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 680 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 682 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 683 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
| 685 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 686 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 689 | if (!rdev->sb_page) { |
| 690 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 691 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 697 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | { |
| 699 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 700 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | rdev->sb_loaded = 0; |
| 702 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 703 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 704 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 706 | if (rdev->bb_page) { |
| 707 | put_page(rdev->bb_page); |
| 708 | rdev->bb_page = NULL; |
| 709 | } |
NeilBrown | 4fa2f32 | 2012-05-22 13:55:01 +1000 | [diff] [blame] | 710 | kfree(rdev->badblocks.page); |
| 711 | rdev->badblocks.page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 713 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 715 | static void super_written(struct bio *bio) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 716 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 717 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 718 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 719 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 720 | if (bio->bi_error) { |
| 721 | printk("md: super_written gets error=%d\n", bio->bi_error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 722 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 723 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 724 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 725 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 726 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 727 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 728 | } |
| 729 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 730 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 731 | sector_t sector, int size, struct page *page) |
| 732 | { |
| 733 | /* write first size bytes of page to sector of rdev |
| 734 | * Increment mddev->pending_writes before returning |
| 735 | * and decrement it on completion, waking up sb_wait |
| 736 | * if zero is reached. |
| 737 | * If an error occurred, call md_error |
| 738 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 739 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 740 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 741 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 742 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 743 | bio_add_page(bio, page, size, 0); |
| 744 | bio->bi_private = rdev; |
| 745 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 746 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 747 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 748 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 749 | } |
| 750 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 751 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 752 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 753 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 754 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 755 | } |
| 756 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 757 | 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] | 758 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 760 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | int ret; |
| 762 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 763 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 764 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 765 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 766 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 767 | else if (rdev->mddev->reshape_position != MaxSector && |
| 768 | (rdev->mddev->reshape_backwards == |
| 769 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 770 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 771 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 772 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | bio_add_page(bio, page, size, 0); |
Kent Overstreet | c170bbb | 2013-11-24 16:32:22 -0700 | [diff] [blame] | 774 | submit_bio_wait(rw, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 776 | ret = !bio->bi_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | bio_put(bio); |
| 778 | return ret; |
| 779 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 780 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 782 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | { |
| 784 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | if (rdev->sb_loaded) |
| 787 | return 0; |
| 788 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 789 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | goto fail; |
| 791 | rdev->sb_loaded = 1; |
| 792 | return 0; |
| 793 | |
| 794 | fail: |
| 795 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 796 | bdevname(rdev->bdev,b)); |
| 797 | return -EINVAL; |
| 798 | } |
| 799 | |
| 800 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 801 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 802 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 803 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 804 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 805 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 809 | { |
| 810 | int ret; |
| 811 | mdp_super_t *tmp1, *tmp2; |
| 812 | |
| 813 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 814 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 815 | |
| 816 | if (!tmp1 || !tmp2) { |
| 817 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 818 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | goto abort; |
| 820 | } |
| 821 | |
| 822 | *tmp1 = *sb1; |
| 823 | *tmp2 = *sb2; |
| 824 | |
| 825 | /* |
| 826 | * nr_disks is not constant |
| 827 | */ |
| 828 | tmp1->nr_disks = 0; |
| 829 | tmp2->nr_disks = 0; |
| 830 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 831 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 833 | kfree(tmp1); |
| 834 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | return ret; |
| 836 | } |
| 837 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 838 | static u32 md_csum_fold(u32 csum) |
| 839 | { |
| 840 | csum = (csum & 0xffff) + (csum >> 16); |
| 841 | return (csum & 0xffff) + (csum >> 16); |
| 842 | } |
| 843 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 844 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 846 | u64 newcsum = 0; |
| 847 | u32 *sb32 = (u32*)sb; |
| 848 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | unsigned int disk_csum, csum; |
| 850 | |
| 851 | disk_csum = sb->sb_csum; |
| 852 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 853 | |
| 854 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 855 | newcsum += sb32[i]; |
| 856 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 857 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 858 | #ifdef CONFIG_ALPHA |
| 859 | /* This used to use csum_partial, which was wrong for several |
| 860 | * reasons including that different results are returned on |
| 861 | * different architectures. It isn't critical that we get exactly |
| 862 | * the same return value as before (we always csum_fold before |
| 863 | * testing, and that removes any differences). However as we |
| 864 | * know that csum_partial always returned a 16bit value on |
| 865 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 866 | */ |
| 867 | sb->sb_csum = md_csum_fold(disk_csum); |
| 868 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 870 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | return csum; |
| 872 | } |
| 873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | /* |
| 875 | * Handle superblock details. |
| 876 | * We want to be able to handle multiple superblock formats |
| 877 | * so we have a common interface to them all, and an array of |
| 878 | * different handlers. |
| 879 | * We rely on user-space to write the initial superblock, and support |
| 880 | * reading and updating of superblocks. |
| 881 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 882 | * 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] | 883 | * loads and validates a superblock on dev. |
| 884 | * if refdev != NULL, compare superblocks on both devices |
| 885 | * Return: |
| 886 | * 0 - dev has a superblock that is compatible with refdev |
| 887 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 888 | * so dev should be used as the refdev in future |
| 889 | * -EINVAL superblock incompatible or invalid |
| 890 | * -othererror e.g. -EIO |
| 891 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 892 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | * Verify that dev is acceptable into mddev. |
| 894 | * The first time, mddev->raid_disks will be 0, and data from |
| 895 | * dev should be merged in. Subsequent calls check that dev |
| 896 | * is new enough. Return 0 or -EINVAL |
| 897 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 898 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | * Update the superblock for rdev with data in mddev |
| 900 | * This does not write to disc. |
| 901 | * |
| 902 | */ |
| 903 | |
| 904 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 905 | char *name; |
| 906 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 907 | int (*load_super)(struct md_rdev *rdev, |
| 908 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 909 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 910 | int (*validate_super)(struct mddev *mddev, |
| 911 | struct md_rdev *rdev); |
| 912 | void (*sync_super)(struct mddev *mddev, |
| 913 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 914 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 915 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 916 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 917 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | }; |
| 919 | |
| 920 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 921 | * Check that the given mddev has no bitmap. |
| 922 | * |
| 923 | * This function is called from the run method of all personalities that do not |
| 924 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 925 | * has a bitmap. Otherwise, it returns 0. |
| 926 | * |
| 927 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 928 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 929 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 930 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 931 | return 0; |
| 932 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 933 | mdname(mddev), mddev->pers->name); |
| 934 | return 1; |
| 935 | } |
| 936 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 937 | |
| 938 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 939 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 941 | 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] | 942 | { |
| 943 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 944 | mdp_super_t *sb; |
| 945 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | |
| 947 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 948 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | * it's at the end of the disk. |
| 950 | * |
| 951 | * It also happens to be a multiple of 4Kb. |
| 952 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 953 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 955 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | if (ret) return ret; |
| 957 | |
| 958 | ret = -EINVAL; |
| 959 | |
| 960 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 961 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | |
| 963 | if (sb->md_magic != MD_SB_MAGIC) { |
| 964 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 965 | b); |
| 966 | goto abort; |
| 967 | } |
| 968 | |
| 969 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 970 | sb->minor_version < 90 || |
| 971 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 973 | sb->major_version, sb->minor_version, |
| 974 | b); |
| 975 | goto abort; |
| 976 | } |
| 977 | |
| 978 | if (sb->raid_disks <= 0) |
| 979 | goto abort; |
| 980 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 981 | 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] | 982 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 983 | b); |
| 984 | goto abort; |
| 985 | } |
| 986 | |
| 987 | rdev->preferred_minor = sb->md_minor; |
| 988 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 989 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 990 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 991 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | |
| 993 | if (sb->level == LEVEL_MULTIPATH) |
| 994 | rdev->desc_nr = -1; |
| 995 | else |
| 996 | rdev->desc_nr = sb->this_disk.number; |
| 997 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 998 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1000 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1002 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | if (!uuid_equal(refsb, sb)) { |
| 1004 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1005 | b, bdevname(refdev->bdev,b2)); |
| 1006 | goto abort; |
| 1007 | } |
| 1008 | if (!sb_equal(refsb, sb)) { |
| 1009 | printk(KERN_WARNING "md: %s has same UUID" |
| 1010 | " but different superblock to %s\n", |
| 1011 | b, bdevname(refdev->bdev, b2)); |
| 1012 | goto abort; |
| 1013 | } |
| 1014 | ev1 = md_event(sb); |
| 1015 | ev2 = md_event(refsb); |
| 1016 | if (ev1 > ev2) |
| 1017 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1018 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | ret = 0; |
| 1020 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1021 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1022 | /* Limit to 4TB as metadata cannot record more than that. |
| 1023 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1024 | * record this size) |
| 1025 | */ |
| 1026 | if (rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1027 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1029 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1030 | /* "this cannot possibly happen" ... */ |
| 1031 | ret = -EINVAL; |
| 1032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | abort: |
| 1034 | return ret; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * validate_super for 0.90.0 |
| 1039 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1040 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | { |
| 1042 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1043 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1044 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1046 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1047 | clear_bit(Faulty, &rdev->flags); |
| 1048 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1049 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1050 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | if (mddev->raid_disks == 0) { |
| 1053 | mddev->major_version = 0; |
| 1054 | mddev->minor_version = sb->minor_version; |
| 1055 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1056 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1057 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | mddev->ctime = sb->ctime; |
| 1059 | mddev->utime = sb->utime; |
| 1060 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1061 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | mddev->layout = sb->layout; |
| 1063 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1064 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1065 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1066 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1067 | mddev->bitmap_info.space = 0; |
| 1068 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1069 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1070 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1071 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1073 | if (mddev->minor_version >= 91) { |
| 1074 | mddev->reshape_position = sb->reshape_position; |
| 1075 | mddev->delta_disks = sb->delta_disks; |
| 1076 | mddev->new_level = sb->new_level; |
| 1077 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1078 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1079 | if (mddev->delta_disks < 0) |
| 1080 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1081 | } else { |
| 1082 | mddev->reshape_position = MaxSector; |
| 1083 | mddev->delta_disks = 0; |
| 1084 | mddev->new_level = mddev->level; |
| 1085 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1086 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1087 | } |
| 1088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1090 | mddev->recovery_cp = MaxSector; |
| 1091 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1092 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | sb->events_lo == sb->cp_events_lo) { |
| 1094 | mddev->recovery_cp = sb->recovery_cp; |
| 1095 | } else |
| 1096 | mddev->recovery_cp = 0; |
| 1097 | } |
| 1098 | |
| 1099 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1100 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1101 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1102 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1103 | |
| 1104 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1105 | |
| 1106 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1107 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1108 | mddev->bitmap_info.offset = |
| 1109 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1110 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1111 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1112 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1113 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1114 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1115 | /* Insist on good event counter while assembling, except |
| 1116 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1118 | if (sb->disks[rdev->desc_nr].state & ( |
| 1119 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1120 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1121 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1122 | } else if (mddev->bitmap) { |
| 1123 | /* if adding to array with a bitmap, then we can accept an |
| 1124 | * older device ... but not too old. |
| 1125 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1126 | if (ev1 < mddev->bitmap->events_cleared) |
| 1127 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1128 | if (ev1 < mddev->events) |
| 1129 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1130 | } else { |
| 1131 | if (ev1 < mddev->events) |
| 1132 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1133 | return 0; |
| 1134 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | desc = sb->disks + rdev->desc_nr; |
| 1138 | |
| 1139 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1140 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1141 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1142 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1143 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1145 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1146 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1147 | /* active but not in sync implies recovery up to |
| 1148 | * reshape position. We don't know exactly where |
| 1149 | * that is, so set to zero for now */ |
| 1150 | if (mddev->minor_version >= 91) { |
| 1151 | rdev->recovery_offset = 0; |
| 1152 | rdev->raid_disk = desc->raid_disk; |
| 1153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1155 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1156 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1157 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1158 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * sync_super for 0.90.0 |
| 1164 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1165 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | { |
| 1167 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1168 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | /* make rdev->sb match mddev data.. |
| 1172 | * |
| 1173 | * 1/ zero out disks |
| 1174 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1175 | * 3/ any empty disks < next_spare become removed |
| 1176 | * |
| 1177 | * disks[0] gets initialised to REMOVED because |
| 1178 | * we cannot be sure from other fields if it has |
| 1179 | * been initialised or not. |
| 1180 | */ |
| 1181 | int i; |
| 1182 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1183 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1184 | rdev->sb_size = MD_SB_BYTES; |
| 1185 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1186 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
| 1188 | memset(sb, 0, sizeof(*sb)); |
| 1189 | |
| 1190 | sb->md_magic = MD_SB_MAGIC; |
| 1191 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | sb->patch_version = mddev->patch_version; |
| 1193 | sb->gvalid_words = 0; /* ignored */ |
| 1194 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1195 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1196 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1197 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1198 | |
| 1199 | sb->ctime = mddev->ctime; |
| 1200 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1201 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | sb->raid_disks = mddev->raid_disks; |
| 1203 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1204 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | sb->utime = mddev->utime; |
| 1206 | sb->state = 0; |
| 1207 | sb->events_hi = (mddev->events>>32); |
| 1208 | sb->events_lo = (u32)mddev->events; |
| 1209 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1210 | if (mddev->reshape_position == MaxSector) |
| 1211 | sb->minor_version = 90; |
| 1212 | else { |
| 1213 | sb->minor_version = 91; |
| 1214 | sb->reshape_position = mddev->reshape_position; |
| 1215 | sb->new_level = mddev->new_level; |
| 1216 | sb->delta_disks = mddev->delta_disks; |
| 1217 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1218 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1219 | } |
| 1220 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | if (mddev->in_sync) |
| 1222 | { |
| 1223 | sb->recovery_cp = mddev->recovery_cp; |
| 1224 | sb->cp_events_hi = (mddev->events>>32); |
| 1225 | sb->cp_events_lo = (u32)mddev->events; |
| 1226 | if (mddev->recovery_cp == MaxSector) |
| 1227 | sb->state = (1<< MD_SB_CLEAN); |
| 1228 | } else |
| 1229 | sb->recovery_cp = 0; |
| 1230 | |
| 1231 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1232 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1234 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1235 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1238 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1240 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1241 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1242 | |
| 1243 | if (rdev2->raid_disk >= 0 && |
| 1244 | sb->minor_version >= 91) |
| 1245 | /* we have nowhere to store the recovery_offset, |
| 1246 | * but if it is not below the reshape_position, |
| 1247 | * we can piggy-back on that. |
| 1248 | */ |
| 1249 | is_active = 1; |
| 1250 | if (rdev2->raid_disk < 0 || |
| 1251 | test_bit(Faulty, &rdev2->flags)) |
| 1252 | is_active = 0; |
| 1253 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1254 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1256 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1257 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | d = &sb->disks[rdev2->desc_nr]; |
| 1259 | nr_disks++; |
| 1260 | d->number = rdev2->desc_nr; |
| 1261 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1262 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1263 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | d->raid_disk = rdev2->raid_disk; |
| 1265 | else |
| 1266 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1267 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1269 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1271 | if (test_bit(In_sync, &rdev2->flags)) |
| 1272 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | active++; |
| 1274 | working++; |
| 1275 | } else { |
| 1276 | d->state = 0; |
| 1277 | spare++; |
| 1278 | working++; |
| 1279 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1280 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1281 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1284 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1285 | mdp_disk_t *d = &sb->disks[i]; |
| 1286 | if (d->state == 0 && d->number == 0) { |
| 1287 | d->number = i; |
| 1288 | d->raid_disk = i; |
| 1289 | d->state = (1<<MD_DISK_REMOVED); |
| 1290 | d->state |= (1<<MD_DISK_FAULTY); |
| 1291 | failed++; |
| 1292 | } |
| 1293 | } |
| 1294 | sb->nr_disks = nr_disks; |
| 1295 | sb->active_disks = active; |
| 1296 | sb->working_disks = working; |
| 1297 | sb->failed_disks = failed; |
| 1298 | sb->spare_disks = spare; |
| 1299 | |
| 1300 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1301 | sb->sb_csum = calc_sb_csum(sb); |
| 1302 | } |
| 1303 | |
| 1304 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1305 | * rdev_size_change for 0.90.0 |
| 1306 | */ |
| 1307 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1308 | 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] | 1309 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1310 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1311 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1312 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1313 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1314 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1315 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1316 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1317 | /* Limit to 4TB as metadata cannot record more than that. |
| 1318 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1319 | */ |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1320 | if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1321 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1322 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1323 | rdev->sb_page); |
| 1324 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1325 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1326 | } |
| 1327 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1328 | static int |
| 1329 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1330 | { |
| 1331 | /* non-zero offset changes not possible with v0.90 */ |
| 1332 | return new_offset == 0; |
| 1333 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1334 | |
| 1335 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | * version 1 superblock |
| 1337 | */ |
| 1338 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1339 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1341 | __le32 disk_csum; |
| 1342 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | unsigned long long newcsum; |
| 1344 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1345 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | |
| 1347 | disk_csum = sb->sb_csum; |
| 1348 | sb->sb_csum = 0; |
| 1349 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1350 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | newcsum += le32_to_cpu(*isuper++); |
| 1352 | |
| 1353 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1354 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | |
| 1356 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1357 | sb->sb_csum = disk_csum; |
| 1358 | return cpu_to_le32(csum); |
| 1359 | } |
| 1360 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1361 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1362 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1363 | 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] | 1364 | { |
| 1365 | struct mdp_superblock_1 *sb; |
| 1366 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1367 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1368 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1370 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
| 1372 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1373 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | * It is always aligned to a 4K boundary and |
| 1375 | * depeding on minor_version, it can be: |
| 1376 | * 0: At least 8K, but less than 12K, from end of device |
| 1377 | * 1: At start of device |
| 1378 | * 2: 4K from start of device. |
| 1379 | */ |
| 1380 | switch(minor_version) { |
| 1381 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1382 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1383 | sb_start -= 8*2; |
| 1384 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | break; |
| 1386 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1387 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | break; |
| 1389 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1390 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | break; |
| 1392 | default: |
| 1393 | return -EINVAL; |
| 1394 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1395 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1397 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1398 | * and it is safe to read 4k, so we do that |
| 1399 | */ |
| 1400 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | if (ret) return ret; |
| 1402 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1403 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
| 1405 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1406 | sb->major_version != cpu_to_le32(1) || |
| 1407 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1408 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1409 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | return -EINVAL; |
| 1411 | |
| 1412 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1413 | printk("md: invalid superblock checksum on %s\n", |
| 1414 | bdevname(rdev->bdev,b)); |
| 1415 | return -EINVAL; |
| 1416 | } |
| 1417 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1418 | printk("md: data_size too small on %s\n", |
| 1419 | bdevname(rdev->bdev,b)); |
| 1420 | return -EINVAL; |
| 1421 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1422 | if (sb->pad0 || |
| 1423 | sb->pad3[0] || |
| 1424 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1425 | /* Some padding is non-zero, might be a new feature */ |
| 1426 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | rdev->preferred_minor = 0xffff; |
| 1429 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1430 | rdev->new_data_offset = rdev->data_offset; |
| 1431 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1432 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1433 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1434 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1436 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1437 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1438 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1439 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1440 | |
| 1441 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1442 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1443 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1444 | if (minor_version |
| 1445 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1446 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1447 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1448 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1449 | rdev->desc_nr = -1; |
| 1450 | else |
| 1451 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1452 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1453 | if (!rdev->bb_page) { |
| 1454 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1455 | if (!rdev->bb_page) |
| 1456 | return -ENOMEM; |
| 1457 | } |
| 1458 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1459 | rdev->badblocks.count == 0) { |
| 1460 | /* need to load the bad block list. |
| 1461 | * Currently we limit it to one page. |
| 1462 | */ |
| 1463 | s32 offset; |
| 1464 | sector_t bb_sector; |
| 1465 | u64 *bbp; |
| 1466 | int i; |
| 1467 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1468 | if (sectors > (PAGE_SIZE / 512)) |
| 1469 | return -EINVAL; |
| 1470 | offset = le32_to_cpu(sb->bblog_offset); |
| 1471 | if (offset == 0) |
| 1472 | return -EINVAL; |
| 1473 | bb_sector = (long long)offset; |
| 1474 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1475 | rdev->bb_page, READ, true)) |
| 1476 | return -EIO; |
| 1477 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1478 | rdev->badblocks.shift = sb->bblog_shift; |
| 1479 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1480 | u64 bb = le64_to_cpu(*bbp); |
| 1481 | int count = bb & (0x3ff); |
| 1482 | u64 sector = bb >> 10; |
| 1483 | sector <<= sb->bblog_shift; |
| 1484 | count <<= sb->bblog_shift; |
| 1485 | if (bb + 1 == 0) |
| 1486 | break; |
| 1487 | if (md_set_badblocks(&rdev->badblocks, |
| 1488 | sector, count, 1) == 0) |
| 1489 | return -EINVAL; |
| 1490 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1491 | } else if (sb->bblog_offset != 0) |
| 1492 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1493 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1494 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1495 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1496 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1498 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | |
| 1500 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1501 | sb->level != refsb->level || |
| 1502 | sb->layout != refsb->layout || |
| 1503 | sb->chunksize != refsb->chunksize) { |
| 1504 | printk(KERN_WARNING "md: %s has strangely different" |
| 1505 | " superblock to %s\n", |
| 1506 | bdevname(rdev->bdev,b), |
| 1507 | bdevname(refdev->bdev,b2)); |
| 1508 | return -EINVAL; |
| 1509 | } |
| 1510 | ev1 = le64_to_cpu(sb->events); |
| 1511 | ev2 = le64_to_cpu(refsb->events); |
| 1512 | |
| 1513 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1514 | ret = 1; |
| 1515 | else |
| 1516 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1518 | if (minor_version) { |
| 1519 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1520 | sectors -= rdev->data_offset; |
| 1521 | } else |
| 1522 | sectors = rdev->sb_start; |
| 1523 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1525 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1526 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1529 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1531 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1532 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1534 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1535 | clear_bit(Faulty, &rdev->flags); |
| 1536 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1537 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1538 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | if (mddev->raid_disks == 0) { |
| 1541 | mddev->major_version = 1; |
| 1542 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1543 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1544 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1546 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1547 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1548 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | mddev->layout = le32_to_cpu(sb->layout); |
| 1550 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1551 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1552 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1553 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1554 | mddev->bitmap_info.space = 0; |
| 1555 | /* Default location for bitmap is 1K after superblock |
| 1556 | * using 3K - total of 4K |
| 1557 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1558 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1559 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1560 | mddev->reshape_backwards = 0; |
| 1561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1563 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1564 | |
| 1565 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1566 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1567 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1568 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1569 | mddev->bitmap_info.offset = |
| 1570 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1571 | /* Metadata doesn't record how much space is available. |
| 1572 | * For 1.0, we assume we can use up to the superblock |
| 1573 | * if before, else to 4K beyond superblock. |
| 1574 | * For others, assume no change is possible. |
| 1575 | */ |
| 1576 | if (mddev->minor_version > 0) |
| 1577 | mddev->bitmap_info.space = 0; |
| 1578 | else if (mddev->bitmap_info.offset > 0) |
| 1579 | mddev->bitmap_info.space = |
| 1580 | 8 - mddev->bitmap_info.offset; |
| 1581 | else |
| 1582 | mddev->bitmap_info.space = |
| 1583 | -mddev->bitmap_info.offset; |
| 1584 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1585 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1586 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1587 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1588 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1589 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1590 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1591 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1592 | if (mddev->delta_disks < 0 || |
| 1593 | (mddev->delta_disks == 0 && |
| 1594 | (le32_to_cpu(sb->feature_map) |
| 1595 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1596 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1597 | } else { |
| 1598 | mddev->reshape_position = MaxSector; |
| 1599 | mddev->delta_disks = 0; |
| 1600 | mddev->new_level = mddev->level; |
| 1601 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1602 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1603 | } |
| 1604 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1605 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1606 | /* Insist of good event counter while assembling, except for |
| 1607 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1609 | if (rdev->desc_nr >= 0 && |
| 1610 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1611 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1612 | if (ev1 < mddev->events) |
| 1613 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1614 | } else if (mddev->bitmap) { |
| 1615 | /* If adding to array with a bitmap, then we can accept an |
| 1616 | * older device, but not too old. |
| 1617 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1618 | if (ev1 < mddev->bitmap->events_cleared) |
| 1619 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1620 | if (ev1 < mddev->events) |
| 1621 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1622 | } else { |
| 1623 | if (ev1 < mddev->events) |
| 1624 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1625 | return 0; |
| 1626 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1628 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1629 | if (rdev->desc_nr < 0 || |
| 1630 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1631 | role = MD_DISK_ROLE_SPARE; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1632 | rdev->desc_nr = -1; |
| 1633 | } else |
| 1634 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | switch(role) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1636 | case MD_DISK_ROLE_SPARE: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | break; |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1638 | case MD_DISK_ROLE_FAULTY: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1639 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | break; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1641 | case MD_DISK_ROLE_JOURNAL: /* journal device */ |
| 1642 | if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) { |
| 1643 | /* journal device without journal feature */ |
| 1644 | printk(KERN_WARNING |
| 1645 | "md: journal device provided without journal feature, ignoring the device\n"); |
| 1646 | return -EINVAL; |
| 1647 | } |
| 1648 | set_bit(Journal, &rdev->flags); |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame^] | 1649 | rdev->journal_tail = le64_to_cpu(sb->journal_tail); |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1650 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1652 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1653 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1654 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1655 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1656 | if (!(le32_to_cpu(sb->feature_map) & |
| 1657 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1658 | rdev->saved_raid_disk = -1; |
| 1659 | } else |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1660 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | rdev->raid_disk = role; |
| 1662 | break; |
| 1663 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1664 | if (sb->devflags & WriteMostly1) |
| 1665 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1666 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1667 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1668 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1669 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | return 0; |
| 1672 | } |
| 1673 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1674 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | { |
| 1676 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1677 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | int max_dev, i; |
| 1679 | /* make rdev->sb match mddev and rdev data. */ |
| 1680 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1681 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | |
| 1683 | sb->feature_map = 0; |
| 1684 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1685 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1687 | |
| 1688 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1689 | sb->events = cpu_to_le64(mddev->events); |
| 1690 | if (mddev->in_sync) |
| 1691 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1692 | else |
| 1693 | sb->resync_offset = cpu_to_le64(0); |
| 1694 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1695 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1696 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1697 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1698 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1699 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1700 | sb->level = cpu_to_le32(mddev->level); |
| 1701 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1702 | |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1703 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1704 | sb->devflags |= WriteMostly1; |
| 1705 | else |
| 1706 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1707 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1708 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1709 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1710 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1711 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1712 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1713 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1714 | |
| 1715 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1716 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1717 | sb->feature_map |= |
| 1718 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1719 | sb->recovery_offset = |
| 1720 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1721 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 1722 | sb->feature_map |= |
| 1723 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1724 | } |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame^] | 1725 | /* Note: recovery_offset and journal_tail share space */ |
| 1726 | if (test_bit(Journal, &rdev->flags)) |
| 1727 | sb->journal_tail = cpu_to_le64(rdev->journal_tail); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1728 | if (test_bit(Replacement, &rdev->flags)) |
| 1729 | sb->feature_map |= |
| 1730 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1731 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1732 | if (mddev->reshape_position != MaxSector) { |
| 1733 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1734 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1735 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1736 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1737 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1738 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1739 | if (mddev->delta_disks == 0 && |
| 1740 | mddev->reshape_backwards) |
| 1741 | sb->feature_map |
| 1742 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1743 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1744 | sb->feature_map |
| 1745 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1746 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1747 | - rdev->data_offset)); |
| 1748 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1749 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1750 | |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 1751 | if (mddev_is_clustered(mddev)) |
| 1752 | sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED); |
| 1753 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1754 | if (rdev->badblocks.count == 0) |
| 1755 | /* Nothing to do for bad blocks*/ ; |
| 1756 | else if (sb->bblog_offset == 0) |
| 1757 | /* Cannot record bad blocks on this device */ |
| 1758 | md_error(mddev, rdev); |
| 1759 | else { |
| 1760 | struct badblocks *bb = &rdev->badblocks; |
| 1761 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1762 | u64 *p = bb->page; |
| 1763 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1764 | if (bb->changed) { |
| 1765 | unsigned seq; |
| 1766 | |
| 1767 | retry: |
| 1768 | seq = read_seqbegin(&bb->lock); |
| 1769 | |
| 1770 | memset(bbp, 0xff, PAGE_SIZE); |
| 1771 | |
| 1772 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1773 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1774 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1775 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1776 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1777 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1778 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1779 | if (read_seqretry(&bb->lock, seq)) |
| 1780 | goto retry; |
| 1781 | |
| 1782 | bb->sector = (rdev->sb_start + |
| 1783 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1784 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1785 | } |
| 1786 | } |
| 1787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1789 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | if (rdev2->desc_nr+1 > max_dev) |
| 1791 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1792 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1793 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1794 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1795 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1796 | rdev->sb_size = max_dev * 2 + 256; |
| 1797 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1798 | if (rdev->sb_size & bmask) |
| 1799 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1800 | } else |
| 1801 | max_dev = le32_to_cpu(sb->max_dev); |
| 1802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | for (i=0; i<max_dev;i++) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1804 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1805 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1806 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1808 | if (test_bit(Faulty, &rdev2->flags)) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1809 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1810 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1812 | else if (test_bit(Journal, &rdev2->flags)) { |
| 1813 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL); |
| 1814 | sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL); |
| 1815 | } else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1816 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | else |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1818 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1822 | } |
| 1823 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1824 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1825 | 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] | 1826 | { |
| 1827 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1828 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1829 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1830 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1831 | if (rdev->data_offset != rdev->new_data_offset) |
| 1832 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1833 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1834 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1835 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1836 | max_sectors -= rdev->data_offset; |
| 1837 | if (!num_sectors || num_sectors > max_sectors) |
| 1838 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1839 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1840 | /* minor version 0 with bitmap we can't move */ |
| 1841 | return 0; |
| 1842 | } else { |
| 1843 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1844 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1845 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1846 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1847 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1848 | if (!num_sectors || num_sectors > max_sectors) |
| 1849 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1850 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1851 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1852 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1853 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1854 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1855 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1856 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1857 | rdev->sb_page); |
| 1858 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1859 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1860 | |
| 1861 | } |
| 1862 | |
| 1863 | static int |
| 1864 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1865 | unsigned long long new_offset) |
| 1866 | { |
| 1867 | /* All necessary checks on new >= old have been done */ |
| 1868 | struct bitmap *bitmap; |
| 1869 | if (new_offset >= rdev->data_offset) |
| 1870 | return 1; |
| 1871 | |
| 1872 | /* with 1.0 metadata, there is no metadata to tread on |
| 1873 | * so we can always move back */ |
| 1874 | if (rdev->mddev->minor_version == 0) |
| 1875 | return 1; |
| 1876 | |
| 1877 | /* otherwise we must be sure not to step on |
| 1878 | * any metadata, so stay: |
| 1879 | * 36K beyond start of superblock |
| 1880 | * beyond end of badblocks |
| 1881 | * beyond write-intent bitmap |
| 1882 | */ |
| 1883 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1884 | return 0; |
| 1885 | bitmap = rdev->mddev->bitmap; |
| 1886 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1887 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1888 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1889 | return 0; |
| 1890 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1891 | return 0; |
| 1892 | |
| 1893 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1894 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1896 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | [0] = { |
| 1898 | .name = "0.90.0", |
| 1899 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1900 | .load_super = super_90_load, |
| 1901 | .validate_super = super_90_validate, |
| 1902 | .sync_super = super_90_sync, |
| 1903 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1904 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | }, |
| 1906 | [1] = { |
| 1907 | .name = "md-1", |
| 1908 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1909 | .load_super = super_1_load, |
| 1910 | .validate_super = super_1_validate, |
| 1911 | .sync_super = super_1_sync, |
| 1912 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1913 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | }, |
| 1915 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1917 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1918 | { |
| 1919 | if (mddev->sync_super) { |
| 1920 | mddev->sync_super(mddev, rdev); |
| 1921 | return; |
| 1922 | } |
| 1923 | |
| 1924 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1925 | |
| 1926 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1927 | } |
| 1928 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1929 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1931 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1933 | rcu_read_lock(); |
| 1934 | rdev_for_each_rcu(rdev, mddev1) |
| 1935 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1936 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1937 | rdev2->bdev->bd_contains) { |
| 1938 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1939 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1940 | } |
| 1941 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | return 0; |
| 1943 | } |
| 1944 | |
| 1945 | static LIST_HEAD(pending_raid_disks); |
| 1946 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1947 | /* |
| 1948 | * Try to register data integrity profile for an mddev |
| 1949 | * |
| 1950 | * This is called when an array is started and after a disk has been kicked |
| 1951 | * from the array. It only succeeds if all working and active component devices |
| 1952 | * are integrity capable with matching profiles. |
| 1953 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1954 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1955 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1956 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1957 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1958 | if (list_empty(&mddev->disks)) |
| 1959 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1960 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1961 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1962 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1963 | /* skip spares and non-functional disks */ |
| 1964 | if (test_bit(Faulty, &rdev->flags)) |
| 1965 | continue; |
| 1966 | if (rdev->raid_disk < 0) |
| 1967 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1968 | if (!reference) { |
| 1969 | /* Use the first rdev as the reference */ |
| 1970 | reference = rdev; |
| 1971 | continue; |
| 1972 | } |
| 1973 | /* does this rdev's profile match the reference profile? */ |
| 1974 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1975 | rdev->bdev->bd_disk) < 0) |
| 1976 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1977 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1978 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1979 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1980 | /* |
| 1981 | * All component devices are integrity capable and have matching |
| 1982 | * profiles, register the common profile for the md device. |
| 1983 | */ |
| 1984 | if (blk_integrity_register(mddev->gendisk, |
| 1985 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1986 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1987 | mdname(mddev)); |
| 1988 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1989 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1990 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1991 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1992 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1993 | mdname(mddev)); |
| 1994 | return -EINVAL; |
| 1995 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1996 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1997 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1998 | EXPORT_SYMBOL(md_integrity_register); |
| 1999 | |
| 2000 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2001 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2002 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2003 | struct blk_integrity *bi_rdev; |
| 2004 | struct blk_integrity *bi_mddev; |
| 2005 | |
| 2006 | if (!mddev->gendisk) |
| 2007 | return; |
| 2008 | |
| 2009 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2010 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2011 | |
| 2012 | if (!bi_mddev) /* nothing to do */ |
| 2013 | return; |
| 2014 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2015 | return; |
| 2016 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2017 | rdev->bdev->bd_disk) >= 0) |
| 2018 | return; |
| 2019 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2020 | blk_integrity_unregister(mddev->gendisk); |
| 2021 | } |
| 2022 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2023 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2024 | 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] | 2025 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2026 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2027 | struct kobject *ko; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2028 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2030 | /* prevent duplicates */ |
| 2031 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2032 | return -EEXIST; |
| 2033 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2034 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2035 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2036 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2037 | if (mddev->pers) { |
| 2038 | /* Cannot change size, so fail |
| 2039 | * If mddev->level <= 0, then we don't care |
| 2040 | * about aligning sizes (e.g. linear) |
| 2041 | */ |
| 2042 | if (mddev->level > 0) |
| 2043 | return -ENOSPC; |
| 2044 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2045 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2046 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | |
| 2048 | /* Verify rdev->desc_nr is unique. |
| 2049 | * If it is -1, assign a free number, else |
| 2050 | * check number is not in use |
| 2051 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2052 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | if (rdev->desc_nr < 0) { |
| 2054 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2055 | if (mddev->pers) |
| 2056 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2057 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | choice++; |
| 2059 | rdev->desc_nr = choice; |
| 2060 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2061 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2062 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2064 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2066 | rcu_read_unlock(); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2067 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2068 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2069 | mdname(mddev), mddev->max_disks); |
| 2070 | return -EBUSY; |
| 2071 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2072 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2073 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2076 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2077 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2078 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2079 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2080 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2081 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2082 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2083 | /* failure here is OK */; |
| 2084 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2085 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2086 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2087 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2088 | |
| 2089 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2090 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2093 | |
| 2094 | fail: |
| 2095 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2096 | b, mdname(mddev)); |
| 2097 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | } |
| 2099 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2100 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2101 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2102 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2103 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2104 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2105 | } |
| 2106 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2107 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | { |
| 2109 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2110 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2111 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2112 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2114 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2115 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2116 | sysfs_put(rdev->sysfs_state); |
| 2117 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2118 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2119 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2120 | * writing to 'remove' to "dev/state". We also need |
| 2121 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2122 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2123 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2124 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2125 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2126 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | /* |
| 2130 | * prevent the device from being mounted, repartitioned or |
| 2131 | * otherwise reused by a RAID array (or any other kernel |
| 2132 | * subsystem), by bd_claiming the device. |
| 2133 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2134 | 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] | 2135 | { |
| 2136 | int err = 0; |
| 2137 | struct block_device *bdev; |
| 2138 | char b[BDEVNAME_SIZE]; |
| 2139 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2140 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2141 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | if (IS_ERR(bdev)) { |
| 2143 | printk(KERN_ERR "md: could not open %s.\n", |
| 2144 | __bdevname(dev, b)); |
| 2145 | return PTR_ERR(bdev); |
| 2146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | rdev->bdev = bdev; |
| 2148 | return err; |
| 2149 | } |
| 2150 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2151 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | { |
| 2153 | struct block_device *bdev = rdev->bdev; |
| 2154 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2155 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | void md_autodetect_dev(dev_t dev); |
| 2159 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2160 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | { |
| 2162 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2165 | bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2166 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2168 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2169 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | #endif |
| 2171 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2172 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | } |
| 2174 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2175 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | { |
| 2177 | unbind_rdev_from_array(rdev); |
| 2178 | export_rdev(rdev); |
| 2179 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2180 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2182 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2184 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2186 | while (!list_empty(&mddev->disks)) { |
| 2187 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2188 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2189 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | mddev->raid_disks = 0; |
| 2192 | mddev->major_version = 0; |
| 2193 | } |
| 2194 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2195 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2197 | /* Update each superblock (in-memory image), but |
| 2198 | * if we are allowed to, skip spares which already |
| 2199 | * have the right event counter, or have one earlier |
| 2200 | * (which would mean they aren't being marked as dirty |
| 2201 | * with the rest of the array) |
| 2202 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2203 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2204 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2205 | if (rdev->sb_events == mddev->events || |
| 2206 | (nospares && |
| 2207 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2208 | rdev->sb_events+1 == mddev->events)) { |
| 2209 | /* Don't update this superblock */ |
| 2210 | rdev->sb_loaded = 2; |
| 2211 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2212 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2213 | rdev->sb_loaded = 1; |
| 2214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | } |
| 2216 | } |
| 2217 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2218 | static bool does_sb_need_changing(struct mddev *mddev) |
| 2219 | { |
| 2220 | struct md_rdev *rdev; |
| 2221 | struct mdp_superblock_1 *sb; |
| 2222 | int role; |
| 2223 | |
| 2224 | /* Find a good rdev */ |
| 2225 | rdev_for_each(rdev, mddev) |
| 2226 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) |
| 2227 | break; |
| 2228 | |
| 2229 | /* No good device found. */ |
| 2230 | if (!rdev) |
| 2231 | return false; |
| 2232 | |
| 2233 | sb = page_address(rdev->sb_page); |
| 2234 | /* Check if a device has become faulty or a spare become active */ |
| 2235 | rdev_for_each(rdev, mddev) { |
| 2236 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 2237 | /* Device activated? */ |
| 2238 | if (role == 0xffff && rdev->raid_disk >=0 && |
| 2239 | !test_bit(Faulty, &rdev->flags)) |
| 2240 | return true; |
| 2241 | /* Device turned faulty? */ |
| 2242 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) |
| 2243 | return true; |
| 2244 | } |
| 2245 | |
| 2246 | /* Check if any mddev parameters have changed */ |
| 2247 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || |
| 2248 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2249 | (mddev->layout != le64_to_cpu(sb->layout)) || |
| 2250 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || |
| 2251 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) |
| 2252 | return true; |
| 2253 | |
| 2254 | return false; |
| 2255 | } |
| 2256 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2257 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2259 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2260 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2261 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2262 | int any_badblocks_changed = 0; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2263 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2265 | if (mddev->ro) { |
| 2266 | if (force_change) |
| 2267 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2268 | return; |
| 2269 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2270 | |
| 2271 | if (mddev_is_clustered(mddev)) { |
| 2272 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2273 | force_change = 1; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2274 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2275 | /* Has someone else has updated the sb */ |
| 2276 | if (!does_sb_need_changing(mddev)) { |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2277 | if (ret == 0) |
| 2278 | md_cluster_ops->metadata_update_cancel(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2279 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2280 | return; |
| 2281 | } |
| 2282 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2284 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2285 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2286 | if (rdev->raid_disk >= 0 && |
| 2287 | mddev->delta_disks >= 0 && |
| 2288 | !test_bit(In_sync, &rdev->flags) && |
| 2289 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2290 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2291 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2292 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2293 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2294 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2295 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2296 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2297 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2298 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2299 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2300 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2301 | md_ack_all_badblocks(&rdev->badblocks); |
| 2302 | md_error(mddev, rdev); |
| 2303 | } |
| 2304 | clear_bit(Blocked, &rdev->flags); |
| 2305 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2306 | wake_up(&rdev->blocked_wait); |
| 2307 | } |
| 2308 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2309 | wake_up(&mddev->sb_wait); |
| 2310 | return; |
| 2311 | } |
| 2312 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2313 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2314 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2315 | mddev->utime = get_seconds(); |
| 2316 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2317 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2318 | force_change = 1; |
| 2319 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2320 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2321 | * though if events isn't the right even/odd, we will have to do |
| 2322 | * spares after all |
| 2323 | */ |
| 2324 | nospares = 1; |
| 2325 | if (force_change) |
| 2326 | nospares = 0; |
| 2327 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2328 | /* If the array is degraded, then skipping spares is both |
| 2329 | * dangerous and fairly pointless. |
| 2330 | * Dangerous because a device that was removed from the array |
| 2331 | * might have a event_count that still looks up-to-date, |
| 2332 | * so it can be re-added without a resync. |
| 2333 | * Pointless because if there are any spares to skip, |
| 2334 | * then a recovery will happen and soon that array won't |
| 2335 | * be degraded any more and the spare can go back to sleep then. |
| 2336 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2337 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2338 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2339 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2340 | |
| 2341 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2342 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2343 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2344 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2345 | && mddev->can_decrease_events |
| 2346 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2347 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2348 | mddev->can_decrease_events = 0; |
| 2349 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2350 | /* otherwise we have to go forward and ... */ |
| 2351 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2352 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2353 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2355 | /* |
| 2356 | * This 64-bit counter should never wrap. |
| 2357 | * Either we are in around ~1 trillion A.C., assuming |
| 2358 | * 1 reboot per second, or we have a bug... |
| 2359 | */ |
| 2360 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2361 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2362 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2363 | if (rdev->badblocks.changed) |
| 2364 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2365 | if (test_bit(Faulty, &rdev->flags)) |
| 2366 | set_bit(FaultRecorded, &rdev->flags); |
| 2367 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2368 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2369 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2370 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2372 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2373 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2375 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2376 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2378 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2379 | if (rdev->sb_loaded != 1) |
| 2380 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2382 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2383 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2384 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2385 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2386 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2387 | bdevname(rdev->bdev, b), |
| 2388 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2389 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2390 | if (rdev->badblocks.size) { |
| 2391 | md_super_write(mddev, rdev, |
| 2392 | rdev->badblocks.sector, |
| 2393 | rdev->badblocks.size << 9, |
| 2394 | rdev->bb_page); |
| 2395 | rdev->badblocks.size = 0; |
| 2396 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2397 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2398 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2399 | pr_debug("md: %s (skipping faulty)\n", |
| 2400 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2401 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2402 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | /* only need to write one superblock... */ |
| 2404 | break; |
| 2405 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2406 | md_super_wait(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2407 | /* 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] | 2408 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2409 | spin_lock(&mddev->lock); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2410 | if (mddev->in_sync != sync_req || |
| 2411 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2412 | /* have to write it out again */ |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2413 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2414 | goto repeat; |
| 2415 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2416 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2417 | spin_unlock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2418 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2419 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2420 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2421 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2422 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2423 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2424 | clear_bit(Blocked, &rdev->flags); |
| 2425 | |
| 2426 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2427 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2428 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2429 | wake_up(&rdev->blocked_wait); |
| 2430 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2431 | |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2432 | if (mddev_is_clustered(mddev) && ret == 0) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2433 | md_cluster_ops->metadata_update_finish(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2435 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2437 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2438 | { |
| 2439 | struct mddev *mddev = rdev->mddev; |
| 2440 | int err = 0; |
| 2441 | |
| 2442 | if (!mddev->pers->hot_remove_disk) { |
| 2443 | /* If there is hot_add_disk but no hot_remove_disk |
| 2444 | * then added disks for geometry changes, |
| 2445 | * and should be added immediately. |
| 2446 | */ |
| 2447 | super_types[mddev->major_version]. |
| 2448 | validate_super(mddev, rdev); |
| 2449 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 2450 | if (err) { |
| 2451 | unbind_rdev_from_array(rdev); |
| 2452 | export_rdev(rdev); |
| 2453 | return err; |
| 2454 | } |
| 2455 | } |
| 2456 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2457 | |
| 2458 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2459 | if (mddev->degraded) |
| 2460 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2461 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2462 | md_new_event(mddev); |
| 2463 | md_wakeup_thread(mddev->thread); |
| 2464 | return 0; |
| 2465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2467 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2468 | * We want to accept with case. For this we use cmd_match. |
| 2469 | */ |
| 2470 | static int cmd_match(const char *cmd, const char *str) |
| 2471 | { |
| 2472 | /* See if cmd, written into a sysfs file, matches |
| 2473 | * str. They must either be the same, or cmd can |
| 2474 | * have a trailing newline |
| 2475 | */ |
| 2476 | while (*cmd && *str && *cmd == *str) { |
| 2477 | cmd++; |
| 2478 | str++; |
| 2479 | } |
| 2480 | if (*cmd == '\n') |
| 2481 | cmd++; |
| 2482 | if (*str || *cmd) |
| 2483 | return 0; |
| 2484 | return 1; |
| 2485 | } |
| 2486 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2487 | struct rdev_sysfs_entry { |
| 2488 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2489 | ssize_t (*show)(struct md_rdev *, char *); |
| 2490 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2491 | }; |
| 2492 | |
| 2493 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2494 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2495 | { |
| 2496 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2497 | size_t len = 0; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2498 | unsigned long flags = ACCESS_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2499 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2500 | if (test_bit(Faulty, &flags) || |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2501 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2502 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2503 | sep = ","; |
| 2504 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2505 | if (test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2506 | len += sprintf(page+len, "%sin_sync",sep); |
| 2507 | sep = ","; |
| 2508 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2509 | if (test_bit(WriteMostly, &flags)) { |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2510 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2511 | sep = ","; |
| 2512 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2513 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2514 | (rdev->badblocks.unacked_exist |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2515 | && !test_bit(Faulty, &flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2516 | len += sprintf(page+len, "%sblocked", sep); |
| 2517 | sep = ","; |
| 2518 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2519 | if (!test_bit(Faulty, &flags) && |
| 2520 | !test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2521 | len += sprintf(page+len, "%sspare", sep); |
| 2522 | sep = ","; |
| 2523 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2524 | if (test_bit(WriteErrorSeen, &flags)) { |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2525 | len += sprintf(page+len, "%swrite_error", sep); |
| 2526 | sep = ","; |
| 2527 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2528 | if (test_bit(WantReplacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2529 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2530 | sep = ","; |
| 2531 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2532 | if (test_bit(Replacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2533 | len += sprintf(page+len, "%sreplacement", sep); |
| 2534 | sep = ","; |
| 2535 | } |
| 2536 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2537 | return len+sprintf(page+len, "\n"); |
| 2538 | } |
| 2539 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2540 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2541 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2542 | { |
| 2543 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2544 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2545 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2546 | * writemostly - sets write_mostly |
| 2547 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2548 | * blocked - sets the Blocked flags |
| 2549 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2550 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2551 | * -insync - clear Insync for a device with a slot assigned, |
| 2552 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2553 | * write_error - sets WriteErrorSeen |
| 2554 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2555 | */ |
| 2556 | int err = -EINVAL; |
| 2557 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2558 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2559 | if (test_bit(Faulty, &rdev->flags)) |
| 2560 | err = 0; |
| 2561 | else |
| 2562 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2563 | } else if (cmd_match(buf, "remove")) { |
| 2564 | if (rdev->raid_disk >= 0) |
| 2565 | err = -EBUSY; |
| 2566 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2567 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2568 | err = 0; |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 2569 | if (mddev_is_clustered(mddev)) |
| 2570 | err = md_cluster_ops->remove_disk(mddev, rdev); |
| 2571 | |
| 2572 | if (err == 0) { |
| 2573 | md_kick_rdev_from_array(rdev); |
| 2574 | if (mddev->pers) |
| 2575 | md_update_sb(mddev, 1); |
| 2576 | md_new_event(mddev); |
| 2577 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2578 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2579 | } else if (cmd_match(buf, "writemostly")) { |
| 2580 | set_bit(WriteMostly, &rdev->flags); |
| 2581 | err = 0; |
| 2582 | } else if (cmd_match(buf, "-writemostly")) { |
| 2583 | clear_bit(WriteMostly, &rdev->flags); |
| 2584 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2585 | } else if (cmd_match(buf, "blocked")) { |
| 2586 | set_bit(Blocked, &rdev->flags); |
| 2587 | err = 0; |
| 2588 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2589 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2590 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2591 | /* metadata handler doesn't understand badblocks, |
| 2592 | * so we need to fail the device |
| 2593 | */ |
| 2594 | md_error(rdev->mddev, rdev); |
| 2595 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2596 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2597 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2598 | wake_up(&rdev->blocked_wait); |
| 2599 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2600 | md_wakeup_thread(rdev->mddev->thread); |
| 2601 | |
| 2602 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2603 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2604 | set_bit(In_sync, &rdev->flags); |
| 2605 | err = 0; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2606 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 2607 | if (rdev->mddev->pers == NULL) { |
| 2608 | clear_bit(In_sync, &rdev->flags); |
| 2609 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2610 | rdev->raid_disk = -1; |
| 2611 | err = 0; |
| 2612 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2613 | } else if (cmd_match(buf, "write_error")) { |
| 2614 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2615 | err = 0; |
| 2616 | } else if (cmd_match(buf, "-write_error")) { |
| 2617 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2618 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2619 | } else if (cmd_match(buf, "want_replacement")) { |
| 2620 | /* Any non-spare device that is not a replacement can |
| 2621 | * become want_replacement at any time, but we then need to |
| 2622 | * check if recovery is needed. |
| 2623 | */ |
| 2624 | if (rdev->raid_disk >= 0 && |
| 2625 | !test_bit(Replacement, &rdev->flags)) |
| 2626 | set_bit(WantReplacement, &rdev->flags); |
| 2627 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2628 | md_wakeup_thread(rdev->mddev->thread); |
| 2629 | err = 0; |
| 2630 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2631 | /* Clearing 'want_replacement' is always allowed. |
| 2632 | * Once replacements starts it is too late though. |
| 2633 | */ |
| 2634 | err = 0; |
| 2635 | clear_bit(WantReplacement, &rdev->flags); |
| 2636 | } else if (cmd_match(buf, "replacement")) { |
| 2637 | /* Can only set a device as a replacement when array has not |
| 2638 | * yet been started. Once running, replacement is automatic |
| 2639 | * from spares, or by assigning 'slot'. |
| 2640 | */ |
| 2641 | if (rdev->mddev->pers) |
| 2642 | err = -EBUSY; |
| 2643 | else { |
| 2644 | set_bit(Replacement, &rdev->flags); |
| 2645 | err = 0; |
| 2646 | } |
| 2647 | } else if (cmd_match(buf, "-replacement")) { |
| 2648 | /* Similarly, can only clear Replacement before start */ |
| 2649 | if (rdev->mddev->pers) |
| 2650 | err = -EBUSY; |
| 2651 | else { |
| 2652 | clear_bit(Replacement, &rdev->flags); |
| 2653 | err = 0; |
| 2654 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2655 | } else if (cmd_match(buf, "re-add")) { |
| 2656 | if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 2657 | /* clear_bit is performed _after_ all the devices |
| 2658 | * have their local Faulty bit cleared. If any writes |
| 2659 | * happen in the meantime in the local node, they |
| 2660 | * will land in the local bitmap, which will be synced |
| 2661 | * by this node eventually |
| 2662 | */ |
| 2663 | if (!mddev_is_clustered(rdev->mddev) || |
| 2664 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 2665 | clear_bit(Faulty, &rdev->flags); |
| 2666 | err = add_bound_rdev(rdev); |
| 2667 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2668 | } else |
| 2669 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2670 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2671 | if (!err) |
| 2672 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2673 | return err ? err : len; |
| 2674 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2675 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 2676 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2677 | |
| 2678 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2679 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2680 | { |
| 2681 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2682 | } |
| 2683 | |
| 2684 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2685 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2686 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2687 | unsigned int n; |
| 2688 | int rv; |
| 2689 | |
| 2690 | rv = kstrtouint(buf, 10, &n); |
| 2691 | if (rv < 0) |
| 2692 | return rv; |
| 2693 | atomic_set(&rdev->corrected_errors, n); |
| 2694 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2695 | } |
| 2696 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2697 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2698 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2699 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2700 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2701 | { |
| 2702 | if (rdev->raid_disk < 0) |
| 2703 | return sprintf(page, "none\n"); |
| 2704 | else |
| 2705 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2706 | } |
| 2707 | |
| 2708 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2709 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2710 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2711 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2712 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2713 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2714 | if (strncmp(buf, "none", 4)==0) |
| 2715 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2716 | else { |
| 2717 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 2718 | if (err < 0) |
| 2719 | return err; |
| 2720 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2721 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2722 | /* Setting 'slot' on an active array requires also |
| 2723 | * updating the 'rd%d' link, and communicating |
| 2724 | * with the personality with ->hot_*_disk. |
| 2725 | * For now we only support removing |
| 2726 | * failed/spare devices. This normally happens automatically, |
| 2727 | * but not when the metadata is externally managed. |
| 2728 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2729 | if (rdev->raid_disk == -1) |
| 2730 | return -EEXIST; |
| 2731 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2732 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2733 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2734 | clear_bit(Blocked, &rdev->flags); |
| 2735 | remove_and_add_spares(rdev->mddev, rdev); |
| 2736 | if (rdev->raid_disk >= 0) |
| 2737 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2738 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2739 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2740 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2741 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2742 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2743 | */ |
| 2744 | |
| 2745 | if (rdev->raid_disk != -1) |
| 2746 | return -EBUSY; |
| 2747 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2748 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2749 | return -EBUSY; |
| 2750 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2751 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2752 | return -EINVAL; |
| 2753 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2754 | if (slot >= rdev->mddev->raid_disks && |
| 2755 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2756 | return -ENOSPC; |
| 2757 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2758 | rdev->raid_disk = slot; |
| 2759 | if (test_bit(In_sync, &rdev->flags)) |
| 2760 | rdev->saved_raid_disk = slot; |
| 2761 | else |
| 2762 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2763 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 2764 | clear_bit(Bitmap_sync, &rdev->flags); |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 2765 | remove_and_add_spares(rdev->mddev, rdev); |
| 2766 | if (rdev->raid_disk == -1) |
| 2767 | return -EBUSY; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2768 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2769 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2770 | if (slot >= rdev->mddev->raid_disks && |
| 2771 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2772 | return -ENOSPC; |
| 2773 | rdev->raid_disk = slot; |
| 2774 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2775 | clear_bit(Faulty, &rdev->flags); |
| 2776 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2777 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2778 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2779 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2780 | return len; |
| 2781 | } |
| 2782 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2783 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2784 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2785 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2786 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2787 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2788 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2789 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2793 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2794 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2795 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2796 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2797 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2798 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2799 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2800 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2801 | /* Must set offset before size, so overlap checks |
| 2802 | * can be sane */ |
| 2803 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2804 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2805 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2806 | return len; |
| 2807 | } |
| 2808 | |
| 2809 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2810 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2811 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2812 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2813 | { |
| 2814 | return sprintf(page, "%llu\n", |
| 2815 | (unsigned long long)rdev->new_data_offset); |
| 2816 | } |
| 2817 | |
| 2818 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2819 | const char *buf, size_t len) |
| 2820 | { |
| 2821 | unsigned long long new_offset; |
| 2822 | struct mddev *mddev = rdev->mddev; |
| 2823 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2824 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2825 | return -EINVAL; |
| 2826 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 2827 | if (mddev->sync_thread || |
| 2828 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2829 | return -EBUSY; |
| 2830 | if (new_offset == rdev->data_offset) |
| 2831 | /* reset is always permitted */ |
| 2832 | ; |
| 2833 | else if (new_offset > rdev->data_offset) { |
| 2834 | /* must not push array size beyond rdev_sectors */ |
| 2835 | if (new_offset - rdev->data_offset |
| 2836 | + mddev->dev_sectors > rdev->sectors) |
| 2837 | return -E2BIG; |
| 2838 | } |
| 2839 | /* Metadata worries about other space details. */ |
| 2840 | |
| 2841 | /* decreasing the offset is inconsistent with a backwards |
| 2842 | * reshape. |
| 2843 | */ |
| 2844 | if (new_offset < rdev->data_offset && |
| 2845 | mddev->reshape_backwards) |
| 2846 | return -EINVAL; |
| 2847 | /* Increasing offset is inconsistent with forwards |
| 2848 | * reshape. reshape_direction should be set to |
| 2849 | * 'backwards' first. |
| 2850 | */ |
| 2851 | if (new_offset > rdev->data_offset && |
| 2852 | !mddev->reshape_backwards) |
| 2853 | return -EINVAL; |
| 2854 | |
| 2855 | if (mddev->pers && mddev->persistent && |
| 2856 | !super_types[mddev->major_version] |
| 2857 | .allow_new_offset(rdev, new_offset)) |
| 2858 | return -E2BIG; |
| 2859 | rdev->new_data_offset = new_offset; |
| 2860 | if (new_offset > rdev->data_offset) |
| 2861 | mddev->reshape_backwards = 1; |
| 2862 | else if (new_offset < rdev->data_offset) |
| 2863 | mddev->reshape_backwards = 0; |
| 2864 | |
| 2865 | return len; |
| 2866 | } |
| 2867 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2868 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2869 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2870 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2871 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2872 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2873 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2874 | } |
| 2875 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2876 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2877 | { |
| 2878 | /* check if two start/length pairs overlap */ |
| 2879 | if (s1+l1 <= s2) |
| 2880 | return 0; |
| 2881 | if (s2+l2 <= s1) |
| 2882 | return 0; |
| 2883 | return 1; |
| 2884 | } |
| 2885 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2886 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2887 | { |
| 2888 | unsigned long long blocks; |
| 2889 | sector_t new; |
| 2890 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2891 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2892 | return -EINVAL; |
| 2893 | |
| 2894 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2895 | return -EINVAL; /* sector conversion overflow */ |
| 2896 | |
| 2897 | new = blocks * 2; |
| 2898 | if (new != blocks * 2) |
| 2899 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2900 | |
| 2901 | *sectors = new; |
| 2902 | return 0; |
| 2903 | } |
| 2904 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2905 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2906 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2907 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2908 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2909 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2910 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2911 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2912 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2913 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2914 | if (rdev->data_offset != rdev->new_data_offset) |
| 2915 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2916 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2917 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2918 | sectors = super_types[my_mddev->major_version]. |
| 2919 | rdev_size_change(rdev, sectors); |
| 2920 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2921 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2922 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2923 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2924 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 2925 | if (!my_mddev->pers->resize) |
| 2926 | /* Cannot change size for RAID0 or Linear etc */ |
| 2927 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2928 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2929 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2930 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2931 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2932 | rdev->sectors = sectors; |
| 2933 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2934 | /* Need to check that all other rdevs with the same |
| 2935 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 2936 | * the rdev lists safely. |
| 2937 | * This check does not provide a hard guarantee, it |
| 2938 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2939 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2940 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2941 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2942 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2943 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2944 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2945 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2946 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2947 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2948 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2949 | if (rdev->bdev == rdev2->bdev && |
| 2950 | rdev != rdev2 && |
| 2951 | overlaps(rdev->data_offset, rdev->sectors, |
| 2952 | rdev2->data_offset, |
| 2953 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2954 | overlap = 1; |
| 2955 | break; |
| 2956 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2957 | if (overlap) { |
| 2958 | mddev_put(mddev); |
| 2959 | break; |
| 2960 | } |
| 2961 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2962 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2963 | if (overlap) { |
| 2964 | /* Someone else could have slipped in a size |
| 2965 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2966 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2967 | * safe, and trust userspace not to race with |
| 2968 | * itself |
| 2969 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2970 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2971 | return -EBUSY; |
| 2972 | } |
| 2973 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2974 | return len; |
| 2975 | } |
| 2976 | |
| 2977 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2978 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2979 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2980 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2981 | { |
| 2982 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2983 | |
| 2984 | if (test_bit(In_sync, &rdev->flags) || |
| 2985 | recovery_start == MaxSector) |
| 2986 | return sprintf(page, "none\n"); |
| 2987 | |
| 2988 | return sprintf(page, "%llu\n", recovery_start); |
| 2989 | } |
| 2990 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2991 | 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] | 2992 | { |
| 2993 | unsigned long long recovery_start; |
| 2994 | |
| 2995 | if (cmd_match(buf, "none")) |
| 2996 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2997 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2998 | return -EINVAL; |
| 2999 | |
| 3000 | if (rdev->mddev->pers && |
| 3001 | rdev->raid_disk >= 0) |
| 3002 | return -EBUSY; |
| 3003 | |
| 3004 | rdev->recovery_offset = recovery_start; |
| 3005 | if (recovery_start == MaxSector) |
| 3006 | set_bit(In_sync, &rdev->flags); |
| 3007 | else |
| 3008 | clear_bit(In_sync, &rdev->flags); |
| 3009 | return len; |
| 3010 | } |
| 3011 | |
| 3012 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3013 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3014 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3015 | static ssize_t |
| 3016 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 3017 | static ssize_t |
| 3018 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 3019 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3020 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3021 | { |
| 3022 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3023 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3024 | 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] | 3025 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3026 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3027 | /* Maybe that ack was all we needed */ |
| 3028 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3029 | wake_up(&rdev->blocked_wait); |
| 3030 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3031 | } |
| 3032 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3033 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3034 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3035 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3036 | { |
| 3037 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3038 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3039 | 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] | 3040 | { |
| 3041 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3042 | } |
| 3043 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3044 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3045 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3046 | static struct attribute *rdev_default_attrs[] = { |
| 3047 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3048 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3049 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3050 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3051 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3052 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3053 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3054 | &rdev_bad_blocks.attr, |
| 3055 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3056 | NULL, |
| 3057 | }; |
| 3058 | static ssize_t |
| 3059 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3060 | { |
| 3061 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3062 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3063 | |
| 3064 | if (!entry->show) |
| 3065 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3066 | if (!rdev->mddev) |
| 3067 | return -EBUSY; |
| 3068 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | static ssize_t |
| 3072 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3073 | const char *page, size_t length) |
| 3074 | { |
| 3075 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3076 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3077 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3078 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3079 | |
| 3080 | if (!entry->store) |
| 3081 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3082 | if (!capable(CAP_SYS_ADMIN)) |
| 3083 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3084 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3085 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3086 | if (rdev->mddev == NULL) |
| 3087 | rv = -EBUSY; |
| 3088 | else |
| 3089 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3090 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3091 | } |
| 3092 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3093 | } |
| 3094 | |
| 3095 | static void rdev_free(struct kobject *ko) |
| 3096 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3097 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3098 | kfree(rdev); |
| 3099 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3100 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3101 | .show = rdev_attr_show, |
| 3102 | .store = rdev_attr_store, |
| 3103 | }; |
| 3104 | static struct kobj_type rdev_ktype = { |
| 3105 | .release = rdev_free, |
| 3106 | .sysfs_ops = &rdev_sysfs_ops, |
| 3107 | .default_attrs = rdev_default_attrs, |
| 3108 | }; |
| 3109 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3110 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3111 | { |
| 3112 | rdev->desc_nr = -1; |
| 3113 | rdev->saved_raid_disk = -1; |
| 3114 | rdev->raid_disk = -1; |
| 3115 | rdev->flags = 0; |
| 3116 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3117 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3118 | rdev->sb_events = 0; |
| 3119 | rdev->last_read_error.tv_sec = 0; |
| 3120 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3121 | rdev->sb_loaded = 0; |
| 3122 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3123 | atomic_set(&rdev->nr_pending, 0); |
| 3124 | atomic_set(&rdev->read_errors, 0); |
| 3125 | atomic_set(&rdev->corrected_errors, 0); |
| 3126 | |
| 3127 | INIT_LIST_HEAD(&rdev->same_set); |
| 3128 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3129 | |
| 3130 | /* Add space to store bad block list. |
| 3131 | * This reserves the space even on arrays where it cannot |
| 3132 | * be used - I wonder if that matters |
| 3133 | */ |
| 3134 | rdev->badblocks.count = 0; |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 3135 | rdev->badblocks.shift = -1; /* disabled until explicitly enabled */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3136 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3137 | seqlock_init(&rdev->badblocks.lock); |
| 3138 | if (rdev->badblocks.page == NULL) |
| 3139 | return -ENOMEM; |
| 3140 | |
| 3141 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3142 | } |
| 3143 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | /* |
| 3145 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3146 | * |
| 3147 | * mark the device faulty if: |
| 3148 | * |
| 3149 | * - the device is nonexistent (zero size) |
| 3150 | * - the device has no valid superblock |
| 3151 | * |
| 3152 | * a faulty rdev _never_ has rdev->sb set. |
| 3153 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3154 | 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] | 3155 | { |
| 3156 | char b[BDEVNAME_SIZE]; |
| 3157 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3158 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | sector_t size; |
| 3160 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3161 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | if (!rdev) { |
| 3163 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3164 | return ERR_PTR(-ENOMEM); |
| 3165 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3166 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3167 | err = md_rdev_init(rdev); |
| 3168 | if (err) |
| 3169 | goto abort_free; |
| 3170 | err = alloc_disk_sb(rdev); |
| 3171 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | goto abort_free; |
| 3173 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3174 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3175 | if (err) |
| 3176 | goto abort_free; |
| 3177 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3178 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3179 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3180 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | if (!size) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3182 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3184 | bdevname(rdev->bdev,b)); |
| 3185 | err = -EINVAL; |
| 3186 | goto abort_free; |
| 3187 | } |
| 3188 | |
| 3189 | if (super_format >= 0) { |
| 3190 | err = super_types[super_format]. |
| 3191 | load_super(rdev, NULL, super_minor); |
| 3192 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3193 | printk(KERN_WARNING |
| 3194 | "md: %s does not have a valid v%d.%d " |
| 3195 | "superblock, not importing!\n", |
| 3196 | bdevname(rdev->bdev,b), |
| 3197 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | goto abort_free; |
| 3199 | } |
| 3200 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3201 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | "md: could not read %s's sb, not importing!\n", |
| 3203 | bdevname(rdev->bdev,b)); |
| 3204 | goto abort_free; |
| 3205 | } |
| 3206 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | return rdev; |
| 3209 | |
| 3210 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3211 | if (rdev->bdev) |
| 3212 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3213 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | kfree(rdev); |
| 3215 | return ERR_PTR(err); |
| 3216 | } |
| 3217 | |
| 3218 | /* |
| 3219 | * Check a full RAID array for plausibility |
| 3220 | */ |
| 3221 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3222 | static void analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | { |
| 3224 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3225 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3226 | char b[BDEVNAME_SIZE]; |
| 3227 | |
| 3228 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3229 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3230 | switch (super_types[mddev->major_version]. |
| 3231 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3232 | case 1: |
| 3233 | freshest = rdev; |
| 3234 | break; |
| 3235 | case 0: |
| 3236 | break; |
| 3237 | default: |
| 3238 | printk( KERN_ERR \ |
| 3239 | "md: fatal superblock inconsistency in %s" |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3240 | " -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3241 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3242 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3243 | } |
| 3244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | super_types[mddev->major_version]. |
| 3246 | validate_super(mddev, freshest); |
| 3247 | |
| 3248 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3249 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3250 | if (mddev->max_disks && |
| 3251 | (rdev->desc_nr >= mddev->max_disks || |
| 3252 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3253 | printk(KERN_WARNING |
| 3254 | "md: %s: %s: only %d devices permitted\n", |
| 3255 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3256 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3257 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3258 | continue; |
| 3259 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3260 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | if (super_types[mddev->major_version]. |
| 3262 | validate_super(mddev, rdev)) { |
| 3263 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3264 | " from array!\n", |
| 3265 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3266 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | continue; |
| 3268 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3269 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3271 | rdev->desc_nr = i++; |
| 3272 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3273 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3274 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3275 | rdev->raid_disk = -1; |
| 3276 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3277 | } |
| 3278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | } |
| 3280 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3281 | /* Read a fixed-point number. |
| 3282 | * Numbers in sysfs attributes should be in "standard" units where |
| 3283 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3284 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3285 | * milliseconds or jiffies. |
| 3286 | * This function takes a decimal number with a possible fractional |
| 3287 | * component, and produces an integer which is the result of |
| 3288 | * multiplying that number by 10^'scale'. |
| 3289 | * all without any floating-point arithmetic. |
| 3290 | */ |
| 3291 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3292 | { |
| 3293 | unsigned long result = 0; |
| 3294 | long decimals = -1; |
| 3295 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3296 | if (*cp == '.') |
| 3297 | decimals = 0; |
| 3298 | else if (decimals < scale) { |
| 3299 | unsigned int value; |
| 3300 | value = *cp - '0'; |
| 3301 | result = result * 10 + value; |
| 3302 | if (decimals >= 0) |
| 3303 | decimals++; |
| 3304 | } |
| 3305 | cp++; |
| 3306 | } |
| 3307 | if (*cp == '\n') |
| 3308 | cp++; |
| 3309 | if (*cp) |
| 3310 | return -EINVAL; |
| 3311 | if (decimals < 0) |
| 3312 | decimals = 0; |
| 3313 | while (decimals < scale) { |
| 3314 | result *= 10; |
| 3315 | decimals ++; |
| 3316 | } |
| 3317 | *res = result; |
| 3318 | return 0; |
| 3319 | } |
| 3320 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3321 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3322 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3323 | { |
| 3324 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3325 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3326 | } |
| 3327 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3328 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3329 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3330 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3331 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3332 | if (mddev_is_clustered(mddev)) { |
| 3333 | pr_info("md: Safemode is disabled for clustered mode\n"); |
| 3334 | return -EINVAL; |
| 3335 | } |
| 3336 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3337 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3338 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3339 | if (msec == 0) |
| 3340 | mddev->safemode_delay = 0; |
| 3341 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3342 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3343 | unsigned long new_delay = (msec*HZ)/1000; |
| 3344 | |
| 3345 | if (new_delay == 0) |
| 3346 | new_delay = 1; |
| 3347 | mddev->safemode_delay = new_delay; |
| 3348 | if (new_delay < old_delay || old_delay == 0) |
| 3349 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3350 | } |
| 3351 | return len; |
| 3352 | } |
| 3353 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3354 | __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] | 3355 | |
| 3356 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3357 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3358 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3359 | struct md_personality *p; |
| 3360 | int ret; |
| 3361 | spin_lock(&mddev->lock); |
| 3362 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3363 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3364 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3365 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3366 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3367 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3368 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3369 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3370 | ret = 0; |
| 3371 | spin_unlock(&mddev->lock); |
| 3372 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3373 | } |
| 3374 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3375 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3376 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3377 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3378 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3379 | ssize_t rv; |
| 3380 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3381 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3382 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3383 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3384 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3385 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3386 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3387 | return -EINVAL; |
| 3388 | |
| 3389 | rv = mddev_lock(mddev); |
| 3390 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3391 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3392 | |
| 3393 | if (mddev->pers == NULL) { |
| 3394 | strncpy(mddev->clevel, buf, slen); |
| 3395 | if (mddev->clevel[slen-1] == '\n') |
| 3396 | slen--; |
| 3397 | mddev->clevel[slen] = 0; |
| 3398 | mddev->level = LEVEL_NONE; |
| 3399 | rv = len; |
| 3400 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3401 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3402 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3403 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3404 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3405 | |
| 3406 | /* request to change the personality. Need to ensure: |
| 3407 | * - array is not engaged in resync/recovery/reshape |
| 3408 | * - old personality can be suspended |
| 3409 | * - new personality will access other array. |
| 3410 | */ |
| 3411 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3412 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3413 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3414 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3415 | mddev->reshape_position != MaxSector || |
| 3416 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3417 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3418 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3419 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3420 | if (!mddev->pers->quiesce) { |
| 3421 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3422 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3423 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3424 | } |
| 3425 | |
| 3426 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3427 | strncpy(clevel, buf, slen); |
| 3428 | if (clevel[slen-1] == '\n') |
| 3429 | slen--; |
| 3430 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3431 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3432 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3433 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3434 | if (request_module("md-%s", clevel) != 0) |
| 3435 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3436 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3437 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3438 | if (!pers || !try_module_get(pers->owner)) { |
| 3439 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3440 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3441 | rv = -EINVAL; |
| 3442 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3443 | } |
| 3444 | spin_unlock(&pers_lock); |
| 3445 | |
| 3446 | if (pers == mddev->pers) { |
| 3447 | /* Nothing to do! */ |
| 3448 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3449 | rv = len; |
| 3450 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3451 | } |
| 3452 | if (!pers->takeover) { |
| 3453 | module_put(pers->owner); |
| 3454 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3455 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3456 | rv = -EINVAL; |
| 3457 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3458 | } |
| 3459 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3460 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3461 | rdev->new_raid_disk = rdev->raid_disk; |
| 3462 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3463 | /* ->takeover must set new_* and/or delta_disks |
| 3464 | * if it succeeds, and may set them when it fails. |
| 3465 | */ |
| 3466 | priv = pers->takeover(mddev); |
| 3467 | if (IS_ERR(priv)) { |
| 3468 | mddev->new_level = mddev->level; |
| 3469 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3470 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3471 | mddev->raid_disks -= mddev->delta_disks; |
| 3472 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3473 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3474 | module_put(pers->owner); |
| 3475 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3476 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3477 | rv = PTR_ERR(priv); |
| 3478 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | /* Looks like we have a winner */ |
| 3482 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 3483 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3484 | |
| 3485 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3486 | oldpers = mddev->pers; |
| 3487 | oldpriv = mddev->private; |
| 3488 | mddev->pers = pers; |
| 3489 | mddev->private = priv; |
| 3490 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3491 | mddev->level = mddev->new_level; |
| 3492 | mddev->layout = mddev->new_layout; |
| 3493 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 3494 | mddev->delta_disks = 0; |
| 3495 | mddev->reshape_backwards = 0; |
| 3496 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3497 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3498 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3499 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3500 | mddev->external) { |
| 3501 | /* We are converting from a no-redundancy array |
| 3502 | * to a redundancy array and metadata is managed |
| 3503 | * externally so we need to be sure that writes |
| 3504 | * won't block due to a need to transition |
| 3505 | * clean->dirty |
| 3506 | * until external management is started. |
| 3507 | */ |
| 3508 | mddev->in_sync = 0; |
| 3509 | mddev->safemode_delay = 0; |
| 3510 | mddev->safemode = 0; |
| 3511 | } |
| 3512 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3513 | oldpers->free(mddev, oldpriv); |
| 3514 | |
| 3515 | if (oldpers->sync_request == NULL && |
| 3516 | pers->sync_request != NULL) { |
| 3517 | /* need to add the md_redundancy_group */ |
| 3518 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3519 | printk(KERN_WARNING |
| 3520 | "md: cannot register extra attributes for %s\n", |
| 3521 | mdname(mddev)); |
| 3522 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3523 | } |
| 3524 | if (oldpers->sync_request != NULL && |
| 3525 | pers->sync_request == NULL) { |
| 3526 | /* need to remove the md_redundancy_group */ |
| 3527 | if (mddev->to_remove == NULL) |
| 3528 | mddev->to_remove = &md_redundancy_group; |
| 3529 | } |
| 3530 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3531 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3532 | if (rdev->raid_disk < 0) |
| 3533 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3534 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3535 | rdev->new_raid_disk = -1; |
| 3536 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3537 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3538 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3539 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3540 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3541 | if (rdev->raid_disk < 0) |
| 3542 | continue; |
| 3543 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3544 | continue; |
| 3545 | rdev->raid_disk = rdev->new_raid_disk; |
| 3546 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3547 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3548 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3549 | if (sysfs_link_rdev(mddev, rdev)) |
| 3550 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3551 | " for %s after level change\n", |
| 3552 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3553 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3554 | } |
| 3555 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3556 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3557 | /* this is now an array without redundancy, so |
| 3558 | * it must always be in_sync |
| 3559 | */ |
| 3560 | mddev->in_sync = 1; |
| 3561 | del_timer_sync(&mddev->safemode_timer); |
| 3562 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 3563 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3564 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3565 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3566 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 3567 | if (!mddev->thread) |
| 3568 | md_update_sb(mddev, 1); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3569 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3570 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3571 | rv = len; |
| 3572 | out_unlock: |
| 3573 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3574 | return rv; |
| 3575 | } |
| 3576 | |
| 3577 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3578 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3579 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3580 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3581 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3582 | { |
| 3583 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3584 | if (mddev->reshape_position != MaxSector && |
| 3585 | mddev->layout != mddev->new_layout) |
| 3586 | return sprintf(page, "%d (%d)\n", |
| 3587 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3588 | return sprintf(page, "%d\n", mddev->layout); |
| 3589 | } |
| 3590 | |
| 3591 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3592 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3593 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3594 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3595 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3596 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3597 | err = kstrtouint(buf, 10, &n); |
| 3598 | if (err < 0) |
| 3599 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3600 | err = mddev_lock(mddev); |
| 3601 | if (err) |
| 3602 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3603 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3604 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3605 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3606 | err = -EBUSY; |
| 3607 | else if (mddev->ro) |
| 3608 | err = -EROFS; |
| 3609 | else { |
| 3610 | mddev->new_layout = n; |
| 3611 | err = mddev->pers->check_reshape(mddev); |
| 3612 | if (err) |
| 3613 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3614 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3615 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3616 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3617 | if (mddev->reshape_position == MaxSector) |
| 3618 | mddev->layout = n; |
| 3619 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3620 | mddev_unlock(mddev); |
| 3621 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3622 | } |
| 3623 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3624 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3625 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3626 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3627 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3628 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3629 | if (mddev->raid_disks == 0) |
| 3630 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3631 | if (mddev->reshape_position != MaxSector && |
| 3632 | mddev->delta_disks != 0) |
| 3633 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3634 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3635 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3636 | } |
| 3637 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3638 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3639 | |
| 3640 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3641 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3642 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3643 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3644 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3645 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3646 | err = kstrtouint(buf, 10, &n); |
| 3647 | if (err < 0) |
| 3648 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3649 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3650 | err = mddev_lock(mddev); |
| 3651 | if (err) |
| 3652 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3653 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3654 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3655 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3656 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3657 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3658 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3659 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3660 | rdev_for_each(rdev, mddev) { |
| 3661 | if (olddisks < n && |
| 3662 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3663 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3664 | if (olddisks > n && |
| 3665 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3666 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3667 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3668 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3669 | mddev->delta_disks = n - olddisks; |
| 3670 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3671 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3672 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3673 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3674 | out_unlock: |
| 3675 | mddev_unlock(mddev); |
| 3676 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3677 | } |
| 3678 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3679 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3680 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3681 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3682 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3683 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3684 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3685 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3686 | return sprintf(page, "%d (%d)\n", |
| 3687 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3688 | mddev->chunk_sectors << 9); |
| 3689 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3693 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3694 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3695 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3696 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3697 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3698 | err = kstrtoul(buf, 10, &n); |
| 3699 | if (err < 0) |
| 3700 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3701 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3702 | err = mddev_lock(mddev); |
| 3703 | if (err) |
| 3704 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3705 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3706 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3707 | err = -EBUSY; |
| 3708 | else if (mddev->ro) |
| 3709 | err = -EROFS; |
| 3710 | else { |
| 3711 | mddev->new_chunk_sectors = n >> 9; |
| 3712 | err = mddev->pers->check_reshape(mddev); |
| 3713 | if (err) |
| 3714 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3715 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3716 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3717 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3718 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3719 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3720 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3721 | mddev_unlock(mddev); |
| 3722 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3723 | } |
| 3724 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3725 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3726 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3727 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3728 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3729 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3730 | if (mddev->recovery_cp == MaxSector) |
| 3731 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3732 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3733 | } |
| 3734 | |
| 3735 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3736 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3737 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3738 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3739 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3740 | |
| 3741 | if (cmd_match(buf, "none")) |
| 3742 | n = MaxSector; |
| 3743 | else { |
| 3744 | err = kstrtoull(buf, 10, &n); |
| 3745 | if (err < 0) |
| 3746 | return err; |
| 3747 | if (n != (sector_t)n) |
| 3748 | return -EINVAL; |
| 3749 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3750 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3751 | err = mddev_lock(mddev); |
| 3752 | if (err) |
| 3753 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3754 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3755 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3756 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3757 | if (!err) { |
| 3758 | mddev->recovery_cp = n; |
| 3759 | if (mddev->pers) |
| 3760 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3761 | } |
| 3762 | mddev_unlock(mddev); |
| 3763 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3764 | } |
| 3765 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3766 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 3767 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3768 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3769 | /* |
| 3770 | * The array state can be: |
| 3771 | * |
| 3772 | * clear |
| 3773 | * No devices, no size, no level |
| 3774 | * Equivalent to STOP_ARRAY ioctl |
| 3775 | * inactive |
| 3776 | * May have some settings, but array is not active |
| 3777 | * all IO results in error |
| 3778 | * When written, doesn't tear down array, but just stops it |
| 3779 | * suspended (not supported yet) |
| 3780 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3781 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3782 | * readonly |
| 3783 | * no resync can happen. no superblocks get written. |
| 3784 | * write requests fail |
| 3785 | * read-auto |
| 3786 | * like readonly, but behaves like 'clean' on a write request. |
| 3787 | * |
| 3788 | * clean - no pending writes, but otherwise active. |
| 3789 | * When written to inactive array, starts without resync |
| 3790 | * If a write request arrives then |
| 3791 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3792 | * if not known, block and switch to write-pending |
| 3793 | * If written to an active array that has pending writes, then fails. |
| 3794 | * active |
| 3795 | * fully active: IO and resync can be happening. |
| 3796 | * When written to inactive array, starts with resync |
| 3797 | * |
| 3798 | * write-pending |
| 3799 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3800 | * |
| 3801 | * active-idle |
| 3802 | * like active, but no writes have been seen for a while (100msec). |
| 3803 | * |
| 3804 | */ |
| 3805 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3806 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3807 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3808 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3809 | "write-pending", "active-idle", NULL }; |
| 3810 | |
| 3811 | static int match_word(const char *word, char **list) |
| 3812 | { |
| 3813 | int n; |
| 3814 | for (n=0; list[n]; n++) |
| 3815 | if (cmd_match(word, list[n])) |
| 3816 | break; |
| 3817 | return n; |
| 3818 | } |
| 3819 | |
| 3820 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3821 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3822 | { |
| 3823 | enum array_state st = inactive; |
| 3824 | |
| 3825 | if (mddev->pers) |
| 3826 | switch(mddev->ro) { |
| 3827 | case 1: |
| 3828 | st = readonly; |
| 3829 | break; |
| 3830 | case 2: |
| 3831 | st = read_auto; |
| 3832 | break; |
| 3833 | case 0: |
| 3834 | if (mddev->in_sync) |
| 3835 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3836 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3837 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3838 | else if (mddev->safemode) |
| 3839 | st = active_idle; |
| 3840 | else |
| 3841 | st = active; |
| 3842 | } |
| 3843 | else { |
| 3844 | if (list_empty(&mddev->disks) && |
| 3845 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3846 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3847 | st = clear; |
| 3848 | else |
| 3849 | st = inactive; |
| 3850 | } |
| 3851 | return sprintf(page, "%s\n", array_states[st]); |
| 3852 | } |
| 3853 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3854 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 3855 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 3856 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3857 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3858 | |
| 3859 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3860 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3861 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3862 | int err; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3863 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3864 | |
| 3865 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 3866 | /* don't take reconfig_mutex when toggling between |
| 3867 | * clean and active |
| 3868 | */ |
| 3869 | spin_lock(&mddev->lock); |
| 3870 | if (st == active) { |
| 3871 | restart_array(mddev); |
| 3872 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 3873 | wake_up(&mddev->sb_wait); |
| 3874 | err = 0; |
| 3875 | } else /* st == clean */ { |
| 3876 | restart_array(mddev); |
| 3877 | if (atomic_read(&mddev->writes_pending) == 0) { |
| 3878 | if (mddev->in_sync == 0) { |
| 3879 | mddev->in_sync = 1; |
| 3880 | if (mddev->safemode == 1) |
| 3881 | mddev->safemode = 0; |
| 3882 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3883 | } |
| 3884 | err = 0; |
| 3885 | } else |
| 3886 | err = -EBUSY; |
| 3887 | } |
| 3888 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 3889 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3890 | } |
| 3891 | err = mddev_lock(mddev); |
| 3892 | if (err) |
| 3893 | return err; |
| 3894 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3895 | switch(st) { |
| 3896 | case bad_word: |
| 3897 | break; |
| 3898 | case clear: |
| 3899 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3900 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3901 | break; |
| 3902 | case inactive: |
| 3903 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3904 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3905 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3906 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3907 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3908 | break; |
| 3909 | case suspended: |
| 3910 | break; /* not supported yet */ |
| 3911 | case readonly: |
| 3912 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3913 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3914 | else { |
| 3915 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3916 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3917 | err = do_md_run(mddev); |
| 3918 | } |
| 3919 | break; |
| 3920 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3921 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3922 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3923 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3924 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3925 | err = restart_array(mddev); |
| 3926 | if (err == 0) { |
| 3927 | mddev->ro = 2; |
| 3928 | set_disk_ro(mddev->gendisk, 0); |
| 3929 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3930 | } else { |
| 3931 | mddev->ro = 2; |
| 3932 | err = do_md_run(mddev); |
| 3933 | } |
| 3934 | break; |
| 3935 | case clean: |
| 3936 | if (mddev->pers) { |
| 3937 | restart_array(mddev); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3938 | spin_lock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3939 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3940 | if (mddev->in_sync == 0) { |
| 3941 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3942 | if (mddev->safemode == 1) |
| 3943 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3944 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3945 | } |
| 3946 | err = 0; |
| 3947 | } else |
| 3948 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3949 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3950 | } else |
| 3951 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3952 | break; |
| 3953 | case active: |
| 3954 | if (mddev->pers) { |
| 3955 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3956 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3957 | wake_up(&mddev->sb_wait); |
| 3958 | err = 0; |
| 3959 | } else { |
| 3960 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3961 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3962 | err = do_md_run(mddev); |
| 3963 | } |
| 3964 | break; |
| 3965 | case write_pending: |
| 3966 | case active_idle: |
| 3967 | /* these cannot be set */ |
| 3968 | break; |
| 3969 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3970 | |
| 3971 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 3972 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3973 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3974 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3975 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3976 | mddev_unlock(mddev); |
| 3977 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3978 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3979 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3980 | __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] | 3981 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3982 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3983 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3984 | return sprintf(page, "%d\n", |
| 3985 | atomic_read(&mddev->max_corr_read_errors)); |
| 3986 | } |
| 3987 | |
| 3988 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3989 | 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] | 3990 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3991 | unsigned int n; |
| 3992 | int rv; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3993 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3994 | rv = kstrtouint(buf, 10, &n); |
| 3995 | if (rv < 0) |
| 3996 | return rv; |
| 3997 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3998 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3999 | } |
| 4000 | |
| 4001 | static struct md_sysfs_entry max_corr_read_errors = |
| 4002 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4003 | max_corrected_read_errors_store); |
| 4004 | |
| 4005 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4006 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4007 | { |
| 4008 | return -EINVAL; |
| 4009 | } |
| 4010 | |
| 4011 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4012 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4013 | { |
| 4014 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4015 | /* The new device is added to the array. |
| 4016 | * If the array has a persistent superblock, we read the |
| 4017 | * superblock to initialise info and check validity. |
| 4018 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4019 | * which mainly checks size. |
| 4020 | */ |
| 4021 | char *e; |
| 4022 | int major = simple_strtoul(buf, &e, 10); |
| 4023 | int minor; |
| 4024 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4025 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4026 | int err; |
| 4027 | |
| 4028 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4029 | return -EINVAL; |
| 4030 | minor = simple_strtoul(e+1, &e, 10); |
| 4031 | if (*e && *e != '\n') |
| 4032 | return -EINVAL; |
| 4033 | dev = MKDEV(major, minor); |
| 4034 | if (major != MAJOR(dev) || |
| 4035 | minor != MINOR(dev)) |
| 4036 | return -EOVERFLOW; |
| 4037 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4038 | flush_workqueue(md_misc_wq); |
| 4039 | |
| 4040 | err = mddev_lock(mddev); |
| 4041 | if (err) |
| 4042 | return err; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4043 | if (mddev->persistent) { |
| 4044 | rdev = md_import_device(dev, mddev->major_version, |
| 4045 | mddev->minor_version); |
| 4046 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4047 | struct md_rdev *rdev0 |
| 4048 | = list_entry(mddev->disks.next, |
| 4049 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4050 | err = super_types[mddev->major_version] |
| 4051 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4052 | if (err < 0) |
| 4053 | goto out; |
| 4054 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4055 | } else if (mddev->external) |
| 4056 | rdev = md_import_device(dev, -2, -1); |
| 4057 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4058 | rdev = md_import_device(dev, -1, -1); |
| 4059 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4060 | if (IS_ERR(rdev)) { |
| 4061 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4062 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4063 | } |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4064 | err = bind_rdev_to_array(rdev, mddev); |
| 4065 | out: |
| 4066 | if (err) |
| 4067 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4068 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4069 | return err ? err : len; |
| 4070 | } |
| 4071 | |
| 4072 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4073 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4074 | |
| 4075 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4076 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4077 | { |
| 4078 | char *end; |
| 4079 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4080 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4081 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4082 | err = mddev_lock(mddev); |
| 4083 | if (err) |
| 4084 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4085 | if (!mddev->bitmap) |
| 4086 | goto out; |
| 4087 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4088 | while (*buf) { |
| 4089 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4090 | if (buf == end) break; |
| 4091 | if (*end == '-') { /* range */ |
| 4092 | buf = end + 1; |
| 4093 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4094 | if (buf == end) break; |
| 4095 | } |
| 4096 | if (*end && !isspace(*end)) break; |
| 4097 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4098 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4099 | } |
| 4100 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4101 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4102 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4103 | return len; |
| 4104 | } |
| 4105 | |
| 4106 | static struct md_sysfs_entry md_bitmap = |
| 4107 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4108 | |
| 4109 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4110 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4111 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4112 | return sprintf(page, "%llu\n", |
| 4113 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4114 | } |
| 4115 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4116 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4117 | |
| 4118 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4119 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4120 | { |
| 4121 | /* If array is inactive, we can reduce the component size, but |
| 4122 | * not increase it (except from 0). |
| 4123 | * If array is active, we can try an on-line resize |
| 4124 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4125 | sector_t sectors; |
| 4126 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4127 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4128 | if (err < 0) |
| 4129 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4130 | err = mddev_lock(mddev); |
| 4131 | if (err) |
| 4132 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4133 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4134 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4135 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4136 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4137 | if (mddev->dev_sectors == 0 || |
| 4138 | mddev->dev_sectors > sectors) |
| 4139 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4140 | else |
| 4141 | err = -ENOSPC; |
| 4142 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4143 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4144 | return err ? err : len; |
| 4145 | } |
| 4146 | |
| 4147 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4148 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4149 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4150 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4151 | * This is one of |
| 4152 | * 'none' for arrays with no metadata (good luck...) |
| 4153 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4154 | * or N.M for internally known formats |
| 4155 | */ |
| 4156 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4157 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4158 | { |
| 4159 | if (mddev->persistent) |
| 4160 | return sprintf(page, "%d.%d\n", |
| 4161 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4162 | else if (mddev->external) |
| 4163 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4164 | else |
| 4165 | return sprintf(page, "none\n"); |
| 4166 | } |
| 4167 | |
| 4168 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4169 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4170 | { |
| 4171 | int major, minor; |
| 4172 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4173 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4174 | /* Changing the details of 'external' metadata is |
| 4175 | * always permitted. Otherwise there must be |
| 4176 | * no devices attached to the array. |
| 4177 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4178 | |
| 4179 | err = mddev_lock(mddev); |
| 4180 | if (err) |
| 4181 | return err; |
| 4182 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4183 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4184 | ; |
| 4185 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4186 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4187 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4188 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4189 | if (cmd_match(buf, "none")) { |
| 4190 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4191 | mddev->external = 0; |
| 4192 | mddev->major_version = 0; |
| 4193 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4194 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4195 | } |
| 4196 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4197 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4198 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4199 | namelen = sizeof(mddev->metadata_type)-1; |
| 4200 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4201 | mddev->metadata_type[namelen] = 0; |
| 4202 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4203 | mddev->metadata_type[--namelen] = 0; |
| 4204 | mddev->persistent = 0; |
| 4205 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4206 | mddev->major_version = 0; |
| 4207 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4208 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4209 | } |
| 4210 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4211 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4212 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4213 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4214 | buf = e+1; |
| 4215 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4216 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4217 | goto out_unlock; |
| 4218 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4219 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4220 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4221 | mddev->major_version = major; |
| 4222 | mddev->minor_version = minor; |
| 4223 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4224 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4225 | err = 0; |
| 4226 | out_unlock: |
| 4227 | mddev_unlock(mddev); |
| 4228 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4229 | } |
| 4230 | |
| 4231 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4232 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4233 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4234 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4235 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4236 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4237 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4238 | unsigned long recovery = mddev->recovery; |
| 4239 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4240 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4241 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4242 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4243 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4244 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4245 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4246 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4247 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4248 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4249 | type = "check"; |
| 4250 | else |
| 4251 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4252 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4253 | type = "recover"; |
NeilBrown | 985ca97 | 2015-07-06 12:26:57 +1000 | [diff] [blame] | 4254 | else if (mddev->reshape_position != MaxSector) |
| 4255 | type = "reshape"; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4256 | } |
| 4257 | return sprintf(page, "%s\n", type); |
| 4258 | } |
| 4259 | |
| 4260 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4261 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4262 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4263 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4264 | return -EINVAL; |
| 4265 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4266 | |
| 4267 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4268 | if (cmd_match(page, "frozen")) |
| 4269 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4270 | else |
| 4271 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4272 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4273 | mddev_lock(mddev) == 0) { |
| 4274 | flush_workqueue(md_misc_wq); |
| 4275 | if (mddev->sync_thread) { |
| 4276 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4277 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4278 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4279 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4280 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4281 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4282 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4283 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4284 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4285 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4286 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4287 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4288 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4289 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4290 | int err; |
| 4291 | if (mddev->pers->start_reshape == NULL) |
| 4292 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4293 | err = mddev_lock(mddev); |
| 4294 | if (!err) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4295 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4296 | err = mddev->pers->start_reshape(mddev); |
| 4297 | mddev_unlock(mddev); |
| 4298 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4299 | if (err) |
| 4300 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4301 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4302 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4303 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4304 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4305 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4306 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4307 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4308 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4309 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4310 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4311 | if (mddev->ro == 2) { |
| 4312 | /* A write to sync_action is enough to justify |
| 4313 | * canceling read-auto mode |
| 4314 | */ |
| 4315 | mddev->ro = 0; |
| 4316 | md_wakeup_thread(mddev->sync_thread); |
| 4317 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4318 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4319 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4320 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4321 | return len; |
| 4322 | } |
| 4323 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4324 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4325 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4326 | |
| 4327 | static ssize_t |
| 4328 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4329 | { |
| 4330 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4331 | } |
| 4332 | |
| 4333 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4334 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4335 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4336 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4337 | { |
| 4338 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4339 | (unsigned long long) |
| 4340 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4341 | } |
| 4342 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4343 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4344 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4345 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4346 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4347 | { |
| 4348 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4349 | mddev->sync_speed_min ? "local": "system"); |
| 4350 | } |
| 4351 | |
| 4352 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4353 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4354 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4355 | unsigned int min; |
| 4356 | int rv; |
| 4357 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4358 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4359 | min = 0; |
| 4360 | } else { |
| 4361 | rv = kstrtouint(buf, 10, &min); |
| 4362 | if (rv < 0) |
| 4363 | return rv; |
| 4364 | if (min == 0) |
| 4365 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4366 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4367 | mddev->sync_speed_min = min; |
| 4368 | return len; |
| 4369 | } |
| 4370 | |
| 4371 | static struct md_sysfs_entry md_sync_min = |
| 4372 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4373 | |
| 4374 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4375 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4376 | { |
| 4377 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4378 | mddev->sync_speed_max ? "local": "system"); |
| 4379 | } |
| 4380 | |
| 4381 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4382 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4383 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4384 | unsigned int max; |
| 4385 | int rv; |
| 4386 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4387 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4388 | max = 0; |
| 4389 | } else { |
| 4390 | rv = kstrtouint(buf, 10, &max); |
| 4391 | if (rv < 0) |
| 4392 | return rv; |
| 4393 | if (max == 0) |
| 4394 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4395 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4396 | mddev->sync_speed_max = max; |
| 4397 | return len; |
| 4398 | } |
| 4399 | |
| 4400 | static struct md_sysfs_entry md_sync_max = |
| 4401 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4402 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4403 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4404 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4405 | { |
| 4406 | return sprintf(page, "%d\n", mddev->degraded); |
| 4407 | } |
| 4408 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4409 | |
| 4410 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4411 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4412 | { |
| 4413 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4414 | } |
| 4415 | |
| 4416 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4417 | 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] | 4418 | { |
| 4419 | long n; |
| 4420 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4421 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4422 | return -EINVAL; |
| 4423 | |
| 4424 | if (n != 0 && n != 1) |
| 4425 | return -EINVAL; |
| 4426 | |
| 4427 | mddev->parallel_resync = n; |
| 4428 | |
| 4429 | if (mddev->sync_thread) |
| 4430 | wake_up(&resync_wait); |
| 4431 | |
| 4432 | return len; |
| 4433 | } |
| 4434 | |
| 4435 | /* force parallel resync, even with shared block devices */ |
| 4436 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4437 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4438 | sync_force_parallel_show, sync_force_parallel_store); |
| 4439 | |
| 4440 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4441 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4442 | { |
| 4443 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4444 | if (mddev->curr_resync == 0) |
| 4445 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4446 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4447 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4448 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4449 | db = resync - mddev->resync_mark_cnt; |
| 4450 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4451 | } |
| 4452 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4453 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4454 | |
| 4455 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4456 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4457 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4458 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4459 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4460 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4461 | return sprintf(page, "none\n"); |
| 4462 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4463 | if (mddev->curr_resync == 1 || |
| 4464 | mddev->curr_resync == 2) |
| 4465 | return sprintf(page, "delayed\n"); |
| 4466 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4467 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4468 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4469 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4470 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4471 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4472 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4473 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4474 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4475 | } |
| 4476 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4477 | static struct md_sysfs_entry md_sync_completed = |
| 4478 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4479 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4480 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4481 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4482 | { |
| 4483 | return sprintf(page, "%llu\n", |
| 4484 | (unsigned long long)mddev->resync_min); |
| 4485 | } |
| 4486 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4487 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4488 | { |
| 4489 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4490 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4491 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4492 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4493 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4494 | |
| 4495 | spin_lock(&mddev->lock); |
| 4496 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4497 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4498 | goto out_unlock; |
| 4499 | |
| 4500 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4501 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4502 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4503 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 4504 | /* Round down to multiple of 4K for safety */ |
| 4505 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4506 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4507 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4508 | out_unlock: |
| 4509 | spin_unlock(&mddev->lock); |
| 4510 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4511 | } |
| 4512 | |
| 4513 | static struct md_sysfs_entry md_min_sync = |
| 4514 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4515 | |
| 4516 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4517 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4518 | { |
| 4519 | if (mddev->resync_max == MaxSector) |
| 4520 | return sprintf(page, "max\n"); |
| 4521 | else |
| 4522 | return sprintf(page, "%llu\n", |
| 4523 | (unsigned long long)mddev->resync_max); |
| 4524 | } |
| 4525 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4526 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4527 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4528 | int err; |
| 4529 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4530 | if (strncmp(buf, "max", 3) == 0) |
| 4531 | mddev->resync_max = MaxSector; |
| 4532 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4533 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4534 | int chunk; |
| 4535 | |
| 4536 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4537 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4538 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4539 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4540 | goto out_unlock; |
| 4541 | |
| 4542 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4543 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4544 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4545 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4546 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4547 | |
| 4548 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4549 | chunk = mddev->chunk_sectors; |
| 4550 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4551 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4552 | |
| 4553 | err = -EINVAL; |
| 4554 | if (sector_div(temp, chunk)) |
| 4555 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4556 | } |
| 4557 | mddev->resync_max = max; |
| 4558 | } |
| 4559 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4560 | err = 0; |
| 4561 | out_unlock: |
| 4562 | spin_unlock(&mddev->lock); |
| 4563 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4564 | } |
| 4565 | |
| 4566 | static struct md_sysfs_entry md_max_sync = |
| 4567 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4568 | |
| 4569 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4570 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4571 | { |
| 4572 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4573 | } |
| 4574 | |
| 4575 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4576 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4577 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4578 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4579 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4580 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4581 | err = kstrtoull(buf, 10, &new); |
| 4582 | if (err < 0) |
| 4583 | return err; |
| 4584 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4585 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4586 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4587 | err = mddev_lock(mddev); |
| 4588 | if (err) |
| 4589 | return err; |
| 4590 | err = -EINVAL; |
| 4591 | if (mddev->pers == NULL || |
| 4592 | mddev->pers->quiesce == NULL) |
| 4593 | goto unlock; |
| 4594 | old = mddev->suspend_lo; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4595 | mddev->suspend_lo = new; |
| 4596 | if (new >= old) |
| 4597 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4598 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4599 | else { |
| 4600 | /* Expanding suspended region - need to wait */ |
| 4601 | mddev->pers->quiesce(mddev, 1); |
| 4602 | mddev->pers->quiesce(mddev, 0); |
| 4603 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4604 | err = 0; |
| 4605 | unlock: |
| 4606 | mddev_unlock(mddev); |
| 4607 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4608 | } |
| 4609 | static struct md_sysfs_entry md_suspend_lo = |
| 4610 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4611 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4612 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4613 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4614 | { |
| 4615 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4616 | } |
| 4617 | |
| 4618 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4619 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4620 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4621 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4622 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4623 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4624 | err = kstrtoull(buf, 10, &new); |
| 4625 | if (err < 0) |
| 4626 | return err; |
| 4627 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4628 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4629 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4630 | err = mddev_lock(mddev); |
| 4631 | if (err) |
| 4632 | return err; |
| 4633 | err = -EINVAL; |
| 4634 | if (mddev->pers == NULL || |
| 4635 | mddev->pers->quiesce == NULL) |
| 4636 | goto unlock; |
| 4637 | old = mddev->suspend_hi; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4638 | mddev->suspend_hi = new; |
| 4639 | if (new <= old) |
| 4640 | /* Shrinking suspended region */ |
| 4641 | mddev->pers->quiesce(mddev, 2); |
| 4642 | else { |
| 4643 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4644 | mddev->pers->quiesce(mddev, 1); |
| 4645 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4646 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4647 | err = 0; |
| 4648 | unlock: |
| 4649 | mddev_unlock(mddev); |
| 4650 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4651 | } |
| 4652 | static struct md_sysfs_entry md_suspend_hi = |
| 4653 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4654 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4655 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4656 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4657 | { |
| 4658 | if (mddev->reshape_position != MaxSector) |
| 4659 | return sprintf(page, "%llu\n", |
| 4660 | (unsigned long long)mddev->reshape_position); |
| 4661 | strcpy(page, "none\n"); |
| 4662 | return 5; |
| 4663 | } |
| 4664 | |
| 4665 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4666 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4667 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4668 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4669 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4670 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4671 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4672 | err = kstrtoull(buf, 10, &new); |
| 4673 | if (err < 0) |
| 4674 | return err; |
| 4675 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4676 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4677 | err = mddev_lock(mddev); |
| 4678 | if (err) |
| 4679 | return err; |
| 4680 | err = -EBUSY; |
| 4681 | if (mddev->pers) |
| 4682 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4683 | mddev->reshape_position = new; |
| 4684 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4685 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4686 | mddev->new_level = mddev->level; |
| 4687 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4688 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4689 | rdev_for_each(rdev, mddev) |
| 4690 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4691 | err = 0; |
| 4692 | unlock: |
| 4693 | mddev_unlock(mddev); |
| 4694 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4695 | } |
| 4696 | |
| 4697 | static struct md_sysfs_entry md_reshape_position = |
| 4698 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4699 | reshape_position_store); |
| 4700 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4701 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4702 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4703 | { |
| 4704 | return sprintf(page, "%s\n", |
| 4705 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4706 | } |
| 4707 | |
| 4708 | static ssize_t |
| 4709 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4710 | { |
| 4711 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4712 | int err; |
| 4713 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4714 | if (cmd_match(buf, "forwards")) |
| 4715 | backwards = 0; |
| 4716 | else if (cmd_match(buf, "backwards")) |
| 4717 | backwards = 1; |
| 4718 | else |
| 4719 | return -EINVAL; |
| 4720 | if (mddev->reshape_backwards == backwards) |
| 4721 | return len; |
| 4722 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4723 | err = mddev_lock(mddev); |
| 4724 | if (err) |
| 4725 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4726 | /* check if we are allowed to change */ |
| 4727 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4728 | err = -EBUSY; |
| 4729 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4730 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4731 | err = -EINVAL; |
| 4732 | else |
| 4733 | mddev->reshape_backwards = backwards; |
| 4734 | mddev_unlock(mddev); |
| 4735 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4736 | } |
| 4737 | |
| 4738 | static struct md_sysfs_entry md_reshape_direction = |
| 4739 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4740 | reshape_direction_store); |
| 4741 | |
| 4742 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4743 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4744 | { |
| 4745 | if (mddev->external_size) |
| 4746 | return sprintf(page, "%llu\n", |
| 4747 | (unsigned long long)mddev->array_sectors/2); |
| 4748 | else |
| 4749 | return sprintf(page, "default\n"); |
| 4750 | } |
| 4751 | |
| 4752 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4753 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4754 | { |
| 4755 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4756 | int err; |
| 4757 | |
| 4758 | err = mddev_lock(mddev); |
| 4759 | if (err) |
| 4760 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4761 | |
| 4762 | if (strncmp(buf, "default", 7) == 0) { |
| 4763 | if (mddev->pers) |
| 4764 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4765 | else |
| 4766 | sectors = mddev->array_sectors; |
| 4767 | |
| 4768 | mddev->external_size = 0; |
| 4769 | } else { |
| 4770 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4771 | err = -EINVAL; |
| 4772 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 4773 | err = -E2BIG; |
| 4774 | else |
| 4775 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4776 | } |
| 4777 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4778 | if (!err) { |
| 4779 | mddev->array_sectors = sectors; |
| 4780 | if (mddev->pers) { |
| 4781 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4782 | revalidate_disk(mddev->gendisk); |
| 4783 | } |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4784 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4785 | mddev_unlock(mddev); |
| 4786 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4787 | } |
| 4788 | |
| 4789 | static struct md_sysfs_entry md_array_size = |
| 4790 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4791 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4792 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4793 | static struct attribute *md_default_attrs[] = { |
| 4794 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4795 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4796 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4797 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4798 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4799 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4800 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4801 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4802 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4803 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4804 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4805 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4806 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4807 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4808 | NULL, |
| 4809 | }; |
| 4810 | |
| 4811 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4812 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4813 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4814 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4815 | &md_sync_min.attr, |
| 4816 | &md_sync_max.attr, |
| 4817 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4818 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4819 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4820 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4821 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4822 | &md_suspend_lo.attr, |
| 4823 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4824 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4825 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4826 | NULL, |
| 4827 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4828 | static struct attribute_group md_redundancy_group = { |
| 4829 | .name = NULL, |
| 4830 | .attrs = md_redundancy_attrs, |
| 4831 | }; |
| 4832 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4833 | static ssize_t |
| 4834 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4835 | { |
| 4836 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4837 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4838 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4839 | |
| 4840 | if (!entry->show) |
| 4841 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4842 | spin_lock(&all_mddevs_lock); |
| 4843 | if (list_empty(&mddev->all_mddevs)) { |
| 4844 | spin_unlock(&all_mddevs_lock); |
| 4845 | return -EBUSY; |
| 4846 | } |
| 4847 | mddev_get(mddev); |
| 4848 | spin_unlock(&all_mddevs_lock); |
| 4849 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4850 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4851 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4852 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | static ssize_t |
| 4856 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4857 | const char *page, size_t length) |
| 4858 | { |
| 4859 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4860 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4861 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4862 | |
| 4863 | if (!entry->store) |
| 4864 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4865 | if (!capable(CAP_SYS_ADMIN)) |
| 4866 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4867 | spin_lock(&all_mddevs_lock); |
| 4868 | if (list_empty(&mddev->all_mddevs)) { |
| 4869 | spin_unlock(&all_mddevs_lock); |
| 4870 | return -EBUSY; |
| 4871 | } |
| 4872 | mddev_get(mddev); |
| 4873 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4874 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4875 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4876 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | static void md_free(struct kobject *ko) |
| 4880 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4881 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4882 | |
| 4883 | if (mddev->sysfs_state) |
| 4884 | sysfs_put(mddev->sysfs_state); |
| 4885 | |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 4886 | if (mddev->queue) |
| 4887 | blk_cleanup_queue(mddev->queue); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4888 | if (mddev->gendisk) { |
| 4889 | del_gendisk(mddev->gendisk); |
| 4890 | put_disk(mddev->gendisk); |
| 4891 | } |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4892 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4893 | kfree(mddev); |
| 4894 | } |
| 4895 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4896 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4897 | .show = md_attr_show, |
| 4898 | .store = md_attr_store, |
| 4899 | }; |
| 4900 | static struct kobj_type md_ktype = { |
| 4901 | .release = md_free, |
| 4902 | .sysfs_ops = &md_sysfs_ops, |
| 4903 | .default_attrs = md_default_attrs, |
| 4904 | }; |
| 4905 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | int mdp_major = 0; |
| 4907 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4908 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4909 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4910 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4911 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4912 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4913 | kobject_del(&mddev->kobj); |
| 4914 | kobject_put(&mddev->kobj); |
| 4915 | } |
| 4916 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4917 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4919 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4920 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4922 | int partitioned; |
| 4923 | int shift; |
| 4924 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4925 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | |
| 4927 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4928 | return -ENODEV; |
| 4929 | |
| 4930 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4931 | shift = partitioned ? MdpMinorShift : 0; |
| 4932 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4934 | /* wait for any previous instance of this device to be |
| 4935 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4936 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4937 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4938 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4939 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4940 | error = -EEXIST; |
| 4941 | if (mddev->gendisk) |
| 4942 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4943 | |
| 4944 | if (name) { |
| 4945 | /* Need to ensure that 'name' is not a duplicate. |
| 4946 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4947 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4948 | spin_lock(&all_mddevs_lock); |
| 4949 | |
| 4950 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4951 | if (mddev2->gendisk && |
| 4952 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4953 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4954 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4955 | } |
| 4956 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4958 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4959 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4960 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4961 | if (!mddev->queue) |
| 4962 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4963 | mddev->queue->queuedata = mddev; |
| 4964 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4965 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 4966 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4968 | disk = alloc_disk(1 << shift); |
| 4969 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4970 | blk_cleanup_queue(mddev->queue); |
| 4971 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4972 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4974 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4976 | if (name) |
| 4977 | strcpy(disk->disk_name, name); |
| 4978 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4980 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | disk->fops = &md_fops; |
| 4983 | disk->private_data = mddev; |
| 4984 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4985 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4986 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4987 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4988 | * remove it now. |
| 4989 | */ |
| 4990 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4991 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4992 | /* As soon as we call add_disk(), another thread could get |
| 4993 | * through to md_open, so make sure it doesn't get too far |
| 4994 | */ |
| 4995 | mutex_lock(&mddev->open_mutex); |
| 4996 | add_disk(disk); |
| 4997 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4998 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4999 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5000 | if (error) { |
| 5001 | /* This isn't possible, but as kobject_init_and_add is marked |
| 5002 | * __must_check, we must do something with the result |
| 5003 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5004 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 5005 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5006 | error = 0; |
| 5007 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5008 | if (mddev->kobj.sd && |
| 5009 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5010 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5011 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5012 | abort: |
| 5013 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5014 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5015 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5016 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5017 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5018 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5019 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5020 | } |
| 5021 | |
| 5022 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 5023 | { |
| 5024 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | return NULL; |
| 5026 | } |
| 5027 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5028 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 5029 | { |
| 5030 | /* val must be "md_*" where * is not all digits. |
| 5031 | * We allocate an array with a large free minor number, and |
| 5032 | * set the name to val. val must not already be an active name. |
| 5033 | */ |
| 5034 | int len = strlen(val); |
| 5035 | char buf[DISK_NAME_LEN]; |
| 5036 | |
| 5037 | while (len && val[len-1] == '\n') |
| 5038 | len--; |
| 5039 | if (len >= DISK_NAME_LEN) |
| 5040 | return -E2BIG; |
| 5041 | strlcpy(buf, val, len+1); |
| 5042 | if (strncmp(buf, "md_", 3) != 0) |
| 5043 | return -EINVAL; |
| 5044 | return md_alloc(0, buf); |
| 5045 | } |
| 5046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5047 | static void md_safemode_timeout(unsigned long data) |
| 5048 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5049 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5051 | if (!atomic_read(&mddev->writes_pending)) { |
| 5052 | mddev->safemode = 1; |
| 5053 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5054 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5055 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5056 | md_wakeup_thread(mddev->thread); |
| 5057 | } |
| 5058 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5059 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5060 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5061 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5062 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5063 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5064 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5065 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5067 | if (list_empty(&mddev->disks)) |
| 5068 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5069 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | |
| 5071 | if (mddev->pers) |
| 5072 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5073 | /* Cannot run until previous stop completes properly */ |
| 5074 | if (mddev->sysfs_active) |
| 5075 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | /* |
| 5078 | * Analyze all RAID superblock(s) |
| 5079 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5080 | if (!mddev->raid_disks) { |
| 5081 | if (!mddev->persistent) |
| 5082 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5083 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5084 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5086 | if (mddev->level != LEVEL_NONE) |
| 5087 | request_module("md-level-%d", mddev->level); |
| 5088 | else if (mddev->clevel[0]) |
| 5089 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5090 | |
| 5091 | /* |
| 5092 | * Drop all container device buffers, from now on |
| 5093 | * the only valid external interface is through the md |
| 5094 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5095 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5096 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5097 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5098 | continue; |
| 5099 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5100 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5101 | |
| 5102 | /* perform some consistency tests on the device. |
| 5103 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5104 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5105 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5106 | if (rdev->meta_bdev) { |
| 5107 | /* Nothing to check */; |
| 5108 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5109 | if (mddev->dev_sectors && |
| 5110 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5111 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5112 | printk("md: %s: data overlaps metadata\n", |
| 5113 | mdname(mddev)); |
| 5114 | return -EINVAL; |
| 5115 | } |
| 5116 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5117 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5118 | > rdev->data_offset) { |
| 5119 | printk("md: %s: metadata overlaps data\n", |
| 5120 | mdname(mddev)); |
| 5121 | return -EINVAL; |
| 5122 | } |
| 5123 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5124 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5125 | } |
| 5126 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5127 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 5128 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5131 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5132 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5133 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5134 | if (mddev->level != LEVEL_NONE) |
| 5135 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5136 | mddev->level); |
| 5137 | else |
| 5138 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5139 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | return -EINVAL; |
| 5141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5143 | if (mddev->level != pers->level) { |
| 5144 | mddev->level = pers->level; |
| 5145 | mddev->new_level = pers->level; |
| 5146 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5147 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5149 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5150 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5151 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5152 | module_put(pers->owner); |
| 5153 | return -EINVAL; |
| 5154 | } |
| 5155 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5156 | if (pers->sync_request) { |
| 5157 | /* Warn if this is a potentially silly |
| 5158 | * configuration. |
| 5159 | */ |
| 5160 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5161 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5162 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5163 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5164 | rdev_for_each(rdev, mddev) |
| 5165 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5166 | if (rdev < rdev2 && |
| 5167 | rdev->bdev->bd_contains == |
| 5168 | rdev2->bdev->bd_contains) { |
| 5169 | printk(KERN_WARNING |
| 5170 | "%s: WARNING: %s appears to be" |
| 5171 | " on the same physical disk as" |
| 5172 | " %s.\n", |
| 5173 | mdname(mddev), |
| 5174 | bdevname(rdev->bdev,b), |
| 5175 | bdevname(rdev2->bdev,b2)); |
| 5176 | warned = 1; |
| 5177 | } |
| 5178 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5179 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5180 | if (warned) |
| 5181 | printk(KERN_WARNING |
| 5182 | "True protection against single-disk" |
| 5183 | " failure might be compromised.\n"); |
| 5184 | } |
| 5185 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5186 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5187 | /* may be over-ridden by personality */ |
| 5188 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5189 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5190 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5192 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5193 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5194 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5195 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5196 | if (err) |
| 5197 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5198 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5199 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5200 | " but 'external_size' not in effect?\n", __func__); |
| 5201 | printk(KERN_ERR |
| 5202 | "md: invalid array_size %llu > default size %llu\n", |
| 5203 | (unsigned long long)mddev->array_sectors / 2, |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5204 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5205 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5206 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5207 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5208 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5209 | struct bitmap *bitmap; |
| 5210 | |
| 5211 | bitmap = bitmap_create(mddev, -1); |
| 5212 | if (IS_ERR(bitmap)) { |
| 5213 | err = PTR_ERR(bitmap); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5214 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5215 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5216 | } else |
| 5217 | mddev->bitmap = bitmap; |
| 5218 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | if (err) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5221 | mddev_detach(mddev); |
NeilBrown | 0c35bd4 | 2015-03-13 11:51:18 +1100 | [diff] [blame] | 5222 | if (mddev->private) |
| 5223 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5224 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5225 | module_put(pers->owner); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5226 | bitmap_destroy(mddev); |
| 5227 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5229 | if (mddev->queue) { |
| 5230 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5231 | mddev->queue->backing_dev_info.congested_fn = md_congested; |
| 5232 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5233 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5234 | if (mddev->kobj.sd && |
| 5235 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5236 | printk(KERN_WARNING |
| 5237 | "md: cannot register extra attributes for %s\n", |
| 5238 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5239 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5240 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5241 | mddev->ro = 0; |
| 5242 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5243 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5244 | atomic_set(&mddev->max_corr_read_errors, |
| 5245 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | mddev->safemode = 0; |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5247 | if (mddev_is_clustered(mddev)) |
| 5248 | mddev->safemode_delay = 0; |
| 5249 | else |
| 5250 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5252 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5253 | spin_lock(&mddev->lock); |
| 5254 | mddev->pers = pers; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5255 | mddev->ready = 1; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5256 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5257 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5258 | if (rdev->raid_disk >= 0) |
| 5259 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5260 | /* failure here is OK */; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5261 | |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 5262 | if (mddev->degraded && !mddev->ro) |
| 5263 | /* This ensures that recovering status is reported immediately |
| 5264 | * via sysfs - until a lack of spares is confirmed. |
| 5265 | */ |
| 5266 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5268 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5269 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5270 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5271 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5272 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5273 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5274 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5275 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | return 0; |
| 5277 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5278 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5280 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5281 | { |
| 5282 | int err; |
| 5283 | |
| 5284 | err = md_run(mddev); |
| 5285 | if (err) |
| 5286 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5287 | err = bitmap_load(mddev); |
| 5288 | if (err) { |
| 5289 | bitmap_destroy(mddev); |
| 5290 | goto out; |
| 5291 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5292 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5293 | if (mddev_is_clustered(mddev)) |
| 5294 | md_allow_write(mddev); |
| 5295 | |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5296 | md_wakeup_thread(mddev->thread); |
| 5297 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5298 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5299 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5300 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5301 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5302 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5303 | out: |
| 5304 | return err; |
| 5305 | } |
| 5306 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5307 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | { |
| 5309 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5310 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5311 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5313 | return -ENXIO; |
| 5314 | if (!mddev->pers) |
| 5315 | return -EINVAL; |
| 5316 | if (!mddev->ro) |
| 5317 | return -EBUSY; |
| 5318 | mddev->safemode = 0; |
| 5319 | mddev->ro = 0; |
| 5320 | set_disk_ro(disk, 0); |
| 5321 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5322 | mdname(mddev)); |
| 5323 | /* Kick recovery or resync if necessary */ |
| 5324 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5325 | md_wakeup_thread(mddev->thread); |
| 5326 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5327 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5328 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5329 | } |
| 5330 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5331 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5332 | { |
| 5333 | mddev->array_sectors = 0; |
| 5334 | mddev->external_size = 0; |
| 5335 | mddev->dev_sectors = 0; |
| 5336 | mddev->raid_disks = 0; |
| 5337 | mddev->recovery_cp = 0; |
| 5338 | mddev->resync_min = 0; |
| 5339 | mddev->resync_max = MaxSector; |
| 5340 | mddev->reshape_position = MaxSector; |
| 5341 | mddev->external = 0; |
| 5342 | mddev->persistent = 0; |
| 5343 | mddev->level = LEVEL_NONE; |
| 5344 | mddev->clevel[0] = 0; |
| 5345 | mddev->flags = 0; |
| 5346 | mddev->ro = 0; |
| 5347 | mddev->metadata_type[0] = 0; |
| 5348 | mddev->chunk_sectors = 0; |
| 5349 | mddev->ctime = mddev->utime = 0; |
| 5350 | mddev->layout = 0; |
| 5351 | mddev->max_disks = 0; |
| 5352 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5353 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5354 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5355 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5356 | mddev->new_level = LEVEL_NONE; |
| 5357 | mddev->new_layout = 0; |
| 5358 | mddev->new_chunk_sectors = 0; |
| 5359 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5360 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5361 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5362 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5363 | mddev->recovery = 0; |
| 5364 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5365 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5366 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5367 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5368 | mddev->private = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5369 | mddev->bitmap_info.offset = 0; |
| 5370 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5371 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5372 | mddev->bitmap_info.chunksize = 0; |
| 5373 | mddev->bitmap_info.daemon_sleep = 0; |
| 5374 | mddev->bitmap_info.max_write_behind = 0; |
| 5375 | } |
| 5376 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5377 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5378 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 5379 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5380 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5381 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5382 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5383 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5384 | } |
| 5385 | |
| 5386 | del_timer_sync(&mddev->safemode_timer); |
| 5387 | |
| 5388 | bitmap_flush(mddev); |
| 5389 | md_super_wait(mddev); |
| 5390 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5391 | if (mddev->ro == 0 && |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5392 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
| 5393 | (mddev->flags & MD_UPDATE_SB_FLAGS))) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5394 | /* mark array as shutdown cleanly */ |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5395 | if (!mddev_is_clustered(mddev)) |
| 5396 | mddev->in_sync = 1; |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5397 | md_update_sb(mddev, 1); |
| 5398 | } |
| 5399 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5400 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5401 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5402 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 5403 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5404 | __md_stop_writes(mddev); |
| 5405 | mddev_unlock(mddev); |
| 5406 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5407 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5408 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5409 | static void mddev_detach(struct mddev *mddev) |
| 5410 | { |
| 5411 | struct bitmap *bitmap = mddev->bitmap; |
| 5412 | /* wait for behind writes to complete */ |
| 5413 | if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { |
| 5414 | printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n", |
| 5415 | mdname(mddev)); |
| 5416 | /* need to kick something here to make sure I/O goes? */ |
| 5417 | wait_event(bitmap->behind_wait, |
| 5418 | atomic_read(&bitmap->behind_writes) == 0); |
| 5419 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5420 | if (mddev->pers && mddev->pers->quiesce) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5421 | mddev->pers->quiesce(mddev, 1); |
| 5422 | mddev->pers->quiesce(mddev, 0); |
| 5423 | } |
| 5424 | md_unregister_thread(&mddev->thread); |
| 5425 | if (mddev->queue) |
| 5426 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 5427 | } |
| 5428 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5429 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5430 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5431 | struct md_personality *pers = mddev->pers; |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5432 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 5433 | /* Ensure ->event_work is done */ |
| 5434 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5435 | spin_lock(&mddev->lock); |
| 5436 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5437 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5438 | spin_unlock(&mddev->lock); |
| 5439 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5440 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5441 | if (pers->sync_request && mddev->to_remove == NULL) |
| 5442 | mddev->to_remove = &md_redundancy_group; |
| 5443 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5444 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5445 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5446 | |
| 5447 | void md_stop(struct mddev *mddev) |
| 5448 | { |
| 5449 | /* stop the array and free an attached data structures. |
| 5450 | * This is called from dm-raid |
| 5451 | */ |
| 5452 | __md_stop(mddev); |
| 5453 | bitmap_destroy(mddev); |
| 5454 | if (mddev->bio_set) |
| 5455 | bioset_free(mddev->bio_set); |
| 5456 | } |
| 5457 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5458 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5459 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5460 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5461 | { |
| 5462 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5463 | int did_freeze = 0; |
| 5464 | |
| 5465 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5466 | did_freeze = 1; |
| 5467 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5468 | md_wakeup_thread(mddev->thread); |
| 5469 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5470 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5471 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5472 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5473 | /* Thread might be blocked waiting for metadata update |
| 5474 | * which will now never happen */ |
| 5475 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5476 | |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5477 | if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
| 5478 | return -EBUSY; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5479 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5480 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 5481 | &mddev->recovery)); |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5482 | wait_event(mddev->sb_wait, |
| 5483 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5484 | mddev_lock_nointr(mddev); |
| 5485 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5486 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5487 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5488 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5489 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5490 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5491 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5492 | if (did_freeze) { |
| 5493 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5494 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5495 | md_wakeup_thread(mddev->thread); |
| 5496 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5497 | err = -EBUSY; |
| 5498 | goto out; |
| 5499 | } |
| 5500 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5501 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5502 | |
| 5503 | err = -ENXIO; |
| 5504 | if (mddev->ro==1) |
| 5505 | goto out; |
| 5506 | mddev->ro = 1; |
| 5507 | set_disk_ro(mddev->gendisk, 1); |
| 5508 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5509 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5510 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5511 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5512 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5513 | } |
| 5514 | out: |
| 5515 | mutex_unlock(&mddev->open_mutex); |
| 5516 | return err; |
| 5517 | } |
| 5518 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5519 | /* mode: |
| 5520 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5521 | * 2 - stop but do not disassemble array |
| 5522 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5523 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5524 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5525 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5527 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5528 | int did_freeze = 0; |
| 5529 | |
| 5530 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5531 | did_freeze = 1; |
| 5532 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5533 | md_wakeup_thread(mddev->thread); |
| 5534 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5535 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5536 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5537 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5538 | /* Thread might be blocked waiting for metadata update |
| 5539 | * which will now never happen */ |
| 5540 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5541 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5542 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5543 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 5544 | !test_bit(MD_RECOVERY_RUNNING, |
| 5545 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5546 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5547 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5548 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5549 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5550 | mddev->sysfs_active || |
| 5551 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5552 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5553 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5554 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5555 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5556 | if (did_freeze) { |
| 5557 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5558 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5559 | md_wakeup_thread(mddev->thread); |
| 5560 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 5561 | return -EBUSY; |
| 5562 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5563 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5564 | if (mddev->ro) |
| 5565 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5566 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5567 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5568 | __md_stop(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5569 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5570 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5571 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5572 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5573 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5574 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5575 | if (rdev->raid_disk >= 0) |
| 5576 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5577 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5578 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5579 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5580 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5581 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5582 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5583 | if (mddev->ro) |
| 5584 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5585 | } else |
| 5586 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5587 | /* |
| 5588 | * Free resources if final stop |
| 5589 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5590 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5592 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5593 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5594 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5595 | struct file *f = mddev->bitmap_info.file; |
| 5596 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5597 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5598 | spin_unlock(&mddev->lock); |
| 5599 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5600 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5601 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5603 | export_array(mddev); |
| 5604 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5605 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5606 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5607 | if (mddev->hold_active == UNTIL_STOP) |
| 5608 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5609 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5610 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5611 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5612 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5613 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5614 | } |
| 5615 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5616 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5617 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5618 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5619 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | int err; |
| 5621 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5622 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | |
| 5625 | printk(KERN_INFO "md: running: "); |
| 5626 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5627 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5628 | char b[BDEVNAME_SIZE]; |
| 5629 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5630 | } |
| 5631 | printk("\n"); |
| 5632 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5633 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | if (err) { |
| 5635 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5636 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5637 | } |
| 5638 | } |
| 5639 | |
| 5640 | /* |
| 5641 | * lets try to run arrays based on all disks that have arrived |
| 5642 | * until now. (those are in pending_raid_disks) |
| 5643 | * |
| 5644 | * the method: pick the first pending disk, collect all disks with |
| 5645 | * the same UUID, remove all from the pending list and put them into |
| 5646 | * the 'same_array' list. Then order this list based on superblock |
| 5647 | * update time (freshest comes first), kick out 'old' disks and |
| 5648 | * compare superblocks. If everything's fine then run it. |
| 5649 | * |
| 5650 | * If "unit" is allocated, then bump its reference count |
| 5651 | */ |
| 5652 | static void autorun_devices(int part) |
| 5653 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5654 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5655 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | char b[BDEVNAME_SIZE]; |
| 5657 | |
| 5658 | printk(KERN_INFO "md: autorun ...\n"); |
| 5659 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5660 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5661 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5662 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5663 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5664 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5665 | |
| 5666 | printk(KERN_INFO "md: considering %s ...\n", |
| 5667 | bdevname(rdev0->bdev,b)); |
| 5668 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5669 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5671 | printk(KERN_INFO "md: adding %s ...\n", |
| 5672 | bdevname(rdev->bdev,b)); |
| 5673 | list_move(&rdev->same_set, &candidates); |
| 5674 | } |
| 5675 | /* |
| 5676 | * now we have a set of devices, with all of them having |
| 5677 | * mostly sane superblocks. It's time to allocate the |
| 5678 | * mddev. |
| 5679 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5680 | if (part) { |
| 5681 | dev = MKDEV(mdp_major, |
| 5682 | rdev0->preferred_minor << MdpMinorShift); |
| 5683 | unit = MINOR(dev) >> MdpMinorShift; |
| 5684 | } else { |
| 5685 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5686 | unit = MINOR(dev); |
| 5687 | } |
| 5688 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5689 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5690 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5691 | break; |
| 5692 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5693 | |
| 5694 | md_probe(dev, NULL, NULL); |
| 5695 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5696 | if (!mddev || !mddev->gendisk) { |
| 5697 | if (mddev) |
| 5698 | mddev_put(mddev); |
| 5699 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5700 | "md: cannot allocate memory for md drive.\n"); |
| 5701 | break; |
| 5702 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5703 | if (mddev_lock(mddev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5704 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5705 | mdname(mddev)); |
| 5706 | else if (mddev->raid_disks || mddev->major_version |
| 5707 | || !list_empty(&mddev->disks)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5708 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | "md: %s already running, cannot run %s\n", |
| 5710 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5711 | mddev_unlock(mddev); |
| 5712 | } else { |
| 5713 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5714 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5715 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5716 | list_del_init(&rdev->same_set); |
| 5717 | if (bind_rdev_to_array(rdev, mddev)) |
| 5718 | export_rdev(rdev); |
| 5719 | } |
| 5720 | autorun_array(mddev); |
| 5721 | mddev_unlock(mddev); |
| 5722 | } |
| 5723 | /* on success, candidates will be empty, on error |
| 5724 | * it won't... |
| 5725 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5726 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5727 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5729 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | mddev_put(mddev); |
| 5731 | } |
| 5732 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5733 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5734 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5736 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5737 | { |
| 5738 | mdu_version_t ver; |
| 5739 | |
| 5740 | ver.major = MD_MAJOR_VERSION; |
| 5741 | ver.minor = MD_MINOR_VERSION; |
| 5742 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5743 | |
| 5744 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5745 | return -EFAULT; |
| 5746 | |
| 5747 | return 0; |
| 5748 | } |
| 5749 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5750 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5751 | { |
| 5752 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5753 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5754 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5755 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5756 | nr = working = insync = failed = spare = 0; |
| 5757 | rcu_read_lock(); |
| 5758 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5759 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5760 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5761 | failed++; |
| 5762 | else { |
| 5763 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5764 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5765 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | else |
| 5767 | spare++; |
| 5768 | } |
| 5769 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5770 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5771 | |
| 5772 | info.major_version = mddev->major_version; |
| 5773 | info.minor_version = mddev->minor_version; |
| 5774 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5775 | info.ctime = mddev->ctime; |
| 5776 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5777 | info.size = mddev->dev_sectors / 2; |
| 5778 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5779 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5780 | info.nr_disks = nr; |
| 5781 | info.raid_disks = mddev->raid_disks; |
| 5782 | info.md_minor = mddev->md_minor; |
| 5783 | info.not_persistent= !mddev->persistent; |
| 5784 | |
| 5785 | info.utime = mddev->utime; |
| 5786 | info.state = 0; |
| 5787 | if (mddev->in_sync) |
| 5788 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5789 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 5790 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 5791 | if (mddev_is_clustered(mddev)) |
| 5792 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5793 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5794 | info.working_disks = working; |
| 5795 | info.failed_disks = failed; |
| 5796 | info.spare_disks = spare; |
| 5797 | |
| 5798 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5799 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5800 | |
| 5801 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5802 | return -EFAULT; |
| 5803 | |
| 5804 | return 0; |
| 5805 | } |
| 5806 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5807 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5808 | { |
| 5809 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5810 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5811 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5812 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5813 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5814 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5815 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5816 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5817 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5818 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5819 | /* bitmap enabled */ |
| 5820 | if (mddev->bitmap_info.file) { |
| 5821 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 5822 | sizeof(file->pathname)); |
| 5823 | if (IS_ERR(ptr)) |
| 5824 | err = PTR_ERR(ptr); |
| 5825 | else |
| 5826 | memmove(file->pathname, ptr, |
| 5827 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 5828 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5829 | spin_unlock(&mddev->lock); |
| 5830 | |
| 5831 | if (err == 0 && |
| 5832 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5833 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5834 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5835 | kfree(file); |
| 5836 | return err; |
| 5837 | } |
| 5838 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5839 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5840 | { |
| 5841 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5842 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5843 | |
| 5844 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5845 | return -EFAULT; |
| 5846 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5847 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 5848 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | if (rdev) { |
| 5850 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5851 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5852 | info.raid_disk = rdev->raid_disk; |
| 5853 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5854 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5855 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5856 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5858 | info.state |= (1<<MD_DISK_SYNC); |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 5859 | } else if (test_bit(Journal, &rdev->flags)) |
| 5860 | info.state |= (1<<MD_DISK_JOURNAL); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5861 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5862 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | } else { |
| 5864 | info.major = info.minor = 0; |
| 5865 | info.raid_disk = -1; |
| 5866 | info.state = (1<<MD_DISK_REMOVED); |
| 5867 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5868 | rcu_read_unlock(); |
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 add_new_disk(struct mddev *mddev, mdu_disk_info_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | { |
| 5878 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5879 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5880 | dev_t dev = MKDEV(info->major,info->minor); |
| 5881 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5882 | if (mddev_is_clustered(mddev) && |
| 5883 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 5884 | pr_err("%s: Cannot add to clustered mddev.\n", |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5885 | mdname(mddev)); |
| 5886 | return -EINVAL; |
| 5887 | } |
| 5888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5889 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5890 | return -EOVERFLOW; |
| 5891 | |
| 5892 | if (!mddev->raid_disks) { |
| 5893 | int err; |
| 5894 | /* expecting a device which has a superblock */ |
| 5895 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5896 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5897 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | "md: md_import_device returned %ld\n", |
| 5899 | PTR_ERR(rdev)); |
| 5900 | return PTR_ERR(rdev); |
| 5901 | } |
| 5902 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5903 | struct md_rdev *rdev0 |
| 5904 | = list_entry(mddev->disks.next, |
| 5905 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5906 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5907 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5908 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5909 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | "md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5911 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | bdevname(rdev0->bdev,b2)); |
| 5913 | export_rdev(rdev); |
| 5914 | return -EINVAL; |
| 5915 | } |
| 5916 | } |
| 5917 | err = bind_rdev_to_array(rdev, mddev); |
| 5918 | if (err) |
| 5919 | export_rdev(rdev); |
| 5920 | return err; |
| 5921 | } |
| 5922 | |
| 5923 | /* |
| 5924 | * add_new_disk can be used once the array is assembled |
| 5925 | * to add "hot spares". They must already have a superblock |
| 5926 | * written |
| 5927 | */ |
| 5928 | if (mddev->pers) { |
| 5929 | int err; |
| 5930 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5931 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | "%s: personality does not support diskops!\n", |
| 5933 | mdname(mddev)); |
| 5934 | return -EINVAL; |
| 5935 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5936 | if (mddev->persistent) |
| 5937 | rdev = md_import_device(dev, mddev->major_version, |
| 5938 | mddev->minor_version); |
| 5939 | else |
| 5940 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5941 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5942 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | "md: md_import_device returned %ld\n", |
| 5944 | PTR_ERR(rdev)); |
| 5945 | return PTR_ERR(rdev); |
| 5946 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5947 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5948 | if (!mddev->persistent) { |
| 5949 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5950 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5951 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5952 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 5953 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5954 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5955 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 5956 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5957 | } else |
| 5958 | super_types[mddev->major_version]. |
| 5959 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5960 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 5961 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5962 | /* This was a hot-add request, but events doesn't |
| 5963 | * match, so reject it. |
| 5964 | */ |
| 5965 | export_rdev(rdev); |
| 5966 | return -EINVAL; |
| 5967 | } |
| 5968 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5969 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5970 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5971 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5972 | else |
| 5973 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5974 | |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 5975 | if (info->state & (1<<MD_DISK_JOURNAL)) |
| 5976 | set_bit(Journal, &rdev->flags); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5977 | /* |
| 5978 | * check whether the device shows up in other nodes |
| 5979 | */ |
| 5980 | if (mddev_is_clustered(mddev)) { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 5981 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5982 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 5983 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5984 | /* --add initiated by this node */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 5985 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5986 | if (err) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5987 | export_rdev(rdev); |
| 5988 | return err; |
| 5989 | } |
| 5990 | } |
| 5991 | } |
| 5992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5993 | rdev->raid_disk = -1; |
| 5994 | err = bind_rdev_to_array(rdev, mddev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 5995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5996 | if (err) |
| 5997 | export_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 5998 | |
| 5999 | if (mddev_is_clustered(mddev)) { |
| 6000 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
| 6001 | md_cluster_ops->new_disk_ack(mddev, (err == 0)); |
| 6002 | else { |
| 6003 | if (err) |
| 6004 | md_cluster_ops->add_new_disk_cancel(mddev); |
| 6005 | else |
| 6006 | err = add_bound_rdev(rdev); |
| 6007 | } |
| 6008 | |
| 6009 | } else if (!err) |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 6010 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6012 | return err; |
| 6013 | } |
| 6014 | |
| 6015 | /* otherwise, add_new_disk is only allowed |
| 6016 | * for major_version==0 superblocks |
| 6017 | */ |
| 6018 | if (mddev->major_version != 0) { |
| 6019 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 6020 | mdname(mddev)); |
| 6021 | return -EINVAL; |
| 6022 | } |
| 6023 | |
| 6024 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 6025 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6026 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6027 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6028 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6029 | "md: error, md_import_device() returned %ld\n", |
| 6030 | PTR_ERR(rdev)); |
| 6031 | return PTR_ERR(rdev); |
| 6032 | } |
| 6033 | rdev->desc_nr = info->number; |
| 6034 | if (info->raid_disk < mddev->raid_disks) |
| 6035 | rdev->raid_disk = info->raid_disk; |
| 6036 | else |
| 6037 | rdev->raid_disk = -1; |
| 6038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6039 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6040 | if (info->state & (1<<MD_DISK_SYNC)) |
| 6041 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6042 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6043 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6044 | set_bit(WriteMostly, &rdev->flags); |
| 6045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6046 | if (!mddev->persistent) { |
| 6047 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6048 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 6049 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6050 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6051 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6052 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6053 | err = bind_rdev_to_array(rdev, mddev); |
| 6054 | if (err) { |
| 6055 | export_rdev(rdev); |
| 6056 | return err; |
| 6057 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6058 | } |
| 6059 | |
| 6060 | return 0; |
| 6061 | } |
| 6062 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6063 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | { |
| 6065 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6066 | struct md_rdev *rdev; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 6067 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6069 | rdev = find_rdev(mddev, dev); |
| 6070 | if (!rdev) |
| 6071 | return -ENXIO; |
| 6072 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6073 | if (mddev_is_clustered(mddev)) |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 6074 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6075 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6076 | if (rdev->raid_disk < 0) |
| 6077 | goto kick_rdev; |
| 6078 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6079 | clear_bit(Blocked, &rdev->flags); |
| 6080 | remove_and_add_spares(mddev, rdev); |
| 6081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | if (rdev->raid_disk >= 0) |
| 6083 | goto busy; |
| 6084 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6085 | kick_rdev: |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 6086 | if (mddev_is_clustered(mddev) && ret == 0) |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6087 | md_cluster_ops->remove_disk(mddev, rdev); |
| 6088 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6089 | md_kick_rdev_from_array(rdev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6090 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6091 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6092 | |
| 6093 | return 0; |
| 6094 | busy: |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 6095 | if (mddev_is_clustered(mddev) && ret == 0) |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6096 | md_cluster_ops->metadata_update_cancel(mddev); |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6097 | |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 6098 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6100 | return -EBUSY; |
| 6101 | } |
| 6102 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6103 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | { |
| 6105 | char b[BDEVNAME_SIZE]; |
| 6106 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6107 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | |
| 6109 | if (!mddev->pers) |
| 6110 | return -ENODEV; |
| 6111 | |
| 6112 | if (mddev->major_version != 0) { |
| 6113 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 6114 | " version-0 superblocks.\n", |
| 6115 | mdname(mddev)); |
| 6116 | return -EINVAL; |
| 6117 | } |
| 6118 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6119 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | "%s: personality does not support diskops!\n", |
| 6121 | mdname(mddev)); |
| 6122 | return -EINVAL; |
| 6123 | } |
| 6124 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6125 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6126 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6127 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | "md: error, md_import_device() returned %ld\n", |
| 6129 | PTR_ERR(rdev)); |
| 6130 | return -EINVAL; |
| 6131 | } |
| 6132 | |
| 6133 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6134 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6136 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6138 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6140 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6141 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | "md: can not hot-add faulty %s disk to %s!\n", |
| 6143 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6144 | err = -EINVAL; |
| 6145 | goto abort_export; |
| 6146 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6147 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6148 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6149 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 6150 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6151 | err = bind_rdev_to_array(rdev, mddev); |
| 6152 | if (err) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 6153 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6154 | |
| 6155 | /* |
| 6156 | * The rest should better be atomic, we can have disk failures |
| 6157 | * noticed in interrupt contexts ... |
| 6158 | */ |
| 6159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | rdev->raid_disk = -1; |
| 6161 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6162 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | /* |
| 6164 | * Kick recovery, maybe this spare has to be added to the |
| 6165 | * array immediately. |
| 6166 | */ |
| 6167 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6168 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6169 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6170 | return 0; |
| 6171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | abort_export: |
| 6173 | export_rdev(rdev); |
| 6174 | return err; |
| 6175 | } |
| 6176 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6177 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6178 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6179 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6180 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6181 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 6182 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6183 | return -EBUSY; |
| 6184 | if (mddev->recovery || mddev->sync_thread) |
| 6185 | return -EBUSY; |
| 6186 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6187 | } |
| 6188 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6189 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6190 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6191 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6192 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6193 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 6194 | return -EEXIST; /* cannot add when bitmap is present */ |
| 6195 | f = fget(fd); |
| 6196 | |
| 6197 | if (f == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6198 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 6199 | mdname(mddev)); |
| 6200 | return -EBADF; |
| 6201 | } |
| 6202 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6203 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6204 | if (!S_ISREG(inode->i_mode)) { |
| 6205 | printk(KERN_ERR "%s: error: bitmap file must be a regular file\n", |
| 6206 | mdname(mddev)); |
| 6207 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6208 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6209 | printk(KERN_ERR "%s: error: bitmap file must open for write\n", |
| 6210 | mdname(mddev)); |
| 6211 | err = -EBADF; |
| 6212 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6213 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 6214 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6215 | err = -EBUSY; |
| 6216 | } |
| 6217 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6218 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6219 | return err; |
| 6220 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6221 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6222 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6223 | } else if (mddev->bitmap == NULL) |
| 6224 | return -ENOENT; /* cannot remove what isn't there */ |
| 6225 | err = 0; |
| 6226 | if (mddev->pers) { |
| 6227 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6228 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6229 | struct bitmap *bitmap; |
| 6230 | |
| 6231 | bitmap = bitmap_create(mddev, -1); |
| 6232 | if (!IS_ERR(bitmap)) { |
| 6233 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6234 | err = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6235 | } else |
| 6236 | err = PTR_ERR(bitmap); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6237 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6238 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6239 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6240 | fd = -1; /* make sure to put the file */ |
| 6241 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6242 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6243 | } |
| 6244 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6245 | struct file *f = mddev->bitmap_info.file; |
| 6246 | if (f) { |
| 6247 | spin_lock(&mddev->lock); |
| 6248 | mddev->bitmap_info.file = NULL; |
| 6249 | spin_unlock(&mddev->lock); |
| 6250 | fput(f); |
| 6251 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6252 | } |
| 6253 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6254 | return err; |
| 6255 | } |
| 6256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6257 | /* |
| 6258 | * set_array_info is used two different ways |
| 6259 | * The original usage is when creating a new array. |
| 6260 | * In this usage, raid_disks is > 0 and it together with |
| 6261 | * level, size, not_persistent,layout,chunksize determine the |
| 6262 | * shape of the array. |
| 6263 | * This will always create an array with a type-0.90.0 superblock. |
| 6264 | * The newer usage is when assembling an array. |
| 6265 | * In this case raid_disks will be 0, and the major_version field is |
| 6266 | * use to determine which style super-blocks are to be found on the devices. |
| 6267 | * The minor and patch _version numbers are also kept incase the |
| 6268 | * super_block handler wishes to interpret them. |
| 6269 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6270 | 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] | 6271 | { |
| 6272 | |
| 6273 | if (info->raid_disks == 0) { |
| 6274 | /* just setting version number for superblock loading */ |
| 6275 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6276 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6277 | super_types[info->major_version].name == NULL) { |
| 6278 | /* maybe try to auto-load a module? */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6279 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6280 | "md: superblock version %d not known\n", |
| 6281 | info->major_version); |
| 6282 | return -EINVAL; |
| 6283 | } |
| 6284 | mddev->major_version = info->major_version; |
| 6285 | mddev->minor_version = info->minor_version; |
| 6286 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6287 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6288 | /* ensure mddev_put doesn't delete this now that there |
| 6289 | * is some minimal configuration. |
| 6290 | */ |
| 6291 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6292 | return 0; |
| 6293 | } |
| 6294 | mddev->major_version = MD_MAJOR_VERSION; |
| 6295 | mddev->minor_version = MD_MINOR_VERSION; |
| 6296 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 6297 | mddev->ctime = get_seconds(); |
| 6298 | |
| 6299 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6300 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6301 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6302 | mddev->raid_disks = info->raid_disks; |
| 6303 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6304 | * openned |
| 6305 | */ |
| 6306 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6307 | mddev->recovery_cp = MaxSector; |
| 6308 | else |
| 6309 | mddev->recovery_cp = 0; |
| 6310 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6311 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6312 | |
| 6313 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6314 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6315 | |
| 6316 | mddev->max_disks = MD_SB_DISKS; |
| 6317 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6318 | if (mddev->persistent) |
| 6319 | mddev->flags = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6320 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6321 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6322 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6323 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6324 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6325 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6326 | mddev->reshape_position = MaxSector; |
| 6327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6328 | /* |
| 6329 | * Generate a 128 bit UUID |
| 6330 | */ |
| 6331 | get_random_bytes(mddev->uuid, 16); |
| 6332 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6333 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6334 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6335 | mddev->new_layout = mddev->layout; |
| 6336 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6337 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6339 | return 0; |
| 6340 | } |
| 6341 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6342 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6343 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6344 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6345 | |
| 6346 | if (mddev->external_size) |
| 6347 | return; |
| 6348 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6349 | mddev->array_sectors = array_sectors; |
| 6350 | } |
| 6351 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6352 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6353 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6354 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6355 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6356 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6357 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6358 | |
| 6359 | if (mddev->pers->resize == NULL) |
| 6360 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6361 | /* The "num_sectors" is the number of sectors of each device that |
| 6362 | * is used. This can only make sense for arrays with redundancy. |
| 6363 | * linear and raid0 always use whatever space is available. We can only |
| 6364 | * consider changing this number if no resync or reconstruction is |
| 6365 | * 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] | 6366 | * 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] | 6367 | * of each device. If num_sectors is zero, we find the largest size |
| 6368 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6369 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6370 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6371 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6372 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6373 | if (mddev->ro) |
| 6374 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6375 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6376 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6377 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6378 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6379 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6380 | num_sectors = avail; |
| 6381 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6382 | return -ENOSPC; |
| 6383 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6384 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6385 | if (!rv) |
| 6386 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6387 | return rv; |
| 6388 | } |
| 6389 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6390 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6391 | { |
| 6392 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6393 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6394 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6395 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6396 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6397 | if (mddev->ro) |
| 6398 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6399 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6400 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6401 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6402 | if (mddev->sync_thread || |
| 6403 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6404 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6405 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6406 | |
| 6407 | rdev_for_each(rdev, mddev) { |
| 6408 | if (mddev->raid_disks < raid_disks && |
| 6409 | rdev->data_offset < rdev->new_data_offset) |
| 6410 | return -EINVAL; |
| 6411 | if (mddev->raid_disks > raid_disks && |
| 6412 | rdev->data_offset > rdev->new_data_offset) |
| 6413 | return -EINVAL; |
| 6414 | } |
| 6415 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6416 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6417 | if (mddev->delta_disks < 0) |
| 6418 | mddev->reshape_backwards = 1; |
| 6419 | else if (mddev->delta_disks > 0) |
| 6420 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6421 | |
| 6422 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6423 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6424 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6425 | mddev->reshape_backwards = 0; |
| 6426 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6427 | return rv; |
| 6428 | } |
| 6429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6430 | /* |
| 6431 | * update_array_info is used to change the configuration of an |
| 6432 | * on-line array. |
| 6433 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6434 | * fields in the info are checked against the array. |
| 6435 | * Any differences that cannot be handled will cause an error. |
| 6436 | * Normally, only one change can be managed at a time. |
| 6437 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6438 | 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] | 6439 | { |
| 6440 | int rv = 0; |
| 6441 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6442 | int state = 0; |
| 6443 | |
| 6444 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6445 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6446 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6447 | |
| 6448 | if (mddev->major_version != info->major_version || |
| 6449 | mddev->minor_version != info->minor_version || |
| 6450 | /* mddev->patch_version != info->patch_version || */ |
| 6451 | mddev->ctime != info->ctime || |
| 6452 | mddev->level != info->level || |
| 6453 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 6454 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6455 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6456 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6457 | ((state^info->state) & 0xfffffe00) |
| 6458 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6459 | return -EINVAL; |
| 6460 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6461 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6462 | cnt++; |
| 6463 | if (mddev->raid_disks != info->raid_disks) |
| 6464 | cnt++; |
| 6465 | if (mddev->layout != info->layout) |
| 6466 | cnt++; |
| 6467 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6468 | cnt++; |
| 6469 | if (cnt == 0) |
| 6470 | return 0; |
| 6471 | if (cnt > 1) |
| 6472 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6473 | |
| 6474 | if (mddev->layout != info->layout) { |
| 6475 | /* Change layout |
| 6476 | * we don't need to do anything at the md level, the |
| 6477 | * personality will take care of it all. |
| 6478 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6479 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6480 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6481 | else { |
| 6482 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6483 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6484 | if (rv) |
| 6485 | mddev->new_layout = mddev->layout; |
| 6486 | return rv; |
| 6487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6488 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6489 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6490 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6491 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6492 | if (mddev->raid_disks != info->raid_disks) |
| 6493 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6494 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6495 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6496 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 6497 | rv = -EINVAL; |
| 6498 | goto err; |
| 6499 | } |
| 6500 | if (mddev->recovery || mddev->sync_thread) { |
| 6501 | rv = -EBUSY; |
| 6502 | goto err; |
| 6503 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6504 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6505 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6506 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6507 | if (mddev->bitmap) { |
| 6508 | rv = -EEXIST; |
| 6509 | goto err; |
| 6510 | } |
| 6511 | if (mddev->bitmap_info.default_offset == 0) { |
| 6512 | rv = -EINVAL; |
| 6513 | goto err; |
| 6514 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6515 | mddev->bitmap_info.offset = |
| 6516 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6517 | mddev->bitmap_info.space = |
| 6518 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6519 | mddev->pers->quiesce(mddev, 1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6520 | bitmap = bitmap_create(mddev, -1); |
| 6521 | if (!IS_ERR(bitmap)) { |
| 6522 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6523 | rv = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6524 | } else |
| 6525 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6526 | if (rv) |
| 6527 | bitmap_destroy(mddev); |
| 6528 | mddev->pers->quiesce(mddev, 0); |
| 6529 | } else { |
| 6530 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6531 | if (!mddev->bitmap) { |
| 6532 | rv = -ENOENT; |
| 6533 | goto err; |
| 6534 | } |
| 6535 | if (mddev->bitmap->storage.file) { |
| 6536 | rv = -EINVAL; |
| 6537 | goto err; |
| 6538 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6539 | mddev->pers->quiesce(mddev, 1); |
| 6540 | bitmap_destroy(mddev); |
| 6541 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6542 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6543 | } |
| 6544 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6545 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6546 | return rv; |
| 6547 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6548 | return rv; |
| 6549 | } |
| 6550 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6551 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6552 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6553 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6554 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6555 | |
| 6556 | if (mddev->pers == NULL) |
| 6557 | return -ENODEV; |
| 6558 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6559 | rcu_read_lock(); |
| 6560 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6562 | err = -ENODEV; |
| 6563 | else { |
| 6564 | md_error(mddev, rdev); |
| 6565 | if (!test_bit(Faulty, &rdev->flags)) |
| 6566 | err = -EBUSY; |
| 6567 | } |
| 6568 | rcu_read_unlock(); |
| 6569 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6570 | } |
| 6571 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6572 | /* |
| 6573 | * We have a problem here : there is no easy way to give a CHS |
| 6574 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6575 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6576 | * dosfs just mad... ;-) |
| 6577 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6578 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6579 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6580 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6581 | |
| 6582 | geo->heads = 2; |
| 6583 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6584 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6585 | return 0; |
| 6586 | } |
| 6587 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6588 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 6589 | { |
| 6590 | switch (cmd) { |
| 6591 | case ADD_NEW_DISK: |
| 6592 | case BLKROSET: |
| 6593 | case GET_ARRAY_INFO: |
| 6594 | case GET_BITMAP_FILE: |
| 6595 | case GET_DISK_INFO: |
| 6596 | case HOT_ADD_DISK: |
| 6597 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6598 | case RAID_AUTORUN: |
| 6599 | case RAID_VERSION: |
| 6600 | case RESTART_ARRAY_RW: |
| 6601 | case RUN_ARRAY: |
| 6602 | case SET_ARRAY_INFO: |
| 6603 | case SET_BITMAP_FILE: |
| 6604 | case SET_DISK_FAULTY: |
| 6605 | case STOP_ARRAY: |
| 6606 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6607 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6608 | return true; |
| 6609 | default: |
| 6610 | return false; |
| 6611 | } |
| 6612 | } |
| 6613 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6614 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6615 | unsigned int cmd, unsigned long arg) |
| 6616 | { |
| 6617 | int err = 0; |
| 6618 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6619 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6620 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6621 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6622 | if (!md_ioctl_valid(cmd)) |
| 6623 | return -ENOTTY; |
| 6624 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6625 | switch (cmd) { |
| 6626 | case RAID_VERSION: |
| 6627 | case GET_ARRAY_INFO: |
| 6628 | case GET_DISK_INFO: |
| 6629 | break; |
| 6630 | default: |
| 6631 | if (!capable(CAP_SYS_ADMIN)) |
| 6632 | return -EACCES; |
| 6633 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6634 | |
| 6635 | /* |
| 6636 | * Commands dealing with the RAID driver but not any |
| 6637 | * particular array: |
| 6638 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6639 | switch (cmd) { |
| 6640 | case RAID_VERSION: |
| 6641 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6642 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6644 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6645 | case RAID_AUTORUN: |
| 6646 | err = 0; |
| 6647 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6648 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6649 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6650 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6651 | } |
| 6652 | |
| 6653 | /* |
| 6654 | * Commands creating/starting a new array: |
| 6655 | */ |
| 6656 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6657 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6658 | |
| 6659 | if (!mddev) { |
| 6660 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6661 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | } |
| 6663 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6664 | /* Some actions do not requires the mutex */ |
| 6665 | switch (cmd) { |
| 6666 | case GET_ARRAY_INFO: |
| 6667 | if (!mddev->raid_disks && !mddev->external) |
| 6668 | err = -ENODEV; |
| 6669 | else |
| 6670 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6671 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6672 | |
| 6673 | case GET_DISK_INFO: |
| 6674 | if (!mddev->raid_disks && !mddev->external) |
| 6675 | err = -ENODEV; |
| 6676 | else |
| 6677 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6678 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6679 | |
| 6680 | case SET_DISK_FAULTY: |
| 6681 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6682 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6683 | |
| 6684 | case GET_BITMAP_FILE: |
| 6685 | err = get_bitmap_file(mddev, argp); |
| 6686 | goto out; |
| 6687 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6688 | } |
| 6689 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 6690 | if (cmd == ADD_NEW_DISK) |
| 6691 | /* need to ensure md_delayed_delete() has completed */ |
| 6692 | flush_workqueue(md_misc_wq); |
| 6693 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 6694 | if (cmd == HOT_REMOVE_DISK) |
| 6695 | /* need to ensure recovery thread has run */ |
| 6696 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 6697 | !test_bit(MD_RECOVERY_NEEDED, |
| 6698 | &mddev->flags), |
| 6699 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6700 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 6701 | /* Need to flush page cache, and ensure no-one else opens |
| 6702 | * and writes |
| 6703 | */ |
| 6704 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6705 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6706 | mutex_unlock(&mddev->open_mutex); |
| 6707 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6708 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6709 | } |
| 6710 | set_bit(MD_STILL_CLOSED, &mddev->flags); |
| 6711 | mutex_unlock(&mddev->open_mutex); |
| 6712 | sync_blockdev(bdev); |
| 6713 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6714 | err = mddev_lock(mddev); |
| 6715 | if (err) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6716 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6717 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6718 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6719 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6720 | } |
| 6721 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6722 | if (cmd == SET_ARRAY_INFO) { |
| 6723 | mdu_array_info_t info; |
| 6724 | if (!arg) |
| 6725 | memset(&info, 0, sizeof(info)); |
| 6726 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6727 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6728 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6729 | } |
| 6730 | if (mddev->pers) { |
| 6731 | err = update_array_info(mddev, &info); |
| 6732 | if (err) { |
| 6733 | printk(KERN_WARNING "md: couldn't update" |
| 6734 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6735 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6736 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6737 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6738 | } |
| 6739 | if (!list_empty(&mddev->disks)) { |
| 6740 | printk(KERN_WARNING |
| 6741 | "md: array %s already has disks!\n", |
| 6742 | mdname(mddev)); |
| 6743 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6744 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6745 | } |
| 6746 | if (mddev->raid_disks) { |
| 6747 | printk(KERN_WARNING |
| 6748 | "md: array %s already initialised!\n", |
| 6749 | mdname(mddev)); |
| 6750 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6751 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6752 | } |
| 6753 | err = set_array_info(mddev, &info); |
| 6754 | if (err) { |
| 6755 | printk(KERN_WARNING "md: couldn't set" |
| 6756 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6757 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6758 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6759 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6760 | } |
| 6761 | |
| 6762 | /* |
| 6763 | * Commands querying/configuring an existing array: |
| 6764 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6765 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6766 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6767 | if ((!mddev->raid_disks && !mddev->external) |
| 6768 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6769 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6770 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6771 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6772 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | } |
| 6774 | |
| 6775 | /* |
| 6776 | * Commands even a read-only array can execute: |
| 6777 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6778 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6779 | case RESTART_ARRAY_RW: |
| 6780 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6781 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6782 | |
| 6783 | case STOP_ARRAY: |
| 6784 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6785 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6786 | |
| 6787 | case STOP_ARRAY_RO: |
| 6788 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6789 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6790 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6791 | case HOT_REMOVE_DISK: |
| 6792 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6793 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6794 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6795 | case ADD_NEW_DISK: |
| 6796 | /* We can support ADD_NEW_DISK on read-only arrays |
| 6797 | * on if we are re-adding a preexisting device. |
| 6798 | * So require mddev->pers and MD_DISK_SYNC. |
| 6799 | */ |
| 6800 | if (mddev->pers) { |
| 6801 | mdu_disk_info_t info; |
| 6802 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6803 | err = -EFAULT; |
| 6804 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 6805 | /* Need to clear read-only for this */ |
| 6806 | break; |
| 6807 | else |
| 6808 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6809 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6810 | } |
| 6811 | break; |
| 6812 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6813 | case BLKROSET: |
| 6814 | if (get_user(ro, (int __user *)(arg))) { |
| 6815 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6816 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6817 | } |
| 6818 | err = -EINVAL; |
| 6819 | |
| 6820 | /* if the bdev is going readonly the value of mddev->ro |
| 6821 | * does not matter, no writes are coming |
| 6822 | */ |
| 6823 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6824 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6825 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6826 | /* are we are already prepared for writes? */ |
| 6827 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6828 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6829 | |
| 6830 | /* transitioning to readauto need only happen for |
| 6831 | * arrays that call md_write_start |
| 6832 | */ |
| 6833 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6834 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6835 | if (err == 0) { |
| 6836 | mddev->ro = 2; |
| 6837 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6838 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6839 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6840 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6841 | } |
| 6842 | |
| 6843 | /* |
| 6844 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6845 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6846 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 6847 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6848 | if (mddev->ro == 2) { |
| 6849 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6850 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6851 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6852 | /* mddev_unlock will wake thread */ |
| 6853 | /* If a device failed while we were read-only, we |
| 6854 | * need to make sure the metadata is updated now. |
| 6855 | */ |
| 6856 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
| 6857 | mddev_unlock(mddev); |
| 6858 | wait_event(mddev->sb_wait, |
| 6859 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && |
| 6860 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6861 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6862 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6863 | } else { |
| 6864 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6865 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6866 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6867 | } |
| 6868 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6869 | switch (cmd) { |
| 6870 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6871 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6872 | mdu_disk_info_t info; |
| 6873 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6874 | err = -EFAULT; |
| 6875 | else |
| 6876 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6877 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6878 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6879 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6880 | case CLUSTERED_DISK_NACK: |
| 6881 | if (mddev_is_clustered(mddev)) |
| 6882 | md_cluster_ops->new_disk_ack(mddev, false); |
| 6883 | else |
| 6884 | err = -EINVAL; |
| 6885 | goto unlock; |
| 6886 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6887 | case HOT_ADD_DISK: |
| 6888 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6889 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6890 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6891 | case RUN_ARRAY: |
| 6892 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6893 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6894 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6895 | case SET_BITMAP_FILE: |
| 6896 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6897 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6898 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6899 | default: |
| 6900 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6901 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6902 | } |
| 6903 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6904 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6905 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6906 | err != -EINVAL) |
| 6907 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6908 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6909 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6910 | return err; |
| 6911 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6912 | #ifdef CONFIG_COMPAT |
| 6913 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6914 | unsigned int cmd, unsigned long arg) |
| 6915 | { |
| 6916 | switch (cmd) { |
| 6917 | case HOT_REMOVE_DISK: |
| 6918 | case HOT_ADD_DISK: |
| 6919 | case SET_DISK_FAULTY: |
| 6920 | case SET_BITMAP_FILE: |
| 6921 | /* These take in integer arg, do not convert */ |
| 6922 | break; |
| 6923 | default: |
| 6924 | arg = (unsigned long)compat_ptr(arg); |
| 6925 | break; |
| 6926 | } |
| 6927 | |
| 6928 | return md_ioctl(bdev, mode, cmd, arg); |
| 6929 | } |
| 6930 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6931 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6932 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6933 | { |
| 6934 | /* |
| 6935 | * Succeed if we can lock the mddev, which confirms that |
| 6936 | * it isn't being stopped right now. |
| 6937 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6938 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6939 | int err; |
| 6940 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 6941 | if (!mddev) |
| 6942 | return -ENODEV; |
| 6943 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6944 | if (mddev->gendisk != bdev->bd_disk) { |
| 6945 | /* we are racing with mddev_put which is discarding this |
| 6946 | * bd_disk. |
| 6947 | */ |
| 6948 | mddev_put(mddev); |
| 6949 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6950 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6951 | /* Then retry the open from the top */ |
| 6952 | return -ERESTARTSYS; |
| 6953 | } |
| 6954 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6955 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6956 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6957 | goto out; |
| 6958 | |
| 6959 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6960 | atomic_inc(&mddev->openers); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6961 | clear_bit(MD_STILL_CLOSED, &mddev->flags); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6962 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6963 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6964 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6965 | out: |
| 6966 | return err; |
| 6967 | } |
| 6968 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 6969 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6971 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6972 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6973 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6974 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6975 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6976 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6977 | |
| 6978 | static int md_media_changed(struct gendisk *disk) |
| 6979 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6980 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6981 | |
| 6982 | return mddev->changed; |
| 6983 | } |
| 6984 | |
| 6985 | static int md_revalidate(struct gendisk *disk) |
| 6986 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6987 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6988 | |
| 6989 | mddev->changed = 0; |
| 6990 | return 0; |
| 6991 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6992 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6993 | { |
| 6994 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6995 | .open = md_open, |
| 6996 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6997 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6998 | #ifdef CONFIG_COMPAT |
| 6999 | .compat_ioctl = md_compat_ioctl, |
| 7000 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7001 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7002 | .media_changed = md_media_changed, |
| 7003 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7004 | }; |
| 7005 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7006 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7007 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7008 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7009 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7010 | /* |
| 7011 | * md_thread is a 'system-thread', it's priority should be very |
| 7012 | * high. We avoid resource deadlocks individually in each |
| 7013 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 7014 | * the very same RT priority as kswapd, thus we will never get |
| 7015 | * into a priority inversion deadlock. |
| 7016 | * |
| 7017 | * we definitely have to have equal or higher priority than |
| 7018 | * bdflush, otherwise bdflush will deadlock if there are too |
| 7019 | * many dirty RAID5 blocks. |
| 7020 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7021 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7022 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7023 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7024 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7025 | /* We need to wait INTERRUPTIBLE so that |
| 7026 | * we don't add to the load-average. |
| 7027 | * That means we need to be sure no signals are |
| 7028 | * pending |
| 7029 | */ |
| 7030 | if (signal_pending(current)) |
| 7031 | flush_signals(current); |
| 7032 | |
| 7033 | wait_event_interruptible_timeout |
| 7034 | (thread->wqueue, |
| 7035 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 7036 | || kthread_should_stop(), |
| 7037 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7038 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7039 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 7040 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7041 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7042 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7044 | return 0; |
| 7045 | } |
| 7046 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7047 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7048 | { |
| 7049 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7050 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7051 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 7052 | wake_up(&thread->wqueue); |
| 7053 | } |
| 7054 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7055 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7056 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7057 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7058 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7059 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7060 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7061 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7062 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7063 | if (!thread) |
| 7064 | return NULL; |
| 7065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7066 | init_waitqueue_head(&thread->wqueue); |
| 7067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7068 | thread->run = run; |
| 7069 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7070 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7071 | thread->tsk = kthread_run(md_thread, thread, |
| 7072 | "%s_%s", |
| 7073 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7074 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7075 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7076 | kfree(thread); |
| 7077 | return NULL; |
| 7078 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7079 | return thread; |
| 7080 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7081 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7082 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7083 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7084 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7085 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7086 | if (!thread) |
| 7087 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7088 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7089 | /* Locking ensures that mddev_unlock does not wake_up a |
| 7090 | * non-existent thread |
| 7091 | */ |
| 7092 | spin_lock(&pers_lock); |
| 7093 | *threadp = NULL; |
| 7094 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7095 | |
| 7096 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7097 | kfree(thread); |
| 7098 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7099 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7100 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7101 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7102 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7103 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7104 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7105 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7106 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7107 | return; |
| 7108 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7109 | if (mddev->degraded) |
| 7110 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7111 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7112 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7113 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7114 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 7115 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7116 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 7117 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7118 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7119 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7120 | |
| 7121 | /* seq_file implementation /proc/mdstat */ |
| 7122 | |
| 7123 | static void status_unused(struct seq_file *seq) |
| 7124 | { |
| 7125 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7126 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | |
| 7128 | seq_printf(seq, "unused devices: "); |
| 7129 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7130 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7131 | char b[BDEVNAME_SIZE]; |
| 7132 | i++; |
| 7133 | seq_printf(seq, "%s ", |
| 7134 | bdevname(rdev->bdev,b)); |
| 7135 | } |
| 7136 | if (!i) |
| 7137 | seq_printf(seq, "<none>"); |
| 7138 | |
| 7139 | seq_printf(seq, "\n"); |
| 7140 | } |
| 7141 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7142 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7143 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7144 | sector_t max_sectors, resync, res; |
| 7145 | unsigned long dt, db; |
| 7146 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7147 | int scale; |
| 7148 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7149 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7150 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 7151 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7152 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7153 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7154 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7155 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7156 | resync = mddev->curr_resync; |
| 7157 | if (resync <= 3) { |
| 7158 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7159 | /* Still cleaning up */ |
| 7160 | resync = max_sectors; |
| 7161 | } else |
| 7162 | resync -= atomic_read(&mddev->recovery_active); |
| 7163 | |
| 7164 | if (resync == 0) { |
| 7165 | if (mddev->recovery_cp < MaxSector) { |
| 7166 | seq_printf(seq, "\tresync=PENDING"); |
| 7167 | return 1; |
| 7168 | } |
| 7169 | return 0; |
| 7170 | } |
| 7171 | if (resync < 3) { |
| 7172 | seq_printf(seq, "\tresync=DELAYED"); |
| 7173 | return 1; |
| 7174 | } |
| 7175 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 7176 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7177 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7178 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7179 | * u32, as those are the requirements for sector_div. |
| 7180 | * Thus 'scale' must be at least 10 |
| 7181 | */ |
| 7182 | scale = 10; |
| 7183 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7184 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7185 | scale++; |
| 7186 | } |
| 7187 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7188 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7189 | |
| 7190 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7191 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7192 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7193 | seq_printf(seq, "["); |
| 7194 | for (i = 0; i < x; i++) |
| 7195 | seq_printf(seq, "="); |
| 7196 | seq_printf(seq, ">"); |
| 7197 | for (i = 0; i < y; i++) |
| 7198 | seq_printf(seq, "."); |
| 7199 | seq_printf(seq, "] "); |
| 7200 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7201 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7202 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 7203 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7204 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 7205 | "check" : |
| 7206 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 7207 | "resync" : "recovery"))), |
| 7208 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7209 | (unsigned long long) resync/2, |
| 7210 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7211 | |
| 7212 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7213 | * dt: time from mark until now |
| 7214 | * db: blocks written from mark until now |
| 7215 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7216 | * |
| 7217 | * rt is a sector_t, so could be 32bit or 64bit. |
| 7218 | * So we divide before multiply in case it is 32bit and close |
| 7219 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 7220 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7221 | * near the end of resync when the number of remaining sectors |
| 7222 | * is close to 'db'. |
| 7223 | * We then divide rt by 32 after multiplying by db to compensate. |
| 7224 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7225 | */ |
| 7226 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 7227 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7228 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 7229 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7230 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7231 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 7232 | sector_div(rt, db/32+1); |
| 7233 | rt *= dt; |
| 7234 | rt >>= 5; |
| 7235 | |
| 7236 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 7237 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7238 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7239 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7240 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7241 | } |
| 7242 | |
| 7243 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 7244 | { |
| 7245 | struct list_head *tmp; |
| 7246 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7247 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7248 | |
| 7249 | if (l >= 0x10000) |
| 7250 | return NULL; |
| 7251 | if (!l--) |
| 7252 | /* header */ |
| 7253 | return (void*)1; |
| 7254 | |
| 7255 | spin_lock(&all_mddevs_lock); |
| 7256 | list_for_each(tmp,&all_mddevs) |
| 7257 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7258 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7259 | mddev_get(mddev); |
| 7260 | spin_unlock(&all_mddevs_lock); |
| 7261 | return mddev; |
| 7262 | } |
| 7263 | spin_unlock(&all_mddevs_lock); |
| 7264 | if (!l--) |
| 7265 | return (void*)2;/* tail */ |
| 7266 | return NULL; |
| 7267 | } |
| 7268 | |
| 7269 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 7270 | { |
| 7271 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7272 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7274 | ++*pos; |
| 7275 | if (v == (void*)2) |
| 7276 | return NULL; |
| 7277 | |
| 7278 | spin_lock(&all_mddevs_lock); |
| 7279 | if (v == (void*)1) |
| 7280 | tmp = all_mddevs.next; |
| 7281 | else |
| 7282 | tmp = mddev->all_mddevs.next; |
| 7283 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7284 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7285 | else { |
| 7286 | next_mddev = (void*)2; |
| 7287 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7289 | spin_unlock(&all_mddevs_lock); |
| 7290 | |
| 7291 | if (v != (void*)1) |
| 7292 | mddev_put(mddev); |
| 7293 | return next_mddev; |
| 7294 | |
| 7295 | } |
| 7296 | |
| 7297 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 7298 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7299 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7300 | |
| 7301 | if (mddev && v != (void*)1 && v != (void*)2) |
| 7302 | mddev_put(mddev); |
| 7303 | } |
| 7304 | |
| 7305 | static int md_seq_show(struct seq_file *seq, void *v) |
| 7306 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7307 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7308 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7309 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7310 | |
| 7311 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7312 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7313 | seq_printf(seq, "Personalities : "); |
| 7314 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7315 | list_for_each_entry(pers, &pers_list, list) |
| 7316 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7317 | |
| 7318 | spin_unlock(&pers_lock); |
| 7319 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7320 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7321 | return 0; |
| 7322 | } |
| 7323 | if (v == (void*)2) { |
| 7324 | status_unused(seq); |
| 7325 | return 0; |
| 7326 | } |
| 7327 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7328 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7329 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7330 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7331 | mddev->pers ? "" : "in"); |
| 7332 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7333 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7334 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7335 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7336 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7337 | seq_printf(seq, " %s", mddev->pers->name); |
| 7338 | } |
| 7339 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7340 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7341 | rcu_read_lock(); |
| 7342 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7343 | char b[BDEVNAME_SIZE]; |
| 7344 | seq_printf(seq, " %s[%d]", |
| 7345 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7346 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7347 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7348 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7349 | seq_printf(seq, "(F)"); |
| 7350 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7351 | } |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 7352 | if (test_bit(Journal, &rdev->flags)) { |
| 7353 | seq_printf(seq, "(J)"); |
| 7354 | continue; |
| 7355 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7356 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7357 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7358 | if (test_bit(Replacement, &rdev->flags)) |
| 7359 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7360 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7361 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7362 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7363 | |
| 7364 | if (!list_empty(&mddev->disks)) { |
| 7365 | if (mddev->pers) |
| 7366 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7367 | (unsigned long long) |
| 7368 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7369 | else |
| 7370 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7371 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7372 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7373 | if (mddev->persistent) { |
| 7374 | if (mddev->major_version != 0 || |
| 7375 | mddev->minor_version != 90) { |
| 7376 | seq_printf(seq," super %d.%d", |
| 7377 | mddev->major_version, |
| 7378 | mddev->minor_version); |
| 7379 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7380 | } else if (mddev->external) |
| 7381 | seq_printf(seq, " super external:%s", |
| 7382 | mddev->metadata_type); |
| 7383 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7384 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7385 | |
| 7386 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7387 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7388 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7389 | if (mddev->pers->sync_request) { |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7390 | if (status_resync(seq, mddev)) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7391 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7392 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7393 | } else |
| 7394 | seq_printf(seq, "\n "); |
| 7395 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7396 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7397 | |
| 7398 | seq_printf(seq, "\n"); |
| 7399 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7400 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7402 | return 0; |
| 7403 | } |
| 7404 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7405 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7406 | .start = md_seq_start, |
| 7407 | .next = md_seq_next, |
| 7408 | .stop = md_seq_stop, |
| 7409 | .show = md_seq_show, |
| 7410 | }; |
| 7411 | |
| 7412 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7413 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7414 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7415 | int error; |
| 7416 | |
| 7417 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7418 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7419 | return error; |
| 7420 | |
| 7421 | seq = file->private_data; |
| 7422 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7423 | return error; |
| 7424 | } |
| 7425 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7426 | static int md_unloading; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7427 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7428 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7429 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7430 | int mask; |
| 7431 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7432 | if (md_unloading) |
kbuild test robot | 7d7e64f | 2014-12-03 16:07:59 +1100 | [diff] [blame] | 7433 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7434 | poll_wait(filp, &md_event_waiters, wait); |
| 7435 | |
| 7436 | /* always allow read */ |
| 7437 | mask = POLLIN | POLLRDNORM; |
| 7438 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7439 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7440 | mask |= POLLERR | POLLPRI; |
| 7441 | return mask; |
| 7442 | } |
| 7443 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7444 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7445 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7446 | .open = md_seq_open, |
| 7447 | .read = seq_read, |
| 7448 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7449 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7450 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7451 | }; |
| 7452 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7453 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7454 | { |
Chao Yu | 50bd377 | 2014-09-25 15:28:34 +0800 | [diff] [blame] | 7455 | printk(KERN_INFO "md: %s personality registered for level %d\n", |
| 7456 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7457 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7458 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7459 | spin_unlock(&pers_lock); |
| 7460 | return 0; |
| 7461 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7462 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7463 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7464 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7465 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7466 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7467 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7468 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7469 | spin_unlock(&pers_lock); |
| 7470 | return 0; |
| 7471 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7472 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7473 | |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7474 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
| 7475 | struct module *module) |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7476 | { |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7477 | int ret = 0; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7478 | spin_lock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7479 | if (md_cluster_ops != NULL) |
| 7480 | ret = -EALREADY; |
| 7481 | else { |
| 7482 | md_cluster_ops = ops; |
| 7483 | md_cluster_mod = module; |
| 7484 | } |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7485 | spin_unlock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7486 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7487 | } |
| 7488 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 7489 | |
| 7490 | int unregister_md_cluster_operations(void) |
| 7491 | { |
| 7492 | spin_lock(&pers_lock); |
| 7493 | md_cluster_ops = NULL; |
| 7494 | spin_unlock(&pers_lock); |
| 7495 | return 0; |
| 7496 | } |
| 7497 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 7498 | |
| 7499 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 7500 | { |
| 7501 | int err; |
| 7502 | |
| 7503 | err = request_module("md-cluster"); |
| 7504 | if (err) { |
| 7505 | pr_err("md-cluster module not found.\n"); |
Goldwyn Rodrigues | b0c26a7 | 2015-07-22 12:09:15 -0500 | [diff] [blame] | 7506 | return -ENOENT; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7507 | } |
| 7508 | |
| 7509 | spin_lock(&pers_lock); |
| 7510 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
| 7511 | spin_unlock(&pers_lock); |
| 7512 | return -ENOENT; |
| 7513 | } |
| 7514 | spin_unlock(&pers_lock); |
| 7515 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 7516 | return md_cluster_ops->join(mddev, nodes); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7517 | } |
| 7518 | |
| 7519 | void md_cluster_stop(struct mddev *mddev) |
| 7520 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 7521 | if (!md_cluster_ops) |
| 7522 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7523 | md_cluster_ops->leave(mddev); |
| 7524 | module_put(md_cluster_mod); |
| 7525 | } |
| 7526 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7527 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7528 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7529 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7530 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7531 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7532 | |
| 7533 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7534 | rcu_read_lock(); |
| 7535 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7536 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7537 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7538 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7539 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7540 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7541 | * as sync_io is counted when a request starts, and |
| 7542 | * disk_stats is counted when it completes. |
| 7543 | * So resync activity will cause curr_events to be smaller than |
| 7544 | * when there was no such activity. |
| 7545 | * non-sync IO will cause disk_stat to increase without |
| 7546 | * increasing sync_io so curr_events will (eventually) |
| 7547 | * be larger than it was before. Once it becomes |
| 7548 | * substantially larger, the test below will cause |
| 7549 | * the array to appear non-idle, and resync will slow |
| 7550 | * down. |
| 7551 | * If there is a lot of outstanding resync activity when |
| 7552 | * we set last_event to curr_events, then all that activity |
| 7553 | * completing might cause the array to appear non-idle |
| 7554 | * and resync will be slowed down even though there might |
| 7555 | * not have been non-resync activity. This will only |
| 7556 | * happen once though. 'last_events' will soon reflect |
| 7557 | * the state where there is little or no outstanding |
| 7558 | * resync requests, and further resync activity will |
| 7559 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7560 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7561 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7562 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7563 | rdev->last_events = curr_events; |
| 7564 | idle = 0; |
| 7565 | } |
| 7566 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7567 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7568 | return idle; |
| 7569 | } |
| 7570 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7571 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7572 | { |
| 7573 | /* another "blocks" (512byte) blocks have been synced */ |
| 7574 | atomic_sub(blocks, &mddev->recovery_active); |
| 7575 | wake_up(&mddev->recovery_wait); |
| 7576 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7577 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7578 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7579 | md_wakeup_thread(mddev->thread); |
| 7580 | // stop recovery, signal do_sync .... |
| 7581 | } |
| 7582 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7583 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7584 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7585 | /* md_write_start(mddev, bi) |
| 7586 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7587 | * in superblock) before writing, schedule a superblock update |
| 7588 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7589 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7590 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7591 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7592 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7593 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7594 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7595 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7596 | BUG_ON(mddev->ro == 1); |
| 7597 | if (mddev->ro == 2) { |
| 7598 | /* need to switch to read/write */ |
| 7599 | mddev->ro = 0; |
| 7600 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7601 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7602 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7603 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7604 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7605 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7606 | if (mddev->safemode == 1) |
| 7607 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7608 | if (mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7609 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7610 | if (mddev->in_sync) { |
| 7611 | mddev->in_sync = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7612 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7613 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7614 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7615 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7616 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7617 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7618 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7619 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7620 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7621 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7622 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7623 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7624 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7625 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7626 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7627 | { |
| 7628 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7629 | if (mddev->safemode == 2) |
| 7630 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7631 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7632 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7633 | } |
| 7634 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7635 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7636 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7637 | /* md_allow_write(mddev) |
| 7638 | * Calling this ensures that the array is marked 'active' so that writes |
| 7639 | * may proceed without blocking. It is important to call this before |
| 7640 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7641 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7642 | * |
| 7643 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7644 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7645 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7646 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7647 | { |
| 7648 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7649 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7650 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7651 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7652 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7653 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7654 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7655 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7656 | if (mddev->in_sync) { |
| 7657 | mddev->in_sync = 0; |
| 7658 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7659 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7660 | if (mddev->safemode_delay && |
| 7661 | mddev->safemode == 0) |
| 7662 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7663 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7664 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7665 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7666 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7667 | spin_unlock(&mddev->lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7668 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7669 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7670 | return -EAGAIN; |
| 7671 | else |
| 7672 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7673 | } |
| 7674 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7675 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7676 | #define SYNC_MARKS 10 |
| 7677 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7678 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7679 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7680 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7681 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7682 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7683 | unsigned int currspeed = 0, |
| 7684 | window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7685 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7686 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7687 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7688 | sector_t mark_cnt[SYNC_MARKS]; |
| 7689 | int last_mark,m; |
| 7690 | struct list_head *tmp; |
| 7691 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7692 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7693 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7694 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7695 | struct blk_plug plug; |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 7696 | bool cluster_resync_finished = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7697 | |
| 7698 | /* just incase thread restarts... */ |
| 7699 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7700 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7701 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 7702 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7703 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7704 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7705 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7706 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7707 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7708 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7709 | action = "check"; |
| 7710 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7711 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7712 | action = "repair"; |
| 7713 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7714 | desc = "resync"; |
| 7715 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7716 | desc = "reshape"; |
| 7717 | else |
| 7718 | desc = "recovery"; |
| 7719 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7720 | mddev->last_sync_action = action ?: desc; |
| 7721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7722 | /* we overload curr_resync somewhat here. |
| 7723 | * 0 == not engaged in resync at all |
| 7724 | * 2 == checking that there is no conflict with another sync |
| 7725 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7726 | * commense |
| 7727 | * other == active in resync - this many blocks |
| 7728 | * |
| 7729 | * Before starting a resync we must have set curr_resync to |
| 7730 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7731 | * less than ours. When we find one that is the same or higher |
| 7732 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7733 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7734 | * This will mean we have to start checking from the beginning again. |
| 7735 | * |
| 7736 | */ |
| 7737 | |
| 7738 | do { |
| 7739 | mddev->curr_resync = 2; |
| 7740 | |
| 7741 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7742 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7743 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7744 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7745 | if (mddev2 == mddev) |
| 7746 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7747 | if (!mddev->parallel_resync |
| 7748 | && mddev2->curr_resync |
| 7749 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7750 | DEFINE_WAIT(wq); |
| 7751 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7752 | /* arbitrarily yield */ |
| 7753 | mddev->curr_resync = 1; |
| 7754 | wake_up(&resync_wait); |
| 7755 | } |
| 7756 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7757 | /* no need to wait here, we can wait the next |
| 7758 | * time 'round when curr_resync == 2 |
| 7759 | */ |
| 7760 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7761 | /* We need to wait 'interruptible' so as not to |
| 7762 | * contribute to the load average, and not to |
| 7763 | * be caught by 'softlockup' |
| 7764 | */ |
| 7765 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7766 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7767 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7768 | printk(KERN_INFO "md: delaying %s of %s" |
| 7769 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7770 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7771 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7772 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7773 | if (signal_pending(current)) |
| 7774 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7775 | schedule(); |
| 7776 | finish_wait(&resync_wait, &wq); |
| 7777 | goto try_again; |
| 7778 | } |
| 7779 | finish_wait(&resync_wait, &wq); |
| 7780 | } |
| 7781 | } |
| 7782 | } while (mddev->curr_resync < 2); |
| 7783 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7784 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7785 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7786 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7787 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7788 | */ |
| 7789 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 7790 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7791 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7792 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7793 | j = mddev->resync_min; |
| 7794 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7795 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7796 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7797 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7798 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7799 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7800 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7801 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7802 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7803 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7804 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7805 | if (rdev->raid_disk >= 0 && |
| 7806 | !test_bit(Faulty, &rdev->flags) && |
| 7807 | !test_bit(In_sync, &rdev->flags) && |
| 7808 | rdev->recovery_offset < j) |
| 7809 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7810 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 7811 | |
| 7812 | /* If there is a bitmap, we need to make sure all |
| 7813 | * writes that started before we added a spare |
| 7814 | * complete before we start doing a recovery. |
| 7815 | * Otherwise the write might complete and (via |
| 7816 | * bitmap_endwrite) set a bit in the bitmap after the |
| 7817 | * recovery has checked that bit and skipped that |
| 7818 | * region. |
| 7819 | */ |
| 7820 | if (mddev->bitmap) { |
| 7821 | mddev->pers->quiesce(mddev, 1); |
| 7822 | mddev->pers->quiesce(mddev, 0); |
| 7823 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7824 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7825 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7826 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7827 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7828 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7829 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7830 | "(but not more than %d KB/sec) for %s.\n", |
| 7831 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7832 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7833 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7834 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7835 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7836 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7837 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7838 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7839 | } |
| 7840 | last_mark = 0; |
| 7841 | mddev->resync_mark = mark[last_mark]; |
| 7842 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7843 | |
| 7844 | /* |
| 7845 | * Tune reconstruction: |
| 7846 | */ |
| 7847 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450c | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7848 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7849 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7850 | |
| 7851 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7852 | last_check = 0; |
| 7853 | |
| 7854 | if (j>2) { |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7855 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7856 | "md: resuming %s of %s from checkpoint.\n", |
| 7857 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7858 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7859 | } else |
| 7860 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7861 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7862 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 7863 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7864 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7865 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7866 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7867 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7868 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7869 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7870 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7871 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7872 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7873 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7874 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7875 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7876 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7877 | (j - mddev->curr_resync_completed)*2 |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 7878 | >= mddev->resync_max - mddev->curr_resync_completed || |
| 7879 | mddev->curr_resync_completed > mddev->resync_max |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7880 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7881 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7882 | wait_event(mddev->recovery_wait, |
| 7883 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7884 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7885 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 7886 | j > mddev->recovery_cp) |
| 7887 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7888 | update_time = jiffies; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7889 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7890 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7891 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7892 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7893 | while (j >= mddev->resync_max && |
| 7894 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7895 | /* As this condition is controlled by user-space, |
| 7896 | * we can block indefinitely, so use '_interruptible' |
| 7897 | * to avoid triggering warnings. |
| 7898 | */ |
| 7899 | flush_signals(current); /* just in case */ |
| 7900 | wait_event_interruptible(mddev->recovery_wait, |
| 7901 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7902 | || test_bit(MD_RECOVERY_INTR, |
| 7903 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7904 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7905 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7906 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7907 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7908 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 7909 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7910 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7911 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7912 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7913 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7914 | |
| 7915 | if (!skipped) { /* actual IO requested */ |
| 7916 | io_sectors += sectors; |
| 7917 | atomic_add(sectors, &mddev->recovery_active); |
| 7918 | } |
| 7919 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7920 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7921 | break; |
| 7922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7923 | j += sectors; |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 7924 | if (j > max_sectors) |
| 7925 | /* when skipping, extra large numbers can be returned. */ |
| 7926 | j = max_sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7927 | if (j > 2) |
| 7928 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7929 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7930 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7931 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7932 | * visible in /proc/mdstat |
| 7933 | */ |
| 7934 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7935 | |
| 7936 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7937 | continue; |
| 7938 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7939 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7940 | repeat: |
| 7941 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7942 | /* step marks */ |
| 7943 | int next = (last_mark+1) % SYNC_MARKS; |
| 7944 | |
| 7945 | mddev->resync_mark = mark[next]; |
| 7946 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7947 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7948 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7949 | last_mark = next; |
| 7950 | } |
| 7951 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7952 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7953 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7954 | |
| 7955 | /* |
| 7956 | * this loop exits only if either when we are slower than |
| 7957 | * the 'hard' speed limit, or the system was IO-idle for |
| 7958 | * a jiffy. |
| 7959 | * the system might be non-idle CPU-wise, but we only care |
| 7960 | * about not overloading the IO subsystem. (things like an |
| 7961 | * e2fsck being done on the RAID array should execute fast) |
| 7962 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7963 | cond_resched(); |
| 7964 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7965 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 7966 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7967 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7968 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7969 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 7970 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7971 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7972 | goto repeat; |
| 7973 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 7974 | if (!is_mddev_idle(mddev, 0)) { |
| 7975 | /* |
| 7976 | * Give other IO more of a chance. |
| 7977 | * The faster the devices, the less we wait. |
| 7978 | */ |
| 7979 | wait_event(mddev->recovery_wait, |
| 7980 | !atomic_read(&mddev->recovery_active)); |
| 7981 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7982 | } |
| 7983 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7984 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
| 7985 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 7986 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7987 | /* |
| 7988 | * this also signals 'finished resyncing' to md_stop |
| 7989 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7990 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7991 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7992 | |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 7993 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7994 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7995 | mddev->curr_resync > 2) { |
| 7996 | mddev->curr_resync_completed = mddev->curr_resync; |
| 7997 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 7998 | } |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 7999 | /* tell personality and other nodes that we are finished */ |
| 8000 | if (mddev_is_clustered(mddev)) { |
| 8001 | md_cluster_ops->resync_finish(mddev); |
| 8002 | cluster_resync_finished = true; |
| 8003 | } |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8004 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8005 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8006 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8007 | mddev->curr_resync > 2) { |
| 8008 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 8009 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8010 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 8011 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8012 | "md: checkpointing %s of %s.\n", |
| 8013 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8014 | if (test_bit(MD_RECOVERY_ERROR, |
| 8015 | &mddev->recovery)) |
| 8016 | mddev->recovery_cp = |
| 8017 | mddev->curr_resync_completed; |
| 8018 | else |
| 8019 | mddev->recovery_cp = |
| 8020 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8021 | } |
| 8022 | } else |
| 8023 | mddev->recovery_cp = MaxSector; |
| 8024 | } else { |
| 8025 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8026 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8027 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8028 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8029 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 8030 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8031 | !test_bit(Faulty, &rdev->flags) && |
| 8032 | !test_bit(In_sync, &rdev->flags) && |
| 8033 | rdev->recovery_offset < mddev->curr_resync) |
| 8034 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8035 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8036 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8037 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 8038 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 8039 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8040 | |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8041 | if (mddev_is_clustered(mddev) && |
| 8042 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8043 | !cluster_resync_finished) |
| 8044 | md_cluster_ops->resync_finish(mddev); |
| 8045 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8046 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 8047 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8048 | /* We completed so min/max setting can be forgotten if used. */ |
| 8049 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8050 | mddev->resync_min = 0; |
| 8051 | mddev->resync_max = MaxSector; |
| 8052 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8053 | mddev->resync_min = mddev->curr_resync_completed; |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8054 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8055 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8056 | spin_unlock(&mddev->lock); |
| 8057 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8058 | wake_up(&resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8059 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 8060 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8061 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8062 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8063 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8064 | static int remove_and_add_spares(struct mddev *mddev, |
| 8065 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8066 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8067 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8068 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8069 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8070 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8071 | rdev_for_each(rdev, mddev) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8072 | if ((this == NULL || rdev == this) && |
| 8073 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8074 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8075 | (test_bit(Faulty, &rdev->flags) || |
| 8076 | ! test_bit(In_sync, &rdev->flags)) && |
| 8077 | atomic_read(&rdev->nr_pending)==0) { |
| 8078 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8079 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 8080 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8081 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8082 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8083 | } |
| 8084 | } |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 8085 | if (removed && mddev->kobj.sd) |
| 8086 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8087 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8088 | if (this && removed) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8089 | goto no_add; |
| 8090 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8091 | rdev_for_each(rdev, mddev) { |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8092 | if (this && this != rdev) |
| 8093 | continue; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 8094 | if (test_bit(Candidate, &rdev->flags)) |
| 8095 | continue; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8096 | if (rdev->raid_disk >= 0 && |
| 8097 | !test_bit(In_sync, &rdev->flags) && |
| 8098 | !test_bit(Faulty, &rdev->flags)) |
| 8099 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8100 | if (rdev->raid_disk >= 0) |
| 8101 | continue; |
| 8102 | if (test_bit(Faulty, &rdev->flags)) |
| 8103 | continue; |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame^] | 8104 | if (test_bit(Journal, &rdev->flags)) |
| 8105 | continue; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8106 | if (mddev->ro && |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8107 | ! (rdev->saved_raid_disk >= 0 && |
| 8108 | !test_bit(Bitmap_sync, &rdev->flags))) |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8109 | continue; |
| 8110 | |
NeilBrown | 7eb4188 | 2014-01-14 15:55:14 +1100 | [diff] [blame] | 8111 | if (rdev->saved_raid_disk < 0) |
| 8112 | rdev->recovery_offset = 0; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8113 | if (mddev->pers-> |
| 8114 | hot_add_disk(mddev, rdev) == 0) { |
| 8115 | if (sysfs_link_rdev(mddev, rdev)) |
| 8116 | /* failure here is OK */; |
| 8117 | spares++; |
| 8118 | md_new_event(mddev); |
| 8119 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8120 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8121 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8122 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 8123 | if (removed) |
| 8124 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8125 | return spares; |
| 8126 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 8127 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8128 | static void md_start_sync(struct work_struct *ws) |
| 8129 | { |
| 8130 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8131 | int ret = 0; |
| 8132 | |
| 8133 | if (mddev_is_clustered(mddev)) { |
| 8134 | ret = md_cluster_ops->resync_start(mddev); |
| 8135 | if (ret) { |
| 8136 | mddev->sync_thread = NULL; |
| 8137 | goto out; |
| 8138 | } |
| 8139 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8140 | |
| 8141 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 8142 | mddev, |
| 8143 | "resync"); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8144 | out: |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8145 | if (!mddev->sync_thread) { |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8146 | if (!(mddev_is_clustered(mddev) && ret == -EAGAIN)) |
| 8147 | printk(KERN_ERR "%s: could not start resync" |
| 8148 | " thread...\n", |
| 8149 | mdname(mddev)); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8150 | /* leave the spares where they are, it shouldn't hurt */ |
| 8151 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8152 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8153 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8154 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 8155 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8156 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8157 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8158 | &mddev->recovery)) |
| 8159 | if (mddev->sysfs_action) |
| 8160 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8161 | } else |
| 8162 | md_wakeup_thread(mddev->sync_thread); |
| 8163 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8164 | md_new_event(mddev); |
| 8165 | } |
| 8166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8167 | /* |
| 8168 | * This routine is regularly called by all per-raid-array threads to |
| 8169 | * deal with generic issues like resync and super-block update. |
| 8170 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 8171 | * need this as they never do any recovery or update the superblock. |
| 8172 | * |
| 8173 | * It does not do any resync itself, but rather "forks" off other threads |
| 8174 | * to do that as needed. |
| 8175 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 8176 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8177 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8178 | * and wakeups up this thread which will reap the thread and finish up. |
| 8179 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 8180 | * |
| 8181 | * The overall approach is: |
| 8182 | * 1/ if the superblock needs updating, update it. |
| 8183 | * 2/ If a recovery thread is running, don't do anything else. |
| 8184 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 8185 | * 4/ If there are any faulty devices, remove them. |
| 8186 | * 5/ If array is degraded, try to add spares devices |
| 8187 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 8188 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8189 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8190 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 8191 | if (mddev->suspended) |
| 8192 | return; |
| 8193 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 8194 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 8195 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8196 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8197 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8198 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8199 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 8200 | mdname(mddev)); |
| 8201 | mddev->safemode = 2; |
| 8202 | } |
| 8203 | flush_signals(current); |
| 8204 | } |
| 8205 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8206 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 8207 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8208 | if ( ! ( |
NeilBrown | 142d44c | 2013-11-28 10:34:18 +1100 | [diff] [blame] | 8209 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8210 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8211 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8212 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8213 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 8214 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8215 | )) |
| 8216 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8217 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 8218 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8219 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8220 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8221 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 8222 | struct md_rdev *rdev; |
| 8223 | if (!mddev->external && mddev->in_sync) |
| 8224 | /* 'Blocked' flag not needed as failed devices |
| 8225 | * will be recorded if array switched to read/write. |
| 8226 | * Leaving it set will prevent the device |
| 8227 | * from being removed. |
| 8228 | */ |
| 8229 | rdev_for_each(rdev, mddev) |
| 8230 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8231 | /* On a read-only array we can: |
| 8232 | * - remove failed devices |
| 8233 | * - add already-in_sync devices if the array itself |
| 8234 | * is in-sync. |
| 8235 | * As we only add devices that are already in-sync, |
| 8236 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8237 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8238 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8239 | /* There is no thread, but we need to call |
| 8240 | * ->spare_active and clear saved_raid_disk |
| 8241 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 8242 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8243 | md_reap_sync_thread(mddev); |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 8244 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8245 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Shaohua Li | d4929ad | 2015-09-18 10:20:12 -0700 | [diff] [blame] | 8246 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8247 | goto unlock; |
| 8248 | } |
| 8249 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8250 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8251 | int did_change = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8252 | spin_lock(&mddev->lock); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8253 | if (mddev->safemode && |
| 8254 | !atomic_read(&mddev->writes_pending) && |
| 8255 | !mddev->in_sync && |
| 8256 | mddev->recovery_cp == MaxSector) { |
| 8257 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8258 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 8259 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8260 | } |
| 8261 | if (mddev->safemode == 1) |
| 8262 | mddev->safemode = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8263 | spin_unlock(&mddev->lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8264 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8265 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8266 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8267 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 8268 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 8269 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8271 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 8272 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 8273 | /* resync/recovery still happening */ |
| 8274 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8275 | goto unlock; |
| 8276 | } |
| 8277 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8278 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8279 | goto unlock; |
| 8280 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8281 | /* Set RUNNING before clearing NEEDED to avoid |
| 8282 | * any transients in the value of "sync_action". |
| 8283 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8284 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8285 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8286 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8287 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8288 | /* Clear some bits that don't mean anything, but |
| 8289 | * might be left set |
| 8290 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8291 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 8292 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8293 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 8294 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 8295 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8296 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8297 | /* no recovery is running. |
| 8298 | * remove any failed drives, then |
| 8299 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8300 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8301 | * the personality to fail the re-add. |
| 8302 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8303 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8304 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8305 | if (mddev->pers->check_reshape == NULL || |
| 8306 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8307 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8308 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8309 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8310 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8311 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8312 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8313 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 8314 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8315 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8316 | } else if (mddev->recovery_cp < MaxSector) { |
| 8317 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8318 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8319 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 8320 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8321 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8323 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 8324 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 8325 | /* We are adding a device or devices to an array |
| 8326 | * which has the bitmap stored on all devices. |
| 8327 | * So make sure all bitmap pages get written |
| 8328 | */ |
| 8329 | bitmap_write_all(mddev->bitmap); |
| 8330 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8331 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 8332 | queue_work(md_misc_wq, &mddev->del_work); |
| 8333 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8334 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8335 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8336 | if (!mddev->sync_thread) { |
| 8337 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8338 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8339 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8340 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 8341 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8342 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8343 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8344 | unlock: |
| 8345 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8346 | mddev_unlock(mddev); |
| 8347 | } |
| 8348 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8349 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8350 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8351 | void md_reap_sync_thread(struct mddev *mddev) |
| 8352 | { |
| 8353 | struct md_rdev *rdev; |
| 8354 | |
| 8355 | /* resync has finished, collect result */ |
| 8356 | md_unregister_thread(&mddev->sync_thread); |
| 8357 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8358 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 8359 | /* success...*/ |
| 8360 | /* activate any spares */ |
| 8361 | if (mddev->pers->spare_active(mddev)) { |
| 8362 | sysfs_notify(&mddev->kobj, NULL, |
| 8363 | "degraded"); |
| 8364 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 8365 | } |
| 8366 | } |
| 8367 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8368 | mddev->pers->finish_reshape) |
| 8369 | mddev->pers->finish_reshape(mddev); |
| 8370 | |
| 8371 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8372 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8373 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8374 | if (!mddev->degraded) |
| 8375 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8376 | rdev->saved_raid_disk = -1; |
| 8377 | |
| 8378 | md_update_sb(mddev, 1); |
| 8379 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 8380 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8381 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8382 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8383 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8384 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8385 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8386 | /* flag recovery needed just to double check */ |
| 8387 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8388 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8389 | md_new_event(mddev); |
| 8390 | if (mddev->event_work.func) |
| 8391 | queue_work(md_misc_wq, &mddev->event_work); |
| 8392 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8393 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8394 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8395 | 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] | 8396 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8397 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8398 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8399 | !test_bit(Blocked, &rdev->flags) && |
| 8400 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8401 | msecs_to_jiffies(5000)); |
| 8402 | rdev_dec_pending(rdev, mddev); |
| 8403 | } |
| 8404 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 8405 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8406 | void md_finish_reshape(struct mddev *mddev) |
| 8407 | { |
| 8408 | /* called be personality module when reshape completes. */ |
| 8409 | struct md_rdev *rdev; |
| 8410 | |
| 8411 | rdev_for_each(rdev, mddev) { |
| 8412 | if (rdev->data_offset > rdev->new_data_offset) |
| 8413 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 8414 | else |
| 8415 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 8416 | rdev->data_offset = rdev->new_data_offset; |
| 8417 | } |
| 8418 | } |
| 8419 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8420 | |
| 8421 | /* Bad block management. |
| 8422 | * We can record which blocks on each device are 'bad' and so just |
| 8423 | * fail those blocks, or that stripe, rather than the whole device. |
| 8424 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 8425 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 8426 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 8427 | * A 'shift' can be set so that larger blocks are tracked and |
| 8428 | * consequently larger devices can be covered. |
| 8429 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 8430 | * |
| 8431 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 8432 | * might need to retry if it is very unlucky. |
| 8433 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 8434 | * so we use the write_seqlock_irq variant. |
| 8435 | * |
| 8436 | * When looking for a bad block we specify a range and want to |
| 8437 | * know if any block in the range is bad. So we binary-search |
| 8438 | * to the last range that starts at-or-before the given endpoint, |
| 8439 | * (or "before the sector after the target range") |
| 8440 | * then see if it ends after the given start. |
| 8441 | * We return |
| 8442 | * 0 if there are no known bad blocks in the range |
| 8443 | * 1 if there are known bad block which are all acknowledged |
| 8444 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 8445 | * plus the start/length of the first bad section we overlap. |
| 8446 | */ |
| 8447 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 8448 | sector_t *first_bad, int *bad_sectors) |
| 8449 | { |
| 8450 | int hi; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8451 | int lo; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8452 | u64 *p = bb->page; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8453 | int rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8454 | sector_t target = s + sectors; |
| 8455 | unsigned seq; |
| 8456 | |
| 8457 | if (bb->shift > 0) { |
| 8458 | /* round the start down, and the end up */ |
| 8459 | s >>= bb->shift; |
| 8460 | target += (1<<bb->shift) - 1; |
| 8461 | target >>= bb->shift; |
| 8462 | sectors = target - s; |
| 8463 | } |
| 8464 | /* 'target' is now the first block after the bad range */ |
| 8465 | |
| 8466 | retry: |
| 8467 | seq = read_seqbegin(&bb->lock); |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8468 | lo = 0; |
| 8469 | rv = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8470 | hi = bb->count; |
| 8471 | |
| 8472 | /* Binary search between lo and hi for 'target' |
| 8473 | * i.e. for the last range that starts before 'target' |
| 8474 | */ |
| 8475 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 8476 | * are known not to be the last range before target. |
| 8477 | * VARIANT: hi-lo is the number of possible |
| 8478 | * ranges, and decreases until it reaches 1 |
| 8479 | */ |
| 8480 | while (hi - lo > 1) { |
| 8481 | int mid = (lo + hi) / 2; |
| 8482 | sector_t a = BB_OFFSET(p[mid]); |
| 8483 | if (a < target) |
| 8484 | /* This could still be the one, earlier ranges |
| 8485 | * could not. */ |
| 8486 | lo = mid; |
| 8487 | else |
| 8488 | /* This and later ranges are definitely out. */ |
| 8489 | hi = mid; |
| 8490 | } |
| 8491 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 8492 | if (hi > lo) { |
| 8493 | /* need to check all range that end after 's' to see if |
| 8494 | * any are unacknowledged. |
| 8495 | */ |
| 8496 | while (lo >= 0 && |
| 8497 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8498 | if (BB_OFFSET(p[lo]) < target) { |
| 8499 | /* starts before the end, and finishes after |
| 8500 | * the start, so they must overlap |
| 8501 | */ |
| 8502 | if (rv != -1 && BB_ACK(p[lo])) |
| 8503 | rv = 1; |
| 8504 | else |
| 8505 | rv = -1; |
| 8506 | *first_bad = BB_OFFSET(p[lo]); |
| 8507 | *bad_sectors = BB_LEN(p[lo]); |
| 8508 | } |
| 8509 | lo--; |
| 8510 | } |
| 8511 | } |
| 8512 | |
| 8513 | if (read_seqretry(&bb->lock, seq)) |
| 8514 | goto retry; |
| 8515 | |
| 8516 | return rv; |
| 8517 | } |
| 8518 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 8519 | |
| 8520 | /* |
| 8521 | * Add a range of bad blocks to the table. |
| 8522 | * This might extend the table, or might contract it |
| 8523 | * if two adjacent ranges can be merged. |
| 8524 | * We binary-search to find the 'insertion' point, then |
| 8525 | * decide how best to handle it. |
| 8526 | */ |
| 8527 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 8528 | int acknowledged) |
| 8529 | { |
| 8530 | u64 *p; |
| 8531 | int lo, hi; |
| 8532 | int rv = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8533 | unsigned long flags; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8534 | |
| 8535 | if (bb->shift < 0) |
| 8536 | /* badblocks are disabled */ |
| 8537 | return 0; |
| 8538 | |
| 8539 | if (bb->shift) { |
| 8540 | /* round the start down, and the end up */ |
| 8541 | sector_t next = s + sectors; |
| 8542 | s >>= bb->shift; |
| 8543 | next += (1<<bb->shift) - 1; |
| 8544 | next >>= bb->shift; |
| 8545 | sectors = next - s; |
| 8546 | } |
| 8547 | |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8548 | write_seqlock_irqsave(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8549 | |
| 8550 | p = bb->page; |
| 8551 | lo = 0; |
| 8552 | hi = bb->count; |
| 8553 | /* Find the last range that starts at-or-before 's' */ |
| 8554 | while (hi - lo > 1) { |
| 8555 | int mid = (lo + hi) / 2; |
| 8556 | sector_t a = BB_OFFSET(p[mid]); |
| 8557 | if (a <= s) |
| 8558 | lo = mid; |
| 8559 | else |
| 8560 | hi = mid; |
| 8561 | } |
| 8562 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 8563 | hi = lo; |
| 8564 | |
| 8565 | if (hi > lo) { |
| 8566 | /* we found a range that might merge with the start |
| 8567 | * of our new range |
| 8568 | */ |
| 8569 | sector_t a = BB_OFFSET(p[lo]); |
| 8570 | sector_t e = a + BB_LEN(p[lo]); |
| 8571 | int ack = BB_ACK(p[lo]); |
| 8572 | if (e >= s) { |
| 8573 | /* Yes, we can merge with a previous range */ |
| 8574 | if (s == a && s + sectors >= e) |
| 8575 | /* new range covers old */ |
| 8576 | ack = acknowledged; |
| 8577 | else |
| 8578 | ack = ack && acknowledged; |
| 8579 | |
| 8580 | if (e < s + sectors) |
| 8581 | e = s + sectors; |
| 8582 | if (e - a <= BB_MAX_LEN) { |
| 8583 | p[lo] = BB_MAKE(a, e-a, ack); |
| 8584 | s = e; |
| 8585 | } else { |
| 8586 | /* does not all fit in one range, |
| 8587 | * make p[lo] maximal |
| 8588 | */ |
| 8589 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8590 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8591 | s = a + BB_MAX_LEN; |
| 8592 | } |
| 8593 | sectors = e - s; |
| 8594 | } |
| 8595 | } |
| 8596 | if (sectors && hi < bb->count) { |
| 8597 | /* 'hi' points to the first range that starts after 's'. |
| 8598 | * Maybe we can merge with the start of that range */ |
| 8599 | sector_t a = BB_OFFSET(p[hi]); |
| 8600 | sector_t e = a + BB_LEN(p[hi]); |
| 8601 | int ack = BB_ACK(p[hi]); |
| 8602 | if (a <= s + sectors) { |
| 8603 | /* merging is possible */ |
| 8604 | if (e <= s + sectors) { |
| 8605 | /* full overlap */ |
| 8606 | e = s + sectors; |
| 8607 | ack = acknowledged; |
| 8608 | } else |
| 8609 | ack = ack && acknowledged; |
| 8610 | |
| 8611 | a = s; |
| 8612 | if (e - a <= BB_MAX_LEN) { |
| 8613 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8614 | s = e; |
| 8615 | } else { |
| 8616 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8617 | s = a + BB_MAX_LEN; |
| 8618 | } |
| 8619 | sectors = e - s; |
| 8620 | lo = hi; |
| 8621 | hi++; |
| 8622 | } |
| 8623 | } |
| 8624 | if (sectors == 0 && hi < bb->count) { |
| 8625 | /* we might be able to combine lo and hi */ |
| 8626 | /* Note: 's' is at the end of 'lo' */ |
| 8627 | sector_t a = BB_OFFSET(p[hi]); |
| 8628 | int lolen = BB_LEN(p[lo]); |
| 8629 | int hilen = BB_LEN(p[hi]); |
| 8630 | int newlen = lolen + hilen - (s - a); |
| 8631 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8632 | /* yes, we can combine them */ |
| 8633 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8634 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8635 | memmove(p + hi, p + hi + 1, |
| 8636 | (bb->count - hi - 1) * 8); |
| 8637 | bb->count--; |
| 8638 | } |
| 8639 | } |
| 8640 | while (sectors) { |
| 8641 | /* didn't merge (it all). |
| 8642 | * Need to add a range just before 'hi' */ |
| 8643 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8644 | /* No room for more */ |
| 8645 | rv = 0; |
| 8646 | break; |
| 8647 | } else { |
| 8648 | int this_sectors = sectors; |
| 8649 | memmove(p + hi + 1, p + hi, |
| 8650 | (bb->count - hi) * 8); |
| 8651 | bb->count++; |
| 8652 | |
| 8653 | if (this_sectors > BB_MAX_LEN) |
| 8654 | this_sectors = BB_MAX_LEN; |
| 8655 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8656 | sectors -= this_sectors; |
| 8657 | s += this_sectors; |
| 8658 | } |
| 8659 | } |
| 8660 | |
| 8661 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8662 | if (!acknowledged) |
| 8663 | bb->unacked_exist = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8664 | write_sequnlock_irqrestore(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8665 | |
| 8666 | return rv; |
| 8667 | } |
| 8668 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8669 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8670 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8671 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8672 | int rv; |
| 8673 | if (is_new) |
| 8674 | s += rdev->new_data_offset; |
| 8675 | else |
| 8676 | s += rdev->data_offset; |
| 8677 | rv = md_set_badblocks(&rdev->badblocks, |
| 8678 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8679 | if (rv) { |
| 8680 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8681 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8682 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
NeilBrown | 55ce74d | 2015-08-14 11:11:10 +1000 | [diff] [blame] | 8683 | set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8684 | md_wakeup_thread(rdev->mddev->thread); |
| 8685 | } |
| 8686 | return rv; |
| 8687 | } |
| 8688 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8689 | |
| 8690 | /* |
| 8691 | * Remove a range of bad blocks from the table. |
| 8692 | * This may involve extending the table if we spilt a region, |
| 8693 | * but it must not fail. So if the table becomes full, we just |
| 8694 | * drop the remove request. |
| 8695 | */ |
| 8696 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8697 | { |
| 8698 | u64 *p; |
| 8699 | int lo, hi; |
| 8700 | sector_t target = s + sectors; |
| 8701 | int rv = 0; |
| 8702 | |
| 8703 | if (bb->shift > 0) { |
| 8704 | /* When clearing we round the start up and the end down. |
| 8705 | * This should not matter as the shift should align with |
| 8706 | * the block size and no rounding should ever be needed. |
| 8707 | * However it is better the think a block is bad when it |
| 8708 | * isn't than to think a block is not bad when it is. |
| 8709 | */ |
| 8710 | s += (1<<bb->shift) - 1; |
| 8711 | s >>= bb->shift; |
| 8712 | target >>= bb->shift; |
| 8713 | sectors = target - s; |
| 8714 | } |
| 8715 | |
| 8716 | write_seqlock_irq(&bb->lock); |
| 8717 | |
| 8718 | p = bb->page; |
| 8719 | lo = 0; |
| 8720 | hi = bb->count; |
| 8721 | /* Find the last range that starts before 'target' */ |
| 8722 | while (hi - lo > 1) { |
| 8723 | int mid = (lo + hi) / 2; |
| 8724 | sector_t a = BB_OFFSET(p[mid]); |
| 8725 | if (a < target) |
| 8726 | lo = mid; |
| 8727 | else |
| 8728 | hi = mid; |
| 8729 | } |
| 8730 | if (hi > lo) { |
| 8731 | /* p[lo] is the last range that could overlap the |
| 8732 | * current range. Earlier ranges could also overlap, |
| 8733 | * but only this one can overlap the end of the range. |
| 8734 | */ |
| 8735 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8736 | /* Partial overlap, leave the tail of this range */ |
| 8737 | int ack = BB_ACK(p[lo]); |
| 8738 | sector_t a = BB_OFFSET(p[lo]); |
| 8739 | sector_t end = a + BB_LEN(p[lo]); |
| 8740 | |
| 8741 | if (a < s) { |
| 8742 | /* we need to split this range */ |
| 8743 | if (bb->count >= MD_MAX_BADBLOCKS) { |
NeilBrown | 8b32bf5 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 8744 | rv = -ENOSPC; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8745 | goto out; |
| 8746 | } |
| 8747 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8748 | bb->count++; |
| 8749 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8750 | lo++; |
| 8751 | } |
| 8752 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8753 | /* there is no longer an overlap */ |
| 8754 | hi = lo; |
| 8755 | lo--; |
| 8756 | } |
| 8757 | while (lo >= 0 && |
| 8758 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8759 | /* This range does overlap */ |
| 8760 | if (BB_OFFSET(p[lo]) < s) { |
| 8761 | /* Keep the early parts of this range. */ |
| 8762 | int ack = BB_ACK(p[lo]); |
| 8763 | sector_t start = BB_OFFSET(p[lo]); |
| 8764 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8765 | /* now low doesn't overlap, so.. */ |
| 8766 | break; |
| 8767 | } |
| 8768 | lo--; |
| 8769 | } |
| 8770 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8771 | * anything between needs to be discarded |
| 8772 | */ |
| 8773 | if (hi - lo > 1) { |
| 8774 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8775 | bb->count -= (hi - lo - 1); |
| 8776 | } |
| 8777 | } |
| 8778 | |
| 8779 | bb->changed = 1; |
| 8780 | out: |
| 8781 | write_sequnlock_irq(&bb->lock); |
| 8782 | return rv; |
| 8783 | } |
| 8784 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8785 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8786 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8787 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8788 | if (is_new) |
| 8789 | s += rdev->new_data_offset; |
| 8790 | else |
| 8791 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8792 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8793 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8794 | } |
| 8795 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8796 | |
| 8797 | /* |
| 8798 | * Acknowledge all bad blocks in a list. |
| 8799 | * This only succeeds if ->changed is clear. It is used by |
| 8800 | * in-kernel metadata updates |
| 8801 | */ |
| 8802 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8803 | { |
| 8804 | if (bb->page == NULL || bb->changed) |
| 8805 | /* no point even trying */ |
| 8806 | return; |
| 8807 | write_seqlock_irq(&bb->lock); |
| 8808 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8809 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8810 | u64 *p = bb->page; |
| 8811 | int i; |
| 8812 | for (i = 0; i < bb->count ; i++) { |
| 8813 | if (!BB_ACK(p[i])) { |
| 8814 | sector_t start = BB_OFFSET(p[i]); |
| 8815 | int len = BB_LEN(p[i]); |
| 8816 | p[i] = BB_MAKE(start, len, 1); |
| 8817 | } |
| 8818 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8819 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8820 | } |
| 8821 | write_sequnlock_irq(&bb->lock); |
| 8822 | } |
| 8823 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8824 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8825 | /* sysfs access to bad-blocks list. |
| 8826 | * We present two files. |
| 8827 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8828 | * are recorded as bad. The list is truncated to fit within |
| 8829 | * the one-page limit of sysfs. |
| 8830 | * Writing "sector length" to this file adds an acknowledged |
| 8831 | * bad block list. |
| 8832 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8833 | * been acknowledged. Writing to this file adds bad blocks |
| 8834 | * without acknowledging them. This is largely for testing. |
| 8835 | */ |
| 8836 | |
| 8837 | static ssize_t |
| 8838 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8839 | { |
| 8840 | size_t len; |
| 8841 | int i; |
| 8842 | u64 *p = bb->page; |
| 8843 | unsigned seq; |
| 8844 | |
| 8845 | if (bb->shift < 0) |
| 8846 | return 0; |
| 8847 | |
| 8848 | retry: |
| 8849 | seq = read_seqbegin(&bb->lock); |
| 8850 | |
| 8851 | len = 0; |
| 8852 | i = 0; |
| 8853 | |
| 8854 | while (len < PAGE_SIZE && i < bb->count) { |
| 8855 | sector_t s = BB_OFFSET(p[i]); |
| 8856 | unsigned int length = BB_LEN(p[i]); |
| 8857 | int ack = BB_ACK(p[i]); |
| 8858 | i++; |
| 8859 | |
| 8860 | if (unack && ack) |
| 8861 | continue; |
| 8862 | |
| 8863 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8864 | (unsigned long long)s << bb->shift, |
| 8865 | length << bb->shift); |
| 8866 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8867 | if (unack && len == 0) |
| 8868 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8869 | |
| 8870 | if (read_seqretry(&bb->lock, seq)) |
| 8871 | goto retry; |
| 8872 | |
| 8873 | return len; |
| 8874 | } |
| 8875 | |
| 8876 | #define DO_DEBUG 1 |
| 8877 | |
| 8878 | static ssize_t |
| 8879 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8880 | { |
| 8881 | unsigned long long sector; |
| 8882 | int length; |
| 8883 | char newline; |
| 8884 | #ifdef DO_DEBUG |
| 8885 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8886 | * Normally only a successful write may clear a badblock |
| 8887 | */ |
| 8888 | int clear = 0; |
| 8889 | if (page[0] == '-') { |
| 8890 | clear = 1; |
| 8891 | page++; |
| 8892 | } |
| 8893 | #endif /* DO_DEBUG */ |
| 8894 | |
| 8895 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8896 | case 3: |
| 8897 | if (newline != '\n') |
| 8898 | return -EINVAL; |
| 8899 | case 2: |
| 8900 | if (length <= 0) |
| 8901 | return -EINVAL; |
| 8902 | break; |
| 8903 | default: |
| 8904 | return -EINVAL; |
| 8905 | } |
| 8906 | |
| 8907 | #ifdef DO_DEBUG |
| 8908 | if (clear) { |
| 8909 | md_clear_badblocks(bb, sector, length); |
| 8910 | return len; |
| 8911 | } |
| 8912 | #endif /* DO_DEBUG */ |
| 8913 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8914 | return len; |
| 8915 | else |
| 8916 | return -ENOSPC; |
| 8917 | } |
| 8918 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8919 | static int md_notify_reboot(struct notifier_block *this, |
| 8920 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8921 | { |
| 8922 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8923 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8924 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8925 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8926 | for_each_mddev(mddev, tmp) { |
| 8927 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8928 | if (mddev->pers) |
| 8929 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 8930 | if (mddev->persistent) |
| 8931 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8932 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8933 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8934 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8935 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8936 | /* |
| 8937 | * certain more exotic SCSI devices are known to be |
| 8938 | * volatile wrt too early system reboots. While the |
| 8939 | * right place to handle this issue is the given |
| 8940 | * driver, we do want to have a safe RAID driver ... |
| 8941 | */ |
| 8942 | if (need_delay) |
| 8943 | mdelay(1000*1); |
| 8944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8945 | return NOTIFY_DONE; |
| 8946 | } |
| 8947 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8948 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8949 | .notifier_call = md_notify_reboot, |
| 8950 | .next = NULL, |
| 8951 | .priority = INT_MAX, /* before any real devices */ |
| 8952 | }; |
| 8953 | |
| 8954 | static void md_geninit(void) |
| 8955 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8956 | 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] | 8957 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8958 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8959 | } |
| 8960 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8961 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8962 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8963 | int ret = -ENOMEM; |
| 8964 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8965 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8966 | if (!md_wq) |
| 8967 | goto err_wq; |
| 8968 | |
| 8969 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8970 | if (!md_misc_wq) |
| 8971 | goto err_misc_wq; |
| 8972 | |
| 8973 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8974 | goto err_md; |
| 8975 | |
| 8976 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8977 | goto err_mdp; |
| 8978 | mdp_major = ret; |
| 8979 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8980 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8981 | md_probe, NULL, NULL); |
| 8982 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8983 | md_probe, NULL, NULL); |
| 8984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8985 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8986 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8987 | |
| 8988 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8989 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8990 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8991 | err_mdp: |
| 8992 | unregister_blkdev(MD_MAJOR, "md"); |
| 8993 | err_md: |
| 8994 | destroy_workqueue(md_misc_wq); |
| 8995 | err_misc_wq: |
| 8996 | destroy_workqueue(md_wq); |
| 8997 | err_wq: |
| 8998 | return ret; |
| 8999 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9000 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9001 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9002 | { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9003 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 9004 | struct md_rdev *rdev2; |
| 9005 | int role, ret; |
| 9006 | char b[BDEVNAME_SIZE]; |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9007 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9008 | /* Check for change of roles in the active devices */ |
| 9009 | rdev_for_each(rdev2, mddev) { |
| 9010 | if (test_bit(Faulty, &rdev2->flags)) |
| 9011 | continue; |
| 9012 | |
| 9013 | /* Check if the roles changed */ |
| 9014 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 9015 | |
| 9016 | if (test_bit(Candidate, &rdev2->flags)) { |
| 9017 | if (role == 0xfffe) { |
| 9018 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); |
| 9019 | md_kick_rdev_from_array(rdev2); |
| 9020 | continue; |
| 9021 | } |
| 9022 | else |
| 9023 | clear_bit(Candidate, &rdev2->flags); |
| 9024 | } |
| 9025 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9026 | if (role != rdev2->raid_disk) { |
| 9027 | /* got activated */ |
| 9028 | if (rdev2->raid_disk == -1 && role != 0xffff) { |
| 9029 | rdev2->saved_raid_disk = role; |
| 9030 | ret = remove_and_add_spares(mddev, rdev2); |
| 9031 | pr_info("Activated spare: %s\n", |
| 9032 | bdevname(rdev2->bdev,b)); |
| 9033 | continue; |
| 9034 | } |
| 9035 | /* device faulty |
| 9036 | * We just want to do the minimum to mark the disk |
| 9037 | * as faulty. The recovery is performed by the |
| 9038 | * one who initiated the error. |
| 9039 | */ |
| 9040 | if ((role == 0xfffe) || (role == 0xfffd)) { |
| 9041 | md_error(mddev, rdev2); |
| 9042 | clear_bit(Blocked, &rdev2->flags); |
| 9043 | } |
| 9044 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9045 | } |
| 9046 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 9047 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) |
| 9048 | update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9049 | |
| 9050 | /* Finally set the event to be up to date */ |
| 9051 | mddev->events = le64_to_cpu(sb->events); |
| 9052 | } |
| 9053 | |
| 9054 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) |
| 9055 | { |
| 9056 | int err; |
| 9057 | struct page *swapout = rdev->sb_page; |
| 9058 | struct mdp_superblock_1 *sb; |
| 9059 | |
| 9060 | /* Store the sb page of the rdev in the swapout temporary |
| 9061 | * variable in case we err in the future |
| 9062 | */ |
| 9063 | rdev->sb_page = NULL; |
| 9064 | alloc_disk_sb(rdev); |
| 9065 | ClearPageUptodate(rdev->sb_page); |
| 9066 | rdev->sb_loaded = 0; |
| 9067 | err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version); |
| 9068 | |
| 9069 | if (err < 0) { |
| 9070 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", |
| 9071 | __func__, __LINE__, rdev->desc_nr, err); |
| 9072 | put_page(rdev->sb_page); |
| 9073 | rdev->sb_page = swapout; |
| 9074 | rdev->sb_loaded = 1; |
| 9075 | return err; |
| 9076 | } |
| 9077 | |
| 9078 | sb = page_address(rdev->sb_page); |
| 9079 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET |
| 9080 | * is not set |
| 9081 | */ |
| 9082 | |
| 9083 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) |
| 9084 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 9085 | |
| 9086 | /* The other node finished recovery, call spare_active to set |
| 9087 | * device In_sync and mddev->degraded |
| 9088 | */ |
| 9089 | if (rdev->recovery_offset == MaxSector && |
| 9090 | !test_bit(In_sync, &rdev->flags) && |
| 9091 | mddev->pers->spare_active(mddev)) |
| 9092 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
| 9093 | |
| 9094 | put_page(swapout); |
| 9095 | return 0; |
| 9096 | } |
| 9097 | |
| 9098 | void md_reload_sb(struct mddev *mddev, int nr) |
| 9099 | { |
| 9100 | struct md_rdev *rdev; |
| 9101 | int err; |
| 9102 | |
| 9103 | /* Find the rdev */ |
| 9104 | rdev_for_each_rcu(rdev, mddev) { |
| 9105 | if (rdev->desc_nr == nr) |
| 9106 | break; |
| 9107 | } |
| 9108 | |
| 9109 | if (!rdev || rdev->desc_nr != nr) { |
| 9110 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); |
| 9111 | return; |
| 9112 | } |
| 9113 | |
| 9114 | err = read_rdev(mddev, rdev); |
| 9115 | if (err < 0) |
| 9116 | return; |
| 9117 | |
| 9118 | check_sb_changes(mddev, rdev); |
| 9119 | |
| 9120 | /* Read all rdev's to update recovery_offset */ |
| 9121 | rdev_for_each_rcu(rdev, mddev) |
| 9122 | read_rdev(mddev, rdev); |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9123 | } |
| 9124 | EXPORT_SYMBOL(md_reload_sb); |
| 9125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9126 | #ifndef MODULE |
| 9127 | |
| 9128 | /* |
| 9129 | * Searches all registered partitions for autorun RAID arrays |
| 9130 | * at boot time. |
| 9131 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9132 | |
| 9133 | static LIST_HEAD(all_detected_devices); |
| 9134 | struct detected_devices_node { |
| 9135 | struct list_head list; |
| 9136 | dev_t dev; |
| 9137 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9138 | |
| 9139 | void md_autodetect_dev(dev_t dev) |
| 9140 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9141 | struct detected_devices_node *node_detected_dev; |
| 9142 | |
| 9143 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 9144 | if (node_detected_dev) { |
| 9145 | node_detected_dev->dev = dev; |
| 9146 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 9147 | } else { |
| 9148 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 9149 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 9150 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9151 | } |
| 9152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9153 | static void autostart_arrays(int part) |
| 9154 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9155 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9156 | struct detected_devices_node *node_detected_dev; |
| 9157 | dev_t dev; |
| 9158 | int i_scanned, i_passed; |
| 9159 | |
| 9160 | i_scanned = 0; |
| 9161 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9162 | |
| 9163 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 9164 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9165 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 9166 | i_scanned++; |
| 9167 | node_detected_dev = list_entry(all_detected_devices.next, |
| 9168 | struct detected_devices_node, list); |
| 9169 | list_del(&node_detected_dev->list); |
| 9170 | dev = node_detected_dev->dev; |
| 9171 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 9172 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9173 | if (IS_ERR(rdev)) |
| 9174 | continue; |
| 9175 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9176 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9177 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9178 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 9179 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9180 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9181 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9182 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9183 | |
| 9184 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 9185 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9186 | |
| 9187 | autorun_devices(part); |
| 9188 | } |
| 9189 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 9190 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9191 | |
| 9192 | static __exit void md_exit(void) |
| 9193 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9194 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9195 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9196 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 9197 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 9198 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 9199 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9200 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 9201 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9202 | unregister_blkdev(mdp_major, "mdp"); |
| 9203 | unregister_reboot_notifier(&md_notifier); |
| 9204 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9205 | |
| 9206 | /* We cannot unload the modules while some process is |
| 9207 | * waiting for us in select() or poll() - wake them up |
| 9208 | */ |
| 9209 | md_unloading = 1; |
| 9210 | while (waitqueue_active(&md_event_waiters)) { |
| 9211 | /* not safe to leave yet */ |
| 9212 | wake_up(&md_event_waiters); |
| 9213 | msleep(delay); |
| 9214 | delay += delay; |
| 9215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9216 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9217 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 9218 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9219 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9220 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9221 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9222 | destroy_workqueue(md_misc_wq); |
| 9223 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9224 | } |
| 9225 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 9226 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9227 | module_exit(md_exit) |
| 9228 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9229 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 9230 | { |
| 9231 | return sprintf(buffer, "%d", start_readonly); |
| 9232 | } |
| 9233 | static int set_ro(const char *val, struct kernel_param *kp) |
| 9234 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 9235 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9236 | } |
| 9237 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 9238 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 9239 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9240 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9242 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 9243 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 9244 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 9245 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |