Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Al Viro | ff01bb48 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 39 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 40 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 41 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 42 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 43 | #include <linux/hdreg.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/random.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 46 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 47 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 49 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 50 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/raid/md_p.h> |
| 52 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 56 | #include "md-cluster.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 59 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 62 | /* pers_list is a list of registered personalities protected |
| 63 | * by pers_lock. |
| 64 | * pers_lock does extra service to protect accesses to |
| 65 | * mddev->thread when the mutex cannot be held. |
| 66 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 67 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static DEFINE_SPINLOCK(pers_lock); |
| 69 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 70 | struct md_cluster_operations *md_cluster_ops; |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame] | 71 | EXPORT_SYMBOL(md_cluster_ops); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 72 | struct module *md_cluster_mod; |
| 73 | EXPORT_SYMBOL(md_cluster_mod); |
| 74 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 75 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 76 | static struct workqueue_struct *md_wq; |
| 77 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 78 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 79 | static int remove_and_add_spares(struct mddev *mddev, |
| 80 | struct md_rdev *this); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 81 | static void mddev_detach(struct mddev *mddev); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 84 | * Default number of read corrections we'll attempt on an rdev |
| 85 | * before ejecting it from the array. We divide the read error |
| 86 | * count by 2 for every hour elapsed between read errors. |
| 87 | */ |
| 88 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 89 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 91 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 92 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 93 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 95 | * speed limit - in case reconstruction slows down your system despite |
| 96 | * idle IO detection. |
| 97 | * |
| 98 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 99 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | */ |
| 101 | |
| 102 | static int sysctl_speed_limit_min = 1000; |
| 103 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 104 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 105 | { |
| 106 | return mddev->sync_speed_min ? |
| 107 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 108 | } |
| 109 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 110 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 111 | { |
| 112 | return mddev->sync_speed_max ? |
| 113 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | static struct ctl_table_header *raid_table_header; |
| 117 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 118 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | .procname = "speed_limit_min", |
| 121 | .data = &sysctl_speed_limit_min, |
| 122 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 123 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 124 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | }, |
| 126 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | .procname = "speed_limit_max", |
| 128 | .data = &sysctl_speed_limit_max, |
| 129 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 130 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 131 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 133 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 136 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | .procname = "raid", |
| 139 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 140 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | .child = raid_table, |
| 142 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 143 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 146 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | .procname = "dev", |
| 149 | .maxlen = 0, |
| 150 | .mode = 0555, |
| 151 | .child = raid_dir_table, |
| 152 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 153 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 156 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 158 | static int start_readonly; |
| 159 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 160 | /* bio_clone_mddev |
| 161 | * like bio_clone, but with a local bio set |
| 162 | */ |
| 163 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 164 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 165 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 166 | { |
| 167 | struct bio *b; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 168 | |
| 169 | if (!mddev || !mddev->bio_set) |
| 170 | return bio_alloc(gfp_mask, nr_iovecs); |
| 171 | |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 172 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 173 | if (!b) |
| 174 | return NULL; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 175 | return b; |
| 176 | } |
| 177 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 178 | |
| 179 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 180 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 181 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 182 | if (!mddev || !mddev->bio_set) |
| 183 | return bio_clone(bio, gfp_mask); |
| 184 | |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 185 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 186 | } |
| 187 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 190 | * We have a system wide 'event count' that is incremented |
| 191 | * on any 'interesting' event, and readers of /proc/mdstat |
| 192 | * can use 'poll' or 'select' to find out when the event |
| 193 | * count increases. |
| 194 | * |
| 195 | * Events are: |
| 196 | * start array, stop array, error, add device, remove device, |
| 197 | * start build, activate spare |
| 198 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 199 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 200 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 201 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 202 | { |
| 203 | atomic_inc(&md_event_count); |
| 204 | wake_up(&md_event_waiters); |
| 205 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 206 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 207 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 208 | /* Alternate version that can be called from interrupts |
| 209 | * when calling sysfs_notify isn't needed. |
| 210 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 211 | static void md_new_event_inintr(struct mddev *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 212 | { |
| 213 | atomic_inc(&md_event_count); |
| 214 | wake_up(&md_event_waiters); |
| 215 | } |
| 216 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 217 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | * Enables to iterate over all existing md arrays |
| 219 | * all_mddevs_lock protects this list. |
| 220 | */ |
| 221 | static LIST_HEAD(all_mddevs); |
| 222 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | /* |
| 225 | * iterates through all used mddevs in the system. |
| 226 | * We take care to grab the all_mddevs_lock whenever navigating |
| 227 | * the list, and to always hold a refcount when unlocked. |
| 228 | * Any code which breaks out of this loop while own |
| 229 | * a reference to the current mddev and must mddev_put it. |
| 230 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 231 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | \ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 233 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 234 | _tmp = all_mddevs.next; \ |
| 235 | _mddev = NULL;}); \ |
| 236 | ({ if (_tmp != &all_mddevs) \ |
| 237 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 239 | if (_mddev) mddev_put(_mddev); \ |
| 240 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 241 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 243 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | ) |
| 245 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 246 | /* Rather than calling directly into the personality make_request function, |
| 247 | * IO requests come here first so that we can check if the device is |
| 248 | * being suspended pending a reconfiguration. |
| 249 | * We hold a refcount over the call to ->make_request. By the time that |
| 250 | * call has finished, the bio has been linked into some internal structure |
| 251 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 252 | */ |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 253 | static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 255 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 256 | struct mddev *mddev = q->queuedata; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 257 | unsigned int sectors; |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 258 | int cpu; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 259 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 260 | blk_queue_split(q, &bio, q->bio_split); |
| 261 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 262 | if (mddev == NULL || mddev->pers == NULL |
| 263 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 264 | bio_io_error(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 265 | return BLK_QC_T_NONE; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 266 | } |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 267 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 268 | if (bio_sectors(bio) != 0) |
| 269 | bio->bi_error = -EROFS; |
| 270 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 271 | return BLK_QC_T_NONE; |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 272 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 273 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 274 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 275 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 276 | DEFINE_WAIT(__wait); |
| 277 | for (;;) { |
| 278 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 279 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 280 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 281 | break; |
| 282 | rcu_read_unlock(); |
| 283 | schedule(); |
| 284 | rcu_read_lock(); |
| 285 | } |
| 286 | finish_wait(&mddev->sb_wait, &__wait); |
| 287 | } |
| 288 | atomic_inc(&mddev->active_io); |
| 289 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 290 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 291 | /* |
| 292 | * save the sectors now since our bio can |
| 293 | * go away inside make_request |
| 294 | */ |
| 295 | sectors = bio_sectors(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 296 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 297 | |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 298 | cpu = part_stat_lock(); |
| 299 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 300 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
| 301 | part_stat_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 302 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 303 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 304 | wake_up(&mddev->sb_wait); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 305 | |
| 306 | return BLK_QC_T_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 309 | /* mddev_suspend makes sure no new requests are submitted |
| 310 | * to the device, and that any requests that have been submitted |
| 311 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 312 | * Once mddev_detach() is called and completes, the module will be |
| 313 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 314 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 315 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 316 | { |
| 317 | BUG_ON(mddev->suspended); |
| 318 | mddev->suspended = 1; |
| 319 | synchronize_rcu(); |
| 320 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 321 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 322 | |
| 323 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 324 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 325 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 326 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 327 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 328 | { |
| 329 | mddev->suspended = 0; |
| 330 | wake_up(&mddev->sb_wait); |
| 331 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 332 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 333 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 334 | md_wakeup_thread(mddev->thread); |
| 335 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 336 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 337 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 338 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 339 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 340 | { |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 341 | struct md_personality *pers = mddev->pers; |
| 342 | int ret = 0; |
| 343 | |
| 344 | rcu_read_lock(); |
| 345 | if (mddev->suspended) |
| 346 | ret = 1; |
| 347 | else if (pers && pers->congested) |
| 348 | ret = pers->congested(mddev, bits); |
| 349 | rcu_read_unlock(); |
| 350 | return ret; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 351 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 352 | EXPORT_SYMBOL_GPL(mddev_congested); |
| 353 | static int md_congested(void *data, int bits) |
| 354 | { |
| 355 | struct mddev *mddev = data; |
| 356 | return mddev_congested(mddev, bits); |
| 357 | } |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 358 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 359 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 360 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 361 | */ |
| 362 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 363 | static void md_end_flush(struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 364 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 365 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 366 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 367 | |
| 368 | rdev_dec_pending(rdev, mddev); |
| 369 | |
| 370 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 371 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 372 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 373 | } |
| 374 | bio_put(bio); |
| 375 | } |
| 376 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 377 | static void md_submit_flush_data(struct work_struct *ws); |
| 378 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 379 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 380 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 381 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 382 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 383 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 384 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 385 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 386 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 387 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 388 | if (rdev->raid_disk >= 0 && |
| 389 | !test_bit(Faulty, &rdev->flags)) { |
| 390 | /* Take two references, one is dropped |
| 391 | * when request finishes, one after |
| 392 | * we reclaim rcu_read_lock |
| 393 | */ |
| 394 | struct bio *bi; |
| 395 | atomic_inc(&rdev->nr_pending); |
| 396 | atomic_inc(&rdev->nr_pending); |
| 397 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 398 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 399 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 400 | bi->bi_private = rdev; |
| 401 | bi->bi_bdev = rdev->bdev; |
| 402 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 403 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 404 | rcu_read_lock(); |
| 405 | rdev_dec_pending(rdev, mddev); |
| 406 | } |
| 407 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 408 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 409 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 410 | } |
| 411 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 412 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 413 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 414 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 415 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 416 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 417 | if (bio->bi_iter.bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 418 | /* an empty barrier - all done */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 419 | bio_endio(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 420 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 421 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 422 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 423 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 424 | |
| 425 | mddev->flush_bio = NULL; |
| 426 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 427 | } |
| 428 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 429 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 430 | { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 431 | spin_lock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 432 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 433 | !mddev->flush_bio, |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 434 | mddev->lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 435 | mddev->flush_bio = bio; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 436 | spin_unlock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 437 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 438 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 439 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 440 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 441 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 442 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 443 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 444 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 445 | struct mddev *mddev = cb->data; |
| 446 | md_wakeup_thread(mddev->thread); |
| 447 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 448 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 449 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 450 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 451 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
| 453 | atomic_inc(&mddev->active); |
| 454 | return mddev; |
| 455 | } |
| 456 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 457 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 458 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 459 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 461 | struct bio_set *bs = NULL; |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 464 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 465 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 466 | mddev->ctime == 0 && !mddev->hold_active) { |
| 467 | /* Array is not configured at all, and not held active, |
| 468 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 469 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 470 | bs = mddev->bio_set; |
| 471 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 472 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 473 | /* We did a probe so need to clean up. Call |
| 474 | * queue_work inside the spinlock so that |
| 475 | * flush_workqueue() after mddev_find will |
| 476 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 477 | */ |
| 478 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 479 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 480 | } else |
| 481 | kfree(mddev); |
| 482 | } |
| 483 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 484 | if (bs) |
| 485 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 488 | static void md_safemode_timeout(unsigned long data); |
| 489 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 490 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 491 | { |
| 492 | mutex_init(&mddev->open_mutex); |
| 493 | mutex_init(&mddev->reconfig_mutex); |
| 494 | mutex_init(&mddev->bitmap_info.mutex); |
| 495 | INIT_LIST_HEAD(&mddev->disks); |
| 496 | INIT_LIST_HEAD(&mddev->all_mddevs); |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 497 | setup_timer(&mddev->safemode_timer, md_safemode_timeout, |
| 498 | (unsigned long) mddev); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 499 | atomic_set(&mddev->active, 1); |
| 500 | atomic_set(&mddev->openers, 0); |
| 501 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 502 | spin_lock_init(&mddev->lock); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 503 | atomic_set(&mddev->flush_pending, 0); |
| 504 | init_waitqueue_head(&mddev->sb_wait); |
| 505 | init_waitqueue_head(&mddev->recovery_wait); |
| 506 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 507 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 508 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 509 | mddev->resync_min = 0; |
| 510 | mddev->resync_max = MaxSector; |
| 511 | mddev->level = LEVEL_NONE; |
| 512 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 513 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 514 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 515 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 517 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 519 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 520 | unit &= ~((1<<MdpMinorShift)-1); |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | retry: |
| 523 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 525 | if (unit) { |
| 526 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 527 | if (mddev->unit == unit) { |
| 528 | mddev_get(mddev); |
| 529 | spin_unlock(&all_mddevs_lock); |
| 530 | kfree(new); |
| 531 | return mddev; |
| 532 | } |
| 533 | |
| 534 | if (new) { |
| 535 | list_add(&new->all_mddevs, &all_mddevs); |
| 536 | spin_unlock(&all_mddevs_lock); |
| 537 | new->hold_active = UNTIL_IOCTL; |
| 538 | return new; |
| 539 | } |
| 540 | } else if (new) { |
| 541 | /* find an unused unit number */ |
| 542 | static int next_minor = 512; |
| 543 | int start = next_minor; |
| 544 | int is_free = 0; |
| 545 | int dev = 0; |
| 546 | while (!is_free) { |
| 547 | dev = MKDEV(MD_MAJOR, next_minor); |
| 548 | next_minor++; |
| 549 | if (next_minor > MINORMASK) |
| 550 | next_minor = 0; |
| 551 | if (next_minor == start) { |
| 552 | /* Oh dear, all in use. */ |
| 553 | spin_unlock(&all_mddevs_lock); |
| 554 | kfree(new); |
| 555 | return NULL; |
| 556 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 557 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 558 | is_free = 1; |
| 559 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 560 | if (mddev->unit == dev) { |
| 561 | is_free = 0; |
| 562 | break; |
| 563 | } |
| 564 | } |
| 565 | new->unit = dev; |
| 566 | new->md_minor = MINOR(dev); |
| 567 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | list_add(&new->all_mddevs, &all_mddevs); |
| 569 | spin_unlock(&all_mddevs_lock); |
| 570 | return new; |
| 571 | } |
| 572 | spin_unlock(&all_mddevs_lock); |
| 573 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 574 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | if (!new) |
| 576 | return NULL; |
| 577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | new->unit = unit; |
| 579 | if (MAJOR(unit) == MD_MAJOR) |
| 580 | new->md_minor = MINOR(unit); |
| 581 | else |
| 582 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 583 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 584 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | goto retry; |
| 587 | } |
| 588 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 589 | static struct attribute_group md_redundancy_group; |
| 590 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 591 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 593 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 594 | /* These cannot be removed under reconfig_mutex as |
| 595 | * an access to the files will try to take reconfig_mutex |
| 596 | * while holding the file unremovable, which leads to |
| 597 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 598 | * So hold set sysfs_active while the remove in happeing, |
| 599 | * and anything else which might set ->to_remove or my |
| 600 | * otherwise change the sysfs namespace will fail with |
| 601 | * -EBUSY if sysfs_active is still set. |
| 602 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 603 | * test it under the same mutex to ensure its correct value |
| 604 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 605 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 606 | struct attribute_group *to_remove = mddev->to_remove; |
| 607 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 608 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 609 | mutex_unlock(&mddev->reconfig_mutex); |
| 610 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 611 | if (mddev->kobj.sd) { |
| 612 | if (to_remove != &md_redundancy_group) |
| 613 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 614 | if (mddev->pers == NULL || |
| 615 | mddev->pers->sync_request == NULL) { |
| 616 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 617 | if (mddev->sysfs_action) |
| 618 | sysfs_put(mddev->sysfs_action); |
| 619 | mddev->sysfs_action = NULL; |
| 620 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 621 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 622 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 623 | } else |
| 624 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 626 | /* As we've dropped the mutex we need a spinlock to |
| 627 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 628 | */ |
| 629 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 630 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 631 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 633 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 635 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 636 | { |
| 637 | struct md_rdev *rdev; |
| 638 | |
| 639 | rdev_for_each_rcu(rdev, mddev) |
| 640 | if (rdev->desc_nr == nr) |
| 641 | return rdev; |
| 642 | |
| 643 | return NULL; |
| 644 | } |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 645 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 646 | |
| 647 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 649 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 651 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | if (rdev->bdev->bd_dev == dev) |
| 653 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | return NULL; |
| 656 | } |
| 657 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 658 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 659 | { |
| 660 | struct md_rdev *rdev; |
| 661 | |
| 662 | rdev_for_each_rcu(rdev, mddev) |
| 663 | if (rdev->bdev->bd_dev == dev) |
| 664 | return rdev; |
| 665 | |
| 666 | return NULL; |
| 667 | } |
| 668 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 669 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 670 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 671 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 672 | list_for_each_entry(pers, &pers_list, list) { |
| 673 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 674 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 675 | if (strcmp(pers->name, clevel)==0) |
| 676 | return pers; |
| 677 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 678 | return NULL; |
| 679 | } |
| 680 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 681 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 682 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 684 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 685 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | } |
| 687 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 688 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 691 | if (!rdev->sb_page) { |
| 692 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 693 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 699 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | { |
| 701 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 702 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | rdev->sb_loaded = 0; |
| 704 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 705 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 706 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 708 | if (rdev->bb_page) { |
| 709 | put_page(rdev->bb_page); |
| 710 | rdev->bb_page = NULL; |
| 711 | } |
NeilBrown | 4fa2f32 | 2012-05-22 13:55:01 +1000 | [diff] [blame] | 712 | kfree(rdev->badblocks.page); |
| 713 | rdev->badblocks.page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 715 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 717 | static void super_written(struct bio *bio) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 718 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 719 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 720 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 721 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 722 | if (bio->bi_error) { |
| 723 | printk("md: super_written gets error=%d\n", bio->bi_error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 724 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 725 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 726 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 727 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 728 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 729 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 730 | } |
| 731 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 732 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 733 | sector_t sector, int size, struct page *page) |
| 734 | { |
| 735 | /* write first size bytes of page to sector of rdev |
| 736 | * Increment mddev->pending_writes before returning |
| 737 | * and decrement it on completion, waking up sb_wait |
| 738 | * if zero is reached. |
| 739 | * If an error occurred, call md_error |
| 740 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 741 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 742 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 743 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 744 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 745 | bio_add_page(bio, page, size, 0); |
| 746 | bio->bi_private = rdev; |
| 747 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 748 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 749 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 750 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 751 | } |
| 752 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 753 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 754 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 755 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 756 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 757 | } |
| 758 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 759 | 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] | 760 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 762 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | int ret; |
| 764 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 765 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 766 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 767 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 768 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 769 | else if (rdev->mddev->reshape_position != MaxSector && |
| 770 | (rdev->mddev->reshape_backwards == |
| 771 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 772 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 773 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 774 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | bio_add_page(bio, page, size, 0); |
Kent Overstreet | c170bbb | 2013-11-24 16:32:22 -0700 | [diff] [blame] | 776 | submit_bio_wait(rw, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 778 | ret = !bio->bi_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | bio_put(bio); |
| 780 | return ret; |
| 781 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 782 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 784 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | { |
| 786 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | if (rdev->sb_loaded) |
| 789 | return 0; |
| 790 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 791 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | goto fail; |
| 793 | rdev->sb_loaded = 1; |
| 794 | return 0; |
| 795 | |
| 796 | fail: |
| 797 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 798 | bdevname(rdev->bdev,b)); |
| 799 | return -EINVAL; |
| 800 | } |
| 801 | |
| 802 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 803 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 804 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 805 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 806 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 807 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 811 | { |
| 812 | int ret; |
| 813 | mdp_super_t *tmp1, *tmp2; |
| 814 | |
| 815 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 816 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 817 | |
| 818 | if (!tmp1 || !tmp2) { |
| 819 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 820 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | goto abort; |
| 822 | } |
| 823 | |
| 824 | *tmp1 = *sb1; |
| 825 | *tmp2 = *sb2; |
| 826 | |
| 827 | /* |
| 828 | * nr_disks is not constant |
| 829 | */ |
| 830 | tmp1->nr_disks = 0; |
| 831 | tmp2->nr_disks = 0; |
| 832 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 833 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 835 | kfree(tmp1); |
| 836 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | return ret; |
| 838 | } |
| 839 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 840 | static u32 md_csum_fold(u32 csum) |
| 841 | { |
| 842 | csum = (csum & 0xffff) + (csum >> 16); |
| 843 | return (csum & 0xffff) + (csum >> 16); |
| 844 | } |
| 845 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 846 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 848 | u64 newcsum = 0; |
| 849 | u32 *sb32 = (u32*)sb; |
| 850 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | unsigned int disk_csum, csum; |
| 852 | |
| 853 | disk_csum = sb->sb_csum; |
| 854 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 855 | |
| 856 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 857 | newcsum += sb32[i]; |
| 858 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 859 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 860 | #ifdef CONFIG_ALPHA |
| 861 | /* This used to use csum_partial, which was wrong for several |
| 862 | * reasons including that different results are returned on |
| 863 | * different architectures. It isn't critical that we get exactly |
| 864 | * the same return value as before (we always csum_fold before |
| 865 | * testing, and that removes any differences). However as we |
| 866 | * know that csum_partial always returned a 16bit value on |
| 867 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 868 | */ |
| 869 | sb->sb_csum = md_csum_fold(disk_csum); |
| 870 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 872 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | return csum; |
| 874 | } |
| 875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | /* |
| 877 | * Handle superblock details. |
| 878 | * We want to be able to handle multiple superblock formats |
| 879 | * so we have a common interface to them all, and an array of |
| 880 | * different handlers. |
| 881 | * We rely on user-space to write the initial superblock, and support |
| 882 | * reading and updating of superblocks. |
| 883 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 884 | * 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] | 885 | * loads and validates a superblock on dev. |
| 886 | * if refdev != NULL, compare superblocks on both devices |
| 887 | * Return: |
| 888 | * 0 - dev has a superblock that is compatible with refdev |
| 889 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 890 | * so dev should be used as the refdev in future |
| 891 | * -EINVAL superblock incompatible or invalid |
| 892 | * -othererror e.g. -EIO |
| 893 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 894 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | * Verify that dev is acceptable into mddev. |
| 896 | * The first time, mddev->raid_disks will be 0, and data from |
| 897 | * dev should be merged in. Subsequent calls check that dev |
| 898 | * is new enough. Return 0 or -EINVAL |
| 899 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 900 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | * Update the superblock for rdev with data in mddev |
| 902 | * This does not write to disc. |
| 903 | * |
| 904 | */ |
| 905 | |
| 906 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 907 | char *name; |
| 908 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 909 | int (*load_super)(struct md_rdev *rdev, |
| 910 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 911 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 912 | int (*validate_super)(struct mddev *mddev, |
| 913 | struct md_rdev *rdev); |
| 914 | void (*sync_super)(struct mddev *mddev, |
| 915 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 916 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 917 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 918 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 919 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | }; |
| 921 | |
| 922 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 923 | * Check that the given mddev has no bitmap. |
| 924 | * |
| 925 | * This function is called from the run method of all personalities that do not |
| 926 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 927 | * has a bitmap. Otherwise, it returns 0. |
| 928 | * |
| 929 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 930 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 931 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 932 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 933 | return 0; |
| 934 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 935 | mdname(mddev), mddev->pers->name); |
| 936 | return 1; |
| 937 | } |
| 938 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 939 | |
| 940 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 941 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 943 | 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] | 944 | { |
| 945 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 946 | mdp_super_t *sb; |
| 947 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
| 949 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 950 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | * it's at the end of the disk. |
| 952 | * |
| 953 | * It also happens to be a multiple of 4Kb. |
| 954 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 955 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 957 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | if (ret) return ret; |
| 959 | |
| 960 | ret = -EINVAL; |
| 961 | |
| 962 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 963 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
| 965 | if (sb->md_magic != MD_SB_MAGIC) { |
| 966 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 967 | b); |
| 968 | goto abort; |
| 969 | } |
| 970 | |
| 971 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 972 | sb->minor_version < 90 || |
| 973 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 975 | sb->major_version, sb->minor_version, |
| 976 | b); |
| 977 | goto abort; |
| 978 | } |
| 979 | |
| 980 | if (sb->raid_disks <= 0) |
| 981 | goto abort; |
| 982 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 983 | 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] | 984 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 985 | b); |
| 986 | goto abort; |
| 987 | } |
| 988 | |
| 989 | rdev->preferred_minor = sb->md_minor; |
| 990 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 991 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 992 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 993 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | if (sb->level == LEVEL_MULTIPATH) |
| 996 | rdev->desc_nr = -1; |
| 997 | else |
| 998 | rdev->desc_nr = sb->this_disk.number; |
| 999 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1000 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1002 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1004 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | if (!uuid_equal(refsb, sb)) { |
| 1006 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1007 | b, bdevname(refdev->bdev,b2)); |
| 1008 | goto abort; |
| 1009 | } |
| 1010 | if (!sb_equal(refsb, sb)) { |
| 1011 | printk(KERN_WARNING "md: %s has same UUID" |
| 1012 | " but different superblock to %s\n", |
| 1013 | b, bdevname(refdev->bdev, b2)); |
| 1014 | goto abort; |
| 1015 | } |
| 1016 | ev1 = md_event(sb); |
| 1017 | ev2 = md_event(refsb); |
| 1018 | if (ev1 > ev2) |
| 1019 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1020 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | ret = 0; |
| 1022 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1023 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1024 | /* Limit to 4TB as metadata cannot record more than that. |
| 1025 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1026 | * record this size) |
| 1027 | */ |
| 1028 | if (rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1029 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1031 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1032 | /* "this cannot possibly happen" ... */ |
| 1033 | ret = -EINVAL; |
| 1034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | abort: |
| 1036 | return ret; |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * validate_super for 0.90.0 |
| 1041 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1042 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | { |
| 1044 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1045 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1046 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1048 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1049 | clear_bit(Faulty, &rdev->flags); |
| 1050 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1051 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1052 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | if (mddev->raid_disks == 0) { |
| 1055 | mddev->major_version = 0; |
| 1056 | mddev->minor_version = sb->minor_version; |
| 1057 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1058 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1059 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | mddev->ctime = sb->ctime; |
| 1061 | mddev->utime = sb->utime; |
| 1062 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1063 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | mddev->layout = sb->layout; |
| 1065 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1066 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1067 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1068 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1069 | mddev->bitmap_info.space = 0; |
| 1070 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1071 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1072 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1073 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1075 | if (mddev->minor_version >= 91) { |
| 1076 | mddev->reshape_position = sb->reshape_position; |
| 1077 | mddev->delta_disks = sb->delta_disks; |
| 1078 | mddev->new_level = sb->new_level; |
| 1079 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1080 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1081 | if (mddev->delta_disks < 0) |
| 1082 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1083 | } else { |
| 1084 | mddev->reshape_position = MaxSector; |
| 1085 | mddev->delta_disks = 0; |
| 1086 | mddev->new_level = mddev->level; |
| 1087 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1088 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1089 | } |
| 1090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1092 | mddev->recovery_cp = MaxSector; |
| 1093 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1094 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | sb->events_lo == sb->cp_events_lo) { |
| 1096 | mddev->recovery_cp = sb->recovery_cp; |
| 1097 | } else |
| 1098 | mddev->recovery_cp = 0; |
| 1099 | } |
| 1100 | |
| 1101 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1102 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1103 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1104 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1105 | |
| 1106 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1107 | |
| 1108 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1109 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1110 | mddev->bitmap_info.offset = |
| 1111 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1112 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1113 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1114 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1115 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1116 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1117 | /* Insist on good event counter while assembling, except |
| 1118 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1120 | if (sb->disks[rdev->desc_nr].state & ( |
| 1121 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1122 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1123 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1124 | } else if (mddev->bitmap) { |
| 1125 | /* if adding to array with a bitmap, then we can accept an |
| 1126 | * older device ... but not too old. |
| 1127 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1128 | if (ev1 < mddev->bitmap->events_cleared) |
| 1129 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1130 | if (ev1 < mddev->events) |
| 1131 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1132 | } else { |
| 1133 | if (ev1 < mddev->events) |
| 1134 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1135 | return 0; |
| 1136 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | desc = sb->disks + rdev->desc_nr; |
| 1140 | |
| 1141 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1142 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1143 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1144 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1145 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1147 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1148 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1149 | /* active but not in sync implies recovery up to |
| 1150 | * reshape position. We don't know exactly where |
| 1151 | * that is, so set to zero for now */ |
| 1152 | if (mddev->minor_version >= 91) { |
| 1153 | rdev->recovery_offset = 0; |
| 1154 | rdev->raid_disk = desc->raid_disk; |
| 1155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1157 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1158 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1159 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1160 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | /* |
| 1165 | * sync_super for 0.90.0 |
| 1166 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1167 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | { |
| 1169 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1170 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | /* make rdev->sb match mddev data.. |
| 1174 | * |
| 1175 | * 1/ zero out disks |
| 1176 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1177 | * 3/ any empty disks < next_spare become removed |
| 1178 | * |
| 1179 | * disks[0] gets initialised to REMOVED because |
| 1180 | * we cannot be sure from other fields if it has |
| 1181 | * been initialised or not. |
| 1182 | */ |
| 1183 | int i; |
| 1184 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1185 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1186 | rdev->sb_size = MD_SB_BYTES; |
| 1187 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1188 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | |
| 1190 | memset(sb, 0, sizeof(*sb)); |
| 1191 | |
| 1192 | sb->md_magic = MD_SB_MAGIC; |
| 1193 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | sb->patch_version = mddev->patch_version; |
| 1195 | sb->gvalid_words = 0; /* ignored */ |
| 1196 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1197 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1198 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1199 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1200 | |
| 1201 | sb->ctime = mddev->ctime; |
| 1202 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1203 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | sb->raid_disks = mddev->raid_disks; |
| 1205 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1206 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | sb->utime = mddev->utime; |
| 1208 | sb->state = 0; |
| 1209 | sb->events_hi = (mddev->events>>32); |
| 1210 | sb->events_lo = (u32)mddev->events; |
| 1211 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1212 | if (mddev->reshape_position == MaxSector) |
| 1213 | sb->minor_version = 90; |
| 1214 | else { |
| 1215 | sb->minor_version = 91; |
| 1216 | sb->reshape_position = mddev->reshape_position; |
| 1217 | sb->new_level = mddev->new_level; |
| 1218 | sb->delta_disks = mddev->delta_disks; |
| 1219 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1220 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1221 | } |
| 1222 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | if (mddev->in_sync) |
| 1224 | { |
| 1225 | sb->recovery_cp = mddev->recovery_cp; |
| 1226 | sb->cp_events_hi = (mddev->events>>32); |
| 1227 | sb->cp_events_lo = (u32)mddev->events; |
| 1228 | if (mddev->recovery_cp == MaxSector) |
| 1229 | sb->state = (1<< MD_SB_CLEAN); |
| 1230 | } else |
| 1231 | sb->recovery_cp = 0; |
| 1232 | |
| 1233 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1234 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1236 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1237 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1240 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1242 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1243 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1244 | |
| 1245 | if (rdev2->raid_disk >= 0 && |
| 1246 | sb->minor_version >= 91) |
| 1247 | /* we have nowhere to store the recovery_offset, |
| 1248 | * but if it is not below the reshape_position, |
| 1249 | * we can piggy-back on that. |
| 1250 | */ |
| 1251 | is_active = 1; |
| 1252 | if (rdev2->raid_disk < 0 || |
| 1253 | test_bit(Faulty, &rdev2->flags)) |
| 1254 | is_active = 0; |
| 1255 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1256 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1258 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1259 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | d = &sb->disks[rdev2->desc_nr]; |
| 1261 | nr_disks++; |
| 1262 | d->number = rdev2->desc_nr; |
| 1263 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1264 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1265 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | d->raid_disk = rdev2->raid_disk; |
| 1267 | else |
| 1268 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1269 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1271 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1273 | if (test_bit(In_sync, &rdev2->flags)) |
| 1274 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | active++; |
| 1276 | working++; |
| 1277 | } else { |
| 1278 | d->state = 0; |
| 1279 | spare++; |
| 1280 | working++; |
| 1281 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1282 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1283 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1286 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1287 | mdp_disk_t *d = &sb->disks[i]; |
| 1288 | if (d->state == 0 && d->number == 0) { |
| 1289 | d->number = i; |
| 1290 | d->raid_disk = i; |
| 1291 | d->state = (1<<MD_DISK_REMOVED); |
| 1292 | d->state |= (1<<MD_DISK_FAULTY); |
| 1293 | failed++; |
| 1294 | } |
| 1295 | } |
| 1296 | sb->nr_disks = nr_disks; |
| 1297 | sb->active_disks = active; |
| 1298 | sb->working_disks = working; |
| 1299 | sb->failed_disks = failed; |
| 1300 | sb->spare_disks = spare; |
| 1301 | |
| 1302 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1303 | sb->sb_csum = calc_sb_csum(sb); |
| 1304 | } |
| 1305 | |
| 1306 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1307 | * rdev_size_change for 0.90.0 |
| 1308 | */ |
| 1309 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1310 | 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] | 1311 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1312 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1313 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1314 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1315 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1316 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1317 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1318 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1319 | /* Limit to 4TB as metadata cannot record more than that. |
| 1320 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1321 | */ |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1322 | if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1323 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1324 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1325 | rdev->sb_page); |
| 1326 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1327 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1328 | } |
| 1329 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1330 | static int |
| 1331 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1332 | { |
| 1333 | /* non-zero offset changes not possible with v0.90 */ |
| 1334 | return new_offset == 0; |
| 1335 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1336 | |
| 1337 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | * version 1 superblock |
| 1339 | */ |
| 1340 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1341 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1343 | __le32 disk_csum; |
| 1344 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | unsigned long long newcsum; |
| 1346 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1347 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | |
| 1349 | disk_csum = sb->sb_csum; |
| 1350 | sb->sb_csum = 0; |
| 1351 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1352 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | newcsum += le32_to_cpu(*isuper++); |
| 1354 | |
| 1355 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1356 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | |
| 1358 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1359 | sb->sb_csum = disk_csum; |
| 1360 | return cpu_to_le32(csum); |
| 1361 | } |
| 1362 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1363 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1364 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1365 | 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] | 1366 | { |
| 1367 | struct mdp_superblock_1 *sb; |
| 1368 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1369 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1370 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1372 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
| 1374 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1375 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | * It is always aligned to a 4K boundary and |
| 1377 | * depeding on minor_version, it can be: |
| 1378 | * 0: At least 8K, but less than 12K, from end of device |
| 1379 | * 1: At start of device |
| 1380 | * 2: 4K from start of device. |
| 1381 | */ |
| 1382 | switch(minor_version) { |
| 1383 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1384 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1385 | sb_start -= 8*2; |
| 1386 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | break; |
| 1388 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1389 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | break; |
| 1391 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1392 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | break; |
| 1394 | default: |
| 1395 | return -EINVAL; |
| 1396 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1397 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1399 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1400 | * and it is safe to read 4k, so we do that |
| 1401 | */ |
| 1402 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | if (ret) return ret; |
| 1404 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1405 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | |
| 1407 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1408 | sb->major_version != cpu_to_le32(1) || |
| 1409 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1410 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1411 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | return -EINVAL; |
| 1413 | |
| 1414 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1415 | printk("md: invalid superblock checksum on %s\n", |
| 1416 | bdevname(rdev->bdev,b)); |
| 1417 | return -EINVAL; |
| 1418 | } |
| 1419 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1420 | printk("md: data_size too small on %s\n", |
| 1421 | bdevname(rdev->bdev,b)); |
| 1422 | return -EINVAL; |
| 1423 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1424 | if (sb->pad0 || |
| 1425 | sb->pad3[0] || |
| 1426 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1427 | /* Some padding is non-zero, might be a new feature */ |
| 1428 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | rdev->preferred_minor = 0xffff; |
| 1431 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1432 | rdev->new_data_offset = rdev->data_offset; |
| 1433 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1434 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1435 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1436 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1438 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1439 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1440 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1441 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1442 | |
| 1443 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1444 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1445 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1446 | if (minor_version |
| 1447 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1448 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1449 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1450 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1451 | rdev->desc_nr = -1; |
| 1452 | else |
| 1453 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1454 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1455 | if (!rdev->bb_page) { |
| 1456 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1457 | if (!rdev->bb_page) |
| 1458 | return -ENOMEM; |
| 1459 | } |
| 1460 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1461 | rdev->badblocks.count == 0) { |
| 1462 | /* need to load the bad block list. |
| 1463 | * Currently we limit it to one page. |
| 1464 | */ |
| 1465 | s32 offset; |
| 1466 | sector_t bb_sector; |
| 1467 | u64 *bbp; |
| 1468 | int i; |
| 1469 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1470 | if (sectors > (PAGE_SIZE / 512)) |
| 1471 | return -EINVAL; |
| 1472 | offset = le32_to_cpu(sb->bblog_offset); |
| 1473 | if (offset == 0) |
| 1474 | return -EINVAL; |
| 1475 | bb_sector = (long long)offset; |
| 1476 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1477 | rdev->bb_page, READ, true)) |
| 1478 | return -EIO; |
| 1479 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1480 | rdev->badblocks.shift = sb->bblog_shift; |
| 1481 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1482 | u64 bb = le64_to_cpu(*bbp); |
| 1483 | int count = bb & (0x3ff); |
| 1484 | u64 sector = bb >> 10; |
| 1485 | sector <<= sb->bblog_shift; |
| 1486 | count <<= sb->bblog_shift; |
| 1487 | if (bb + 1 == 0) |
| 1488 | break; |
| 1489 | if (md_set_badblocks(&rdev->badblocks, |
| 1490 | sector, count, 1) == 0) |
| 1491 | return -EINVAL; |
| 1492 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1493 | } else if (sb->bblog_offset != 0) |
| 1494 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1495 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1496 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1497 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1498 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1500 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | |
| 1502 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1503 | sb->level != refsb->level || |
| 1504 | sb->layout != refsb->layout || |
| 1505 | sb->chunksize != refsb->chunksize) { |
| 1506 | printk(KERN_WARNING "md: %s has strangely different" |
| 1507 | " superblock to %s\n", |
| 1508 | bdevname(rdev->bdev,b), |
| 1509 | bdevname(refdev->bdev,b2)); |
| 1510 | return -EINVAL; |
| 1511 | } |
| 1512 | ev1 = le64_to_cpu(sb->events); |
| 1513 | ev2 = le64_to_cpu(refsb->events); |
| 1514 | |
| 1515 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1516 | ret = 1; |
| 1517 | else |
| 1518 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1520 | if (minor_version) { |
| 1521 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1522 | sectors -= rdev->data_offset; |
| 1523 | } else |
| 1524 | sectors = rdev->sb_start; |
| 1525 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1527 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1528 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1531 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1533 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1534 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1536 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1537 | clear_bit(Faulty, &rdev->flags); |
| 1538 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1539 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1540 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | if (mddev->raid_disks == 0) { |
| 1543 | mddev->major_version = 1; |
| 1544 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1545 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1546 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1548 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1549 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1550 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | mddev->layout = le32_to_cpu(sb->layout); |
| 1552 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1553 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1554 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1555 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1556 | mddev->bitmap_info.space = 0; |
| 1557 | /* Default location for bitmap is 1K after superblock |
| 1558 | * using 3K - total of 4K |
| 1559 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1560 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1561 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1562 | mddev->reshape_backwards = 0; |
| 1563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1565 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1566 | |
| 1567 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1568 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1569 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1570 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1571 | mddev->bitmap_info.offset = |
| 1572 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1573 | /* Metadata doesn't record how much space is available. |
| 1574 | * For 1.0, we assume we can use up to the superblock |
| 1575 | * if before, else to 4K beyond superblock. |
| 1576 | * For others, assume no change is possible. |
| 1577 | */ |
| 1578 | if (mddev->minor_version > 0) |
| 1579 | mddev->bitmap_info.space = 0; |
| 1580 | else if (mddev->bitmap_info.offset > 0) |
| 1581 | mddev->bitmap_info.space = |
| 1582 | 8 - mddev->bitmap_info.offset; |
| 1583 | else |
| 1584 | mddev->bitmap_info.space = |
| 1585 | -mddev->bitmap_info.offset; |
| 1586 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1587 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1588 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1589 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1590 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1591 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1592 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1593 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1594 | if (mddev->delta_disks < 0 || |
| 1595 | (mddev->delta_disks == 0 && |
| 1596 | (le32_to_cpu(sb->feature_map) |
| 1597 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1598 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1599 | } else { |
| 1600 | mddev->reshape_position = MaxSector; |
| 1601 | mddev->delta_disks = 0; |
| 1602 | mddev->new_level = mddev->level; |
| 1603 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1604 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1605 | } |
| 1606 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1607 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1608 | /* Insist of good event counter while assembling, except for |
| 1609 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1611 | if (rdev->desc_nr >= 0 && |
| 1612 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
Song Liu | a3dfbda | 2015-10-08 21:54:11 -0700 | [diff] [blame] | 1613 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1614 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1615 | if (ev1 < mddev->events) |
| 1616 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1617 | } else if (mddev->bitmap) { |
| 1618 | /* If adding to array with a bitmap, then we can accept an |
| 1619 | * older device, but not too old. |
| 1620 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1621 | if (ev1 < mddev->bitmap->events_cleared) |
| 1622 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1623 | if (ev1 < mddev->events) |
| 1624 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1625 | } else { |
| 1626 | if (ev1 < mddev->events) |
| 1627 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1628 | return 0; |
| 1629 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1631 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1632 | if (rdev->desc_nr < 0 || |
| 1633 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1634 | role = MD_DISK_ROLE_SPARE; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1635 | rdev->desc_nr = -1; |
| 1636 | } else |
| 1637 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | switch(role) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1639 | case MD_DISK_ROLE_SPARE: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | break; |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1641 | case MD_DISK_ROLE_FAULTY: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1642 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | break; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1644 | case MD_DISK_ROLE_JOURNAL: /* journal device */ |
| 1645 | if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) { |
| 1646 | /* journal device without journal feature */ |
| 1647 | printk(KERN_WARNING |
| 1648 | "md: journal device provided without journal feature, ignoring the device\n"); |
| 1649 | return -EINVAL; |
| 1650 | } |
| 1651 | set_bit(Journal, &rdev->flags); |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1652 | rdev->journal_tail = le64_to_cpu(sb->journal_tail); |
Shaohua Li | bd18f64 | 2015-09-02 13:49:50 -0700 | [diff] [blame] | 1653 | if (mddev->recovery_cp == MaxSector) |
| 1654 | set_bit(MD_JOURNAL_CLEAN, &mddev->flags); |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 1655 | rdev->raid_disk = mddev->raid_disks; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1656 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1658 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1659 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1660 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1661 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1662 | if (!(le32_to_cpu(sb->feature_map) & |
| 1663 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1664 | rdev->saved_raid_disk = -1; |
| 1665 | } else |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1666 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | rdev->raid_disk = role; |
| 1668 | break; |
| 1669 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1670 | if (sb->devflags & WriteMostly1) |
| 1671 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1672 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1673 | set_bit(Replacement, &rdev->flags); |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1674 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) |
| 1675 | set_bit(MD_HAS_JOURNAL, &mddev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1676 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1677 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | return 0; |
| 1680 | } |
| 1681 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1682 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | { |
| 1684 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1685 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | int max_dev, i; |
| 1687 | /* make rdev->sb match mddev and rdev data. */ |
| 1688 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1689 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | |
| 1691 | sb->feature_map = 0; |
| 1692 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1693 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1695 | |
| 1696 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1697 | sb->events = cpu_to_le64(mddev->events); |
| 1698 | if (mddev->in_sync) |
| 1699 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
Shaohua Li | bd18f64 | 2015-09-02 13:49:50 -0700 | [diff] [blame] | 1700 | else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags)) |
| 1701 | sb->resync_offset = cpu_to_le64(MaxSector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | else |
| 1703 | sb->resync_offset = cpu_to_le64(0); |
| 1704 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1705 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1706 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1707 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1708 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1709 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1710 | sb->level = cpu_to_le32(mddev->level); |
| 1711 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1712 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1713 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1714 | sb->devflags |= WriteMostly1; |
| 1715 | else |
| 1716 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1717 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1718 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1719 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1720 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1721 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1722 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1723 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1724 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 1725 | if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1726 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1727 | sb->feature_map |= |
| 1728 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1729 | sb->recovery_offset = |
| 1730 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1731 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 1732 | sb->feature_map |= |
| 1733 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1734 | } |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1735 | /* Note: recovery_offset and journal_tail share space */ |
| 1736 | if (test_bit(Journal, &rdev->flags)) |
| 1737 | sb->journal_tail = cpu_to_le64(rdev->journal_tail); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1738 | if (test_bit(Replacement, &rdev->flags)) |
| 1739 | sb->feature_map |= |
| 1740 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1741 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1742 | if (mddev->reshape_position != MaxSector) { |
| 1743 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1744 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1745 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1746 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1747 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1748 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1749 | if (mddev->delta_disks == 0 && |
| 1750 | mddev->reshape_backwards) |
| 1751 | sb->feature_map |
| 1752 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1753 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1754 | sb->feature_map |
| 1755 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1756 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1757 | - rdev->data_offset)); |
| 1758 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1759 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1760 | |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 1761 | if (mddev_is_clustered(mddev)) |
| 1762 | sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED); |
| 1763 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1764 | if (rdev->badblocks.count == 0) |
| 1765 | /* Nothing to do for bad blocks*/ ; |
| 1766 | else if (sb->bblog_offset == 0) |
| 1767 | /* Cannot record bad blocks on this device */ |
| 1768 | md_error(mddev, rdev); |
| 1769 | else { |
| 1770 | struct badblocks *bb = &rdev->badblocks; |
| 1771 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1772 | u64 *p = bb->page; |
| 1773 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1774 | if (bb->changed) { |
| 1775 | unsigned seq; |
| 1776 | |
| 1777 | retry: |
| 1778 | seq = read_seqbegin(&bb->lock); |
| 1779 | |
| 1780 | memset(bbp, 0xff, PAGE_SIZE); |
| 1781 | |
| 1782 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1783 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1784 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1785 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1786 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1787 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1788 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1789 | if (read_seqretry(&bb->lock, seq)) |
| 1790 | goto retry; |
| 1791 | |
| 1792 | bb->sector = (rdev->sb_start + |
| 1793 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1794 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1795 | } |
| 1796 | } |
| 1797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1799 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | if (rdev2->desc_nr+1 > max_dev) |
| 1801 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1802 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1803 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1804 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1805 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1806 | rdev->sb_size = max_dev * 2 + 256; |
| 1807 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1808 | if (rdev->sb_size & bmask) |
| 1809 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1810 | } else |
| 1811 | max_dev = le32_to_cpu(sb->max_dev); |
| 1812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | for (i=0; i<max_dev;i++) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1814 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1815 | |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1816 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) |
| 1817 | sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1819 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1821 | if (test_bit(Faulty, &rdev2->flags)) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1822 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1823 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1825 | else if (test_bit(Journal, &rdev2->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1826 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1827 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1828 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | else |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1830 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1834 | } |
| 1835 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1836 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1837 | 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] | 1838 | { |
| 1839 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1840 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1841 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1842 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1843 | if (rdev->data_offset != rdev->new_data_offset) |
| 1844 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1845 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1846 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1847 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1848 | max_sectors -= rdev->data_offset; |
| 1849 | if (!num_sectors || num_sectors > max_sectors) |
| 1850 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1851 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1852 | /* minor version 0 with bitmap we can't move */ |
| 1853 | return 0; |
| 1854 | } else { |
| 1855 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1856 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1857 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1858 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1859 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1860 | if (!num_sectors || num_sectors > max_sectors) |
| 1861 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1862 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1863 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1864 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1865 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1866 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1867 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1868 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1869 | rdev->sb_page); |
| 1870 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1871 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1872 | |
| 1873 | } |
| 1874 | |
| 1875 | static int |
| 1876 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1877 | unsigned long long new_offset) |
| 1878 | { |
| 1879 | /* All necessary checks on new >= old have been done */ |
| 1880 | struct bitmap *bitmap; |
| 1881 | if (new_offset >= rdev->data_offset) |
| 1882 | return 1; |
| 1883 | |
| 1884 | /* with 1.0 metadata, there is no metadata to tread on |
| 1885 | * so we can always move back */ |
| 1886 | if (rdev->mddev->minor_version == 0) |
| 1887 | return 1; |
| 1888 | |
| 1889 | /* otherwise we must be sure not to step on |
| 1890 | * any metadata, so stay: |
| 1891 | * 36K beyond start of superblock |
| 1892 | * beyond end of badblocks |
| 1893 | * beyond write-intent bitmap |
| 1894 | */ |
| 1895 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1896 | return 0; |
| 1897 | bitmap = rdev->mddev->bitmap; |
| 1898 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1899 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1900 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1901 | return 0; |
| 1902 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1903 | return 0; |
| 1904 | |
| 1905 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1906 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1908 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | [0] = { |
| 1910 | .name = "0.90.0", |
| 1911 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1912 | .load_super = super_90_load, |
| 1913 | .validate_super = super_90_validate, |
| 1914 | .sync_super = super_90_sync, |
| 1915 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1916 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | }, |
| 1918 | [1] = { |
| 1919 | .name = "md-1", |
| 1920 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1921 | .load_super = super_1_load, |
| 1922 | .validate_super = super_1_validate, |
| 1923 | .sync_super = super_1_sync, |
| 1924 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1925 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | }, |
| 1927 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1929 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1930 | { |
| 1931 | if (mddev->sync_super) { |
| 1932 | mddev->sync_super(mddev, rdev); |
| 1933 | return; |
| 1934 | } |
| 1935 | |
| 1936 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1937 | |
| 1938 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1939 | } |
| 1940 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1941 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1943 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1945 | rcu_read_lock(); |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 1946 | rdev_for_each_rcu(rdev, mddev1) { |
| 1947 | if (test_bit(Faulty, &rdev->flags) || |
| 1948 | test_bit(Journal, &rdev->flags) || |
| 1949 | rdev->raid_disk == -1) |
| 1950 | continue; |
| 1951 | rdev_for_each_rcu(rdev2, mddev2) { |
| 1952 | if (test_bit(Faulty, &rdev2->flags) || |
| 1953 | test_bit(Journal, &rdev2->flags) || |
| 1954 | rdev2->raid_disk == -1) |
| 1955 | continue; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1956 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1957 | rdev2->bdev->bd_contains) { |
| 1958 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1959 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1960 | } |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 1961 | } |
| 1962 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1963 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | return 0; |
| 1965 | } |
| 1966 | |
| 1967 | static LIST_HEAD(pending_raid_disks); |
| 1968 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1969 | /* |
| 1970 | * Try to register data integrity profile for an mddev |
| 1971 | * |
| 1972 | * This is called when an array is started and after a disk has been kicked |
| 1973 | * from the array. It only succeeds if all working and active component devices |
| 1974 | * are integrity capable with matching profiles. |
| 1975 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1976 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1977 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1978 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1979 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1980 | if (list_empty(&mddev->disks)) |
| 1981 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1982 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1983 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1984 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1985 | /* skip spares and non-functional disks */ |
| 1986 | if (test_bit(Faulty, &rdev->flags)) |
| 1987 | continue; |
| 1988 | if (rdev->raid_disk < 0) |
| 1989 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1990 | if (!reference) { |
| 1991 | /* Use the first rdev as the reference */ |
| 1992 | reference = rdev; |
| 1993 | continue; |
| 1994 | } |
| 1995 | /* does this rdev's profile match the reference profile? */ |
| 1996 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1997 | rdev->bdev->bd_disk) < 0) |
| 1998 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1999 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2000 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2001 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2002 | /* |
| 2003 | * All component devices are integrity capable and have matching |
| 2004 | * profiles, register the common profile for the md device. |
| 2005 | */ |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 2006 | blk_integrity_register(mddev->gendisk, |
| 2007 | bdev_get_integrity(reference->bdev)); |
| 2008 | |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2009 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 2010 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 2011 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 2012 | mdname(mddev)); |
| 2013 | return -EINVAL; |
| 2014 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2015 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2016 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2017 | EXPORT_SYMBOL(md_integrity_register); |
| 2018 | |
| 2019 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2020 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2021 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2022 | struct blk_integrity *bi_rdev; |
| 2023 | struct blk_integrity *bi_mddev; |
| 2024 | |
| 2025 | if (!mddev->gendisk) |
| 2026 | return; |
| 2027 | |
| 2028 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2029 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2030 | |
| 2031 | if (!bi_mddev) /* nothing to do */ |
| 2032 | return; |
| 2033 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2034 | return; |
| 2035 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2036 | rdev->bdev->bd_disk) >= 0) |
| 2037 | return; |
Dan Williams | c7bfced | 2015-10-21 13:20:02 -0400 | [diff] [blame] | 2038 | WARN_ON_ONCE(!mddev->suspended); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2039 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2040 | blk_integrity_unregister(mddev->gendisk); |
| 2041 | } |
| 2042 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2043 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2044 | 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] | 2045 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2046 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2047 | struct kobject *ko; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2048 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2050 | /* prevent duplicates */ |
| 2051 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2052 | return -EEXIST; |
| 2053 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2054 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2055 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2056 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2057 | if (mddev->pers) { |
| 2058 | /* Cannot change size, so fail |
| 2059 | * If mddev->level <= 0, then we don't care |
| 2060 | * about aligning sizes (e.g. linear) |
| 2061 | */ |
| 2062 | if (mddev->level > 0) |
| 2063 | return -ENOSPC; |
| 2064 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2065 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2066 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | |
| 2068 | /* Verify rdev->desc_nr is unique. |
| 2069 | * If it is -1, assign a free number, else |
| 2070 | * check number is not in use |
| 2071 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2072 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | if (rdev->desc_nr < 0) { |
| 2074 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2075 | if (mddev->pers) |
| 2076 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2077 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | choice++; |
| 2079 | rdev->desc_nr = choice; |
| 2080 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2081 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2082 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2084 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2086 | rcu_read_unlock(); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2087 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2088 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2089 | mdname(mddev), mddev->max_disks); |
| 2090 | return -EBUSY; |
| 2091 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2092 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2093 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2096 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2097 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2098 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2099 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2100 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2101 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2102 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2103 | /* failure here is OK */; |
| 2104 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2105 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2106 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2107 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2108 | |
| 2109 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2110 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2113 | |
| 2114 | fail: |
| 2115 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2116 | b, mdname(mddev)); |
| 2117 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | } |
| 2119 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2120 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2121 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2122 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2123 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2124 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2125 | } |
| 2126 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2127 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | { |
| 2129 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2130 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2131 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2132 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2134 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2135 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2136 | sysfs_put(rdev->sysfs_state); |
| 2137 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2138 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2139 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2140 | * writing to 'remove' to "dev/state". We also need |
| 2141 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2142 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2143 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2144 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2145 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2146 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | /* |
| 2150 | * prevent the device from being mounted, repartitioned or |
| 2151 | * otherwise reused by a RAID array (or any other kernel |
| 2152 | * subsystem), by bd_claiming the device. |
| 2153 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2154 | 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] | 2155 | { |
| 2156 | int err = 0; |
| 2157 | struct block_device *bdev; |
| 2158 | char b[BDEVNAME_SIZE]; |
| 2159 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2160 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2161 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | if (IS_ERR(bdev)) { |
| 2163 | printk(KERN_ERR "md: could not open %s.\n", |
| 2164 | __bdevname(dev, b)); |
| 2165 | return PTR_ERR(bdev); |
| 2166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | rdev->bdev = bdev; |
| 2168 | return err; |
| 2169 | } |
| 2170 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2171 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | { |
| 2173 | struct block_device *bdev = rdev->bdev; |
| 2174 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2175 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | void md_autodetect_dev(dev_t dev); |
| 2179 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2180 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | { |
| 2182 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2185 | bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2186 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2188 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2189 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | #endif |
| 2191 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2192 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | } |
| 2194 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2195 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | { |
| 2197 | unbind_rdev_from_array(rdev); |
| 2198 | export_rdev(rdev); |
| 2199 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2200 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2202 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2204 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2206 | while (!list_empty(&mddev->disks)) { |
| 2207 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2208 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2209 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | mddev->raid_disks = 0; |
| 2212 | mddev->major_version = 0; |
| 2213 | } |
| 2214 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2215 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2217 | /* Update each superblock (in-memory image), but |
| 2218 | * if we are allowed to, skip spares which already |
| 2219 | * have the right event counter, or have one earlier |
| 2220 | * (which would mean they aren't being marked as dirty |
| 2221 | * with the rest of the array) |
| 2222 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2223 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2224 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2225 | if (rdev->sb_events == mddev->events || |
| 2226 | (nospares && |
| 2227 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2228 | rdev->sb_events+1 == mddev->events)) { |
| 2229 | /* Don't update this superblock */ |
| 2230 | rdev->sb_loaded = 2; |
| 2231 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2232 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2233 | rdev->sb_loaded = 1; |
| 2234 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | } |
| 2236 | } |
| 2237 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2238 | static bool does_sb_need_changing(struct mddev *mddev) |
| 2239 | { |
| 2240 | struct md_rdev *rdev; |
| 2241 | struct mdp_superblock_1 *sb; |
| 2242 | int role; |
| 2243 | |
| 2244 | /* Find a good rdev */ |
| 2245 | rdev_for_each(rdev, mddev) |
| 2246 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) |
| 2247 | break; |
| 2248 | |
| 2249 | /* No good device found. */ |
| 2250 | if (!rdev) |
| 2251 | return false; |
| 2252 | |
| 2253 | sb = page_address(rdev->sb_page); |
| 2254 | /* Check if a device has become faulty or a spare become active */ |
| 2255 | rdev_for_each(rdev, mddev) { |
| 2256 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 2257 | /* Device activated? */ |
| 2258 | if (role == 0xffff && rdev->raid_disk >=0 && |
| 2259 | !test_bit(Faulty, &rdev->flags)) |
| 2260 | return true; |
| 2261 | /* Device turned faulty? */ |
| 2262 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) |
| 2263 | return true; |
| 2264 | } |
| 2265 | |
| 2266 | /* Check if any mddev parameters have changed */ |
| 2267 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || |
| 2268 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2269 | (mddev->layout != le64_to_cpu(sb->layout)) || |
| 2270 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || |
| 2271 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) |
| 2272 | return true; |
| 2273 | |
| 2274 | return false; |
| 2275 | } |
| 2276 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2277 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2279 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2280 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2281 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2282 | int any_badblocks_changed = 0; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2283 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2285 | if (mddev->ro) { |
| 2286 | if (force_change) |
| 2287 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2288 | return; |
| 2289 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2290 | |
| 2291 | if (mddev_is_clustered(mddev)) { |
| 2292 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2293 | force_change = 1; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2294 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2295 | /* Has someone else has updated the sb */ |
| 2296 | if (!does_sb_need_changing(mddev)) { |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2297 | if (ret == 0) |
| 2298 | md_cluster_ops->metadata_update_cancel(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2299 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2300 | return; |
| 2301 | } |
| 2302 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2304 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2305 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2306 | if (rdev->raid_disk >= 0 && |
| 2307 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2308 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2309 | !test_bit(In_sync, &rdev->flags) && |
| 2310 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2311 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2312 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2313 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2314 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2315 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2316 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2317 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2318 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2319 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2320 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2321 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2322 | md_ack_all_badblocks(&rdev->badblocks); |
| 2323 | md_error(mddev, rdev); |
| 2324 | } |
| 2325 | clear_bit(Blocked, &rdev->flags); |
| 2326 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2327 | wake_up(&rdev->blocked_wait); |
| 2328 | } |
| 2329 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2330 | wake_up(&mddev->sb_wait); |
| 2331 | return; |
| 2332 | } |
| 2333 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2334 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2335 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2336 | mddev->utime = get_seconds(); |
| 2337 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2338 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2339 | force_change = 1; |
| 2340 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2341 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2342 | * though if events isn't the right even/odd, we will have to do |
| 2343 | * spares after all |
| 2344 | */ |
| 2345 | nospares = 1; |
| 2346 | if (force_change) |
| 2347 | nospares = 0; |
| 2348 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2349 | /* If the array is degraded, then skipping spares is both |
| 2350 | * dangerous and fairly pointless. |
| 2351 | * Dangerous because a device that was removed from the array |
| 2352 | * might have a event_count that still looks up-to-date, |
| 2353 | * so it can be re-added without a resync. |
| 2354 | * Pointless because if there are any spares to skip, |
| 2355 | * then a recovery will happen and soon that array won't |
| 2356 | * be degraded any more and the spare can go back to sleep then. |
| 2357 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2358 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2359 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2360 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2361 | |
| 2362 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2363 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2364 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2365 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2366 | && mddev->can_decrease_events |
| 2367 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2368 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2369 | mddev->can_decrease_events = 0; |
| 2370 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2371 | /* otherwise we have to go forward and ... */ |
| 2372 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2373 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2376 | /* |
| 2377 | * This 64-bit counter should never wrap. |
| 2378 | * Either we are in around ~1 trillion A.C., assuming |
| 2379 | * 1 reboot per second, or we have a bug... |
| 2380 | */ |
| 2381 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2382 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2383 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2384 | if (rdev->badblocks.changed) |
| 2385 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2386 | if (test_bit(Faulty, &rdev->flags)) |
| 2387 | set_bit(FaultRecorded, &rdev->flags); |
| 2388 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2389 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2390 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2391 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2393 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2394 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2396 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2397 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2399 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2400 | if (rdev->sb_loaded != 1) |
| 2401 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2403 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2404 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2405 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2406 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2407 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2408 | bdevname(rdev->bdev, b), |
| 2409 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2410 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2411 | if (rdev->badblocks.size) { |
| 2412 | md_super_write(mddev, rdev, |
| 2413 | rdev->badblocks.sector, |
| 2414 | rdev->badblocks.size << 9, |
| 2415 | rdev->bb_page); |
| 2416 | rdev->badblocks.size = 0; |
| 2417 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2418 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2419 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2420 | pr_debug("md: %s (skipping faulty)\n", |
| 2421 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2422 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2423 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | /* only need to write one superblock... */ |
| 2425 | break; |
| 2426 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2427 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2428 | /* 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] | 2429 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2430 | spin_lock(&mddev->lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2431 | if (mddev->in_sync != sync_req || |
| 2432 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2433 | /* have to write it out again */ |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2434 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2435 | goto repeat; |
| 2436 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2437 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2438 | spin_unlock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2439 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2440 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2441 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2442 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2443 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2444 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2445 | clear_bit(Blocked, &rdev->flags); |
| 2446 | |
| 2447 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2448 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2449 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2450 | wake_up(&rdev->blocked_wait); |
| 2451 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2452 | |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2453 | if (mddev_is_clustered(mddev) && ret == 0) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2454 | md_cluster_ops->metadata_update_finish(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2456 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2458 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2459 | { |
| 2460 | struct mddev *mddev = rdev->mddev; |
| 2461 | int err = 0; |
| 2462 | |
| 2463 | if (!mddev->pers->hot_remove_disk) { |
| 2464 | /* If there is hot_add_disk but no hot_remove_disk |
| 2465 | * then added disks for geometry changes, |
| 2466 | * and should be added immediately. |
| 2467 | */ |
| 2468 | super_types[mddev->major_version]. |
| 2469 | validate_super(mddev, rdev); |
| 2470 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 2471 | if (err) { |
| 2472 | unbind_rdev_from_array(rdev); |
| 2473 | export_rdev(rdev); |
| 2474 | return err; |
| 2475 | } |
| 2476 | } |
| 2477 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2478 | |
| 2479 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2480 | if (mddev->degraded) |
| 2481 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2482 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2483 | md_new_event(mddev); |
| 2484 | md_wakeup_thread(mddev->thread); |
| 2485 | return 0; |
| 2486 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2487 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2488 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2489 | * We want to accept with case. For this we use cmd_match. |
| 2490 | */ |
| 2491 | static int cmd_match(const char *cmd, const char *str) |
| 2492 | { |
| 2493 | /* See if cmd, written into a sysfs file, matches |
| 2494 | * str. They must either be the same, or cmd can |
| 2495 | * have a trailing newline |
| 2496 | */ |
| 2497 | while (*cmd && *str && *cmd == *str) { |
| 2498 | cmd++; |
| 2499 | str++; |
| 2500 | } |
| 2501 | if (*cmd == '\n') |
| 2502 | cmd++; |
| 2503 | if (*str || *cmd) |
| 2504 | return 0; |
| 2505 | return 1; |
| 2506 | } |
| 2507 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2508 | struct rdev_sysfs_entry { |
| 2509 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2510 | ssize_t (*show)(struct md_rdev *, char *); |
| 2511 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2512 | }; |
| 2513 | |
| 2514 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2515 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2516 | { |
| 2517 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2518 | size_t len = 0; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2519 | unsigned long flags = ACCESS_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2520 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2521 | if (test_bit(Faulty, &flags) || |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2522 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2523 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2524 | sep = ","; |
| 2525 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2526 | if (test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2527 | len += sprintf(page+len, "%sin_sync",sep); |
| 2528 | sep = ","; |
| 2529 | } |
Shaohua Li | ac6096e | 2015-10-04 09:20:11 -0700 | [diff] [blame] | 2530 | if (test_bit(Journal, &flags)) { |
| 2531 | len += sprintf(page+len, "%sjournal",sep); |
| 2532 | sep = ","; |
| 2533 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2534 | if (test_bit(WriteMostly, &flags)) { |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2535 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2536 | sep = ","; |
| 2537 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2538 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2539 | (rdev->badblocks.unacked_exist |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2540 | && !test_bit(Faulty, &flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2541 | len += sprintf(page+len, "%sblocked", sep); |
| 2542 | sep = ","; |
| 2543 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2544 | if (!test_bit(Faulty, &flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2545 | !test_bit(Journal, &flags) && |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2546 | !test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2547 | len += sprintf(page+len, "%sspare", sep); |
| 2548 | sep = ","; |
| 2549 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2550 | if (test_bit(WriteErrorSeen, &flags)) { |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2551 | len += sprintf(page+len, "%swrite_error", sep); |
| 2552 | sep = ","; |
| 2553 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2554 | if (test_bit(WantReplacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2555 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2556 | sep = ","; |
| 2557 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2558 | if (test_bit(Replacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2559 | len += sprintf(page+len, "%sreplacement", sep); |
| 2560 | sep = ","; |
| 2561 | } |
| 2562 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2563 | return len+sprintf(page+len, "\n"); |
| 2564 | } |
| 2565 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2566 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2567 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2568 | { |
| 2569 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2570 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2571 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2572 | * writemostly - sets write_mostly |
| 2573 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2574 | * blocked - sets the Blocked flags |
| 2575 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2576 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2577 | * -insync - clear Insync for a device with a slot assigned, |
| 2578 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2579 | * write_error - sets WriteErrorSeen |
| 2580 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2581 | */ |
| 2582 | int err = -EINVAL; |
| 2583 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2584 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2585 | if (test_bit(Faulty, &rdev->flags)) |
| 2586 | err = 0; |
| 2587 | else |
| 2588 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2589 | } else if (cmd_match(buf, "remove")) { |
| 2590 | if (rdev->raid_disk >= 0) |
| 2591 | err = -EBUSY; |
| 2592 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2593 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2594 | err = 0; |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 2595 | if (mddev_is_clustered(mddev)) |
| 2596 | err = md_cluster_ops->remove_disk(mddev, rdev); |
| 2597 | |
| 2598 | if (err == 0) { |
| 2599 | md_kick_rdev_from_array(rdev); |
| 2600 | if (mddev->pers) |
| 2601 | md_update_sb(mddev, 1); |
| 2602 | md_new_event(mddev); |
| 2603 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2604 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2605 | } else if (cmd_match(buf, "writemostly")) { |
| 2606 | set_bit(WriteMostly, &rdev->flags); |
| 2607 | err = 0; |
| 2608 | } else if (cmd_match(buf, "-writemostly")) { |
| 2609 | clear_bit(WriteMostly, &rdev->flags); |
| 2610 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2611 | } else if (cmd_match(buf, "blocked")) { |
| 2612 | set_bit(Blocked, &rdev->flags); |
| 2613 | err = 0; |
| 2614 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2615 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2616 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2617 | /* metadata handler doesn't understand badblocks, |
| 2618 | * so we need to fail the device |
| 2619 | */ |
| 2620 | md_error(rdev->mddev, rdev); |
| 2621 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2622 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2623 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2624 | wake_up(&rdev->blocked_wait); |
| 2625 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2626 | md_wakeup_thread(rdev->mddev->thread); |
| 2627 | |
| 2628 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2629 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2630 | set_bit(In_sync, &rdev->flags); |
| 2631 | err = 0; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2632 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 && |
| 2633 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 2634 | if (rdev->mddev->pers == NULL) { |
| 2635 | clear_bit(In_sync, &rdev->flags); |
| 2636 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2637 | rdev->raid_disk = -1; |
| 2638 | err = 0; |
| 2639 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2640 | } else if (cmd_match(buf, "write_error")) { |
| 2641 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2642 | err = 0; |
| 2643 | } else if (cmd_match(buf, "-write_error")) { |
| 2644 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2645 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2646 | } else if (cmd_match(buf, "want_replacement")) { |
| 2647 | /* Any non-spare device that is not a replacement can |
| 2648 | * become want_replacement at any time, but we then need to |
| 2649 | * check if recovery is needed. |
| 2650 | */ |
| 2651 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2652 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2653 | !test_bit(Replacement, &rdev->flags)) |
| 2654 | set_bit(WantReplacement, &rdev->flags); |
| 2655 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2656 | md_wakeup_thread(rdev->mddev->thread); |
| 2657 | err = 0; |
| 2658 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2659 | /* Clearing 'want_replacement' is always allowed. |
| 2660 | * Once replacements starts it is too late though. |
| 2661 | */ |
| 2662 | err = 0; |
| 2663 | clear_bit(WantReplacement, &rdev->flags); |
| 2664 | } else if (cmd_match(buf, "replacement")) { |
| 2665 | /* Can only set a device as a replacement when array has not |
| 2666 | * yet been started. Once running, replacement is automatic |
| 2667 | * from spares, or by assigning 'slot'. |
| 2668 | */ |
| 2669 | if (rdev->mddev->pers) |
| 2670 | err = -EBUSY; |
| 2671 | else { |
| 2672 | set_bit(Replacement, &rdev->flags); |
| 2673 | err = 0; |
| 2674 | } |
| 2675 | } else if (cmd_match(buf, "-replacement")) { |
| 2676 | /* Similarly, can only clear Replacement before start */ |
| 2677 | if (rdev->mddev->pers) |
| 2678 | err = -EBUSY; |
| 2679 | else { |
| 2680 | clear_bit(Replacement, &rdev->flags); |
| 2681 | err = 0; |
| 2682 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2683 | } else if (cmd_match(buf, "re-add")) { |
| 2684 | if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 2685 | /* clear_bit is performed _after_ all the devices |
| 2686 | * have their local Faulty bit cleared. If any writes |
| 2687 | * happen in the meantime in the local node, they |
| 2688 | * will land in the local bitmap, which will be synced |
| 2689 | * by this node eventually |
| 2690 | */ |
| 2691 | if (!mddev_is_clustered(rdev->mddev) || |
| 2692 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 2693 | clear_bit(Faulty, &rdev->flags); |
| 2694 | err = add_bound_rdev(rdev); |
| 2695 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2696 | } else |
| 2697 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2698 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2699 | if (!err) |
| 2700 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2701 | return err ? err : len; |
| 2702 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2703 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 2704 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2705 | |
| 2706 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2707 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2708 | { |
| 2709 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2710 | } |
| 2711 | |
| 2712 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2713 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2714 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2715 | unsigned int n; |
| 2716 | int rv; |
| 2717 | |
| 2718 | rv = kstrtouint(buf, 10, &n); |
| 2719 | if (rv < 0) |
| 2720 | return rv; |
| 2721 | atomic_set(&rdev->corrected_errors, n); |
| 2722 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2723 | } |
| 2724 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2725 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2726 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2727 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2728 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2729 | { |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2730 | if (test_bit(Journal, &rdev->flags)) |
| 2731 | return sprintf(page, "journal\n"); |
| 2732 | else if (rdev->raid_disk < 0) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2733 | return sprintf(page, "none\n"); |
| 2734 | else |
| 2735 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2736 | } |
| 2737 | |
| 2738 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2739 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2740 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2741 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2742 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2743 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2744 | if (test_bit(Journal, &rdev->flags)) |
| 2745 | return -EBUSY; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2746 | if (strncmp(buf, "none", 4)==0) |
| 2747 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2748 | else { |
| 2749 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 2750 | if (err < 0) |
| 2751 | return err; |
| 2752 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2753 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2754 | /* Setting 'slot' on an active array requires also |
| 2755 | * updating the 'rd%d' link, and communicating |
| 2756 | * with the personality with ->hot_*_disk. |
| 2757 | * For now we only support removing |
| 2758 | * failed/spare devices. This normally happens automatically, |
| 2759 | * but not when the metadata is externally managed. |
| 2760 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2761 | if (rdev->raid_disk == -1) |
| 2762 | return -EEXIST; |
| 2763 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2764 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2765 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2766 | clear_bit(Blocked, &rdev->flags); |
| 2767 | remove_and_add_spares(rdev->mddev, rdev); |
| 2768 | if (rdev->raid_disk >= 0) |
| 2769 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2770 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2771 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2772 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2773 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2774 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2775 | */ |
| 2776 | |
| 2777 | if (rdev->raid_disk != -1) |
| 2778 | return -EBUSY; |
| 2779 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2780 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2781 | return -EBUSY; |
| 2782 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2783 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2784 | return -EINVAL; |
| 2785 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2786 | if (slot >= rdev->mddev->raid_disks && |
| 2787 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2788 | return -ENOSPC; |
| 2789 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2790 | rdev->raid_disk = slot; |
| 2791 | if (test_bit(In_sync, &rdev->flags)) |
| 2792 | rdev->saved_raid_disk = slot; |
| 2793 | else |
| 2794 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2795 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 2796 | clear_bit(Bitmap_sync, &rdev->flags); |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 2797 | remove_and_add_spares(rdev->mddev, rdev); |
| 2798 | if (rdev->raid_disk == -1) |
| 2799 | return -EBUSY; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2800 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2801 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2802 | if (slot >= rdev->mddev->raid_disks && |
| 2803 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2804 | return -ENOSPC; |
| 2805 | rdev->raid_disk = slot; |
| 2806 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2807 | clear_bit(Faulty, &rdev->flags); |
| 2808 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2809 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2810 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2811 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2812 | return len; |
| 2813 | } |
| 2814 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2815 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2816 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2817 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2818 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2819 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2820 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2821 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2822 | } |
| 2823 | |
| 2824 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2825 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2826 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2827 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2828 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2829 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2830 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2831 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2832 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2833 | /* Must set offset before size, so overlap checks |
| 2834 | * can be sane */ |
| 2835 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2836 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2837 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2838 | return len; |
| 2839 | } |
| 2840 | |
| 2841 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2842 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2843 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2844 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2845 | { |
| 2846 | return sprintf(page, "%llu\n", |
| 2847 | (unsigned long long)rdev->new_data_offset); |
| 2848 | } |
| 2849 | |
| 2850 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2851 | const char *buf, size_t len) |
| 2852 | { |
| 2853 | unsigned long long new_offset; |
| 2854 | struct mddev *mddev = rdev->mddev; |
| 2855 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2856 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2857 | return -EINVAL; |
| 2858 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 2859 | if (mddev->sync_thread || |
| 2860 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2861 | return -EBUSY; |
| 2862 | if (new_offset == rdev->data_offset) |
| 2863 | /* reset is always permitted */ |
| 2864 | ; |
| 2865 | else if (new_offset > rdev->data_offset) { |
| 2866 | /* must not push array size beyond rdev_sectors */ |
| 2867 | if (new_offset - rdev->data_offset |
| 2868 | + mddev->dev_sectors > rdev->sectors) |
| 2869 | return -E2BIG; |
| 2870 | } |
| 2871 | /* Metadata worries about other space details. */ |
| 2872 | |
| 2873 | /* decreasing the offset is inconsistent with a backwards |
| 2874 | * reshape. |
| 2875 | */ |
| 2876 | if (new_offset < rdev->data_offset && |
| 2877 | mddev->reshape_backwards) |
| 2878 | return -EINVAL; |
| 2879 | /* Increasing offset is inconsistent with forwards |
| 2880 | * reshape. reshape_direction should be set to |
| 2881 | * 'backwards' first. |
| 2882 | */ |
| 2883 | if (new_offset > rdev->data_offset && |
| 2884 | !mddev->reshape_backwards) |
| 2885 | return -EINVAL; |
| 2886 | |
| 2887 | if (mddev->pers && mddev->persistent && |
| 2888 | !super_types[mddev->major_version] |
| 2889 | .allow_new_offset(rdev, new_offset)) |
| 2890 | return -E2BIG; |
| 2891 | rdev->new_data_offset = new_offset; |
| 2892 | if (new_offset > rdev->data_offset) |
| 2893 | mddev->reshape_backwards = 1; |
| 2894 | else if (new_offset < rdev->data_offset) |
| 2895 | mddev->reshape_backwards = 0; |
| 2896 | |
| 2897 | return len; |
| 2898 | } |
| 2899 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2900 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2901 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2902 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2903 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2904 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2905 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2906 | } |
| 2907 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2908 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2909 | { |
| 2910 | /* check if two start/length pairs overlap */ |
| 2911 | if (s1+l1 <= s2) |
| 2912 | return 0; |
| 2913 | if (s2+l2 <= s1) |
| 2914 | return 0; |
| 2915 | return 1; |
| 2916 | } |
| 2917 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2918 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2919 | { |
| 2920 | unsigned long long blocks; |
| 2921 | sector_t new; |
| 2922 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2923 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2924 | return -EINVAL; |
| 2925 | |
| 2926 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2927 | return -EINVAL; /* sector conversion overflow */ |
| 2928 | |
| 2929 | new = blocks * 2; |
| 2930 | if (new != blocks * 2) |
| 2931 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2932 | |
| 2933 | *sectors = new; |
| 2934 | return 0; |
| 2935 | } |
| 2936 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2937 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2938 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2939 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2940 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2941 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2942 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2943 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2944 | if (test_bit(Journal, &rdev->flags)) |
| 2945 | return -EBUSY; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2946 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2947 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2948 | if (rdev->data_offset != rdev->new_data_offset) |
| 2949 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2950 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2951 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2952 | sectors = super_types[my_mddev->major_version]. |
| 2953 | rdev_size_change(rdev, sectors); |
| 2954 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2955 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2956 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2957 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2958 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 2959 | if (!my_mddev->pers->resize) |
| 2960 | /* Cannot change size for RAID0 or Linear etc */ |
| 2961 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2962 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2963 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2964 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2965 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2966 | rdev->sectors = sectors; |
| 2967 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2968 | /* Need to check that all other rdevs with the same |
| 2969 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 2970 | * the rdev lists safely. |
| 2971 | * This check does not provide a hard guarantee, it |
| 2972 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2973 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2974 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2975 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2976 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2977 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2978 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2979 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2980 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2981 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2982 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2983 | if (rdev->bdev == rdev2->bdev && |
| 2984 | rdev != rdev2 && |
| 2985 | overlaps(rdev->data_offset, rdev->sectors, |
| 2986 | rdev2->data_offset, |
| 2987 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2988 | overlap = 1; |
| 2989 | break; |
| 2990 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2991 | if (overlap) { |
| 2992 | mddev_put(mddev); |
| 2993 | break; |
| 2994 | } |
| 2995 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2996 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2997 | if (overlap) { |
| 2998 | /* Someone else could have slipped in a size |
| 2999 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3000 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3001 | * safe, and trust userspace not to race with |
| 3002 | * itself |
| 3003 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3004 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3005 | return -EBUSY; |
| 3006 | } |
| 3007 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3008 | return len; |
| 3009 | } |
| 3010 | |
| 3011 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3012 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3013 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3014 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3015 | { |
| 3016 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3017 | |
| 3018 | if (test_bit(In_sync, &rdev->flags) || |
| 3019 | recovery_start == MaxSector) |
| 3020 | return sprintf(page, "none\n"); |
| 3021 | |
| 3022 | return sprintf(page, "%llu\n", recovery_start); |
| 3023 | } |
| 3024 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3025 | 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] | 3026 | { |
| 3027 | unsigned long long recovery_start; |
| 3028 | |
| 3029 | if (cmd_match(buf, "none")) |
| 3030 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3031 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3032 | return -EINVAL; |
| 3033 | |
| 3034 | if (rdev->mddev->pers && |
| 3035 | rdev->raid_disk >= 0) |
| 3036 | return -EBUSY; |
| 3037 | |
| 3038 | rdev->recovery_offset = recovery_start; |
| 3039 | if (recovery_start == MaxSector) |
| 3040 | set_bit(In_sync, &rdev->flags); |
| 3041 | else |
| 3042 | clear_bit(In_sync, &rdev->flags); |
| 3043 | return len; |
| 3044 | } |
| 3045 | |
| 3046 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3047 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3048 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3049 | static ssize_t |
| 3050 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 3051 | static ssize_t |
| 3052 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 3053 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3054 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3055 | { |
| 3056 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3057 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3058 | 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] | 3059 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3060 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3061 | /* Maybe that ack was all we needed */ |
| 3062 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3063 | wake_up(&rdev->blocked_wait); |
| 3064 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3065 | } |
| 3066 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3067 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3068 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3069 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3070 | { |
| 3071 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3072 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3073 | 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] | 3074 | { |
| 3075 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3076 | } |
| 3077 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3078 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3079 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3080 | static struct attribute *rdev_default_attrs[] = { |
| 3081 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3082 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3083 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3084 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3085 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3086 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3087 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3088 | &rdev_bad_blocks.attr, |
| 3089 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3090 | NULL, |
| 3091 | }; |
| 3092 | static ssize_t |
| 3093 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3094 | { |
| 3095 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3096 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3097 | |
| 3098 | if (!entry->show) |
| 3099 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3100 | if (!rdev->mddev) |
| 3101 | return -EBUSY; |
| 3102 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | static ssize_t |
| 3106 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3107 | const char *page, size_t length) |
| 3108 | { |
| 3109 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3110 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3111 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3112 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3113 | |
| 3114 | if (!entry->store) |
| 3115 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3116 | if (!capable(CAP_SYS_ADMIN)) |
| 3117 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3118 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3119 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3120 | if (rdev->mddev == NULL) |
| 3121 | rv = -EBUSY; |
| 3122 | else |
| 3123 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3124 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3125 | } |
| 3126 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3127 | } |
| 3128 | |
| 3129 | static void rdev_free(struct kobject *ko) |
| 3130 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3131 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3132 | kfree(rdev); |
| 3133 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3134 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3135 | .show = rdev_attr_show, |
| 3136 | .store = rdev_attr_store, |
| 3137 | }; |
| 3138 | static struct kobj_type rdev_ktype = { |
| 3139 | .release = rdev_free, |
| 3140 | .sysfs_ops = &rdev_sysfs_ops, |
| 3141 | .default_attrs = rdev_default_attrs, |
| 3142 | }; |
| 3143 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3144 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3145 | { |
| 3146 | rdev->desc_nr = -1; |
| 3147 | rdev->saved_raid_disk = -1; |
| 3148 | rdev->raid_disk = -1; |
| 3149 | rdev->flags = 0; |
| 3150 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3151 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3152 | rdev->sb_events = 0; |
| 3153 | rdev->last_read_error.tv_sec = 0; |
| 3154 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3155 | rdev->sb_loaded = 0; |
| 3156 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3157 | atomic_set(&rdev->nr_pending, 0); |
| 3158 | atomic_set(&rdev->read_errors, 0); |
| 3159 | atomic_set(&rdev->corrected_errors, 0); |
| 3160 | |
| 3161 | INIT_LIST_HEAD(&rdev->same_set); |
| 3162 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3163 | |
| 3164 | /* Add space to store bad block list. |
| 3165 | * This reserves the space even on arrays where it cannot |
| 3166 | * be used - I wonder if that matters |
| 3167 | */ |
| 3168 | rdev->badblocks.count = 0; |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 3169 | rdev->badblocks.shift = -1; /* disabled until explicitly enabled */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3170 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3171 | seqlock_init(&rdev->badblocks.lock); |
| 3172 | if (rdev->badblocks.page == NULL) |
| 3173 | return -ENOMEM; |
| 3174 | |
| 3175 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3176 | } |
| 3177 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | /* |
| 3179 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3180 | * |
| 3181 | * mark the device faulty if: |
| 3182 | * |
| 3183 | * - the device is nonexistent (zero size) |
| 3184 | * - the device has no valid superblock |
| 3185 | * |
| 3186 | * a faulty rdev _never_ has rdev->sb set. |
| 3187 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3188 | 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] | 3189 | { |
| 3190 | char b[BDEVNAME_SIZE]; |
| 3191 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3192 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | sector_t size; |
| 3194 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3195 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3196 | if (!rdev) { |
| 3197 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3198 | return ERR_PTR(-ENOMEM); |
| 3199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3201 | err = md_rdev_init(rdev); |
| 3202 | if (err) |
| 3203 | goto abort_free; |
| 3204 | err = alloc_disk_sb(rdev); |
| 3205 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | goto abort_free; |
| 3207 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3208 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | if (err) |
| 3210 | goto abort_free; |
| 3211 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3212 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3213 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3214 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3215 | if (!size) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3216 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3218 | bdevname(rdev->bdev,b)); |
| 3219 | err = -EINVAL; |
| 3220 | goto abort_free; |
| 3221 | } |
| 3222 | |
| 3223 | if (super_format >= 0) { |
| 3224 | err = super_types[super_format]. |
| 3225 | load_super(rdev, NULL, super_minor); |
| 3226 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3227 | printk(KERN_WARNING |
| 3228 | "md: %s does not have a valid v%d.%d " |
| 3229 | "superblock, not importing!\n", |
| 3230 | bdevname(rdev->bdev,b), |
| 3231 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | goto abort_free; |
| 3233 | } |
| 3234 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3235 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | "md: could not read %s's sb, not importing!\n", |
| 3237 | bdevname(rdev->bdev,b)); |
| 3238 | goto abort_free; |
| 3239 | } |
| 3240 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3242 | return rdev; |
| 3243 | |
| 3244 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3245 | if (rdev->bdev) |
| 3246 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3247 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | kfree(rdev); |
| 3249 | return ERR_PTR(err); |
| 3250 | } |
| 3251 | |
| 3252 | /* |
| 3253 | * Check a full RAID array for plausibility |
| 3254 | */ |
| 3255 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3256 | static void analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | { |
| 3258 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3259 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | char b[BDEVNAME_SIZE]; |
| 3261 | |
| 3262 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3263 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | switch (super_types[mddev->major_version]. |
| 3265 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3266 | case 1: |
| 3267 | freshest = rdev; |
| 3268 | break; |
| 3269 | case 0: |
| 3270 | break; |
| 3271 | default: |
| 3272 | printk( KERN_ERR \ |
| 3273 | "md: fatal superblock inconsistency in %s" |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3274 | " -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3276 | md_kick_rdev_from_array(rdev); |
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 | super_types[mddev->major_version]. |
| 3280 | validate_super(mddev, freshest); |
| 3281 | |
| 3282 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3283 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3284 | if (mddev->max_disks && |
| 3285 | (rdev->desc_nr >= mddev->max_disks || |
| 3286 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3287 | printk(KERN_WARNING |
| 3288 | "md: %s: %s: only %d devices permitted\n", |
| 3289 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3290 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3291 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3292 | continue; |
| 3293 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3294 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3295 | if (super_types[mddev->major_version]. |
| 3296 | validate_super(mddev, rdev)) { |
| 3297 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3298 | " from array!\n", |
| 3299 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3300 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3301 | continue; |
| 3302 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3303 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3305 | rdev->desc_nr = i++; |
| 3306 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3307 | set_bit(In_sync, &rdev->flags); |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3308 | } else if (rdev->raid_disk >= |
| 3309 | (mddev->raid_disks - min(0, mddev->delta_disks)) && |
| 3310 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3311 | rdev->raid_disk = -1; |
| 3312 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3313 | } |
| 3314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | } |
| 3316 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3317 | /* Read a fixed-point number. |
| 3318 | * Numbers in sysfs attributes should be in "standard" units where |
| 3319 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3320 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3321 | * milliseconds or jiffies. |
| 3322 | * This function takes a decimal number with a possible fractional |
| 3323 | * component, and produces an integer which is the result of |
| 3324 | * multiplying that number by 10^'scale'. |
| 3325 | * all without any floating-point arithmetic. |
| 3326 | */ |
| 3327 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3328 | { |
| 3329 | unsigned long result = 0; |
| 3330 | long decimals = -1; |
| 3331 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3332 | if (*cp == '.') |
| 3333 | decimals = 0; |
| 3334 | else if (decimals < scale) { |
| 3335 | unsigned int value; |
| 3336 | value = *cp - '0'; |
| 3337 | result = result * 10 + value; |
| 3338 | if (decimals >= 0) |
| 3339 | decimals++; |
| 3340 | } |
| 3341 | cp++; |
| 3342 | } |
| 3343 | if (*cp == '\n') |
| 3344 | cp++; |
| 3345 | if (*cp) |
| 3346 | return -EINVAL; |
| 3347 | if (decimals < 0) |
| 3348 | decimals = 0; |
| 3349 | while (decimals < scale) { |
| 3350 | result *= 10; |
| 3351 | decimals ++; |
| 3352 | } |
| 3353 | *res = result; |
| 3354 | return 0; |
| 3355 | } |
| 3356 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3357 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3358 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3359 | { |
| 3360 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3361 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3362 | } |
| 3363 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3364 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3365 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3366 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3367 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3368 | if (mddev_is_clustered(mddev)) { |
| 3369 | pr_info("md: Safemode is disabled for clustered mode\n"); |
| 3370 | return -EINVAL; |
| 3371 | } |
| 3372 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3373 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3374 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3375 | if (msec == 0) |
| 3376 | mddev->safemode_delay = 0; |
| 3377 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3378 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3379 | unsigned long new_delay = (msec*HZ)/1000; |
| 3380 | |
| 3381 | if (new_delay == 0) |
| 3382 | new_delay = 1; |
| 3383 | mddev->safemode_delay = new_delay; |
| 3384 | if (new_delay < old_delay || old_delay == 0) |
| 3385 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3386 | } |
| 3387 | return len; |
| 3388 | } |
| 3389 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3390 | __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] | 3391 | |
| 3392 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3393 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3394 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3395 | struct md_personality *p; |
| 3396 | int ret; |
| 3397 | spin_lock(&mddev->lock); |
| 3398 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3399 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3400 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3401 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3402 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3403 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3404 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3405 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3406 | ret = 0; |
| 3407 | spin_unlock(&mddev->lock); |
| 3408 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3409 | } |
| 3410 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3411 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3412 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3413 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3414 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3415 | ssize_t rv; |
| 3416 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3417 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3418 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3419 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3420 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3421 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3422 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3423 | return -EINVAL; |
| 3424 | |
| 3425 | rv = mddev_lock(mddev); |
| 3426 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3427 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3428 | |
| 3429 | if (mddev->pers == NULL) { |
| 3430 | strncpy(mddev->clevel, buf, slen); |
| 3431 | if (mddev->clevel[slen-1] == '\n') |
| 3432 | slen--; |
| 3433 | mddev->clevel[slen] = 0; |
| 3434 | mddev->level = LEVEL_NONE; |
| 3435 | rv = len; |
| 3436 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3437 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3438 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3439 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3440 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3441 | |
| 3442 | /* request to change the personality. Need to ensure: |
| 3443 | * - array is not engaged in resync/recovery/reshape |
| 3444 | * - old personality can be suspended |
| 3445 | * - new personality will access other array. |
| 3446 | */ |
| 3447 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3448 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3449 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3450 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3451 | mddev->reshape_position != MaxSector || |
| 3452 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3453 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3454 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3455 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3456 | if (!mddev->pers->quiesce) { |
| 3457 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3458 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3459 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3460 | } |
| 3461 | |
| 3462 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3463 | strncpy(clevel, buf, slen); |
| 3464 | if (clevel[slen-1] == '\n') |
| 3465 | slen--; |
| 3466 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3467 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3468 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3469 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3470 | if (request_module("md-%s", clevel) != 0) |
| 3471 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3472 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3473 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3474 | if (!pers || !try_module_get(pers->owner)) { |
| 3475 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3476 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3477 | rv = -EINVAL; |
| 3478 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3479 | } |
| 3480 | spin_unlock(&pers_lock); |
| 3481 | |
| 3482 | if (pers == mddev->pers) { |
| 3483 | /* Nothing to do! */ |
| 3484 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3485 | rv = len; |
| 3486 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3487 | } |
| 3488 | if (!pers->takeover) { |
| 3489 | module_put(pers->owner); |
| 3490 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3491 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3492 | rv = -EINVAL; |
| 3493 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3494 | } |
| 3495 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3496 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3497 | rdev->new_raid_disk = rdev->raid_disk; |
| 3498 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3499 | /* ->takeover must set new_* and/or delta_disks |
| 3500 | * if it succeeds, and may set them when it fails. |
| 3501 | */ |
| 3502 | priv = pers->takeover(mddev); |
| 3503 | if (IS_ERR(priv)) { |
| 3504 | mddev->new_level = mddev->level; |
| 3505 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3506 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3507 | mddev->raid_disks -= mddev->delta_disks; |
| 3508 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3509 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3510 | module_put(pers->owner); |
| 3511 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3512 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3513 | rv = PTR_ERR(priv); |
| 3514 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | /* Looks like we have a winner */ |
| 3518 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 3519 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3520 | |
| 3521 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3522 | oldpers = mddev->pers; |
| 3523 | oldpriv = mddev->private; |
| 3524 | mddev->pers = pers; |
| 3525 | mddev->private = priv; |
| 3526 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3527 | mddev->level = mddev->new_level; |
| 3528 | mddev->layout = mddev->new_layout; |
| 3529 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 3530 | mddev->delta_disks = 0; |
| 3531 | mddev->reshape_backwards = 0; |
| 3532 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3533 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3534 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3535 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3536 | mddev->external) { |
| 3537 | /* We are converting from a no-redundancy array |
| 3538 | * to a redundancy array and metadata is managed |
| 3539 | * externally so we need to be sure that writes |
| 3540 | * won't block due to a need to transition |
| 3541 | * clean->dirty |
| 3542 | * until external management is started. |
| 3543 | */ |
| 3544 | mddev->in_sync = 0; |
| 3545 | mddev->safemode_delay = 0; |
| 3546 | mddev->safemode = 0; |
| 3547 | } |
| 3548 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3549 | oldpers->free(mddev, oldpriv); |
| 3550 | |
| 3551 | if (oldpers->sync_request == NULL && |
| 3552 | pers->sync_request != NULL) { |
| 3553 | /* need to add the md_redundancy_group */ |
| 3554 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3555 | printk(KERN_WARNING |
| 3556 | "md: cannot register extra attributes for %s\n", |
| 3557 | mdname(mddev)); |
| 3558 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3559 | } |
| 3560 | if (oldpers->sync_request != NULL && |
| 3561 | pers->sync_request == NULL) { |
| 3562 | /* need to remove the md_redundancy_group */ |
| 3563 | if (mddev->to_remove == NULL) |
| 3564 | mddev->to_remove = &md_redundancy_group; |
| 3565 | } |
| 3566 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3567 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3568 | if (rdev->raid_disk < 0) |
| 3569 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3570 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3571 | rdev->new_raid_disk = -1; |
| 3572 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3573 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3574 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3575 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3576 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3577 | if (rdev->raid_disk < 0) |
| 3578 | continue; |
| 3579 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3580 | continue; |
| 3581 | rdev->raid_disk = rdev->new_raid_disk; |
| 3582 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3583 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3584 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3585 | if (sysfs_link_rdev(mddev, rdev)) |
| 3586 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3587 | " for %s after level change\n", |
| 3588 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3589 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3590 | } |
| 3591 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3592 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3593 | /* this is now an array without redundancy, so |
| 3594 | * it must always be in_sync |
| 3595 | */ |
| 3596 | mddev->in_sync = 1; |
| 3597 | del_timer_sync(&mddev->safemode_timer); |
| 3598 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 3599 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3600 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3601 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3602 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 3603 | if (!mddev->thread) |
| 3604 | md_update_sb(mddev, 1); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3605 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3606 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3607 | rv = len; |
| 3608 | out_unlock: |
| 3609 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3610 | return rv; |
| 3611 | } |
| 3612 | |
| 3613 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3614 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3615 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3616 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3617 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3618 | { |
| 3619 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3620 | if (mddev->reshape_position != MaxSector && |
| 3621 | mddev->layout != mddev->new_layout) |
| 3622 | return sprintf(page, "%d (%d)\n", |
| 3623 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3624 | return sprintf(page, "%d\n", mddev->layout); |
| 3625 | } |
| 3626 | |
| 3627 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3628 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3629 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3630 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3631 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3632 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3633 | err = kstrtouint(buf, 10, &n); |
| 3634 | if (err < 0) |
| 3635 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3636 | err = mddev_lock(mddev); |
| 3637 | if (err) |
| 3638 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3639 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3640 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3641 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3642 | err = -EBUSY; |
| 3643 | else if (mddev->ro) |
| 3644 | err = -EROFS; |
| 3645 | else { |
| 3646 | mddev->new_layout = n; |
| 3647 | err = mddev->pers->check_reshape(mddev); |
| 3648 | if (err) |
| 3649 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3650 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3651 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3652 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3653 | if (mddev->reshape_position == MaxSector) |
| 3654 | mddev->layout = n; |
| 3655 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3656 | mddev_unlock(mddev); |
| 3657 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3658 | } |
| 3659 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3660 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3661 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3662 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3663 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3664 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3665 | if (mddev->raid_disks == 0) |
| 3666 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3667 | if (mddev->reshape_position != MaxSector && |
| 3668 | mddev->delta_disks != 0) |
| 3669 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3670 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3671 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3672 | } |
| 3673 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3674 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3675 | |
| 3676 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3677 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3678 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3679 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3680 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3681 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3682 | err = kstrtouint(buf, 10, &n); |
| 3683 | if (err < 0) |
| 3684 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3685 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3686 | err = mddev_lock(mddev); |
| 3687 | if (err) |
| 3688 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3689 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3690 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3691 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3692 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3693 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3694 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3695 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3696 | rdev_for_each(rdev, mddev) { |
| 3697 | if (olddisks < n && |
| 3698 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3699 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3700 | if (olddisks > n && |
| 3701 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3702 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3703 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3704 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3705 | mddev->delta_disks = n - olddisks; |
| 3706 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3707 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3708 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3709 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3710 | out_unlock: |
| 3711 | mddev_unlock(mddev); |
| 3712 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3713 | } |
| 3714 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3715 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3716 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3717 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3718 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3719 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3720 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3721 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3722 | return sprintf(page, "%d (%d)\n", |
| 3723 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3724 | mddev->chunk_sectors << 9); |
| 3725 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3729 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3730 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3731 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3732 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3733 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3734 | err = kstrtoul(buf, 10, &n); |
| 3735 | if (err < 0) |
| 3736 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3737 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3738 | err = mddev_lock(mddev); |
| 3739 | if (err) |
| 3740 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3741 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3742 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3743 | err = -EBUSY; |
| 3744 | else if (mddev->ro) |
| 3745 | err = -EROFS; |
| 3746 | else { |
| 3747 | mddev->new_chunk_sectors = n >> 9; |
| 3748 | err = mddev->pers->check_reshape(mddev); |
| 3749 | if (err) |
| 3750 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3751 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3752 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3753 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3754 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3755 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3756 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3757 | mddev_unlock(mddev); |
| 3758 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3759 | } |
| 3760 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3761 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3762 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3763 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3764 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3765 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3766 | if (mddev->recovery_cp == MaxSector) |
| 3767 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3768 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3769 | } |
| 3770 | |
| 3771 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3772 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3773 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3774 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3775 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3776 | |
| 3777 | if (cmd_match(buf, "none")) |
| 3778 | n = MaxSector; |
| 3779 | else { |
| 3780 | err = kstrtoull(buf, 10, &n); |
| 3781 | if (err < 0) |
| 3782 | return err; |
| 3783 | if (n != (sector_t)n) |
| 3784 | return -EINVAL; |
| 3785 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3786 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3787 | err = mddev_lock(mddev); |
| 3788 | if (err) |
| 3789 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3790 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3791 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3792 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3793 | if (!err) { |
| 3794 | mddev->recovery_cp = n; |
| 3795 | if (mddev->pers) |
| 3796 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3797 | } |
| 3798 | mddev_unlock(mddev); |
| 3799 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3800 | } |
| 3801 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3802 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 3803 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3804 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3805 | /* |
| 3806 | * The array state can be: |
| 3807 | * |
| 3808 | * clear |
| 3809 | * No devices, no size, no level |
| 3810 | * Equivalent to STOP_ARRAY ioctl |
| 3811 | * inactive |
| 3812 | * May have some settings, but array is not active |
| 3813 | * all IO results in error |
| 3814 | * When written, doesn't tear down array, but just stops it |
| 3815 | * suspended (not supported yet) |
| 3816 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3817 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3818 | * readonly |
| 3819 | * no resync can happen. no superblocks get written. |
| 3820 | * write requests fail |
| 3821 | * read-auto |
| 3822 | * like readonly, but behaves like 'clean' on a write request. |
| 3823 | * |
| 3824 | * clean - no pending writes, but otherwise active. |
| 3825 | * When written to inactive array, starts without resync |
| 3826 | * If a write request arrives then |
| 3827 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3828 | * if not known, block and switch to write-pending |
| 3829 | * If written to an active array that has pending writes, then fails. |
| 3830 | * active |
| 3831 | * fully active: IO and resync can be happening. |
| 3832 | * When written to inactive array, starts with resync |
| 3833 | * |
| 3834 | * write-pending |
| 3835 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3836 | * |
| 3837 | * active-idle |
| 3838 | * like active, but no writes have been seen for a while (100msec). |
| 3839 | * |
| 3840 | */ |
| 3841 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3842 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3843 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3844 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3845 | "write-pending", "active-idle", NULL }; |
| 3846 | |
| 3847 | static int match_word(const char *word, char **list) |
| 3848 | { |
| 3849 | int n; |
| 3850 | for (n=0; list[n]; n++) |
| 3851 | if (cmd_match(word, list[n])) |
| 3852 | break; |
| 3853 | return n; |
| 3854 | } |
| 3855 | |
| 3856 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3857 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3858 | { |
| 3859 | enum array_state st = inactive; |
| 3860 | |
| 3861 | if (mddev->pers) |
| 3862 | switch(mddev->ro) { |
| 3863 | case 1: |
| 3864 | st = readonly; |
| 3865 | break; |
| 3866 | case 2: |
| 3867 | st = read_auto; |
| 3868 | break; |
| 3869 | case 0: |
| 3870 | if (mddev->in_sync) |
| 3871 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3872 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3873 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3874 | else if (mddev->safemode) |
| 3875 | st = active_idle; |
| 3876 | else |
| 3877 | st = active; |
| 3878 | } |
| 3879 | else { |
| 3880 | if (list_empty(&mddev->disks) && |
| 3881 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3882 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3883 | st = clear; |
| 3884 | else |
| 3885 | st = inactive; |
| 3886 | } |
| 3887 | return sprintf(page, "%s\n", array_states[st]); |
| 3888 | } |
| 3889 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3890 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 3891 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 3892 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3893 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3894 | |
| 3895 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3896 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3897 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3898 | int err; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3899 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3900 | |
| 3901 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 3902 | /* don't take reconfig_mutex when toggling between |
| 3903 | * clean and active |
| 3904 | */ |
| 3905 | spin_lock(&mddev->lock); |
| 3906 | if (st == active) { |
| 3907 | restart_array(mddev); |
| 3908 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 3909 | wake_up(&mddev->sb_wait); |
| 3910 | err = 0; |
| 3911 | } else /* st == clean */ { |
| 3912 | restart_array(mddev); |
| 3913 | if (atomic_read(&mddev->writes_pending) == 0) { |
| 3914 | if (mddev->in_sync == 0) { |
| 3915 | mddev->in_sync = 1; |
| 3916 | if (mddev->safemode == 1) |
| 3917 | mddev->safemode = 0; |
| 3918 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3919 | } |
| 3920 | err = 0; |
| 3921 | } else |
| 3922 | err = -EBUSY; |
| 3923 | } |
| 3924 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 3925 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3926 | } |
| 3927 | err = mddev_lock(mddev); |
| 3928 | if (err) |
| 3929 | return err; |
| 3930 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3931 | switch(st) { |
| 3932 | case bad_word: |
| 3933 | break; |
| 3934 | case clear: |
| 3935 | /* stopping an active array */ |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3936 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3937 | break; |
| 3938 | case inactive: |
| 3939 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3940 | if (mddev->pers) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3941 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3942 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3943 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3944 | break; |
| 3945 | case suspended: |
| 3946 | break; /* not supported yet */ |
| 3947 | case readonly: |
| 3948 | if (mddev->pers) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3949 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3950 | else { |
| 3951 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3952 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3953 | err = do_md_run(mddev); |
| 3954 | } |
| 3955 | break; |
| 3956 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3957 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3958 | if (mddev->ro == 0) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3959 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3960 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3961 | err = restart_array(mddev); |
| 3962 | if (err == 0) { |
| 3963 | mddev->ro = 2; |
| 3964 | set_disk_ro(mddev->gendisk, 0); |
| 3965 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3966 | } else { |
| 3967 | mddev->ro = 2; |
| 3968 | err = do_md_run(mddev); |
| 3969 | } |
| 3970 | break; |
| 3971 | case clean: |
| 3972 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 3973 | err = restart_array(mddev); |
| 3974 | if (err) |
| 3975 | break; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3976 | spin_lock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3977 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3978 | if (mddev->in_sync == 0) { |
| 3979 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3980 | if (mddev->safemode == 1) |
| 3981 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3982 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3983 | } |
| 3984 | err = 0; |
| 3985 | } else |
| 3986 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3987 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3988 | } else |
| 3989 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3990 | break; |
| 3991 | case active: |
| 3992 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 3993 | err = restart_array(mddev); |
| 3994 | if (err) |
| 3995 | break; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3996 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3997 | wake_up(&mddev->sb_wait); |
| 3998 | err = 0; |
| 3999 | } else { |
| 4000 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4001 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4002 | err = do_md_run(mddev); |
| 4003 | } |
| 4004 | break; |
| 4005 | case write_pending: |
| 4006 | case active_idle: |
| 4007 | /* these cannot be set */ |
| 4008 | break; |
| 4009 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4010 | |
| 4011 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 4012 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4013 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4014 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4015 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4016 | mddev_unlock(mddev); |
| 4017 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4018 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4019 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4020 | __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] | 4021 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4022 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4023 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4024 | return sprintf(page, "%d\n", |
| 4025 | atomic_read(&mddev->max_corr_read_errors)); |
| 4026 | } |
| 4027 | |
| 4028 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4029 | 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] | 4030 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4031 | unsigned int n; |
| 4032 | int rv; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4033 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4034 | rv = kstrtouint(buf, 10, &n); |
| 4035 | if (rv < 0) |
| 4036 | return rv; |
| 4037 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4038 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4039 | } |
| 4040 | |
| 4041 | static struct md_sysfs_entry max_corr_read_errors = |
| 4042 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4043 | max_corrected_read_errors_store); |
| 4044 | |
| 4045 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4046 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4047 | { |
| 4048 | return -EINVAL; |
| 4049 | } |
| 4050 | |
| 4051 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4052 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4053 | { |
| 4054 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4055 | /* The new device is added to the array. |
| 4056 | * If the array has a persistent superblock, we read the |
| 4057 | * superblock to initialise info and check validity. |
| 4058 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4059 | * which mainly checks size. |
| 4060 | */ |
| 4061 | char *e; |
| 4062 | int major = simple_strtoul(buf, &e, 10); |
| 4063 | int minor; |
| 4064 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4065 | struct md_rdev *rdev; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4066 | int err; |
| 4067 | |
| 4068 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4069 | return -EINVAL; |
| 4070 | minor = simple_strtoul(e+1, &e, 10); |
| 4071 | if (*e && *e != '\n') |
| 4072 | return -EINVAL; |
| 4073 | dev = MKDEV(major, minor); |
| 4074 | if (major != MAJOR(dev) || |
| 4075 | minor != MINOR(dev)) |
| 4076 | return -EOVERFLOW; |
| 4077 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4078 | flush_workqueue(md_misc_wq); |
| 4079 | |
| 4080 | err = mddev_lock(mddev); |
| 4081 | if (err) |
| 4082 | return err; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4083 | if (mddev->persistent) { |
| 4084 | rdev = md_import_device(dev, mddev->major_version, |
| 4085 | mddev->minor_version); |
| 4086 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4087 | struct md_rdev *rdev0 |
| 4088 | = list_entry(mddev->disks.next, |
| 4089 | struct md_rdev, same_set); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4090 | err = super_types[mddev->major_version] |
| 4091 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4092 | if (err < 0) |
| 4093 | goto out; |
| 4094 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4095 | } else if (mddev->external) |
| 4096 | rdev = md_import_device(dev, -2, -1); |
| 4097 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4098 | rdev = md_import_device(dev, -1, -1); |
| 4099 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4100 | if (IS_ERR(rdev)) { |
| 4101 | mddev_unlock(mddev); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4102 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4103 | } |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4104 | err = bind_rdev_to_array(rdev, mddev); |
| 4105 | out: |
| 4106 | if (err) |
| 4107 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4108 | mddev_unlock(mddev); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4109 | return err ? err : len; |
| 4110 | } |
| 4111 | |
| 4112 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4113 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4114 | |
| 4115 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4116 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4117 | { |
| 4118 | char *end; |
| 4119 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4120 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4121 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4122 | err = mddev_lock(mddev); |
| 4123 | if (err) |
| 4124 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4125 | if (!mddev->bitmap) |
| 4126 | goto out; |
| 4127 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4128 | while (*buf) { |
| 4129 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4130 | if (buf == end) break; |
| 4131 | if (*end == '-') { /* range */ |
| 4132 | buf = end + 1; |
| 4133 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4134 | if (buf == end) break; |
| 4135 | } |
| 4136 | if (*end && !isspace(*end)) break; |
| 4137 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4138 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4139 | } |
| 4140 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4141 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4142 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4143 | return len; |
| 4144 | } |
| 4145 | |
| 4146 | static struct md_sysfs_entry md_bitmap = |
| 4147 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4148 | |
| 4149 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4150 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4151 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4152 | return sprintf(page, "%llu\n", |
| 4153 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4154 | } |
| 4155 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4156 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4157 | |
| 4158 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4159 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4160 | { |
| 4161 | /* If array is inactive, we can reduce the component size, but |
| 4162 | * not increase it (except from 0). |
| 4163 | * If array is active, we can try an on-line resize |
| 4164 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4165 | sector_t sectors; |
| 4166 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4167 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4168 | if (err < 0) |
| 4169 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4170 | err = mddev_lock(mddev); |
| 4171 | if (err) |
| 4172 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4173 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4174 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4175 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4176 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4177 | if (mddev->dev_sectors == 0 || |
| 4178 | mddev->dev_sectors > sectors) |
| 4179 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4180 | else |
| 4181 | err = -ENOSPC; |
| 4182 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4183 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4184 | return err ? err : len; |
| 4185 | } |
| 4186 | |
| 4187 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4188 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4189 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4190 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4191 | * This is one of |
| 4192 | * 'none' for arrays with no metadata (good luck...) |
| 4193 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4194 | * or N.M for internally known formats |
| 4195 | */ |
| 4196 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4197 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4198 | { |
| 4199 | if (mddev->persistent) |
| 4200 | return sprintf(page, "%d.%d\n", |
| 4201 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4202 | else if (mddev->external) |
| 4203 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4204 | else |
| 4205 | return sprintf(page, "none\n"); |
| 4206 | } |
| 4207 | |
| 4208 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4209 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4210 | { |
| 4211 | int major, minor; |
| 4212 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4213 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4214 | /* Changing the details of 'external' metadata is |
| 4215 | * always permitted. Otherwise there must be |
| 4216 | * no devices attached to the array. |
| 4217 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4218 | |
| 4219 | err = mddev_lock(mddev); |
| 4220 | if (err) |
| 4221 | return err; |
| 4222 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4223 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4224 | ; |
| 4225 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4226 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4227 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4228 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4229 | if (cmd_match(buf, "none")) { |
| 4230 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4231 | mddev->external = 0; |
| 4232 | mddev->major_version = 0; |
| 4233 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4234 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4235 | } |
| 4236 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4237 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4238 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4239 | namelen = sizeof(mddev->metadata_type)-1; |
| 4240 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4241 | mddev->metadata_type[namelen] = 0; |
| 4242 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4243 | mddev->metadata_type[--namelen] = 0; |
| 4244 | mddev->persistent = 0; |
| 4245 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4246 | mddev->major_version = 0; |
| 4247 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4248 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4249 | } |
| 4250 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4251 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4252 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4253 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4254 | buf = e+1; |
| 4255 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4256 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4257 | goto out_unlock; |
| 4258 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4259 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4260 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4261 | mddev->major_version = major; |
| 4262 | mddev->minor_version = minor; |
| 4263 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4264 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4265 | err = 0; |
| 4266 | out_unlock: |
| 4267 | mddev_unlock(mddev); |
| 4268 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4269 | } |
| 4270 | |
| 4271 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4272 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4273 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4274 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4275 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4276 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4277 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4278 | unsigned long recovery = mddev->recovery; |
| 4279 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4280 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4281 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4282 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4283 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4284 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4285 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4286 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4287 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4288 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4289 | type = "check"; |
| 4290 | else |
| 4291 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4292 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4293 | type = "recover"; |
NeilBrown | 985ca97 | 2015-07-06 12:26:57 +1000 | [diff] [blame] | 4294 | else if (mddev->reshape_position != MaxSector) |
| 4295 | type = "reshape"; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4296 | } |
| 4297 | return sprintf(page, "%s\n", type); |
| 4298 | } |
| 4299 | |
| 4300 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4301 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4302 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4303 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4304 | return -EINVAL; |
| 4305 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4306 | |
| 4307 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4308 | if (cmd_match(page, "frozen")) |
| 4309 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4310 | else |
| 4311 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4312 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4313 | mddev_lock(mddev) == 0) { |
| 4314 | flush_workqueue(md_misc_wq); |
| 4315 | if (mddev->sync_thread) { |
| 4316 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4317 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4318 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4319 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4320 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4321 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4322 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4323 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4324 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4325 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4326 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4327 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4328 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4329 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4330 | int err; |
| 4331 | if (mddev->pers->start_reshape == NULL) |
| 4332 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4333 | err = mddev_lock(mddev); |
| 4334 | if (!err) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4335 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4336 | err = mddev->pers->start_reshape(mddev); |
| 4337 | mddev_unlock(mddev); |
| 4338 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4339 | if (err) |
| 4340 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4341 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4342 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4343 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4344 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4345 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4346 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4347 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4348 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4349 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4350 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4351 | if (mddev->ro == 2) { |
| 4352 | /* A write to sync_action is enough to justify |
| 4353 | * canceling read-auto mode |
| 4354 | */ |
| 4355 | mddev->ro = 0; |
| 4356 | md_wakeup_thread(mddev->sync_thread); |
| 4357 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4358 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4359 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4360 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4361 | return len; |
| 4362 | } |
| 4363 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4364 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4365 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4366 | |
| 4367 | static ssize_t |
| 4368 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4369 | { |
| 4370 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4371 | } |
| 4372 | |
| 4373 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4374 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4375 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4376 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4377 | { |
| 4378 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4379 | (unsigned long long) |
| 4380 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4381 | } |
| 4382 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4383 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4384 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4385 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4386 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4387 | { |
| 4388 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4389 | mddev->sync_speed_min ? "local": "system"); |
| 4390 | } |
| 4391 | |
| 4392 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4393 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4394 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4395 | unsigned int min; |
| 4396 | int rv; |
| 4397 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4398 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4399 | min = 0; |
| 4400 | } else { |
| 4401 | rv = kstrtouint(buf, 10, &min); |
| 4402 | if (rv < 0) |
| 4403 | return rv; |
| 4404 | if (min == 0) |
| 4405 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4406 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4407 | mddev->sync_speed_min = min; |
| 4408 | return len; |
| 4409 | } |
| 4410 | |
| 4411 | static struct md_sysfs_entry md_sync_min = |
| 4412 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4413 | |
| 4414 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4415 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4416 | { |
| 4417 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4418 | mddev->sync_speed_max ? "local": "system"); |
| 4419 | } |
| 4420 | |
| 4421 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4422 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4423 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4424 | unsigned int max; |
| 4425 | int rv; |
| 4426 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4427 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4428 | max = 0; |
| 4429 | } else { |
| 4430 | rv = kstrtouint(buf, 10, &max); |
| 4431 | if (rv < 0) |
| 4432 | return rv; |
| 4433 | if (max == 0) |
| 4434 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4435 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4436 | mddev->sync_speed_max = max; |
| 4437 | return len; |
| 4438 | } |
| 4439 | |
| 4440 | static struct md_sysfs_entry md_sync_max = |
| 4441 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4442 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4443 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4444 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4445 | { |
| 4446 | return sprintf(page, "%d\n", mddev->degraded); |
| 4447 | } |
| 4448 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4449 | |
| 4450 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4451 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4452 | { |
| 4453 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4454 | } |
| 4455 | |
| 4456 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4457 | 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] | 4458 | { |
| 4459 | long n; |
| 4460 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4461 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4462 | return -EINVAL; |
| 4463 | |
| 4464 | if (n != 0 && n != 1) |
| 4465 | return -EINVAL; |
| 4466 | |
| 4467 | mddev->parallel_resync = n; |
| 4468 | |
| 4469 | if (mddev->sync_thread) |
| 4470 | wake_up(&resync_wait); |
| 4471 | |
| 4472 | return len; |
| 4473 | } |
| 4474 | |
| 4475 | /* force parallel resync, even with shared block devices */ |
| 4476 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4477 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4478 | sync_force_parallel_show, sync_force_parallel_store); |
| 4479 | |
| 4480 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4481 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4482 | { |
| 4483 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4484 | if (mddev->curr_resync == 0) |
| 4485 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4486 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4487 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4488 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4489 | db = resync - mddev->resync_mark_cnt; |
| 4490 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4491 | } |
| 4492 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4493 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4494 | |
| 4495 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4496 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4497 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4498 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4499 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4500 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4501 | return sprintf(page, "none\n"); |
| 4502 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4503 | if (mddev->curr_resync == 1 || |
| 4504 | mddev->curr_resync == 2) |
| 4505 | return sprintf(page, "delayed\n"); |
| 4506 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4507 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4508 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4509 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4510 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4511 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4512 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4513 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4514 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4515 | } |
| 4516 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4517 | static struct md_sysfs_entry md_sync_completed = |
| 4518 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4519 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4520 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4521 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4522 | { |
| 4523 | return sprintf(page, "%llu\n", |
| 4524 | (unsigned long long)mddev->resync_min); |
| 4525 | } |
| 4526 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4527 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4528 | { |
| 4529 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4530 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4531 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4532 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4533 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4534 | |
| 4535 | spin_lock(&mddev->lock); |
| 4536 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4537 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4538 | goto out_unlock; |
| 4539 | |
| 4540 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4541 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4542 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4543 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 4544 | /* Round down to multiple of 4K for safety */ |
| 4545 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4546 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4547 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4548 | out_unlock: |
| 4549 | spin_unlock(&mddev->lock); |
| 4550 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4551 | } |
| 4552 | |
| 4553 | static struct md_sysfs_entry md_min_sync = |
| 4554 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4555 | |
| 4556 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4557 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4558 | { |
| 4559 | if (mddev->resync_max == MaxSector) |
| 4560 | return sprintf(page, "max\n"); |
| 4561 | else |
| 4562 | return sprintf(page, "%llu\n", |
| 4563 | (unsigned long long)mddev->resync_max); |
| 4564 | } |
| 4565 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4566 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4567 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4568 | int err; |
| 4569 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4570 | if (strncmp(buf, "max", 3) == 0) |
| 4571 | mddev->resync_max = MaxSector; |
| 4572 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4573 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4574 | int chunk; |
| 4575 | |
| 4576 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4577 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4578 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4579 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4580 | goto out_unlock; |
| 4581 | |
| 4582 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4583 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4584 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4585 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4586 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4587 | |
| 4588 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4589 | chunk = mddev->chunk_sectors; |
| 4590 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4591 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4592 | |
| 4593 | err = -EINVAL; |
| 4594 | if (sector_div(temp, chunk)) |
| 4595 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4596 | } |
| 4597 | mddev->resync_max = max; |
| 4598 | } |
| 4599 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4600 | err = 0; |
| 4601 | out_unlock: |
| 4602 | spin_unlock(&mddev->lock); |
| 4603 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4604 | } |
| 4605 | |
| 4606 | static struct md_sysfs_entry md_max_sync = |
| 4607 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4608 | |
| 4609 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4610 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4611 | { |
| 4612 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4613 | } |
| 4614 | |
| 4615 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4616 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4617 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4618 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4619 | int err; |
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 | err = kstrtoull(buf, 10, &new); |
| 4622 | if (err < 0) |
| 4623 | return err; |
| 4624 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4625 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4626 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4627 | err = mddev_lock(mddev); |
| 4628 | if (err) |
| 4629 | return err; |
| 4630 | err = -EINVAL; |
| 4631 | if (mddev->pers == NULL || |
| 4632 | mddev->pers->quiesce == NULL) |
| 4633 | goto unlock; |
| 4634 | old = mddev->suspend_lo; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4635 | mddev->suspend_lo = new; |
| 4636 | if (new >= old) |
| 4637 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4638 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4639 | else { |
| 4640 | /* Expanding suspended region - need to wait */ |
| 4641 | mddev->pers->quiesce(mddev, 1); |
| 4642 | mddev->pers->quiesce(mddev, 0); |
| 4643 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4644 | err = 0; |
| 4645 | unlock: |
| 4646 | mddev_unlock(mddev); |
| 4647 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4648 | } |
| 4649 | static struct md_sysfs_entry md_suspend_lo = |
| 4650 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4651 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4652 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4653 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4654 | { |
| 4655 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4656 | } |
| 4657 | |
| 4658 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4659 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4660 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4661 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4662 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4663 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4664 | err = kstrtoull(buf, 10, &new); |
| 4665 | if (err < 0) |
| 4666 | return err; |
| 4667 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4668 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4669 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4670 | err = mddev_lock(mddev); |
| 4671 | if (err) |
| 4672 | return err; |
| 4673 | err = -EINVAL; |
| 4674 | if (mddev->pers == NULL || |
| 4675 | mddev->pers->quiesce == NULL) |
| 4676 | goto unlock; |
| 4677 | old = mddev->suspend_hi; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4678 | mddev->suspend_hi = new; |
| 4679 | if (new <= old) |
| 4680 | /* Shrinking suspended region */ |
| 4681 | mddev->pers->quiesce(mddev, 2); |
| 4682 | else { |
| 4683 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4684 | mddev->pers->quiesce(mddev, 1); |
| 4685 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4686 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4687 | err = 0; |
| 4688 | unlock: |
| 4689 | mddev_unlock(mddev); |
| 4690 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4691 | } |
| 4692 | static struct md_sysfs_entry md_suspend_hi = |
| 4693 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4694 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4695 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4696 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4697 | { |
| 4698 | if (mddev->reshape_position != MaxSector) |
| 4699 | return sprintf(page, "%llu\n", |
| 4700 | (unsigned long long)mddev->reshape_position); |
| 4701 | strcpy(page, "none\n"); |
| 4702 | return 5; |
| 4703 | } |
| 4704 | |
| 4705 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4706 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4707 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4708 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4709 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4710 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4711 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4712 | err = kstrtoull(buf, 10, &new); |
| 4713 | if (err < 0) |
| 4714 | return err; |
| 4715 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4716 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4717 | err = mddev_lock(mddev); |
| 4718 | if (err) |
| 4719 | return err; |
| 4720 | err = -EBUSY; |
| 4721 | if (mddev->pers) |
| 4722 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4723 | mddev->reshape_position = new; |
| 4724 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4725 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4726 | mddev->new_level = mddev->level; |
| 4727 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4728 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4729 | rdev_for_each(rdev, mddev) |
| 4730 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4731 | err = 0; |
| 4732 | unlock: |
| 4733 | mddev_unlock(mddev); |
| 4734 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | static struct md_sysfs_entry md_reshape_position = |
| 4738 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4739 | reshape_position_store); |
| 4740 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4741 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4742 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4743 | { |
| 4744 | return sprintf(page, "%s\n", |
| 4745 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4746 | } |
| 4747 | |
| 4748 | static ssize_t |
| 4749 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4750 | { |
| 4751 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4752 | int err; |
| 4753 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4754 | if (cmd_match(buf, "forwards")) |
| 4755 | backwards = 0; |
| 4756 | else if (cmd_match(buf, "backwards")) |
| 4757 | backwards = 1; |
| 4758 | else |
| 4759 | return -EINVAL; |
| 4760 | if (mddev->reshape_backwards == backwards) |
| 4761 | return len; |
| 4762 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4763 | err = mddev_lock(mddev); |
| 4764 | if (err) |
| 4765 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4766 | /* check if we are allowed to change */ |
| 4767 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4768 | err = -EBUSY; |
| 4769 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4770 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4771 | err = -EINVAL; |
| 4772 | else |
| 4773 | mddev->reshape_backwards = backwards; |
| 4774 | mddev_unlock(mddev); |
| 4775 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4776 | } |
| 4777 | |
| 4778 | static struct md_sysfs_entry md_reshape_direction = |
| 4779 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4780 | reshape_direction_store); |
| 4781 | |
| 4782 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4783 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4784 | { |
| 4785 | if (mddev->external_size) |
| 4786 | return sprintf(page, "%llu\n", |
| 4787 | (unsigned long long)mddev->array_sectors/2); |
| 4788 | else |
| 4789 | return sprintf(page, "default\n"); |
| 4790 | } |
| 4791 | |
| 4792 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4793 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4794 | { |
| 4795 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4796 | int err; |
| 4797 | |
| 4798 | err = mddev_lock(mddev); |
| 4799 | if (err) |
| 4800 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4801 | |
| 4802 | if (strncmp(buf, "default", 7) == 0) { |
| 4803 | if (mddev->pers) |
| 4804 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4805 | else |
| 4806 | sectors = mddev->array_sectors; |
| 4807 | |
| 4808 | mddev->external_size = 0; |
| 4809 | } else { |
| 4810 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4811 | err = -EINVAL; |
| 4812 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 4813 | err = -E2BIG; |
| 4814 | else |
| 4815 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4816 | } |
| 4817 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4818 | if (!err) { |
| 4819 | mddev->array_sectors = sectors; |
| 4820 | if (mddev->pers) { |
| 4821 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4822 | revalidate_disk(mddev->gendisk); |
| 4823 | } |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4824 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4825 | mddev_unlock(mddev); |
| 4826 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4827 | } |
| 4828 | |
| 4829 | static struct md_sysfs_entry md_array_size = |
| 4830 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4831 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4832 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4833 | static struct attribute *md_default_attrs[] = { |
| 4834 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4835 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4836 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4837 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4838 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4839 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4840 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4841 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4842 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4843 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4844 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4845 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4846 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4847 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4848 | NULL, |
| 4849 | }; |
| 4850 | |
| 4851 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4852 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4853 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4854 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4855 | &md_sync_min.attr, |
| 4856 | &md_sync_max.attr, |
| 4857 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4858 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4859 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4860 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4861 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4862 | &md_suspend_lo.attr, |
| 4863 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4864 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4865 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4866 | NULL, |
| 4867 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4868 | static struct attribute_group md_redundancy_group = { |
| 4869 | .name = NULL, |
| 4870 | .attrs = md_redundancy_attrs, |
| 4871 | }; |
| 4872 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4873 | static ssize_t |
| 4874 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4875 | { |
| 4876 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4877 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4878 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4879 | |
| 4880 | if (!entry->show) |
| 4881 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4882 | spin_lock(&all_mddevs_lock); |
| 4883 | if (list_empty(&mddev->all_mddevs)) { |
| 4884 | spin_unlock(&all_mddevs_lock); |
| 4885 | return -EBUSY; |
| 4886 | } |
| 4887 | mddev_get(mddev); |
| 4888 | spin_unlock(&all_mddevs_lock); |
| 4889 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4890 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4891 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4892 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4893 | } |
| 4894 | |
| 4895 | static ssize_t |
| 4896 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4897 | const char *page, size_t length) |
| 4898 | { |
| 4899 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4900 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4901 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4902 | |
| 4903 | if (!entry->store) |
| 4904 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4905 | if (!capable(CAP_SYS_ADMIN)) |
| 4906 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4907 | spin_lock(&all_mddevs_lock); |
| 4908 | if (list_empty(&mddev->all_mddevs)) { |
| 4909 | spin_unlock(&all_mddevs_lock); |
| 4910 | return -EBUSY; |
| 4911 | } |
| 4912 | mddev_get(mddev); |
| 4913 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4914 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4915 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4916 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4917 | } |
| 4918 | |
| 4919 | static void md_free(struct kobject *ko) |
| 4920 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4921 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4922 | |
| 4923 | if (mddev->sysfs_state) |
| 4924 | sysfs_put(mddev->sysfs_state); |
| 4925 | |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 4926 | if (mddev->queue) |
| 4927 | blk_cleanup_queue(mddev->queue); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4928 | if (mddev->gendisk) { |
| 4929 | del_gendisk(mddev->gendisk); |
| 4930 | put_disk(mddev->gendisk); |
| 4931 | } |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4932 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4933 | kfree(mddev); |
| 4934 | } |
| 4935 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4936 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4937 | .show = md_attr_show, |
| 4938 | .store = md_attr_store, |
| 4939 | }; |
| 4940 | static struct kobj_type md_ktype = { |
| 4941 | .release = md_free, |
| 4942 | .sysfs_ops = &md_sysfs_ops, |
| 4943 | .default_attrs = md_default_attrs, |
| 4944 | }; |
| 4945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | int mdp_major = 0; |
| 4947 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4948 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4949 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4950 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4951 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4952 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4953 | kobject_del(&mddev->kobj); |
| 4954 | kobject_put(&mddev->kobj); |
| 4955 | } |
| 4956 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4957 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4958 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4959 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4960 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4962 | int partitioned; |
| 4963 | int shift; |
| 4964 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4965 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | |
| 4967 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4968 | return -ENODEV; |
| 4969 | |
| 4970 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4971 | shift = partitioned ? MdpMinorShift : 0; |
| 4972 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4974 | /* wait for any previous instance of this device to be |
| 4975 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4976 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4977 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4978 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4979 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4980 | error = -EEXIST; |
| 4981 | if (mddev->gendisk) |
| 4982 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4983 | |
| 4984 | if (name) { |
| 4985 | /* Need to ensure that 'name' is not a duplicate. |
| 4986 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4987 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4988 | spin_lock(&all_mddevs_lock); |
| 4989 | |
| 4990 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4991 | if (mddev2->gendisk && |
| 4992 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4993 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4994 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4995 | } |
| 4996 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4997 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4998 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4999 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5000 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5001 | if (!mddev->queue) |
| 5002 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5003 | mddev->queue->queuedata = mddev; |
| 5004 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5005 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 5006 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | disk = alloc_disk(1 << shift); |
| 5009 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5010 | blk_cleanup_queue(mddev->queue); |
| 5011 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5012 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5014 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5016 | if (name) |
| 5017 | strcpy(disk->disk_name, name); |
| 5018 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5020 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5021 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5022 | disk->fops = &md_fops; |
| 5023 | disk->private_data = mddev; |
| 5024 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5025 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5026 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5027 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5028 | * remove it now. |
| 5029 | */ |
| 5030 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5032 | /* As soon as we call add_disk(), another thread could get |
| 5033 | * through to md_open, so make sure it doesn't get too far |
| 5034 | */ |
| 5035 | mutex_lock(&mddev->open_mutex); |
| 5036 | add_disk(disk); |
| 5037 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 5038 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 5039 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5040 | if (error) { |
| 5041 | /* This isn't possible, but as kobject_init_and_add is marked |
| 5042 | * __must_check, we must do something with the result |
| 5043 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5044 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 5045 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5046 | error = 0; |
| 5047 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5048 | if (mddev->kobj.sd && |
| 5049 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5050 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5051 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5052 | abort: |
| 5053 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5054 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5055 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5056 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5057 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5058 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5059 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5060 | } |
| 5061 | |
| 5062 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 5063 | { |
| 5064 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | return NULL; |
| 5066 | } |
| 5067 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5068 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 5069 | { |
| 5070 | /* val must be "md_*" where * is not all digits. |
| 5071 | * We allocate an array with a large free minor number, and |
| 5072 | * set the name to val. val must not already be an active name. |
| 5073 | */ |
| 5074 | int len = strlen(val); |
| 5075 | char buf[DISK_NAME_LEN]; |
| 5076 | |
| 5077 | while (len && val[len-1] == '\n') |
| 5078 | len--; |
| 5079 | if (len >= DISK_NAME_LEN) |
| 5080 | return -E2BIG; |
| 5081 | strlcpy(buf, val, len+1); |
| 5082 | if (strncmp(buf, "md_", 3) != 0) |
| 5083 | return -EINVAL; |
| 5084 | return md_alloc(0, buf); |
| 5085 | } |
| 5086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5087 | static void md_safemode_timeout(unsigned long data) |
| 5088 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5089 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5090 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5091 | if (!atomic_read(&mddev->writes_pending)) { |
| 5092 | mddev->safemode = 1; |
| 5093 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5094 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5095 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | md_wakeup_thread(mddev->thread); |
| 5097 | } |
| 5098 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5099 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5101 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5103 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5104 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5105 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5107 | if (list_empty(&mddev->disks)) |
| 5108 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5109 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5110 | |
| 5111 | if (mddev->pers) |
| 5112 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5113 | /* Cannot run until previous stop completes properly */ |
| 5114 | if (mddev->sysfs_active) |
| 5115 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | /* |
| 5118 | * Analyze all RAID superblock(s) |
| 5119 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5120 | if (!mddev->raid_disks) { |
| 5121 | if (!mddev->persistent) |
| 5122 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5123 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5124 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5125 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5126 | if (mddev->level != LEVEL_NONE) |
| 5127 | request_module("md-level-%d", mddev->level); |
| 5128 | else if (mddev->clevel[0]) |
| 5129 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | |
| 5131 | /* |
| 5132 | * Drop all container device buffers, from now on |
| 5133 | * the only valid external interface is through the md |
| 5134 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5135 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5136 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5137 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5138 | continue; |
| 5139 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5140 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5141 | |
| 5142 | /* perform some consistency tests on the device. |
| 5143 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5144 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5145 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5146 | if (rdev->meta_bdev) { |
| 5147 | /* Nothing to check */; |
| 5148 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5149 | if (mddev->dev_sectors && |
| 5150 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5151 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5152 | printk("md: %s: data overlaps metadata\n", |
| 5153 | mdname(mddev)); |
| 5154 | return -EINVAL; |
| 5155 | } |
| 5156 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5157 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5158 | > rdev->data_offset) { |
| 5159 | printk("md: %s: metadata overlaps data\n", |
| 5160 | mdname(mddev)); |
| 5161 | return -EINVAL; |
| 5162 | } |
| 5163 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5164 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5165 | } |
| 5166 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5167 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 5168 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5171 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5172 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5173 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5174 | if (mddev->level != LEVEL_NONE) |
| 5175 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5176 | mddev->level); |
| 5177 | else |
| 5178 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5179 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | return -EINVAL; |
| 5181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5182 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5183 | if (mddev->level != pers->level) { |
| 5184 | mddev->level = pers->level; |
| 5185 | mddev->new_level = pers->level; |
| 5186 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5187 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5188 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5189 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5190 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5191 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5192 | module_put(pers->owner); |
| 5193 | return -EINVAL; |
| 5194 | } |
| 5195 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5196 | if (pers->sync_request) { |
| 5197 | /* Warn if this is a potentially silly |
| 5198 | * configuration. |
| 5199 | */ |
| 5200 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5201 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5202 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5203 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5204 | rdev_for_each(rdev, mddev) |
| 5205 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5206 | if (rdev < rdev2 && |
| 5207 | rdev->bdev->bd_contains == |
| 5208 | rdev2->bdev->bd_contains) { |
| 5209 | printk(KERN_WARNING |
| 5210 | "%s: WARNING: %s appears to be" |
| 5211 | " on the same physical disk as" |
| 5212 | " %s.\n", |
| 5213 | mdname(mddev), |
| 5214 | bdevname(rdev->bdev,b), |
| 5215 | bdevname(rdev2->bdev,b2)); |
| 5216 | warned = 1; |
| 5217 | } |
| 5218 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5219 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5220 | if (warned) |
| 5221 | printk(KERN_WARNING |
| 5222 | "True protection against single-disk" |
| 5223 | " failure might be compromised.\n"); |
| 5224 | } |
| 5225 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5226 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5227 | /* may be over-ridden by personality */ |
| 5228 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5229 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5230 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5232 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5233 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5234 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5235 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5236 | if (err) |
| 5237 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5238 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5239 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5240 | " but 'external_size' not in effect?\n", __func__); |
| 5241 | printk(KERN_ERR |
| 5242 | "md: invalid array_size %llu > default size %llu\n", |
| 5243 | (unsigned long long)mddev->array_sectors / 2, |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5244 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5245 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5246 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5247 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5248 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5249 | struct bitmap *bitmap; |
| 5250 | |
| 5251 | bitmap = bitmap_create(mddev, -1); |
| 5252 | if (IS_ERR(bitmap)) { |
| 5253 | err = PTR_ERR(bitmap); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5254 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5255 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5256 | } else |
| 5257 | mddev->bitmap = bitmap; |
| 5258 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5259 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | if (err) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5261 | mddev_detach(mddev); |
NeilBrown | 0c35bd4 | 2015-03-13 11:51:18 +1100 | [diff] [blame] | 5262 | if (mddev->private) |
| 5263 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5264 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5265 | module_put(pers->owner); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5266 | bitmap_destroy(mddev); |
| 5267 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5269 | if (mddev->queue) { |
| 5270 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5271 | mddev->queue->backing_dev_info.congested_fn = md_congested; |
| 5272 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5273 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5274 | if (mddev->kobj.sd && |
| 5275 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5276 | printk(KERN_WARNING |
| 5277 | "md: cannot register extra attributes for %s\n", |
| 5278 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5279 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5280 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5281 | mddev->ro = 0; |
| 5282 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5283 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5284 | atomic_set(&mddev->max_corr_read_errors, |
| 5285 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | mddev->safemode = 0; |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5287 | if (mddev_is_clustered(mddev)) |
| 5288 | mddev->safemode_delay = 0; |
| 5289 | else |
| 5290 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5292 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5293 | spin_lock(&mddev->lock); |
| 5294 | mddev->pers = pers; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5295 | mddev->ready = 1; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5296 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5297 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5298 | if (rdev->raid_disk >= 0) |
| 5299 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5300 | /* failure here is OK */; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5301 | |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 5302 | if (mddev->degraded && !mddev->ro) |
| 5303 | /* This ensures that recovering status is reported immediately |
| 5304 | * via sysfs - until a lack of spares is confirmed. |
| 5305 | */ |
| 5306 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5307 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5308 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5309 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5310 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5311 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5312 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5313 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5314 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5315 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | return 0; |
| 5317 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5318 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5320 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5321 | { |
| 5322 | int err; |
| 5323 | |
| 5324 | err = md_run(mddev); |
| 5325 | if (err) |
| 5326 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5327 | err = bitmap_load(mddev); |
| 5328 | if (err) { |
| 5329 | bitmap_destroy(mddev); |
| 5330 | goto out; |
| 5331 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5332 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5333 | if (mddev_is_clustered(mddev)) |
| 5334 | md_allow_write(mddev); |
| 5335 | |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5336 | md_wakeup_thread(mddev->thread); |
| 5337 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5338 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5339 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5340 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5341 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5342 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5343 | out: |
| 5344 | return err; |
| 5345 | } |
| 5346 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5347 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | { |
| 5349 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5351 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5353 | return -ENXIO; |
| 5354 | if (!mddev->pers) |
| 5355 | return -EINVAL; |
| 5356 | if (!mddev->ro) |
| 5357 | return -EBUSY; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 5358 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 5359 | struct md_rdev *rdev; |
| 5360 | bool has_journal = false; |
| 5361 | |
| 5362 | rcu_read_lock(); |
| 5363 | rdev_for_each_rcu(rdev, mddev) { |
| 5364 | if (test_bit(Journal, &rdev->flags) && |
| 5365 | !test_bit(Faulty, &rdev->flags)) { |
| 5366 | has_journal = true; |
| 5367 | break; |
| 5368 | } |
| 5369 | } |
| 5370 | rcu_read_unlock(); |
| 5371 | |
| 5372 | /* Don't restart rw with journal missing/faulty */ |
| 5373 | if (!has_journal) |
| 5374 | return -EINVAL; |
| 5375 | } |
| 5376 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5377 | mddev->safemode = 0; |
| 5378 | mddev->ro = 0; |
| 5379 | set_disk_ro(disk, 0); |
| 5380 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5381 | mdname(mddev)); |
| 5382 | /* Kick recovery or resync if necessary */ |
| 5383 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5384 | md_wakeup_thread(mddev->thread); |
| 5385 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5386 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5387 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | } |
| 5389 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5390 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5391 | { |
| 5392 | mddev->array_sectors = 0; |
| 5393 | mddev->external_size = 0; |
| 5394 | mddev->dev_sectors = 0; |
| 5395 | mddev->raid_disks = 0; |
| 5396 | mddev->recovery_cp = 0; |
| 5397 | mddev->resync_min = 0; |
| 5398 | mddev->resync_max = MaxSector; |
| 5399 | mddev->reshape_position = MaxSector; |
| 5400 | mddev->external = 0; |
| 5401 | mddev->persistent = 0; |
| 5402 | mddev->level = LEVEL_NONE; |
| 5403 | mddev->clevel[0] = 0; |
| 5404 | mddev->flags = 0; |
| 5405 | mddev->ro = 0; |
| 5406 | mddev->metadata_type[0] = 0; |
| 5407 | mddev->chunk_sectors = 0; |
| 5408 | mddev->ctime = mddev->utime = 0; |
| 5409 | mddev->layout = 0; |
| 5410 | mddev->max_disks = 0; |
| 5411 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5412 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5413 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5414 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5415 | mddev->new_level = LEVEL_NONE; |
| 5416 | mddev->new_layout = 0; |
| 5417 | mddev->new_chunk_sectors = 0; |
| 5418 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5419 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5420 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5421 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5422 | mddev->recovery = 0; |
| 5423 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5424 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5425 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5426 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5427 | mddev->private = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5428 | mddev->bitmap_info.offset = 0; |
| 5429 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5430 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5431 | mddev->bitmap_info.chunksize = 0; |
| 5432 | mddev->bitmap_info.daemon_sleep = 0; |
| 5433 | mddev->bitmap_info.max_write_behind = 0; |
| 5434 | } |
| 5435 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5436 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5437 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 5438 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5439 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5440 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5441 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5442 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5443 | } |
| 5444 | |
| 5445 | del_timer_sync(&mddev->safemode_timer); |
| 5446 | |
| 5447 | bitmap_flush(mddev); |
| 5448 | md_super_wait(mddev); |
| 5449 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5450 | if (mddev->ro == 0 && |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5451 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
| 5452 | (mddev->flags & MD_UPDATE_SB_FLAGS))) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5453 | /* mark array as shutdown cleanly */ |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5454 | if (!mddev_is_clustered(mddev)) |
| 5455 | mddev->in_sync = 1; |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5456 | md_update_sb(mddev, 1); |
| 5457 | } |
| 5458 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5459 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5460 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5461 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 5462 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5463 | __md_stop_writes(mddev); |
| 5464 | mddev_unlock(mddev); |
| 5465 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5466 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5467 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5468 | static void mddev_detach(struct mddev *mddev) |
| 5469 | { |
| 5470 | struct bitmap *bitmap = mddev->bitmap; |
| 5471 | /* wait for behind writes to complete */ |
| 5472 | if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { |
| 5473 | printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n", |
| 5474 | mdname(mddev)); |
| 5475 | /* need to kick something here to make sure I/O goes? */ |
| 5476 | wait_event(bitmap->behind_wait, |
| 5477 | atomic_read(&bitmap->behind_writes) == 0); |
| 5478 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5479 | if (mddev->pers && mddev->pers->quiesce) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5480 | mddev->pers->quiesce(mddev, 1); |
| 5481 | mddev->pers->quiesce(mddev, 0); |
| 5482 | } |
| 5483 | md_unregister_thread(&mddev->thread); |
| 5484 | if (mddev->queue) |
| 5485 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 5486 | } |
| 5487 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5488 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5489 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5490 | struct md_personality *pers = mddev->pers; |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5491 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 5492 | /* Ensure ->event_work is done */ |
| 5493 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5494 | spin_lock(&mddev->lock); |
| 5495 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5496 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5497 | spin_unlock(&mddev->lock); |
| 5498 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5499 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5500 | if (pers->sync_request && mddev->to_remove == NULL) |
| 5501 | mddev->to_remove = &md_redundancy_group; |
| 5502 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5503 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5504 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5505 | |
| 5506 | void md_stop(struct mddev *mddev) |
| 5507 | { |
| 5508 | /* stop the array and free an attached data structures. |
| 5509 | * This is called from dm-raid |
| 5510 | */ |
| 5511 | __md_stop(mddev); |
| 5512 | bitmap_destroy(mddev); |
| 5513 | if (mddev->bio_set) |
| 5514 | bioset_free(mddev->bio_set); |
| 5515 | } |
| 5516 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5517 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5518 | |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5519 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5520 | { |
| 5521 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5522 | int did_freeze = 0; |
| 5523 | |
| 5524 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5525 | did_freeze = 1; |
| 5526 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5527 | md_wakeup_thread(mddev->thread); |
| 5528 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5529 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5530 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5531 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5532 | /* Thread might be blocked waiting for metadata update |
| 5533 | * which will now never happen */ |
| 5534 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5535 | |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5536 | if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
| 5537 | return -EBUSY; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5538 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5539 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 5540 | &mddev->recovery)); |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5541 | wait_event(mddev->sb_wait, |
| 5542 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5543 | mddev_lock_nointr(mddev); |
| 5544 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5545 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5546 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5547 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5548 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5549 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5550 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5551 | if (did_freeze) { |
| 5552 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5553 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5554 | md_wakeup_thread(mddev->thread); |
| 5555 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5556 | err = -EBUSY; |
| 5557 | goto out; |
| 5558 | } |
| 5559 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5560 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5561 | |
| 5562 | err = -ENXIO; |
| 5563 | if (mddev->ro==1) |
| 5564 | goto out; |
| 5565 | mddev->ro = 1; |
| 5566 | set_disk_ro(mddev->gendisk, 1); |
| 5567 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5568 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5569 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5570 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5571 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5572 | } |
| 5573 | out: |
| 5574 | mutex_unlock(&mddev->open_mutex); |
| 5575 | return err; |
| 5576 | } |
| 5577 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5578 | /* mode: |
| 5579 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5580 | * 2 - stop but do not disassemble array |
| 5581 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5582 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5583 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5584 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5585 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5586 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5587 | int did_freeze = 0; |
| 5588 | |
| 5589 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5590 | did_freeze = 1; |
| 5591 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5592 | md_wakeup_thread(mddev->thread); |
| 5593 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5594 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5595 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5596 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5597 | /* Thread might be blocked waiting for metadata update |
| 5598 | * which will now never happen */ |
| 5599 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5600 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5601 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5602 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 5603 | !test_bit(MD_RECOVERY_RUNNING, |
| 5604 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5605 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5606 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5607 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5608 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5609 | mddev->sysfs_active || |
| 5610 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5611 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5612 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5613 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5614 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5615 | if (did_freeze) { |
| 5616 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5617 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5618 | md_wakeup_thread(mddev->thread); |
| 5619 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 5620 | return -EBUSY; |
| 5621 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5622 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5623 | if (mddev->ro) |
| 5624 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5625 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5626 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5627 | __md_stop(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5628 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5629 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5630 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5631 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5632 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5633 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5634 | if (rdev->raid_disk >= 0) |
| 5635 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5636 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5637 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5638 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5639 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5640 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5641 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5642 | if (mddev->ro) |
| 5643 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5644 | } else |
| 5645 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | /* |
| 5647 | * Free resources if final stop |
| 5648 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5649 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5650 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5651 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5652 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5653 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5654 | struct file *f = mddev->bitmap_info.file; |
| 5655 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5656 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5657 | spin_unlock(&mddev->lock); |
| 5658 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5659 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5660 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5662 | export_array(mddev); |
| 5663 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5664 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5665 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5666 | if (mddev->hold_active == UNTIL_STOP) |
| 5667 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5668 | } |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5669 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5670 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5671 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5672 | } |
| 5673 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5674 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5675 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5676 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5677 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | int err; |
| 5679 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5680 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5681 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | |
| 5683 | printk(KERN_INFO "md: running: "); |
| 5684 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5685 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | char b[BDEVNAME_SIZE]; |
| 5687 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5688 | } |
| 5689 | printk("\n"); |
| 5690 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5691 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | if (err) { |
| 5693 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5694 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | } |
| 5696 | } |
| 5697 | |
| 5698 | /* |
| 5699 | * lets try to run arrays based on all disks that have arrived |
| 5700 | * until now. (those are in pending_raid_disks) |
| 5701 | * |
| 5702 | * the method: pick the first pending disk, collect all disks with |
| 5703 | * the same UUID, remove all from the pending list and put them into |
| 5704 | * the 'same_array' list. Then order this list based on superblock |
| 5705 | * update time (freshest comes first), kick out 'old' disks and |
| 5706 | * compare superblocks. If everything's fine then run it. |
| 5707 | * |
| 5708 | * If "unit" is allocated, then bump its reference count |
| 5709 | */ |
| 5710 | static void autorun_devices(int part) |
| 5711 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5712 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5713 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5714 | char b[BDEVNAME_SIZE]; |
| 5715 | |
| 5716 | printk(KERN_INFO "md: autorun ...\n"); |
| 5717 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5718 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5719 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5720 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5721 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5722 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | |
| 5724 | printk(KERN_INFO "md: considering %s ...\n", |
| 5725 | bdevname(rdev0->bdev,b)); |
| 5726 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5727 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5729 | printk(KERN_INFO "md: adding %s ...\n", |
| 5730 | bdevname(rdev->bdev,b)); |
| 5731 | list_move(&rdev->same_set, &candidates); |
| 5732 | } |
| 5733 | /* |
| 5734 | * now we have a set of devices, with all of them having |
| 5735 | * mostly sane superblocks. It's time to allocate the |
| 5736 | * mddev. |
| 5737 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5738 | if (part) { |
| 5739 | dev = MKDEV(mdp_major, |
| 5740 | rdev0->preferred_minor << MdpMinorShift); |
| 5741 | unit = MINOR(dev) >> MdpMinorShift; |
| 5742 | } else { |
| 5743 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5744 | unit = MINOR(dev); |
| 5745 | } |
| 5746 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5748 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5749 | break; |
| 5750 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5751 | |
| 5752 | md_probe(dev, NULL, NULL); |
| 5753 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5754 | if (!mddev || !mddev->gendisk) { |
| 5755 | if (mddev) |
| 5756 | mddev_put(mddev); |
| 5757 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | "md: cannot allocate memory for md drive.\n"); |
| 5759 | break; |
| 5760 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5761 | if (mddev_lock(mddev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5763 | mdname(mddev)); |
| 5764 | else if (mddev->raid_disks || mddev->major_version |
| 5765 | || !list_empty(&mddev->disks)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5766 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5767 | "md: %s already running, cannot run %s\n", |
| 5768 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5769 | mddev_unlock(mddev); |
| 5770 | } else { |
| 5771 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5772 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5773 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | list_del_init(&rdev->same_set); |
| 5775 | if (bind_rdev_to_array(rdev, mddev)) |
| 5776 | export_rdev(rdev); |
| 5777 | } |
| 5778 | autorun_array(mddev); |
| 5779 | mddev_unlock(mddev); |
| 5780 | } |
| 5781 | /* on success, candidates will be empty, on error |
| 5782 | * it won't... |
| 5783 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5784 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5785 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5786 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5787 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5788 | mddev_put(mddev); |
| 5789 | } |
| 5790 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5791 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5792 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5793 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5794 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5795 | { |
| 5796 | mdu_version_t ver; |
| 5797 | |
| 5798 | ver.major = MD_MAJOR_VERSION; |
| 5799 | ver.minor = MD_MINOR_VERSION; |
| 5800 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5801 | |
| 5802 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5803 | return -EFAULT; |
| 5804 | |
| 5805 | return 0; |
| 5806 | } |
| 5807 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5808 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5809 | { |
| 5810 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5811 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5812 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5813 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5814 | nr = working = insync = failed = spare = 0; |
| 5815 | rcu_read_lock(); |
| 5816 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5817 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5818 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5819 | failed++; |
| 5820 | else { |
| 5821 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5822 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5823 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | else |
| 5825 | spare++; |
| 5826 | } |
| 5827 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5828 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5829 | |
| 5830 | info.major_version = mddev->major_version; |
| 5831 | info.minor_version = mddev->minor_version; |
| 5832 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5833 | info.ctime = mddev->ctime; |
| 5834 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5835 | info.size = mddev->dev_sectors / 2; |
| 5836 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5837 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5838 | info.nr_disks = nr; |
| 5839 | info.raid_disks = mddev->raid_disks; |
| 5840 | info.md_minor = mddev->md_minor; |
| 5841 | info.not_persistent= !mddev->persistent; |
| 5842 | |
| 5843 | info.utime = mddev->utime; |
| 5844 | info.state = 0; |
| 5845 | if (mddev->in_sync) |
| 5846 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5847 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 5848 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 5849 | if (mddev_is_clustered(mddev)) |
| 5850 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5851 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | info.working_disks = working; |
| 5853 | info.failed_disks = failed; |
| 5854 | info.spare_disks = spare; |
| 5855 | |
| 5856 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5857 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | |
| 5859 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5860 | return -EFAULT; |
| 5861 | |
| 5862 | return 0; |
| 5863 | } |
| 5864 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5865 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5866 | { |
| 5867 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5868 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5869 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5870 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5871 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5872 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5873 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5874 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5875 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5876 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5877 | /* bitmap enabled */ |
| 5878 | if (mddev->bitmap_info.file) { |
| 5879 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 5880 | sizeof(file->pathname)); |
| 5881 | if (IS_ERR(ptr)) |
| 5882 | err = PTR_ERR(ptr); |
| 5883 | else |
| 5884 | memmove(file->pathname, ptr, |
| 5885 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 5886 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5887 | spin_unlock(&mddev->lock); |
| 5888 | |
| 5889 | if (err == 0 && |
| 5890 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5891 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5892 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5893 | kfree(file); |
| 5894 | return err; |
| 5895 | } |
| 5896 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5897 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | { |
| 5899 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5900 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5901 | |
| 5902 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5903 | return -EFAULT; |
| 5904 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5905 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 5906 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5907 | if (rdev) { |
| 5908 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5909 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5910 | info.raid_disk = rdev->raid_disk; |
| 5911 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5912 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5913 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5914 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5915 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5916 | info.state |= (1<<MD_DISK_SYNC); |
| 5917 | } |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 5918 | if (test_bit(Journal, &rdev->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 5919 | info.state |= (1<<MD_DISK_JOURNAL); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5920 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5921 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | } else { |
| 5923 | info.major = info.minor = 0; |
| 5924 | info.raid_disk = -1; |
| 5925 | info.state = (1<<MD_DISK_REMOVED); |
| 5926 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5927 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5928 | |
| 5929 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5930 | return -EFAULT; |
| 5931 | |
| 5932 | return 0; |
| 5933 | } |
| 5934 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5935 | 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] | 5936 | { |
| 5937 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5938 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | dev_t dev = MKDEV(info->major,info->minor); |
| 5940 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5941 | if (mddev_is_clustered(mddev) && |
| 5942 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 5943 | pr_err("%s: Cannot add to clustered mddev.\n", |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5944 | mdname(mddev)); |
| 5945 | return -EINVAL; |
| 5946 | } |
| 5947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5949 | return -EOVERFLOW; |
| 5950 | |
| 5951 | if (!mddev->raid_disks) { |
| 5952 | int err; |
| 5953 | /* expecting a device which has a superblock */ |
| 5954 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5955 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5956 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5957 | "md: md_import_device returned %ld\n", |
| 5958 | PTR_ERR(rdev)); |
| 5959 | return PTR_ERR(rdev); |
| 5960 | } |
| 5961 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5962 | struct md_rdev *rdev0 |
| 5963 | = list_entry(mddev->disks.next, |
| 5964 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5965 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5966 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5967 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5968 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5969 | "md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5970 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5971 | bdevname(rdev0->bdev,b2)); |
| 5972 | export_rdev(rdev); |
| 5973 | return -EINVAL; |
| 5974 | } |
| 5975 | } |
| 5976 | err = bind_rdev_to_array(rdev, mddev); |
| 5977 | if (err) |
| 5978 | export_rdev(rdev); |
| 5979 | return err; |
| 5980 | } |
| 5981 | |
| 5982 | /* |
| 5983 | * add_new_disk can be used once the array is assembled |
| 5984 | * to add "hot spares". They must already have a superblock |
| 5985 | * written |
| 5986 | */ |
| 5987 | if (mddev->pers) { |
| 5988 | int err; |
| 5989 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5990 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | "%s: personality does not support diskops!\n", |
| 5992 | mdname(mddev)); |
| 5993 | return -EINVAL; |
| 5994 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5995 | if (mddev->persistent) |
| 5996 | rdev = md_import_device(dev, mddev->major_version, |
| 5997 | mddev->minor_version); |
| 5998 | else |
| 5999 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6001 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6002 | "md: md_import_device returned %ld\n", |
| 6003 | PTR_ERR(rdev)); |
| 6004 | return PTR_ERR(rdev); |
| 6005 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 6006 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6007 | if (!mddev->persistent) { |
| 6008 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6009 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6010 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6011 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 6012 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6013 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6014 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 6015 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6016 | } else |
| 6017 | super_types[mddev->major_version]. |
| 6018 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6019 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 6020 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6021 | /* This was a hot-add request, but events doesn't |
| 6022 | * match, so reject it. |
| 6023 | */ |
| 6024 | export_rdev(rdev); |
| 6025 | return -EINVAL; |
| 6026 | } |
| 6027 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6028 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6029 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6030 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6031 | else |
| 6032 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6033 | |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6034 | if (info->state & (1<<MD_DISK_JOURNAL)) |
| 6035 | set_bit(Journal, &rdev->flags); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6036 | /* |
| 6037 | * check whether the device shows up in other nodes |
| 6038 | */ |
| 6039 | if (mddev_is_clustered(mddev)) { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6040 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6041 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6042 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6043 | /* --add initiated by this node */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6044 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6045 | if (err) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6046 | export_rdev(rdev); |
| 6047 | return err; |
| 6048 | } |
| 6049 | } |
| 6050 | } |
| 6051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6052 | rdev->raid_disk = -1; |
| 6053 | err = bind_rdev_to_array(rdev, mddev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6054 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6055 | if (err) |
| 6056 | export_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6057 | |
| 6058 | if (mddev_is_clustered(mddev)) { |
| 6059 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
| 6060 | md_cluster_ops->new_disk_ack(mddev, (err == 0)); |
| 6061 | else { |
| 6062 | if (err) |
| 6063 | md_cluster_ops->add_new_disk_cancel(mddev); |
| 6064 | else |
| 6065 | err = add_bound_rdev(rdev); |
| 6066 | } |
| 6067 | |
| 6068 | } else if (!err) |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 6069 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | return err; |
| 6072 | } |
| 6073 | |
| 6074 | /* otherwise, add_new_disk is only allowed |
| 6075 | * for major_version==0 superblocks |
| 6076 | */ |
| 6077 | if (mddev->major_version != 0) { |
| 6078 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 6079 | mdname(mddev)); |
| 6080 | return -EINVAL; |
| 6081 | } |
| 6082 | |
| 6083 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 6084 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6085 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6087 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6088 | "md: error, md_import_device() returned %ld\n", |
| 6089 | PTR_ERR(rdev)); |
| 6090 | return PTR_ERR(rdev); |
| 6091 | } |
| 6092 | rdev->desc_nr = info->number; |
| 6093 | if (info->raid_disk < mddev->raid_disks) |
| 6094 | rdev->raid_disk = info->raid_disk; |
| 6095 | else |
| 6096 | rdev->raid_disk = -1; |
| 6097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6098 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6099 | if (info->state & (1<<MD_DISK_SYNC)) |
| 6100 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6101 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6102 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6103 | set_bit(WriteMostly, &rdev->flags); |
| 6104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | if (!mddev->persistent) { |
| 6106 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6107 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 6108 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6109 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6110 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6111 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6112 | err = bind_rdev_to_array(rdev, mddev); |
| 6113 | if (err) { |
| 6114 | export_rdev(rdev); |
| 6115 | return err; |
| 6116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6117 | } |
| 6118 | |
| 6119 | return 0; |
| 6120 | } |
| 6121 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6122 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | { |
| 6124 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6125 | struct md_rdev *rdev; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 6126 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | rdev = find_rdev(mddev, dev); |
| 6129 | if (!rdev) |
| 6130 | return -ENXIO; |
| 6131 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6132 | if (mddev_is_clustered(mddev)) |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 6133 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6134 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6135 | if (rdev->raid_disk < 0) |
| 6136 | goto kick_rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6138 | clear_bit(Blocked, &rdev->flags); |
| 6139 | remove_and_add_spares(mddev, rdev); |
| 6140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6141 | if (rdev->raid_disk >= 0) |
| 6142 | goto busy; |
| 6143 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6144 | kick_rdev: |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 6145 | if (mddev_is_clustered(mddev) && ret == 0) |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6146 | md_cluster_ops->remove_disk(mddev, rdev); |
| 6147 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6148 | md_kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6149 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6150 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6151 | |
| 6152 | return 0; |
| 6153 | busy: |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 6154 | if (mddev_is_clustered(mddev) && ret == 0) |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6155 | md_cluster_ops->metadata_update_cancel(mddev); |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6156 | |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 6157 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6159 | return -EBUSY; |
| 6160 | } |
| 6161 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6162 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | { |
| 6164 | char b[BDEVNAME_SIZE]; |
| 6165 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6166 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6167 | |
| 6168 | if (!mddev->pers) |
| 6169 | return -ENODEV; |
| 6170 | |
| 6171 | if (mddev->major_version != 0) { |
| 6172 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 6173 | " version-0 superblocks.\n", |
| 6174 | mdname(mddev)); |
| 6175 | return -EINVAL; |
| 6176 | } |
| 6177 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6178 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | "%s: personality does not support diskops!\n", |
| 6180 | mdname(mddev)); |
| 6181 | return -EINVAL; |
| 6182 | } |
| 6183 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6184 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6185 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6186 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6187 | "md: error, md_import_device() returned %ld\n", |
| 6188 | PTR_ERR(rdev)); |
| 6189 | return -EINVAL; |
| 6190 | } |
| 6191 | |
| 6192 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6193 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6194 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6195 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6196 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6197 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6198 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6199 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6200 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6201 | "md: can not hot-add faulty %s disk to %s!\n", |
| 6202 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6203 | err = -EINVAL; |
| 6204 | goto abort_export; |
| 6205 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6206 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6207 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6208 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 6209 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6210 | err = bind_rdev_to_array(rdev, mddev); |
| 6211 | if (err) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 6212 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6213 | |
| 6214 | /* |
| 6215 | * The rest should better be atomic, we can have disk failures |
| 6216 | * noticed in interrupt contexts ... |
| 6217 | */ |
| 6218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6219 | rdev->raid_disk = -1; |
| 6220 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6221 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6222 | /* |
| 6223 | * Kick recovery, maybe this spare has to be added to the |
| 6224 | * array immediately. |
| 6225 | */ |
| 6226 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6227 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6228 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6229 | return 0; |
| 6230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6231 | abort_export: |
| 6232 | export_rdev(rdev); |
| 6233 | return err; |
| 6234 | } |
| 6235 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6236 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6237 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6238 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6239 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6240 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 6241 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6242 | return -EBUSY; |
| 6243 | if (mddev->recovery || mddev->sync_thread) |
| 6244 | return -EBUSY; |
| 6245 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6246 | } |
| 6247 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6248 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6249 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6250 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6251 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6252 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 6253 | return -EEXIST; /* cannot add when bitmap is present */ |
| 6254 | f = fget(fd); |
| 6255 | |
| 6256 | if (f == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6257 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 6258 | mdname(mddev)); |
| 6259 | return -EBADF; |
| 6260 | } |
| 6261 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6262 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6263 | if (!S_ISREG(inode->i_mode)) { |
| 6264 | printk(KERN_ERR "%s: error: bitmap file must be a regular file\n", |
| 6265 | mdname(mddev)); |
| 6266 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6267 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6268 | printk(KERN_ERR "%s: error: bitmap file must open for write\n", |
| 6269 | mdname(mddev)); |
| 6270 | err = -EBADF; |
| 6271 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6272 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 6273 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6274 | err = -EBUSY; |
| 6275 | } |
| 6276 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6277 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6278 | return err; |
| 6279 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6280 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6281 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6282 | } else if (mddev->bitmap == NULL) |
| 6283 | return -ENOENT; /* cannot remove what isn't there */ |
| 6284 | err = 0; |
| 6285 | if (mddev->pers) { |
| 6286 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6287 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6288 | struct bitmap *bitmap; |
| 6289 | |
| 6290 | bitmap = bitmap_create(mddev, -1); |
| 6291 | if (!IS_ERR(bitmap)) { |
| 6292 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6293 | err = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6294 | } else |
| 6295 | err = PTR_ERR(bitmap); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6296 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6297 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6298 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6299 | fd = -1; /* make sure to put the file */ |
| 6300 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6301 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6302 | } |
| 6303 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6304 | struct file *f = mddev->bitmap_info.file; |
| 6305 | if (f) { |
| 6306 | spin_lock(&mddev->lock); |
| 6307 | mddev->bitmap_info.file = NULL; |
| 6308 | spin_unlock(&mddev->lock); |
| 6309 | fput(f); |
| 6310 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6311 | } |
| 6312 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6313 | return err; |
| 6314 | } |
| 6315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6316 | /* |
| 6317 | * set_array_info is used two different ways |
| 6318 | * The original usage is when creating a new array. |
| 6319 | * In this usage, raid_disks is > 0 and it together with |
| 6320 | * level, size, not_persistent,layout,chunksize determine the |
| 6321 | * shape of the array. |
| 6322 | * This will always create an array with a type-0.90.0 superblock. |
| 6323 | * The newer usage is when assembling an array. |
| 6324 | * In this case raid_disks will be 0, and the major_version field is |
| 6325 | * use to determine which style super-blocks are to be found on the devices. |
| 6326 | * The minor and patch _version numbers are also kept incase the |
| 6327 | * super_block handler wishes to interpret them. |
| 6328 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6329 | 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] | 6330 | { |
| 6331 | |
| 6332 | if (info->raid_disks == 0) { |
| 6333 | /* just setting version number for superblock loading */ |
| 6334 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6335 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6336 | super_types[info->major_version].name == NULL) { |
| 6337 | /* maybe try to auto-load a module? */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6338 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6339 | "md: superblock version %d not known\n", |
| 6340 | info->major_version); |
| 6341 | return -EINVAL; |
| 6342 | } |
| 6343 | mddev->major_version = info->major_version; |
| 6344 | mddev->minor_version = info->minor_version; |
| 6345 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6346 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6347 | /* ensure mddev_put doesn't delete this now that there |
| 6348 | * is some minimal configuration. |
| 6349 | */ |
| 6350 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6351 | return 0; |
| 6352 | } |
| 6353 | mddev->major_version = MD_MAJOR_VERSION; |
| 6354 | mddev->minor_version = MD_MINOR_VERSION; |
| 6355 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 6356 | mddev->ctime = get_seconds(); |
| 6357 | |
| 6358 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6359 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6360 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6361 | mddev->raid_disks = info->raid_disks; |
| 6362 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6363 | * openned |
| 6364 | */ |
| 6365 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6366 | mddev->recovery_cp = MaxSector; |
| 6367 | else |
| 6368 | mddev->recovery_cp = 0; |
| 6369 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6370 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | |
| 6372 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6373 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6374 | |
| 6375 | mddev->max_disks = MD_SB_DISKS; |
| 6376 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6377 | if (mddev->persistent) |
| 6378 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6379 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6380 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6381 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6382 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6383 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6384 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6385 | mddev->reshape_position = MaxSector; |
| 6386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6387 | /* |
| 6388 | * Generate a 128 bit UUID |
| 6389 | */ |
| 6390 | get_random_bytes(mddev->uuid, 16); |
| 6391 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6392 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6393 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6394 | mddev->new_layout = mddev->layout; |
| 6395 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6396 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | return 0; |
| 6399 | } |
| 6400 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6401 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6402 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6403 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6404 | |
| 6405 | if (mddev->external_size) |
| 6406 | return; |
| 6407 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6408 | mddev->array_sectors = array_sectors; |
| 6409 | } |
| 6410 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6411 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6412 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6413 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6414 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6415 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6416 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6417 | |
| 6418 | if (mddev->pers->resize == NULL) |
| 6419 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6420 | /* The "num_sectors" is the number of sectors of each device that |
| 6421 | * is used. This can only make sense for arrays with redundancy. |
| 6422 | * linear and raid0 always use whatever space is available. We can only |
| 6423 | * consider changing this number if no resync or reconstruction is |
| 6424 | * 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] | 6425 | * 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] | 6426 | * of each device. If num_sectors is zero, we find the largest size |
| 6427 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6428 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6429 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6430 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6431 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6432 | if (mddev->ro) |
| 6433 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6434 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6435 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6436 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6437 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6438 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6439 | num_sectors = avail; |
| 6440 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6441 | return -ENOSPC; |
| 6442 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6443 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6444 | if (!rv) |
| 6445 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6446 | return rv; |
| 6447 | } |
| 6448 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6449 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6450 | { |
| 6451 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6452 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6453 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6454 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6455 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6456 | if (mddev->ro) |
| 6457 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6458 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6459 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6460 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6461 | if (mddev->sync_thread || |
| 6462 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6463 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6464 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6465 | |
| 6466 | rdev_for_each(rdev, mddev) { |
| 6467 | if (mddev->raid_disks < raid_disks && |
| 6468 | rdev->data_offset < rdev->new_data_offset) |
| 6469 | return -EINVAL; |
| 6470 | if (mddev->raid_disks > raid_disks && |
| 6471 | rdev->data_offset > rdev->new_data_offset) |
| 6472 | return -EINVAL; |
| 6473 | } |
| 6474 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6475 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6476 | if (mddev->delta_disks < 0) |
| 6477 | mddev->reshape_backwards = 1; |
| 6478 | else if (mddev->delta_disks > 0) |
| 6479 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6480 | |
| 6481 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6482 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6483 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6484 | mddev->reshape_backwards = 0; |
| 6485 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6486 | return rv; |
| 6487 | } |
| 6488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6489 | /* |
| 6490 | * update_array_info is used to change the configuration of an |
| 6491 | * on-line array. |
| 6492 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6493 | * fields in the info are checked against the array. |
| 6494 | * Any differences that cannot be handled will cause an error. |
| 6495 | * Normally, only one change can be managed at a time. |
| 6496 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6497 | 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] | 6498 | { |
| 6499 | int rv = 0; |
| 6500 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6501 | int state = 0; |
| 6502 | |
| 6503 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6504 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6505 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6506 | |
| 6507 | if (mddev->major_version != info->major_version || |
| 6508 | mddev->minor_version != info->minor_version || |
| 6509 | /* mddev->patch_version != info->patch_version || */ |
| 6510 | mddev->ctime != info->ctime || |
| 6511 | mddev->level != info->level || |
| 6512 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 6513 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6514 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6515 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6516 | ((state^info->state) & 0xfffffe00) |
| 6517 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6518 | return -EINVAL; |
| 6519 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6520 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6521 | cnt++; |
| 6522 | if (mddev->raid_disks != info->raid_disks) |
| 6523 | cnt++; |
| 6524 | if (mddev->layout != info->layout) |
| 6525 | cnt++; |
| 6526 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6527 | cnt++; |
| 6528 | if (cnt == 0) |
| 6529 | return 0; |
| 6530 | if (cnt > 1) |
| 6531 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6532 | |
| 6533 | if (mddev->layout != info->layout) { |
| 6534 | /* Change layout |
| 6535 | * we don't need to do anything at the md level, the |
| 6536 | * personality will take care of it all. |
| 6537 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6538 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6540 | else { |
| 6541 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6542 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6543 | if (rv) |
| 6544 | mddev->new_layout = mddev->layout; |
| 6545 | return rv; |
| 6546 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6547 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6548 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6549 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6550 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6551 | if (mddev->raid_disks != info->raid_disks) |
| 6552 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6553 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6554 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6555 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 6556 | rv = -EINVAL; |
| 6557 | goto err; |
| 6558 | } |
| 6559 | if (mddev->recovery || mddev->sync_thread) { |
| 6560 | rv = -EBUSY; |
| 6561 | goto err; |
| 6562 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6563 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6564 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6565 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6566 | if (mddev->bitmap) { |
| 6567 | rv = -EEXIST; |
| 6568 | goto err; |
| 6569 | } |
| 6570 | if (mddev->bitmap_info.default_offset == 0) { |
| 6571 | rv = -EINVAL; |
| 6572 | goto err; |
| 6573 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6574 | mddev->bitmap_info.offset = |
| 6575 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6576 | mddev->bitmap_info.space = |
| 6577 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6578 | mddev->pers->quiesce(mddev, 1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6579 | bitmap = bitmap_create(mddev, -1); |
| 6580 | if (!IS_ERR(bitmap)) { |
| 6581 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6582 | rv = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6583 | } else |
| 6584 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6585 | if (rv) |
| 6586 | bitmap_destroy(mddev); |
| 6587 | mddev->pers->quiesce(mddev, 0); |
| 6588 | } else { |
| 6589 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6590 | if (!mddev->bitmap) { |
| 6591 | rv = -ENOENT; |
| 6592 | goto err; |
| 6593 | } |
| 6594 | if (mddev->bitmap->storage.file) { |
| 6595 | rv = -EINVAL; |
| 6596 | goto err; |
| 6597 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6598 | mddev->pers->quiesce(mddev, 1); |
| 6599 | bitmap_destroy(mddev); |
| 6600 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6601 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6602 | } |
| 6603 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6604 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6605 | return rv; |
| 6606 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6607 | return rv; |
| 6608 | } |
| 6609 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6610 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6611 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6612 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6613 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6614 | |
| 6615 | if (mddev->pers == NULL) |
| 6616 | return -ENODEV; |
| 6617 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6618 | rcu_read_lock(); |
| 6619 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6620 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6621 | err = -ENODEV; |
| 6622 | else { |
| 6623 | md_error(mddev, rdev); |
| 6624 | if (!test_bit(Faulty, &rdev->flags)) |
| 6625 | err = -EBUSY; |
| 6626 | } |
| 6627 | rcu_read_unlock(); |
| 6628 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6629 | } |
| 6630 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6631 | /* |
| 6632 | * We have a problem here : there is no easy way to give a CHS |
| 6633 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6634 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6635 | * dosfs just mad... ;-) |
| 6636 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6637 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6638 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6639 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6640 | |
| 6641 | geo->heads = 2; |
| 6642 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6643 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6644 | return 0; |
| 6645 | } |
| 6646 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6647 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 6648 | { |
| 6649 | switch (cmd) { |
| 6650 | case ADD_NEW_DISK: |
| 6651 | case BLKROSET: |
| 6652 | case GET_ARRAY_INFO: |
| 6653 | case GET_BITMAP_FILE: |
| 6654 | case GET_DISK_INFO: |
| 6655 | case HOT_ADD_DISK: |
| 6656 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6657 | case RAID_AUTORUN: |
| 6658 | case RAID_VERSION: |
| 6659 | case RESTART_ARRAY_RW: |
| 6660 | case RUN_ARRAY: |
| 6661 | case SET_ARRAY_INFO: |
| 6662 | case SET_BITMAP_FILE: |
| 6663 | case SET_DISK_FAULTY: |
| 6664 | case STOP_ARRAY: |
| 6665 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6666 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6667 | return true; |
| 6668 | default: |
| 6669 | return false; |
| 6670 | } |
| 6671 | } |
| 6672 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6673 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6674 | unsigned int cmd, unsigned long arg) |
| 6675 | { |
| 6676 | int err = 0; |
| 6677 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6678 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6679 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6680 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6681 | if (!md_ioctl_valid(cmd)) |
| 6682 | return -ENOTTY; |
| 6683 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6684 | switch (cmd) { |
| 6685 | case RAID_VERSION: |
| 6686 | case GET_ARRAY_INFO: |
| 6687 | case GET_DISK_INFO: |
| 6688 | break; |
| 6689 | default: |
| 6690 | if (!capable(CAP_SYS_ADMIN)) |
| 6691 | return -EACCES; |
| 6692 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6693 | |
| 6694 | /* |
| 6695 | * Commands dealing with the RAID driver but not any |
| 6696 | * particular array: |
| 6697 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6698 | switch (cmd) { |
| 6699 | case RAID_VERSION: |
| 6700 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6701 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6703 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6704 | case RAID_AUTORUN: |
| 6705 | err = 0; |
| 6706 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6707 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6708 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6709 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6710 | } |
| 6711 | |
| 6712 | /* |
| 6713 | * Commands creating/starting a new array: |
| 6714 | */ |
| 6715 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6716 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6717 | |
| 6718 | if (!mddev) { |
| 6719 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6720 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6721 | } |
| 6722 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6723 | /* Some actions do not requires the mutex */ |
| 6724 | switch (cmd) { |
| 6725 | case GET_ARRAY_INFO: |
| 6726 | if (!mddev->raid_disks && !mddev->external) |
| 6727 | err = -ENODEV; |
| 6728 | else |
| 6729 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6730 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6731 | |
| 6732 | case GET_DISK_INFO: |
| 6733 | if (!mddev->raid_disks && !mddev->external) |
| 6734 | err = -ENODEV; |
| 6735 | else |
| 6736 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6737 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6738 | |
| 6739 | case SET_DISK_FAULTY: |
| 6740 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6741 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6742 | |
| 6743 | case GET_BITMAP_FILE: |
| 6744 | err = get_bitmap_file(mddev, argp); |
| 6745 | goto out; |
| 6746 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6747 | } |
| 6748 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 6749 | if (cmd == ADD_NEW_DISK) |
| 6750 | /* need to ensure md_delayed_delete() has completed */ |
| 6751 | flush_workqueue(md_misc_wq); |
| 6752 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 6753 | if (cmd == HOT_REMOVE_DISK) |
| 6754 | /* need to ensure recovery thread has run */ |
| 6755 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 6756 | !test_bit(MD_RECOVERY_NEEDED, |
| 6757 | &mddev->flags), |
| 6758 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6759 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 6760 | /* Need to flush page cache, and ensure no-one else opens |
| 6761 | * and writes |
| 6762 | */ |
| 6763 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6764 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6765 | mutex_unlock(&mddev->open_mutex); |
| 6766 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6767 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6768 | } |
| 6769 | set_bit(MD_STILL_CLOSED, &mddev->flags); |
| 6770 | mutex_unlock(&mddev->open_mutex); |
| 6771 | sync_blockdev(bdev); |
| 6772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | err = mddev_lock(mddev); |
| 6774 | if (err) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6775 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6776 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6777 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6778 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6779 | } |
| 6780 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6781 | if (cmd == SET_ARRAY_INFO) { |
| 6782 | mdu_array_info_t info; |
| 6783 | if (!arg) |
| 6784 | memset(&info, 0, sizeof(info)); |
| 6785 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6786 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6787 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6788 | } |
| 6789 | if (mddev->pers) { |
| 6790 | err = update_array_info(mddev, &info); |
| 6791 | if (err) { |
| 6792 | printk(KERN_WARNING "md: couldn't update" |
| 6793 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6794 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6795 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6796 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6797 | } |
| 6798 | if (!list_empty(&mddev->disks)) { |
| 6799 | printk(KERN_WARNING |
| 6800 | "md: array %s already has disks!\n", |
| 6801 | mdname(mddev)); |
| 6802 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6803 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6804 | } |
| 6805 | if (mddev->raid_disks) { |
| 6806 | printk(KERN_WARNING |
| 6807 | "md: array %s already initialised!\n", |
| 6808 | mdname(mddev)); |
| 6809 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6810 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6811 | } |
| 6812 | err = set_array_info(mddev, &info); |
| 6813 | if (err) { |
| 6814 | printk(KERN_WARNING "md: couldn't set" |
| 6815 | " array info. %d\n", err); |
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 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6818 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6819 | } |
| 6820 | |
| 6821 | /* |
| 6822 | * Commands querying/configuring an existing array: |
| 6823 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6824 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6825 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6826 | if ((!mddev->raid_disks && !mddev->external) |
| 6827 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6828 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6829 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6830 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6831 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6832 | } |
| 6833 | |
| 6834 | /* |
| 6835 | * Commands even a read-only array can execute: |
| 6836 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6837 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6838 | case RESTART_ARRAY_RW: |
| 6839 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6840 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6841 | |
| 6842 | case STOP_ARRAY: |
| 6843 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6844 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6845 | |
| 6846 | case STOP_ARRAY_RO: |
| 6847 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6848 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6849 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6850 | case HOT_REMOVE_DISK: |
| 6851 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6852 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6853 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6854 | case ADD_NEW_DISK: |
| 6855 | /* We can support ADD_NEW_DISK on read-only arrays |
| 6856 | * on if we are re-adding a preexisting device. |
| 6857 | * So require mddev->pers and MD_DISK_SYNC. |
| 6858 | */ |
| 6859 | if (mddev->pers) { |
| 6860 | mdu_disk_info_t info; |
| 6861 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6862 | err = -EFAULT; |
| 6863 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 6864 | /* Need to clear read-only for this */ |
| 6865 | break; |
| 6866 | else |
| 6867 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6868 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6869 | } |
| 6870 | break; |
| 6871 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6872 | case BLKROSET: |
| 6873 | if (get_user(ro, (int __user *)(arg))) { |
| 6874 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6875 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6876 | } |
| 6877 | err = -EINVAL; |
| 6878 | |
| 6879 | /* if the bdev is going readonly the value of mddev->ro |
| 6880 | * does not matter, no writes are coming |
| 6881 | */ |
| 6882 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6883 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6884 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6885 | /* are we are already prepared for writes? */ |
| 6886 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6887 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6888 | |
| 6889 | /* transitioning to readauto need only happen for |
| 6890 | * arrays that call md_write_start |
| 6891 | */ |
| 6892 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6893 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6894 | if (err == 0) { |
| 6895 | mddev->ro = 2; |
| 6896 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6897 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6898 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6899 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6900 | } |
| 6901 | |
| 6902 | /* |
| 6903 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6904 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6905 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 6906 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6907 | if (mddev->ro == 2) { |
| 6908 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6909 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6910 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6911 | /* mddev_unlock will wake thread */ |
| 6912 | /* If a device failed while we were read-only, we |
| 6913 | * need to make sure the metadata is updated now. |
| 6914 | */ |
| 6915 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
| 6916 | mddev_unlock(mddev); |
| 6917 | wait_event(mddev->sb_wait, |
| 6918 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && |
| 6919 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6920 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6921 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6922 | } else { |
| 6923 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6924 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6925 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6926 | } |
| 6927 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6928 | switch (cmd) { |
| 6929 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6930 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6931 | mdu_disk_info_t info; |
| 6932 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6933 | err = -EFAULT; |
| 6934 | else |
| 6935 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6936 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6937 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6938 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6939 | case CLUSTERED_DISK_NACK: |
| 6940 | if (mddev_is_clustered(mddev)) |
| 6941 | md_cluster_ops->new_disk_ack(mddev, false); |
| 6942 | else |
| 6943 | err = -EINVAL; |
| 6944 | goto unlock; |
| 6945 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6946 | case HOT_ADD_DISK: |
| 6947 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6948 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6949 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6950 | case RUN_ARRAY: |
| 6951 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6952 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6953 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6954 | case SET_BITMAP_FILE: |
| 6955 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6956 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6957 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6958 | default: |
| 6959 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6960 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6961 | } |
| 6962 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6963 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6964 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6965 | err != -EINVAL) |
| 6966 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6967 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6968 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6969 | return err; |
| 6970 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6971 | #ifdef CONFIG_COMPAT |
| 6972 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6973 | unsigned int cmd, unsigned long arg) |
| 6974 | { |
| 6975 | switch (cmd) { |
| 6976 | case HOT_REMOVE_DISK: |
| 6977 | case HOT_ADD_DISK: |
| 6978 | case SET_DISK_FAULTY: |
| 6979 | case SET_BITMAP_FILE: |
| 6980 | /* These take in integer arg, do not convert */ |
| 6981 | break; |
| 6982 | default: |
| 6983 | arg = (unsigned long)compat_ptr(arg); |
| 6984 | break; |
| 6985 | } |
| 6986 | |
| 6987 | return md_ioctl(bdev, mode, cmd, arg); |
| 6988 | } |
| 6989 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6990 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6991 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6992 | { |
| 6993 | /* |
| 6994 | * Succeed if we can lock the mddev, which confirms that |
| 6995 | * it isn't being stopped right now. |
| 6996 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6997 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6998 | int err; |
| 6999 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 7000 | if (!mddev) |
| 7001 | return -ENODEV; |
| 7002 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7003 | if (mddev->gendisk != bdev->bd_disk) { |
| 7004 | /* we are racing with mddev_put which is discarding this |
| 7005 | * bd_disk. |
| 7006 | */ |
| 7007 | mddev_put(mddev); |
| 7008 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7009 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7010 | /* Then retry the open from the top */ |
| 7011 | return -ERESTARTSYS; |
| 7012 | } |
| 7013 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 7014 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7015 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7016 | goto out; |
| 7017 | |
| 7018 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7019 | atomic_inc(&mddev->openers); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7020 | clear_bit(MD_STILL_CLOSED, &mddev->flags); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7021 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7022 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7023 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7024 | out: |
| 7025 | return err; |
| 7026 | } |
| 7027 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 7028 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7029 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7030 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7031 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 7032 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7033 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7034 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7035 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7036 | |
| 7037 | static int md_media_changed(struct gendisk *disk) |
| 7038 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7039 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7040 | |
| 7041 | return mddev->changed; |
| 7042 | } |
| 7043 | |
| 7044 | static int md_revalidate(struct gendisk *disk) |
| 7045 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7046 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7047 | |
| 7048 | mddev->changed = 0; |
| 7049 | return 0; |
| 7050 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 7051 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7052 | { |
| 7053 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7054 | .open = md_open, |
| 7055 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 7056 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7057 | #ifdef CONFIG_COMPAT |
| 7058 | .compat_ioctl = md_compat_ioctl, |
| 7059 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7060 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7061 | .media_changed = md_media_changed, |
| 7062 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7063 | }; |
| 7064 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7065 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7066 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7067 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7068 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7069 | /* |
| 7070 | * md_thread is a 'system-thread', it's priority should be very |
| 7071 | * high. We avoid resource deadlocks individually in each |
| 7072 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 7073 | * the very same RT priority as kswapd, thus we will never get |
| 7074 | * into a priority inversion deadlock. |
| 7075 | * |
| 7076 | * we definitely have to have equal or higher priority than |
| 7077 | * bdflush, otherwise bdflush will deadlock if there are too |
| 7078 | * many dirty RAID5 blocks. |
| 7079 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7080 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7081 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7082 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7083 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7084 | /* We need to wait INTERRUPTIBLE so that |
| 7085 | * we don't add to the load-average. |
| 7086 | * That means we need to be sure no signals are |
| 7087 | * pending |
| 7088 | */ |
| 7089 | if (signal_pending(current)) |
| 7090 | flush_signals(current); |
| 7091 | |
| 7092 | wait_event_interruptible_timeout |
| 7093 | (thread->wqueue, |
| 7094 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 7095 | || kthread_should_stop(), |
| 7096 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7097 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7098 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 7099 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7100 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7101 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7103 | return 0; |
| 7104 | } |
| 7105 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7106 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7107 | { |
| 7108 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7109 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7110 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 7111 | wake_up(&thread->wqueue); |
| 7112 | } |
| 7113 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7114 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7115 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7116 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7117 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7118 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7119 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7120 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7121 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7122 | if (!thread) |
| 7123 | return NULL; |
| 7124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7125 | init_waitqueue_head(&thread->wqueue); |
| 7126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | thread->run = run; |
| 7128 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7129 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7130 | thread->tsk = kthread_run(md_thread, thread, |
| 7131 | "%s_%s", |
| 7132 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7133 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7134 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7135 | kfree(thread); |
| 7136 | return NULL; |
| 7137 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7138 | return thread; |
| 7139 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7140 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7141 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7142 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7143 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7144 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7145 | if (!thread) |
| 7146 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7147 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7148 | /* Locking ensures that mddev_unlock does not wake_up a |
| 7149 | * non-existent thread |
| 7150 | */ |
| 7151 | spin_lock(&pers_lock); |
| 7152 | *threadp = NULL; |
| 7153 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7154 | |
| 7155 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7156 | kfree(thread); |
| 7157 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7158 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7159 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7160 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7161 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7162 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7163 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7164 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7165 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7166 | return; |
| 7167 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7168 | if (mddev->degraded) |
| 7169 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7170 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7171 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7172 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7173 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 7174 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7175 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 7176 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7177 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7178 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7179 | |
| 7180 | /* seq_file implementation /proc/mdstat */ |
| 7181 | |
| 7182 | static void status_unused(struct seq_file *seq) |
| 7183 | { |
| 7184 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7185 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7186 | |
| 7187 | seq_printf(seq, "unused devices: "); |
| 7188 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7189 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7190 | char b[BDEVNAME_SIZE]; |
| 7191 | i++; |
| 7192 | seq_printf(seq, "%s ", |
| 7193 | bdevname(rdev->bdev,b)); |
| 7194 | } |
| 7195 | if (!i) |
| 7196 | seq_printf(seq, "<none>"); |
| 7197 | |
| 7198 | seq_printf(seq, "\n"); |
| 7199 | } |
| 7200 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7201 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7202 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7203 | sector_t max_sectors, resync, res; |
| 7204 | unsigned long dt, db; |
| 7205 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7206 | int scale; |
| 7207 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7208 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7209 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 7210 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7211 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7212 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7213 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7214 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7215 | resync = mddev->curr_resync; |
| 7216 | if (resync <= 3) { |
| 7217 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7218 | /* Still cleaning up */ |
| 7219 | resync = max_sectors; |
| 7220 | } else |
| 7221 | resync -= atomic_read(&mddev->recovery_active); |
| 7222 | |
| 7223 | if (resync == 0) { |
| 7224 | if (mddev->recovery_cp < MaxSector) { |
| 7225 | seq_printf(seq, "\tresync=PENDING"); |
| 7226 | return 1; |
| 7227 | } |
| 7228 | return 0; |
| 7229 | } |
| 7230 | if (resync < 3) { |
| 7231 | seq_printf(seq, "\tresync=DELAYED"); |
| 7232 | return 1; |
| 7233 | } |
| 7234 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 7235 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7236 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7237 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7238 | * u32, as those are the requirements for sector_div. |
| 7239 | * Thus 'scale' must be at least 10 |
| 7240 | */ |
| 7241 | scale = 10; |
| 7242 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7243 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7244 | scale++; |
| 7245 | } |
| 7246 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7247 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7248 | |
| 7249 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7250 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7251 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7252 | seq_printf(seq, "["); |
| 7253 | for (i = 0; i < x; i++) |
| 7254 | seq_printf(seq, "="); |
| 7255 | seq_printf(seq, ">"); |
| 7256 | for (i = 0; i < y; i++) |
| 7257 | seq_printf(seq, "."); |
| 7258 | seq_printf(seq, "] "); |
| 7259 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7260 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7261 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 7262 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7263 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 7264 | "check" : |
| 7265 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 7266 | "resync" : "recovery"))), |
| 7267 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7268 | (unsigned long long) resync/2, |
| 7269 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7270 | |
| 7271 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7272 | * dt: time from mark until now |
| 7273 | * db: blocks written from mark until now |
| 7274 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7275 | * |
| 7276 | * rt is a sector_t, so could be 32bit or 64bit. |
| 7277 | * So we divide before multiply in case it is 32bit and close |
| 7278 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 7279 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7280 | * near the end of resync when the number of remaining sectors |
| 7281 | * is close to 'db'. |
| 7282 | * We then divide rt by 32 after multiplying by db to compensate. |
| 7283 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7284 | */ |
| 7285 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 7286 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7287 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 7288 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7289 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7290 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 7291 | sector_div(rt, db/32+1); |
| 7292 | rt *= dt; |
| 7293 | rt >>= 5; |
| 7294 | |
| 7295 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 7296 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7297 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7298 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7299 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7300 | } |
| 7301 | |
| 7302 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 7303 | { |
| 7304 | struct list_head *tmp; |
| 7305 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7306 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7307 | |
| 7308 | if (l >= 0x10000) |
| 7309 | return NULL; |
| 7310 | if (!l--) |
| 7311 | /* header */ |
| 7312 | return (void*)1; |
| 7313 | |
| 7314 | spin_lock(&all_mddevs_lock); |
| 7315 | list_for_each(tmp,&all_mddevs) |
| 7316 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7317 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7318 | mddev_get(mddev); |
| 7319 | spin_unlock(&all_mddevs_lock); |
| 7320 | return mddev; |
| 7321 | } |
| 7322 | spin_unlock(&all_mddevs_lock); |
| 7323 | if (!l--) |
| 7324 | return (void*)2;/* tail */ |
| 7325 | return NULL; |
| 7326 | } |
| 7327 | |
| 7328 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 7329 | { |
| 7330 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7331 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7333 | ++*pos; |
| 7334 | if (v == (void*)2) |
| 7335 | return NULL; |
| 7336 | |
| 7337 | spin_lock(&all_mddevs_lock); |
| 7338 | if (v == (void*)1) |
| 7339 | tmp = all_mddevs.next; |
| 7340 | else |
| 7341 | tmp = mddev->all_mddevs.next; |
| 7342 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7343 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7344 | else { |
| 7345 | next_mddev = (void*)2; |
| 7346 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7347 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7348 | spin_unlock(&all_mddevs_lock); |
| 7349 | |
| 7350 | if (v != (void*)1) |
| 7351 | mddev_put(mddev); |
| 7352 | return next_mddev; |
| 7353 | |
| 7354 | } |
| 7355 | |
| 7356 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 7357 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7358 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7359 | |
| 7360 | if (mddev && v != (void*)1 && v != (void*)2) |
| 7361 | mddev_put(mddev); |
| 7362 | } |
| 7363 | |
| 7364 | static int md_seq_show(struct seq_file *seq, void *v) |
| 7365 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7366 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7367 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7368 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7369 | |
| 7370 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7371 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7372 | seq_printf(seq, "Personalities : "); |
| 7373 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7374 | list_for_each_entry(pers, &pers_list, list) |
| 7375 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7376 | |
| 7377 | spin_unlock(&pers_lock); |
| 7378 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7379 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7380 | return 0; |
| 7381 | } |
| 7382 | if (v == (void*)2) { |
| 7383 | status_unused(seq); |
| 7384 | return 0; |
| 7385 | } |
| 7386 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7387 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7388 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7389 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7390 | mddev->pers ? "" : "in"); |
| 7391 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7392 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7393 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7394 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7395 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7396 | seq_printf(seq, " %s", mddev->pers->name); |
| 7397 | } |
| 7398 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7399 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7400 | rcu_read_lock(); |
| 7401 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7402 | char b[BDEVNAME_SIZE]; |
| 7403 | seq_printf(seq, " %s[%d]", |
| 7404 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7405 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7406 | seq_printf(seq, "(W)"); |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 7407 | if (test_bit(Journal, &rdev->flags)) |
| 7408 | seq_printf(seq, "(J)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7409 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7410 | seq_printf(seq, "(F)"); |
| 7411 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7412 | } |
| 7413 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7414 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7415 | if (test_bit(Replacement, &rdev->flags)) |
| 7416 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7417 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7418 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7419 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7420 | |
| 7421 | if (!list_empty(&mddev->disks)) { |
| 7422 | if (mddev->pers) |
| 7423 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7424 | (unsigned long long) |
| 7425 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7426 | else |
| 7427 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7428 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7429 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7430 | if (mddev->persistent) { |
| 7431 | if (mddev->major_version != 0 || |
| 7432 | mddev->minor_version != 90) { |
| 7433 | seq_printf(seq," super %d.%d", |
| 7434 | mddev->major_version, |
| 7435 | mddev->minor_version); |
| 7436 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7437 | } else if (mddev->external) |
| 7438 | seq_printf(seq, " super external:%s", |
| 7439 | mddev->metadata_type); |
| 7440 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7441 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7442 | |
| 7443 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7444 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7445 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7446 | if (mddev->pers->sync_request) { |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7447 | if (status_resync(seq, mddev)) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7448 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7449 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7450 | } else |
| 7451 | seq_printf(seq, "\n "); |
| 7452 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7453 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7454 | |
| 7455 | seq_printf(seq, "\n"); |
| 7456 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7457 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7459 | return 0; |
| 7460 | } |
| 7461 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7462 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7463 | .start = md_seq_start, |
| 7464 | .next = md_seq_next, |
| 7465 | .stop = md_seq_stop, |
| 7466 | .show = md_seq_show, |
| 7467 | }; |
| 7468 | |
| 7469 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7470 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7471 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7472 | int error; |
| 7473 | |
| 7474 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7475 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7476 | return error; |
| 7477 | |
| 7478 | seq = file->private_data; |
| 7479 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7480 | return error; |
| 7481 | } |
| 7482 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7483 | static int md_unloading; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7484 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7485 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7486 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7487 | int mask; |
| 7488 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7489 | if (md_unloading) |
kbuild test robot | 7d7e64f | 2014-12-03 16:07:59 +1100 | [diff] [blame] | 7490 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7491 | poll_wait(filp, &md_event_waiters, wait); |
| 7492 | |
| 7493 | /* always allow read */ |
| 7494 | mask = POLLIN | POLLRDNORM; |
| 7495 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7496 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7497 | mask |= POLLERR | POLLPRI; |
| 7498 | return mask; |
| 7499 | } |
| 7500 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7501 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7502 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7503 | .open = md_seq_open, |
| 7504 | .read = seq_read, |
| 7505 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7506 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7507 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7508 | }; |
| 7509 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7510 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7511 | { |
Chao Yu | 50bd377 | 2014-09-25 15:28:34 +0800 | [diff] [blame] | 7512 | printk(KERN_INFO "md: %s personality registered for level %d\n", |
| 7513 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7514 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7515 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7516 | spin_unlock(&pers_lock); |
| 7517 | return 0; |
| 7518 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7519 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7520 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7521 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7522 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7523 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7524 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7525 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7526 | spin_unlock(&pers_lock); |
| 7527 | return 0; |
| 7528 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7529 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7530 | |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7531 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
| 7532 | struct module *module) |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7533 | { |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7534 | int ret = 0; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7535 | spin_lock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7536 | if (md_cluster_ops != NULL) |
| 7537 | ret = -EALREADY; |
| 7538 | else { |
| 7539 | md_cluster_ops = ops; |
| 7540 | md_cluster_mod = module; |
| 7541 | } |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7542 | spin_unlock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7543 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7544 | } |
| 7545 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 7546 | |
| 7547 | int unregister_md_cluster_operations(void) |
| 7548 | { |
| 7549 | spin_lock(&pers_lock); |
| 7550 | md_cluster_ops = NULL; |
| 7551 | spin_unlock(&pers_lock); |
| 7552 | return 0; |
| 7553 | } |
| 7554 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 7555 | |
| 7556 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 7557 | { |
| 7558 | int err; |
| 7559 | |
| 7560 | err = request_module("md-cluster"); |
| 7561 | if (err) { |
| 7562 | pr_err("md-cluster module not found.\n"); |
Goldwyn Rodrigues | b0c26a7 | 2015-07-22 12:09:15 -0500 | [diff] [blame] | 7563 | return -ENOENT; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7564 | } |
| 7565 | |
| 7566 | spin_lock(&pers_lock); |
| 7567 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
| 7568 | spin_unlock(&pers_lock); |
| 7569 | return -ENOENT; |
| 7570 | } |
| 7571 | spin_unlock(&pers_lock); |
| 7572 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 7573 | return md_cluster_ops->join(mddev, nodes); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7574 | } |
| 7575 | |
| 7576 | void md_cluster_stop(struct mddev *mddev) |
| 7577 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 7578 | if (!md_cluster_ops) |
| 7579 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7580 | md_cluster_ops->leave(mddev); |
| 7581 | module_put(md_cluster_mod); |
| 7582 | } |
| 7583 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7584 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7585 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7586 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7587 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7588 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7589 | |
| 7590 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7591 | rcu_read_lock(); |
| 7592 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7593 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7594 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7595 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7596 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7597 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7598 | * as sync_io is counted when a request starts, and |
| 7599 | * disk_stats is counted when it completes. |
| 7600 | * So resync activity will cause curr_events to be smaller than |
| 7601 | * when there was no such activity. |
| 7602 | * non-sync IO will cause disk_stat to increase without |
| 7603 | * increasing sync_io so curr_events will (eventually) |
| 7604 | * be larger than it was before. Once it becomes |
| 7605 | * substantially larger, the test below will cause |
| 7606 | * the array to appear non-idle, and resync will slow |
| 7607 | * down. |
| 7608 | * If there is a lot of outstanding resync activity when |
| 7609 | * we set last_event to curr_events, then all that activity |
| 7610 | * completing might cause the array to appear non-idle |
| 7611 | * and resync will be slowed down even though there might |
| 7612 | * not have been non-resync activity. This will only |
| 7613 | * happen once though. 'last_events' will soon reflect |
| 7614 | * the state where there is little or no outstanding |
| 7615 | * resync requests, and further resync activity will |
| 7616 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7617 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7618 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7619 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7620 | rdev->last_events = curr_events; |
| 7621 | idle = 0; |
| 7622 | } |
| 7623 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7624 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7625 | return idle; |
| 7626 | } |
| 7627 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7628 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7629 | { |
| 7630 | /* another "blocks" (512byte) blocks have been synced */ |
| 7631 | atomic_sub(blocks, &mddev->recovery_active); |
| 7632 | wake_up(&mddev->recovery_wait); |
| 7633 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7634 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7635 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7636 | md_wakeup_thread(mddev->thread); |
| 7637 | // stop recovery, signal do_sync .... |
| 7638 | } |
| 7639 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7640 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7641 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7642 | /* md_write_start(mddev, bi) |
| 7643 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7644 | * in superblock) before writing, schedule a superblock update |
| 7645 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7646 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7647 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7648 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7649 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7650 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7651 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7652 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7653 | BUG_ON(mddev->ro == 1); |
| 7654 | if (mddev->ro == 2) { |
| 7655 | /* need to switch to read/write */ |
| 7656 | mddev->ro = 0; |
| 7657 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7658 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7659 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7660 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7661 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7662 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7663 | if (mddev->safemode == 1) |
| 7664 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7665 | if (mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7666 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7667 | if (mddev->in_sync) { |
| 7668 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7669 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7670 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7671 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7672 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7673 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7674 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7675 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7676 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7677 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7678 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7679 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7680 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7681 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7682 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7683 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7684 | { |
| 7685 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7686 | if (mddev->safemode == 2) |
| 7687 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7688 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7689 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7690 | } |
| 7691 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7692 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7693 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7694 | /* md_allow_write(mddev) |
| 7695 | * Calling this ensures that the array is marked 'active' so that writes |
| 7696 | * may proceed without blocking. It is important to call this before |
| 7697 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7698 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7699 | * |
| 7700 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7701 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7702 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7703 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7704 | { |
| 7705 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7706 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7707 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7708 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7709 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7710 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7711 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7712 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7713 | if (mddev->in_sync) { |
| 7714 | mddev->in_sync = 0; |
| 7715 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7716 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7717 | if (mddev->safemode_delay && |
| 7718 | mddev->safemode == 0) |
| 7719 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7720 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7721 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7722 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7723 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7724 | spin_unlock(&mddev->lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7725 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7726 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7727 | return -EAGAIN; |
| 7728 | else |
| 7729 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7730 | } |
| 7731 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7733 | #define SYNC_MARKS 10 |
| 7734 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7735 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7736 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7737 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7738 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7739 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7740 | unsigned int currspeed = 0, |
| 7741 | window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7742 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7743 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7744 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7745 | sector_t mark_cnt[SYNC_MARKS]; |
| 7746 | int last_mark,m; |
| 7747 | struct list_head *tmp; |
| 7748 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7749 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7750 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7751 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7752 | struct blk_plug plug; |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 7753 | bool cluster_resync_finished = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7754 | |
| 7755 | /* just incase thread restarts... */ |
| 7756 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7757 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7758 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 7759 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7760 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7761 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7762 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7763 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7764 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7765 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7766 | action = "check"; |
| 7767 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7768 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7769 | action = "repair"; |
| 7770 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7771 | desc = "resync"; |
| 7772 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7773 | desc = "reshape"; |
| 7774 | else |
| 7775 | desc = "recovery"; |
| 7776 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7777 | mddev->last_sync_action = action ?: desc; |
| 7778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7779 | /* we overload curr_resync somewhat here. |
| 7780 | * 0 == not engaged in resync at all |
| 7781 | * 2 == checking that there is no conflict with another sync |
| 7782 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7783 | * commense |
| 7784 | * other == active in resync - this many blocks |
| 7785 | * |
| 7786 | * Before starting a resync we must have set curr_resync to |
| 7787 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7788 | * less than ours. When we find one that is the same or higher |
| 7789 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7790 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7791 | * This will mean we have to start checking from the beginning again. |
| 7792 | * |
| 7793 | */ |
| 7794 | |
| 7795 | do { |
| 7796 | mddev->curr_resync = 2; |
| 7797 | |
| 7798 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7799 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7800 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7801 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7802 | if (mddev2 == mddev) |
| 7803 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7804 | if (!mddev->parallel_resync |
| 7805 | && mddev2->curr_resync |
| 7806 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7807 | DEFINE_WAIT(wq); |
| 7808 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7809 | /* arbitrarily yield */ |
| 7810 | mddev->curr_resync = 1; |
| 7811 | wake_up(&resync_wait); |
| 7812 | } |
| 7813 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7814 | /* no need to wait here, we can wait the next |
| 7815 | * time 'round when curr_resync == 2 |
| 7816 | */ |
| 7817 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7818 | /* We need to wait 'interruptible' so as not to |
| 7819 | * contribute to the load average, and not to |
| 7820 | * be caught by 'softlockup' |
| 7821 | */ |
| 7822 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7823 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7824 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7825 | printk(KERN_INFO "md: delaying %s of %s" |
| 7826 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7827 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7828 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7829 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7830 | if (signal_pending(current)) |
| 7831 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7832 | schedule(); |
| 7833 | finish_wait(&resync_wait, &wq); |
| 7834 | goto try_again; |
| 7835 | } |
| 7836 | finish_wait(&resync_wait, &wq); |
| 7837 | } |
| 7838 | } |
| 7839 | } while (mddev->curr_resync < 2); |
| 7840 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7841 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7842 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7843 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7844 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7845 | */ |
| 7846 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 7847 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7848 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7849 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7850 | j = mddev->resync_min; |
| 7851 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7852 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7853 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7854 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7855 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7856 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7857 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7858 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7859 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7860 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7861 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7862 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 7863 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7864 | !test_bit(Faulty, &rdev->flags) && |
| 7865 | !test_bit(In_sync, &rdev->flags) && |
| 7866 | rdev->recovery_offset < j) |
| 7867 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7868 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 7869 | |
| 7870 | /* If there is a bitmap, we need to make sure all |
| 7871 | * writes that started before we added a spare |
| 7872 | * complete before we start doing a recovery. |
| 7873 | * Otherwise the write might complete and (via |
| 7874 | * bitmap_endwrite) set a bit in the bitmap after the |
| 7875 | * recovery has checked that bit and skipped that |
| 7876 | * region. |
| 7877 | */ |
| 7878 | if (mddev->bitmap) { |
| 7879 | mddev->pers->quiesce(mddev, 1); |
| 7880 | mddev->pers->quiesce(mddev, 0); |
| 7881 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7882 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7883 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7884 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7885 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7886 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7887 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7888 | "(but not more than %d KB/sec) for %s.\n", |
| 7889 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7890 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7891 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7892 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7893 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7894 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7895 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7896 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7897 | } |
| 7898 | last_mark = 0; |
| 7899 | mddev->resync_mark = mark[last_mark]; |
| 7900 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7901 | |
| 7902 | /* |
| 7903 | * Tune reconstruction: |
| 7904 | */ |
| 7905 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7906 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7907 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7908 | |
| 7909 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7910 | last_check = 0; |
| 7911 | |
| 7912 | if (j>2) { |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7913 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7914 | "md: resuming %s of %s from checkpoint.\n", |
| 7915 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7916 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7917 | } else |
| 7918 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7919 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7920 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 7921 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7922 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7923 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7924 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7925 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7926 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7927 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7928 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7929 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7930 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7931 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7932 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7933 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7934 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7935 | (j - mddev->curr_resync_completed)*2 |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 7936 | >= mddev->resync_max - mddev->curr_resync_completed || |
| 7937 | mddev->curr_resync_completed > mddev->resync_max |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7938 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7939 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7940 | wait_event(mddev->recovery_wait, |
| 7941 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7942 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7943 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 7944 | j > mddev->recovery_cp) |
| 7945 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7946 | update_time = jiffies; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7947 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7948 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7949 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7950 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7951 | while (j >= mddev->resync_max && |
| 7952 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7953 | /* As this condition is controlled by user-space, |
| 7954 | * we can block indefinitely, so use '_interruptible' |
| 7955 | * to avoid triggering warnings. |
| 7956 | */ |
| 7957 | flush_signals(current); /* just in case */ |
| 7958 | wait_event_interruptible(mddev->recovery_wait, |
| 7959 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7960 | || test_bit(MD_RECOVERY_INTR, |
| 7961 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7962 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7963 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7964 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7965 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7966 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 7967 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7968 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7969 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7970 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7971 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7972 | |
| 7973 | if (!skipped) { /* actual IO requested */ |
| 7974 | io_sectors += sectors; |
| 7975 | atomic_add(sectors, &mddev->recovery_active); |
| 7976 | } |
| 7977 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7978 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7979 | break; |
| 7980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7981 | j += sectors; |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 7982 | if (j > max_sectors) |
| 7983 | /* when skipping, extra large numbers can be returned. */ |
| 7984 | j = max_sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7985 | if (j > 2) |
| 7986 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7987 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7988 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7989 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7990 | * visible in /proc/mdstat |
| 7991 | */ |
| 7992 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7993 | |
| 7994 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7995 | continue; |
| 7996 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7997 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7998 | repeat: |
| 7999 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 8000 | /* step marks */ |
| 8001 | int next = (last_mark+1) % SYNC_MARKS; |
| 8002 | |
| 8003 | mddev->resync_mark = mark[next]; |
| 8004 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 8005 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8006 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8007 | last_mark = next; |
| 8008 | } |
| 8009 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8010 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8011 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8012 | |
| 8013 | /* |
| 8014 | * this loop exits only if either when we are slower than |
| 8015 | * the 'hard' speed limit, or the system was IO-idle for |
| 8016 | * a jiffy. |
| 8017 | * the system might be non-idle CPU-wise, but we only care |
| 8018 | * about not overloading the IO subsystem. (things like an |
| 8019 | * e2fsck being done on the RAID array should execute fast) |
| 8020 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8021 | cond_resched(); |
| 8022 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8023 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 8024 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8025 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8026 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 8027 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8028 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8029 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8030 | goto repeat; |
| 8031 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8032 | if (!is_mddev_idle(mddev, 0)) { |
| 8033 | /* |
| 8034 | * Give other IO more of a chance. |
| 8035 | * The faster the devices, the less we wait. |
| 8036 | */ |
| 8037 | wait_event(mddev->recovery_wait, |
| 8038 | !atomic_read(&mddev->recovery_active)); |
| 8039 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8040 | } |
| 8041 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8042 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
| 8043 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 8044 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8045 | /* |
| 8046 | * this also signals 'finished resyncing' to md_stop |
| 8047 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8048 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8049 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 8050 | |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8051 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8052 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8053 | mddev->curr_resync > 2) { |
| 8054 | mddev->curr_resync_completed = mddev->curr_resync; |
| 8055 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 8056 | } |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8057 | /* tell personality and other nodes that we are finished */ |
| 8058 | if (mddev_is_clustered(mddev)) { |
| 8059 | md_cluster_ops->resync_finish(mddev); |
| 8060 | cluster_resync_finished = true; |
| 8061 | } |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8062 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8063 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8064 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8065 | mddev->curr_resync > 2) { |
| 8066 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 8067 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8068 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 8069 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8070 | "md: checkpointing %s of %s.\n", |
| 8071 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8072 | if (test_bit(MD_RECOVERY_ERROR, |
| 8073 | &mddev->recovery)) |
| 8074 | mddev->recovery_cp = |
| 8075 | mddev->curr_resync_completed; |
| 8076 | else |
| 8077 | mddev->recovery_cp = |
| 8078 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8079 | } |
| 8080 | } else |
| 8081 | mddev->recovery_cp = MaxSector; |
| 8082 | } else { |
| 8083 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8084 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8085 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8086 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8087 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 8088 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8089 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8090 | !test_bit(Faulty, &rdev->flags) && |
| 8091 | !test_bit(In_sync, &rdev->flags) && |
| 8092 | rdev->recovery_offset < mddev->curr_resync) |
| 8093 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8094 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8095 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8096 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 8097 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 8098 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8099 | |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8100 | if (mddev_is_clustered(mddev) && |
| 8101 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8102 | !cluster_resync_finished) |
| 8103 | md_cluster_ops->resync_finish(mddev); |
| 8104 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8105 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 8106 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8107 | /* We completed so min/max setting can be forgotten if used. */ |
| 8108 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8109 | mddev->resync_min = 0; |
| 8110 | mddev->resync_max = MaxSector; |
| 8111 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8112 | mddev->resync_min = mddev->curr_resync_completed; |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8113 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8114 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8115 | spin_unlock(&mddev->lock); |
| 8116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8117 | wake_up(&resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8118 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 8119 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8120 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8121 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8122 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8123 | static int remove_and_add_spares(struct mddev *mddev, |
| 8124 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8125 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8126 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8127 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8128 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8129 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8130 | rdev_for_each(rdev, mddev) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8131 | if ((this == NULL || rdev == this) && |
| 8132 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8133 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8134 | (test_bit(Faulty, &rdev->flags) || |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8135 | (!test_bit(In_sync, &rdev->flags) && |
| 8136 | !test_bit(Journal, &rdev->flags))) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8137 | atomic_read(&rdev->nr_pending)==0) { |
| 8138 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8139 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 8140 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8141 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8142 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8143 | } |
| 8144 | } |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 8145 | if (removed && mddev->kobj.sd) |
| 8146 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8147 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8148 | if (this && removed) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8149 | goto no_add; |
| 8150 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8151 | rdev_for_each(rdev, mddev) { |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8152 | if (this && this != rdev) |
| 8153 | continue; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 8154 | if (test_bit(Candidate, &rdev->flags)) |
| 8155 | continue; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8156 | if (rdev->raid_disk >= 0 && |
| 8157 | !test_bit(In_sync, &rdev->flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8158 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8159 | !test_bit(Faulty, &rdev->flags)) |
| 8160 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8161 | if (rdev->raid_disk >= 0) |
| 8162 | continue; |
| 8163 | if (test_bit(Faulty, &rdev->flags)) |
| 8164 | continue; |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 8165 | if (test_bit(Journal, &rdev->flags)) |
| 8166 | continue; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8167 | if (mddev->ro && |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8168 | ! (rdev->saved_raid_disk >= 0 && |
| 8169 | !test_bit(Bitmap_sync, &rdev->flags))) |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8170 | continue; |
| 8171 | |
NeilBrown | d01552a | 2015-10-31 11:00:56 +1100 | [diff] [blame] | 8172 | rdev->recovery_offset = 0; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8173 | if (mddev->pers-> |
| 8174 | hot_add_disk(mddev, rdev) == 0) { |
| 8175 | if (sysfs_link_rdev(mddev, rdev)) |
| 8176 | /* failure here is OK */; |
| 8177 | spares++; |
| 8178 | md_new_event(mddev); |
| 8179 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8180 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8181 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8182 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 8183 | if (removed) |
| 8184 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8185 | return spares; |
| 8186 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 8187 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8188 | static void md_start_sync(struct work_struct *ws) |
| 8189 | { |
| 8190 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8191 | int ret = 0; |
| 8192 | |
| 8193 | if (mddev_is_clustered(mddev)) { |
| 8194 | ret = md_cluster_ops->resync_start(mddev); |
| 8195 | if (ret) { |
| 8196 | mddev->sync_thread = NULL; |
| 8197 | goto out; |
| 8198 | } |
| 8199 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8200 | |
| 8201 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 8202 | mddev, |
| 8203 | "resync"); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8204 | out: |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8205 | if (!mddev->sync_thread) { |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8206 | if (!(mddev_is_clustered(mddev) && ret == -EAGAIN)) |
| 8207 | printk(KERN_ERR "%s: could not start resync" |
| 8208 | " thread...\n", |
| 8209 | mdname(mddev)); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8210 | /* leave the spares where they are, it shouldn't hurt */ |
| 8211 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8212 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8213 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8214 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 8215 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8216 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8217 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8218 | &mddev->recovery)) |
| 8219 | if (mddev->sysfs_action) |
| 8220 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8221 | } else |
| 8222 | md_wakeup_thread(mddev->sync_thread); |
| 8223 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8224 | md_new_event(mddev); |
| 8225 | } |
| 8226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8227 | /* |
| 8228 | * This routine is regularly called by all per-raid-array threads to |
| 8229 | * deal with generic issues like resync and super-block update. |
| 8230 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 8231 | * need this as they never do any recovery or update the superblock. |
| 8232 | * |
| 8233 | * It does not do any resync itself, but rather "forks" off other threads |
| 8234 | * to do that as needed. |
| 8235 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 8236 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8237 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8238 | * and wakeups up this thread which will reap the thread and finish up. |
| 8239 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 8240 | * |
| 8241 | * The overall approach is: |
| 8242 | * 1/ if the superblock needs updating, update it. |
| 8243 | * 2/ If a recovery thread is running, don't do anything else. |
| 8244 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 8245 | * 4/ If there are any faulty devices, remove them. |
| 8246 | * 5/ If array is degraded, try to add spares devices |
| 8247 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 8248 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8249 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8250 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 8251 | if (mddev->suspended) |
| 8252 | return; |
| 8253 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 8254 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 8255 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8256 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8257 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8258 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8259 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 8260 | mdname(mddev)); |
| 8261 | mddev->safemode = 2; |
| 8262 | } |
| 8263 | flush_signals(current); |
| 8264 | } |
| 8265 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8266 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 8267 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8268 | if ( ! ( |
NeilBrown | 142d44c | 2013-11-28 10:34:18 +1100 | [diff] [blame] | 8269 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8270 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8271 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8272 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8273 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 8274 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8275 | )) |
| 8276 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8277 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 8278 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8279 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8280 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8281 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 8282 | struct md_rdev *rdev; |
| 8283 | if (!mddev->external && mddev->in_sync) |
| 8284 | /* 'Blocked' flag not needed as failed devices |
| 8285 | * will be recorded if array switched to read/write. |
| 8286 | * Leaving it set will prevent the device |
| 8287 | * from being removed. |
| 8288 | */ |
| 8289 | rdev_for_each(rdev, mddev) |
| 8290 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8291 | /* On a read-only array we can: |
| 8292 | * - remove failed devices |
| 8293 | * - add already-in_sync devices if the array itself |
| 8294 | * is in-sync. |
| 8295 | * As we only add devices that are already in-sync, |
| 8296 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8297 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8298 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8299 | /* There is no thread, but we need to call |
| 8300 | * ->spare_active and clear saved_raid_disk |
| 8301 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 8302 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8303 | md_reap_sync_thread(mddev); |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 8304 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8305 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Shaohua Li | d4929ad | 2015-09-18 10:20:12 -0700 | [diff] [blame] | 8306 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8307 | goto unlock; |
| 8308 | } |
| 8309 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8310 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8311 | int did_change = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8312 | spin_lock(&mddev->lock); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8313 | if (mddev->safemode && |
| 8314 | !atomic_read(&mddev->writes_pending) && |
| 8315 | !mddev->in_sync && |
| 8316 | mddev->recovery_cp == MaxSector) { |
| 8317 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8318 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 8319 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8320 | } |
| 8321 | if (mddev->safemode == 1) |
| 8322 | mddev->safemode = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8323 | spin_unlock(&mddev->lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8324 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8325 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8326 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8327 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 8328 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 8329 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8331 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 8332 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 8333 | /* resync/recovery still happening */ |
| 8334 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8335 | goto unlock; |
| 8336 | } |
| 8337 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8338 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8339 | goto unlock; |
| 8340 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8341 | /* Set RUNNING before clearing NEEDED to avoid |
| 8342 | * any transients in the value of "sync_action". |
| 8343 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8344 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8345 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8346 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8347 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8348 | /* Clear some bits that don't mean anything, but |
| 8349 | * might be left set |
| 8350 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8351 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 8352 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8353 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 8354 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 8355 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8356 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8357 | /* no recovery is running. |
| 8358 | * remove any failed drives, then |
| 8359 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8360 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8361 | * the personality to fail the re-add. |
| 8362 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8363 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8364 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8365 | if (mddev->pers->check_reshape == NULL || |
| 8366 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8367 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8368 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8369 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8370 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8371 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8372 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8373 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 8374 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8375 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8376 | } else if (mddev->recovery_cp < MaxSector) { |
| 8377 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8378 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8379 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 8380 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8381 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8383 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 8384 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 8385 | /* We are adding a device or devices to an array |
| 8386 | * which has the bitmap stored on all devices. |
| 8387 | * So make sure all bitmap pages get written |
| 8388 | */ |
| 8389 | bitmap_write_all(mddev->bitmap); |
| 8390 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8391 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 8392 | queue_work(md_misc_wq, &mddev->del_work); |
| 8393 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8394 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8395 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8396 | if (!mddev->sync_thread) { |
| 8397 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8398 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8399 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8400 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 8401 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8402 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8403 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8404 | unlock: |
| 8405 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8406 | mddev_unlock(mddev); |
| 8407 | } |
| 8408 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8409 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8410 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8411 | void md_reap_sync_thread(struct mddev *mddev) |
| 8412 | { |
| 8413 | struct md_rdev *rdev; |
| 8414 | |
| 8415 | /* resync has finished, collect result */ |
| 8416 | md_unregister_thread(&mddev->sync_thread); |
| 8417 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8418 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 8419 | /* success...*/ |
| 8420 | /* activate any spares */ |
| 8421 | if (mddev->pers->spare_active(mddev)) { |
| 8422 | sysfs_notify(&mddev->kobj, NULL, |
| 8423 | "degraded"); |
| 8424 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 8425 | } |
| 8426 | } |
| 8427 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8428 | mddev->pers->finish_reshape) |
| 8429 | mddev->pers->finish_reshape(mddev); |
| 8430 | |
| 8431 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8432 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8433 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8434 | if (!mddev->degraded) |
| 8435 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8436 | rdev->saved_raid_disk = -1; |
| 8437 | |
| 8438 | md_update_sb(mddev, 1); |
| 8439 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 8440 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8441 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8442 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8443 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8444 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8445 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8446 | /* flag recovery needed just to double check */ |
| 8447 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8448 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8449 | md_new_event(mddev); |
| 8450 | if (mddev->event_work.func) |
| 8451 | queue_work(md_misc_wq, &mddev->event_work); |
| 8452 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8453 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8454 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8455 | 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] | 8456 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8457 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8458 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8459 | !test_bit(Blocked, &rdev->flags) && |
| 8460 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8461 | msecs_to_jiffies(5000)); |
| 8462 | rdev_dec_pending(rdev, mddev); |
| 8463 | } |
| 8464 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 8465 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8466 | void md_finish_reshape(struct mddev *mddev) |
| 8467 | { |
| 8468 | /* called be personality module when reshape completes. */ |
| 8469 | struct md_rdev *rdev; |
| 8470 | |
| 8471 | rdev_for_each(rdev, mddev) { |
| 8472 | if (rdev->data_offset > rdev->new_data_offset) |
| 8473 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 8474 | else |
| 8475 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 8476 | rdev->data_offset = rdev->new_data_offset; |
| 8477 | } |
| 8478 | } |
| 8479 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8480 | |
| 8481 | /* Bad block management. |
| 8482 | * We can record which blocks on each device are 'bad' and so just |
| 8483 | * fail those blocks, or that stripe, rather than the whole device. |
| 8484 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 8485 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 8486 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 8487 | * A 'shift' can be set so that larger blocks are tracked and |
| 8488 | * consequently larger devices can be covered. |
| 8489 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 8490 | * |
| 8491 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 8492 | * might need to retry if it is very unlucky. |
| 8493 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 8494 | * so we use the write_seqlock_irq variant. |
| 8495 | * |
| 8496 | * When looking for a bad block we specify a range and want to |
| 8497 | * know if any block in the range is bad. So we binary-search |
| 8498 | * to the last range that starts at-or-before the given endpoint, |
| 8499 | * (or "before the sector after the target range") |
| 8500 | * then see if it ends after the given start. |
| 8501 | * We return |
| 8502 | * 0 if there are no known bad blocks in the range |
| 8503 | * 1 if there are known bad block which are all acknowledged |
| 8504 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 8505 | * plus the start/length of the first bad section we overlap. |
| 8506 | */ |
| 8507 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 8508 | sector_t *first_bad, int *bad_sectors) |
| 8509 | { |
| 8510 | int hi; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8511 | int lo; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8512 | u64 *p = bb->page; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8513 | int rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8514 | sector_t target = s + sectors; |
| 8515 | unsigned seq; |
| 8516 | |
| 8517 | if (bb->shift > 0) { |
| 8518 | /* round the start down, and the end up */ |
| 8519 | s >>= bb->shift; |
| 8520 | target += (1<<bb->shift) - 1; |
| 8521 | target >>= bb->shift; |
| 8522 | sectors = target - s; |
| 8523 | } |
| 8524 | /* 'target' is now the first block after the bad range */ |
| 8525 | |
| 8526 | retry: |
| 8527 | seq = read_seqbegin(&bb->lock); |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8528 | lo = 0; |
| 8529 | rv = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8530 | hi = bb->count; |
| 8531 | |
| 8532 | /* Binary search between lo and hi for 'target' |
| 8533 | * i.e. for the last range that starts before 'target' |
| 8534 | */ |
| 8535 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 8536 | * are known not to be the last range before target. |
| 8537 | * VARIANT: hi-lo is the number of possible |
| 8538 | * ranges, and decreases until it reaches 1 |
| 8539 | */ |
| 8540 | while (hi - lo > 1) { |
| 8541 | int mid = (lo + hi) / 2; |
| 8542 | sector_t a = BB_OFFSET(p[mid]); |
| 8543 | if (a < target) |
| 8544 | /* This could still be the one, earlier ranges |
| 8545 | * could not. */ |
| 8546 | lo = mid; |
| 8547 | else |
| 8548 | /* This and later ranges are definitely out. */ |
| 8549 | hi = mid; |
| 8550 | } |
| 8551 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 8552 | if (hi > lo) { |
| 8553 | /* need to check all range that end after 's' to see if |
| 8554 | * any are unacknowledged. |
| 8555 | */ |
| 8556 | while (lo >= 0 && |
| 8557 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8558 | if (BB_OFFSET(p[lo]) < target) { |
| 8559 | /* starts before the end, and finishes after |
| 8560 | * the start, so they must overlap |
| 8561 | */ |
| 8562 | if (rv != -1 && BB_ACK(p[lo])) |
| 8563 | rv = 1; |
| 8564 | else |
| 8565 | rv = -1; |
| 8566 | *first_bad = BB_OFFSET(p[lo]); |
| 8567 | *bad_sectors = BB_LEN(p[lo]); |
| 8568 | } |
| 8569 | lo--; |
| 8570 | } |
| 8571 | } |
| 8572 | |
| 8573 | if (read_seqretry(&bb->lock, seq)) |
| 8574 | goto retry; |
| 8575 | |
| 8576 | return rv; |
| 8577 | } |
| 8578 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 8579 | |
| 8580 | /* |
| 8581 | * Add a range of bad blocks to the table. |
| 8582 | * This might extend the table, or might contract it |
| 8583 | * if two adjacent ranges can be merged. |
| 8584 | * We binary-search to find the 'insertion' point, then |
| 8585 | * decide how best to handle it. |
| 8586 | */ |
| 8587 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 8588 | int acknowledged) |
| 8589 | { |
| 8590 | u64 *p; |
| 8591 | int lo, hi; |
| 8592 | int rv = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8593 | unsigned long flags; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8594 | |
| 8595 | if (bb->shift < 0) |
| 8596 | /* badblocks are disabled */ |
| 8597 | return 0; |
| 8598 | |
| 8599 | if (bb->shift) { |
| 8600 | /* round the start down, and the end up */ |
| 8601 | sector_t next = s + sectors; |
| 8602 | s >>= bb->shift; |
| 8603 | next += (1<<bb->shift) - 1; |
| 8604 | next >>= bb->shift; |
| 8605 | sectors = next - s; |
| 8606 | } |
| 8607 | |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8608 | write_seqlock_irqsave(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8609 | |
| 8610 | p = bb->page; |
| 8611 | lo = 0; |
| 8612 | hi = bb->count; |
| 8613 | /* Find the last range that starts at-or-before 's' */ |
| 8614 | while (hi - lo > 1) { |
| 8615 | int mid = (lo + hi) / 2; |
| 8616 | sector_t a = BB_OFFSET(p[mid]); |
| 8617 | if (a <= s) |
| 8618 | lo = mid; |
| 8619 | else |
| 8620 | hi = mid; |
| 8621 | } |
| 8622 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 8623 | hi = lo; |
| 8624 | |
| 8625 | if (hi > lo) { |
| 8626 | /* we found a range that might merge with the start |
| 8627 | * of our new range |
| 8628 | */ |
| 8629 | sector_t a = BB_OFFSET(p[lo]); |
| 8630 | sector_t e = a + BB_LEN(p[lo]); |
| 8631 | int ack = BB_ACK(p[lo]); |
| 8632 | if (e >= s) { |
| 8633 | /* Yes, we can merge with a previous range */ |
| 8634 | if (s == a && s + sectors >= e) |
| 8635 | /* new range covers old */ |
| 8636 | ack = acknowledged; |
| 8637 | else |
| 8638 | ack = ack && acknowledged; |
| 8639 | |
| 8640 | if (e < s + sectors) |
| 8641 | e = s + sectors; |
| 8642 | if (e - a <= BB_MAX_LEN) { |
| 8643 | p[lo] = BB_MAKE(a, e-a, ack); |
| 8644 | s = e; |
| 8645 | } else { |
| 8646 | /* does not all fit in one range, |
| 8647 | * make p[lo] maximal |
| 8648 | */ |
| 8649 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8650 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8651 | s = a + BB_MAX_LEN; |
| 8652 | } |
| 8653 | sectors = e - s; |
| 8654 | } |
| 8655 | } |
| 8656 | if (sectors && hi < bb->count) { |
| 8657 | /* 'hi' points to the first range that starts after 's'. |
| 8658 | * Maybe we can merge with the start of that range */ |
| 8659 | sector_t a = BB_OFFSET(p[hi]); |
| 8660 | sector_t e = a + BB_LEN(p[hi]); |
| 8661 | int ack = BB_ACK(p[hi]); |
| 8662 | if (a <= s + sectors) { |
| 8663 | /* merging is possible */ |
| 8664 | if (e <= s + sectors) { |
| 8665 | /* full overlap */ |
| 8666 | e = s + sectors; |
| 8667 | ack = acknowledged; |
| 8668 | } else |
| 8669 | ack = ack && acknowledged; |
| 8670 | |
| 8671 | a = s; |
| 8672 | if (e - a <= BB_MAX_LEN) { |
| 8673 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8674 | s = e; |
| 8675 | } else { |
| 8676 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8677 | s = a + BB_MAX_LEN; |
| 8678 | } |
| 8679 | sectors = e - s; |
| 8680 | lo = hi; |
| 8681 | hi++; |
| 8682 | } |
| 8683 | } |
| 8684 | if (sectors == 0 && hi < bb->count) { |
| 8685 | /* we might be able to combine lo and hi */ |
| 8686 | /* Note: 's' is at the end of 'lo' */ |
| 8687 | sector_t a = BB_OFFSET(p[hi]); |
| 8688 | int lolen = BB_LEN(p[lo]); |
| 8689 | int hilen = BB_LEN(p[hi]); |
| 8690 | int newlen = lolen + hilen - (s - a); |
| 8691 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8692 | /* yes, we can combine them */ |
| 8693 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8694 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8695 | memmove(p + hi, p + hi + 1, |
| 8696 | (bb->count - hi - 1) * 8); |
| 8697 | bb->count--; |
| 8698 | } |
| 8699 | } |
| 8700 | while (sectors) { |
| 8701 | /* didn't merge (it all). |
| 8702 | * Need to add a range just before 'hi' */ |
| 8703 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8704 | /* No room for more */ |
| 8705 | rv = 0; |
| 8706 | break; |
| 8707 | } else { |
| 8708 | int this_sectors = sectors; |
| 8709 | memmove(p + hi + 1, p + hi, |
| 8710 | (bb->count - hi) * 8); |
| 8711 | bb->count++; |
| 8712 | |
| 8713 | if (this_sectors > BB_MAX_LEN) |
| 8714 | this_sectors = BB_MAX_LEN; |
| 8715 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8716 | sectors -= this_sectors; |
| 8717 | s += this_sectors; |
| 8718 | } |
| 8719 | } |
| 8720 | |
| 8721 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8722 | if (!acknowledged) |
| 8723 | bb->unacked_exist = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8724 | write_sequnlock_irqrestore(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8725 | |
| 8726 | return rv; |
| 8727 | } |
| 8728 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8729 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8730 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8731 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8732 | int rv; |
| 8733 | if (is_new) |
| 8734 | s += rdev->new_data_offset; |
| 8735 | else |
| 8736 | s += rdev->data_offset; |
| 8737 | rv = md_set_badblocks(&rdev->badblocks, |
| 8738 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8739 | if (rv) { |
| 8740 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8741 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8742 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
NeilBrown | 55ce74d | 2015-08-14 11:11:10 +1000 | [diff] [blame] | 8743 | set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8744 | md_wakeup_thread(rdev->mddev->thread); |
| 8745 | } |
| 8746 | return rv; |
| 8747 | } |
| 8748 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8749 | |
| 8750 | /* |
| 8751 | * Remove a range of bad blocks from the table. |
| 8752 | * This may involve extending the table if we spilt a region, |
| 8753 | * but it must not fail. So if the table becomes full, we just |
| 8754 | * drop the remove request. |
| 8755 | */ |
| 8756 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8757 | { |
| 8758 | u64 *p; |
| 8759 | int lo, hi; |
| 8760 | sector_t target = s + sectors; |
| 8761 | int rv = 0; |
| 8762 | |
| 8763 | if (bb->shift > 0) { |
| 8764 | /* When clearing we round the start up and the end down. |
| 8765 | * This should not matter as the shift should align with |
| 8766 | * the block size and no rounding should ever be needed. |
| 8767 | * However it is better the think a block is bad when it |
| 8768 | * isn't than to think a block is not bad when it is. |
| 8769 | */ |
| 8770 | s += (1<<bb->shift) - 1; |
| 8771 | s >>= bb->shift; |
| 8772 | target >>= bb->shift; |
| 8773 | sectors = target - s; |
| 8774 | } |
| 8775 | |
| 8776 | write_seqlock_irq(&bb->lock); |
| 8777 | |
| 8778 | p = bb->page; |
| 8779 | lo = 0; |
| 8780 | hi = bb->count; |
| 8781 | /* Find the last range that starts before 'target' */ |
| 8782 | while (hi - lo > 1) { |
| 8783 | int mid = (lo + hi) / 2; |
| 8784 | sector_t a = BB_OFFSET(p[mid]); |
| 8785 | if (a < target) |
| 8786 | lo = mid; |
| 8787 | else |
| 8788 | hi = mid; |
| 8789 | } |
| 8790 | if (hi > lo) { |
| 8791 | /* p[lo] is the last range that could overlap the |
| 8792 | * current range. Earlier ranges could also overlap, |
| 8793 | * but only this one can overlap the end of the range. |
| 8794 | */ |
| 8795 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8796 | /* Partial overlap, leave the tail of this range */ |
| 8797 | int ack = BB_ACK(p[lo]); |
| 8798 | sector_t a = BB_OFFSET(p[lo]); |
| 8799 | sector_t end = a + BB_LEN(p[lo]); |
| 8800 | |
| 8801 | if (a < s) { |
| 8802 | /* we need to split this range */ |
| 8803 | if (bb->count >= MD_MAX_BADBLOCKS) { |
NeilBrown | 8b32bf5 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 8804 | rv = -ENOSPC; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8805 | goto out; |
| 8806 | } |
| 8807 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8808 | bb->count++; |
| 8809 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8810 | lo++; |
| 8811 | } |
| 8812 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8813 | /* there is no longer an overlap */ |
| 8814 | hi = lo; |
| 8815 | lo--; |
| 8816 | } |
| 8817 | while (lo >= 0 && |
| 8818 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8819 | /* This range does overlap */ |
| 8820 | if (BB_OFFSET(p[lo]) < s) { |
| 8821 | /* Keep the early parts of this range. */ |
| 8822 | int ack = BB_ACK(p[lo]); |
| 8823 | sector_t start = BB_OFFSET(p[lo]); |
| 8824 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8825 | /* now low doesn't overlap, so.. */ |
| 8826 | break; |
| 8827 | } |
| 8828 | lo--; |
| 8829 | } |
| 8830 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8831 | * anything between needs to be discarded |
| 8832 | */ |
| 8833 | if (hi - lo > 1) { |
| 8834 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8835 | bb->count -= (hi - lo - 1); |
| 8836 | } |
| 8837 | } |
| 8838 | |
| 8839 | bb->changed = 1; |
| 8840 | out: |
| 8841 | write_sequnlock_irq(&bb->lock); |
| 8842 | return rv; |
| 8843 | } |
| 8844 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8845 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8846 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8847 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8848 | if (is_new) |
| 8849 | s += rdev->new_data_offset; |
| 8850 | else |
| 8851 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8852 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8853 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8854 | } |
| 8855 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8856 | |
| 8857 | /* |
| 8858 | * Acknowledge all bad blocks in a list. |
| 8859 | * This only succeeds if ->changed is clear. It is used by |
| 8860 | * in-kernel metadata updates |
| 8861 | */ |
| 8862 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8863 | { |
| 8864 | if (bb->page == NULL || bb->changed) |
| 8865 | /* no point even trying */ |
| 8866 | return; |
| 8867 | write_seqlock_irq(&bb->lock); |
| 8868 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8869 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8870 | u64 *p = bb->page; |
| 8871 | int i; |
| 8872 | for (i = 0; i < bb->count ; i++) { |
| 8873 | if (!BB_ACK(p[i])) { |
| 8874 | sector_t start = BB_OFFSET(p[i]); |
| 8875 | int len = BB_LEN(p[i]); |
| 8876 | p[i] = BB_MAKE(start, len, 1); |
| 8877 | } |
| 8878 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8879 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8880 | } |
| 8881 | write_sequnlock_irq(&bb->lock); |
| 8882 | } |
| 8883 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8884 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8885 | /* sysfs access to bad-blocks list. |
| 8886 | * We present two files. |
| 8887 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8888 | * are recorded as bad. The list is truncated to fit within |
| 8889 | * the one-page limit of sysfs. |
| 8890 | * Writing "sector length" to this file adds an acknowledged |
| 8891 | * bad block list. |
| 8892 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8893 | * been acknowledged. Writing to this file adds bad blocks |
| 8894 | * without acknowledging them. This is largely for testing. |
| 8895 | */ |
| 8896 | |
| 8897 | static ssize_t |
| 8898 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8899 | { |
| 8900 | size_t len; |
| 8901 | int i; |
| 8902 | u64 *p = bb->page; |
| 8903 | unsigned seq; |
| 8904 | |
| 8905 | if (bb->shift < 0) |
| 8906 | return 0; |
| 8907 | |
| 8908 | retry: |
| 8909 | seq = read_seqbegin(&bb->lock); |
| 8910 | |
| 8911 | len = 0; |
| 8912 | i = 0; |
| 8913 | |
| 8914 | while (len < PAGE_SIZE && i < bb->count) { |
| 8915 | sector_t s = BB_OFFSET(p[i]); |
| 8916 | unsigned int length = BB_LEN(p[i]); |
| 8917 | int ack = BB_ACK(p[i]); |
| 8918 | i++; |
| 8919 | |
| 8920 | if (unack && ack) |
| 8921 | continue; |
| 8922 | |
| 8923 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8924 | (unsigned long long)s << bb->shift, |
| 8925 | length << bb->shift); |
| 8926 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8927 | if (unack && len == 0) |
| 8928 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8929 | |
| 8930 | if (read_seqretry(&bb->lock, seq)) |
| 8931 | goto retry; |
| 8932 | |
| 8933 | return len; |
| 8934 | } |
| 8935 | |
| 8936 | #define DO_DEBUG 1 |
| 8937 | |
| 8938 | static ssize_t |
| 8939 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8940 | { |
| 8941 | unsigned long long sector; |
| 8942 | int length; |
| 8943 | char newline; |
| 8944 | #ifdef DO_DEBUG |
| 8945 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8946 | * Normally only a successful write may clear a badblock |
| 8947 | */ |
| 8948 | int clear = 0; |
| 8949 | if (page[0] == '-') { |
| 8950 | clear = 1; |
| 8951 | page++; |
| 8952 | } |
| 8953 | #endif /* DO_DEBUG */ |
| 8954 | |
| 8955 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8956 | case 3: |
| 8957 | if (newline != '\n') |
| 8958 | return -EINVAL; |
| 8959 | case 2: |
| 8960 | if (length <= 0) |
| 8961 | return -EINVAL; |
| 8962 | break; |
| 8963 | default: |
| 8964 | return -EINVAL; |
| 8965 | } |
| 8966 | |
| 8967 | #ifdef DO_DEBUG |
| 8968 | if (clear) { |
| 8969 | md_clear_badblocks(bb, sector, length); |
| 8970 | return len; |
| 8971 | } |
| 8972 | #endif /* DO_DEBUG */ |
| 8973 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8974 | return len; |
| 8975 | else |
| 8976 | return -ENOSPC; |
| 8977 | } |
| 8978 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8979 | static int md_notify_reboot(struct notifier_block *this, |
| 8980 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8981 | { |
| 8982 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8983 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8984 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8985 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8986 | for_each_mddev(mddev, tmp) { |
| 8987 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8988 | if (mddev->pers) |
| 8989 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 8990 | if (mddev->persistent) |
| 8991 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8992 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8993 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8994 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8995 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8996 | /* |
| 8997 | * certain more exotic SCSI devices are known to be |
| 8998 | * volatile wrt too early system reboots. While the |
| 8999 | * right place to handle this issue is the given |
| 9000 | * driver, we do want to have a safe RAID driver ... |
| 9001 | */ |
| 9002 | if (need_delay) |
| 9003 | mdelay(1000*1); |
| 9004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9005 | return NOTIFY_DONE; |
| 9006 | } |
| 9007 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9008 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9009 | .notifier_call = md_notify_reboot, |
| 9010 | .next = NULL, |
| 9011 | .priority = INT_MAX, /* before any real devices */ |
| 9012 | }; |
| 9013 | |
| 9014 | static void md_geninit(void) |
| 9015 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 9016 | 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] | 9017 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 9018 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9019 | } |
| 9020 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9021 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9022 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9023 | int ret = -ENOMEM; |
| 9024 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 9025 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9026 | if (!md_wq) |
| 9027 | goto err_wq; |
| 9028 | |
| 9029 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 9030 | if (!md_misc_wq) |
| 9031 | goto err_misc_wq; |
| 9032 | |
| 9033 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 9034 | goto err_md; |
| 9035 | |
| 9036 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 9037 | goto err_mdp; |
| 9038 | mdp_major = ret; |
| 9039 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 9040 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 9041 | md_probe, NULL, NULL); |
| 9042 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9043 | md_probe, NULL, NULL); |
| 9044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9045 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 9046 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9047 | |
| 9048 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 9049 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9050 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9051 | err_mdp: |
| 9052 | unregister_blkdev(MD_MAJOR, "md"); |
| 9053 | err_md: |
| 9054 | destroy_workqueue(md_misc_wq); |
| 9055 | err_misc_wq: |
| 9056 | destroy_workqueue(md_wq); |
| 9057 | err_wq: |
| 9058 | return ret; |
| 9059 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9060 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9061 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9062 | { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9063 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 9064 | struct md_rdev *rdev2; |
| 9065 | int role, ret; |
| 9066 | char b[BDEVNAME_SIZE]; |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9067 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9068 | /* Check for change of roles in the active devices */ |
| 9069 | rdev_for_each(rdev2, mddev) { |
| 9070 | if (test_bit(Faulty, &rdev2->flags)) |
| 9071 | continue; |
| 9072 | |
| 9073 | /* Check if the roles changed */ |
| 9074 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 9075 | |
| 9076 | if (test_bit(Candidate, &rdev2->flags)) { |
| 9077 | if (role == 0xfffe) { |
| 9078 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); |
| 9079 | md_kick_rdev_from_array(rdev2); |
| 9080 | continue; |
| 9081 | } |
| 9082 | else |
| 9083 | clear_bit(Candidate, &rdev2->flags); |
| 9084 | } |
| 9085 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9086 | if (role != rdev2->raid_disk) { |
| 9087 | /* got activated */ |
| 9088 | if (rdev2->raid_disk == -1 && role != 0xffff) { |
| 9089 | rdev2->saved_raid_disk = role; |
| 9090 | ret = remove_and_add_spares(mddev, rdev2); |
| 9091 | pr_info("Activated spare: %s\n", |
| 9092 | bdevname(rdev2->bdev,b)); |
| 9093 | continue; |
| 9094 | } |
| 9095 | /* device faulty |
| 9096 | * We just want to do the minimum to mark the disk |
| 9097 | * as faulty. The recovery is performed by the |
| 9098 | * one who initiated the error. |
| 9099 | */ |
| 9100 | if ((role == 0xfffe) || (role == 0xfffd)) { |
| 9101 | md_error(mddev, rdev2); |
| 9102 | clear_bit(Blocked, &rdev2->flags); |
| 9103 | } |
| 9104 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9105 | } |
| 9106 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 9107 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) |
| 9108 | update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9109 | |
| 9110 | /* Finally set the event to be up to date */ |
| 9111 | mddev->events = le64_to_cpu(sb->events); |
| 9112 | } |
| 9113 | |
| 9114 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) |
| 9115 | { |
| 9116 | int err; |
| 9117 | struct page *swapout = rdev->sb_page; |
| 9118 | struct mdp_superblock_1 *sb; |
| 9119 | |
| 9120 | /* Store the sb page of the rdev in the swapout temporary |
| 9121 | * variable in case we err in the future |
| 9122 | */ |
| 9123 | rdev->sb_page = NULL; |
| 9124 | alloc_disk_sb(rdev); |
| 9125 | ClearPageUptodate(rdev->sb_page); |
| 9126 | rdev->sb_loaded = 0; |
| 9127 | err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version); |
| 9128 | |
| 9129 | if (err < 0) { |
| 9130 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", |
| 9131 | __func__, __LINE__, rdev->desc_nr, err); |
| 9132 | put_page(rdev->sb_page); |
| 9133 | rdev->sb_page = swapout; |
| 9134 | rdev->sb_loaded = 1; |
| 9135 | return err; |
| 9136 | } |
| 9137 | |
| 9138 | sb = page_address(rdev->sb_page); |
| 9139 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET |
| 9140 | * is not set |
| 9141 | */ |
| 9142 | |
| 9143 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) |
| 9144 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 9145 | |
| 9146 | /* The other node finished recovery, call spare_active to set |
| 9147 | * device In_sync and mddev->degraded |
| 9148 | */ |
| 9149 | if (rdev->recovery_offset == MaxSector && |
| 9150 | !test_bit(In_sync, &rdev->flags) && |
| 9151 | mddev->pers->spare_active(mddev)) |
| 9152 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
| 9153 | |
| 9154 | put_page(swapout); |
| 9155 | return 0; |
| 9156 | } |
| 9157 | |
| 9158 | void md_reload_sb(struct mddev *mddev, int nr) |
| 9159 | { |
| 9160 | struct md_rdev *rdev; |
| 9161 | int err; |
| 9162 | |
| 9163 | /* Find the rdev */ |
| 9164 | rdev_for_each_rcu(rdev, mddev) { |
| 9165 | if (rdev->desc_nr == nr) |
| 9166 | break; |
| 9167 | } |
| 9168 | |
| 9169 | if (!rdev || rdev->desc_nr != nr) { |
| 9170 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); |
| 9171 | return; |
| 9172 | } |
| 9173 | |
| 9174 | err = read_rdev(mddev, rdev); |
| 9175 | if (err < 0) |
| 9176 | return; |
| 9177 | |
| 9178 | check_sb_changes(mddev, rdev); |
| 9179 | |
| 9180 | /* Read all rdev's to update recovery_offset */ |
| 9181 | rdev_for_each_rcu(rdev, mddev) |
| 9182 | read_rdev(mddev, rdev); |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9183 | } |
| 9184 | EXPORT_SYMBOL(md_reload_sb); |
| 9185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9186 | #ifndef MODULE |
| 9187 | |
| 9188 | /* |
| 9189 | * Searches all registered partitions for autorun RAID arrays |
| 9190 | * at boot time. |
| 9191 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9192 | |
| 9193 | static LIST_HEAD(all_detected_devices); |
| 9194 | struct detected_devices_node { |
| 9195 | struct list_head list; |
| 9196 | dev_t dev; |
| 9197 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9198 | |
| 9199 | void md_autodetect_dev(dev_t dev) |
| 9200 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9201 | struct detected_devices_node *node_detected_dev; |
| 9202 | |
| 9203 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 9204 | if (node_detected_dev) { |
| 9205 | node_detected_dev->dev = dev; |
| 9206 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 9207 | } else { |
| 9208 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 9209 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 9210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9211 | } |
| 9212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9213 | static void autostart_arrays(int part) |
| 9214 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9215 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9216 | struct detected_devices_node *node_detected_dev; |
| 9217 | dev_t dev; |
| 9218 | int i_scanned, i_passed; |
| 9219 | |
| 9220 | i_scanned = 0; |
| 9221 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9222 | |
| 9223 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 9224 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9225 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 9226 | i_scanned++; |
| 9227 | node_detected_dev = list_entry(all_detected_devices.next, |
| 9228 | struct detected_devices_node, list); |
| 9229 | list_del(&node_detected_dev->list); |
| 9230 | dev = node_detected_dev->dev; |
| 9231 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 9232 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9233 | if (IS_ERR(rdev)) |
| 9234 | continue; |
| 9235 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9236 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9237 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9238 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 9239 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9240 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9241 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9242 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9243 | |
| 9244 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 9245 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9246 | |
| 9247 | autorun_devices(part); |
| 9248 | } |
| 9249 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 9250 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9251 | |
| 9252 | static __exit void md_exit(void) |
| 9253 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9254 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9255 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9256 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 9257 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 9258 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 9259 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9260 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 9261 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9262 | unregister_blkdev(mdp_major, "mdp"); |
| 9263 | unregister_reboot_notifier(&md_notifier); |
| 9264 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9265 | |
| 9266 | /* We cannot unload the modules while some process is |
| 9267 | * waiting for us in select() or poll() - wake them up |
| 9268 | */ |
| 9269 | md_unloading = 1; |
| 9270 | while (waitqueue_active(&md_event_waiters)) { |
| 9271 | /* not safe to leave yet */ |
| 9272 | wake_up(&md_event_waiters); |
| 9273 | msleep(delay); |
| 9274 | delay += delay; |
| 9275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9276 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9277 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 9278 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9279 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9280 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9281 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9282 | destroy_workqueue(md_misc_wq); |
| 9283 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9284 | } |
| 9285 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 9286 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9287 | module_exit(md_exit) |
| 9288 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9289 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 9290 | { |
| 9291 | return sprintf(buffer, "%d", start_readonly); |
| 9292 | } |
| 9293 | static int set_ro(const char *val, struct kernel_param *kp) |
| 9294 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 9295 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9296 | } |
| 9297 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 9298 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 9299 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9300 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9302 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 9303 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 9304 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 9305 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |