Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 39 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 40 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 41 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 42 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 43 | #include <linux/hdreg.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/random.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 46 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 47 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 49 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 50 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/raid/md_p.h> |
| 52 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 56 | #include "md-cluster.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 59 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 62 | /* pers_list is a list of registered personalities protected |
| 63 | * by pers_lock. |
| 64 | * pers_lock does extra service to protect accesses to |
| 65 | * mddev->thread when the mutex cannot be held. |
| 66 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 67 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static DEFINE_SPINLOCK(pers_lock); |
| 69 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 70 | struct md_cluster_operations *md_cluster_ops; |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame^] | 71 | EXPORT_SYMBOL(md_cluster_ops); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 72 | struct module *md_cluster_mod; |
| 73 | EXPORT_SYMBOL(md_cluster_mod); |
| 74 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 75 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 76 | static struct workqueue_struct *md_wq; |
| 77 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 78 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 79 | static int remove_and_add_spares(struct mddev *mddev, |
| 80 | struct md_rdev *this); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 81 | static void mddev_detach(struct mddev *mddev); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 84 | * Default number of read corrections we'll attempt on an rdev |
| 85 | * before ejecting it from the array. We divide the read error |
| 86 | * count by 2 for every hour elapsed between read errors. |
| 87 | */ |
| 88 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 89 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 91 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 92 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 93 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 95 | * speed limit - in case reconstruction slows down your system despite |
| 96 | * idle IO detection. |
| 97 | * |
| 98 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 99 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | */ |
| 101 | |
| 102 | static int sysctl_speed_limit_min = 1000; |
| 103 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 104 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 105 | { |
| 106 | return mddev->sync_speed_min ? |
| 107 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 108 | } |
| 109 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 110 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 111 | { |
| 112 | return mddev->sync_speed_max ? |
| 113 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | static struct ctl_table_header *raid_table_header; |
| 117 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 118 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | .procname = "speed_limit_min", |
| 121 | .data = &sysctl_speed_limit_min, |
| 122 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 123 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 124 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | }, |
| 126 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | .procname = "speed_limit_max", |
| 128 | .data = &sysctl_speed_limit_max, |
| 129 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 130 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 131 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 133 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 136 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | .procname = "raid", |
| 139 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 140 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | .child = raid_table, |
| 142 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 143 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 146 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | .procname = "dev", |
| 149 | .maxlen = 0, |
| 150 | .mode = 0555, |
| 151 | .child = raid_dir_table, |
| 152 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 153 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 156 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 158 | static int start_readonly; |
| 159 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 160 | /* bio_clone_mddev |
| 161 | * like bio_clone, but with a local bio set |
| 162 | */ |
| 163 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 164 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 165 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 166 | { |
| 167 | struct bio *b; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 168 | |
| 169 | if (!mddev || !mddev->bio_set) |
| 170 | return bio_alloc(gfp_mask, nr_iovecs); |
| 171 | |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 172 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 173 | if (!b) |
| 174 | return NULL; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 175 | return b; |
| 176 | } |
| 177 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 178 | |
| 179 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 180 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 181 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 182 | if (!mddev || !mddev->bio_set) |
| 183 | return bio_clone(bio, gfp_mask); |
| 184 | |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 185 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 186 | } |
| 187 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 190 | * We have a system wide 'event count' that is incremented |
| 191 | * on any 'interesting' event, and readers of /proc/mdstat |
| 192 | * can use 'poll' or 'select' to find out when the event |
| 193 | * count increases. |
| 194 | * |
| 195 | * Events are: |
| 196 | * start array, stop array, error, add device, remove device, |
| 197 | * start build, activate spare |
| 198 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 199 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 200 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 201 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 202 | { |
| 203 | atomic_inc(&md_event_count); |
| 204 | wake_up(&md_event_waiters); |
| 205 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 206 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 207 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 208 | /* Alternate version that can be called from interrupts |
| 209 | * when calling sysfs_notify isn't needed. |
| 210 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 211 | static void md_new_event_inintr(struct mddev *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 212 | { |
| 213 | atomic_inc(&md_event_count); |
| 214 | wake_up(&md_event_waiters); |
| 215 | } |
| 216 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 217 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | * Enables to iterate over all existing md arrays |
| 219 | * all_mddevs_lock protects this list. |
| 220 | */ |
| 221 | static LIST_HEAD(all_mddevs); |
| 222 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | /* |
| 225 | * iterates through all used mddevs in the system. |
| 226 | * We take care to grab the all_mddevs_lock whenever navigating |
| 227 | * the list, and to always hold a refcount when unlocked. |
| 228 | * Any code which breaks out of this loop while own |
| 229 | * a reference to the current mddev and must mddev_put it. |
| 230 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 231 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | \ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 233 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 234 | _tmp = all_mddevs.next; \ |
| 235 | _mddev = NULL;}); \ |
| 236 | ({ if (_tmp != &all_mddevs) \ |
| 237 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 239 | if (_mddev) mddev_put(_mddev); \ |
| 240 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 241 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 243 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | ) |
| 245 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 246 | /* Rather than calling directly into the personality make_request function, |
| 247 | * IO requests come here first so that we can check if the device is |
| 248 | * being suspended pending a reconfiguration. |
| 249 | * We hold a refcount over the call to ->make_request. By the time that |
| 250 | * call has finished, the bio has been linked into some internal structure |
| 251 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 252 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 253 | static void md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 255 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 256 | struct mddev *mddev = q->queuedata; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 257 | unsigned int sectors; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 258 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 259 | if (mddev == NULL || mddev->pers == NULL |
| 260 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 261 | bio_io_error(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 262 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 263 | } |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 264 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
| 265 | bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS); |
| 266 | return; |
| 267 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 268 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 269 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 270 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 271 | DEFINE_WAIT(__wait); |
| 272 | for (;;) { |
| 273 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 274 | TASK_UNINTERRUPTIBLE); |
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 | break; |
| 277 | rcu_read_unlock(); |
| 278 | schedule(); |
| 279 | rcu_read_lock(); |
| 280 | } |
| 281 | finish_wait(&mddev->sb_wait, &__wait); |
| 282 | } |
| 283 | atomic_inc(&mddev->active_io); |
| 284 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 285 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 286 | /* |
| 287 | * save the sectors now since our bio can |
| 288 | * go away inside make_request |
| 289 | */ |
| 290 | sectors = bio_sectors(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 291 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 292 | |
Gu Zheng | 18c0b22 | 2014-11-24 11:05:26 +0800 | [diff] [blame] | 293 | generic_start_io_acct(rw, sectors, &mddev->gendisk->part0); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 294 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 295 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 296 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 299 | /* mddev_suspend makes sure no new requests are submitted |
| 300 | * to the device, and that any requests that have been submitted |
| 301 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 302 | * Once mddev_detach() is called and completes, the module will be |
| 303 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 304 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 305 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 306 | { |
| 307 | BUG_ON(mddev->suspended); |
| 308 | mddev->suspended = 1; |
| 309 | synchronize_rcu(); |
| 310 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 311 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 312 | |
| 313 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 314 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 315 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 316 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 317 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 318 | { |
| 319 | mddev->suspended = 0; |
| 320 | wake_up(&mddev->sb_wait); |
| 321 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 322 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 323 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 324 | md_wakeup_thread(mddev->thread); |
| 325 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 326 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 327 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 328 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 329 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 330 | { |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 331 | struct md_personality *pers = mddev->pers; |
| 332 | int ret = 0; |
| 333 | |
| 334 | rcu_read_lock(); |
| 335 | if (mddev->suspended) |
| 336 | ret = 1; |
| 337 | else if (pers && pers->congested) |
| 338 | ret = pers->congested(mddev, bits); |
| 339 | rcu_read_unlock(); |
| 340 | return ret; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 341 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 342 | EXPORT_SYMBOL_GPL(mddev_congested); |
| 343 | static int md_congested(void *data, int bits) |
| 344 | { |
| 345 | struct mddev *mddev = data; |
| 346 | return mddev_congested(mddev, bits); |
| 347 | } |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 348 | |
NeilBrown | 64590f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 349 | static int md_mergeable_bvec(struct request_queue *q, |
| 350 | struct bvec_merge_data *bvm, |
| 351 | struct bio_vec *biovec) |
| 352 | { |
| 353 | struct mddev *mddev = q->queuedata; |
| 354 | int ret; |
| 355 | rcu_read_lock(); |
| 356 | if (mddev->suspended) { |
| 357 | /* Must always allow one vec */ |
| 358 | if (bvm->bi_size == 0) |
| 359 | ret = biovec->bv_len; |
| 360 | else |
| 361 | ret = 0; |
| 362 | } else { |
| 363 | struct md_personality *pers = mddev->pers; |
| 364 | if (pers && pers->mergeable_bvec) |
| 365 | ret = pers->mergeable_bvec(mddev, bvm, biovec); |
| 366 | else |
| 367 | ret = biovec->bv_len; |
| 368 | } |
| 369 | rcu_read_unlock(); |
| 370 | return ret; |
| 371 | } |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 372 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 373 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 374 | */ |
| 375 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 376 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 377 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 378 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 379 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 380 | |
| 381 | rdev_dec_pending(rdev, mddev); |
| 382 | |
| 383 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 384 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 385 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 386 | } |
| 387 | bio_put(bio); |
| 388 | } |
| 389 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 390 | static void md_submit_flush_data(struct work_struct *ws); |
| 391 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 392 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 393 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 394 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 395 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 396 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 397 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 398 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 399 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 400 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 401 | if (rdev->raid_disk >= 0 && |
| 402 | !test_bit(Faulty, &rdev->flags)) { |
| 403 | /* Take two references, one is dropped |
| 404 | * when request finishes, one after |
| 405 | * we reclaim rcu_read_lock |
| 406 | */ |
| 407 | struct bio *bi; |
| 408 | atomic_inc(&rdev->nr_pending); |
| 409 | atomic_inc(&rdev->nr_pending); |
| 410 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 411 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 412 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 413 | bi->bi_private = rdev; |
| 414 | bi->bi_bdev = rdev->bdev; |
| 415 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 416 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 417 | rcu_read_lock(); |
| 418 | rdev_dec_pending(rdev, mddev); |
| 419 | } |
| 420 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 421 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 422 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 423 | } |
| 424 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 425 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 426 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 427 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 428 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 429 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 430 | if (bio->bi_iter.bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 431 | /* an empty barrier - all done */ |
| 432 | bio_endio(bio, 0); |
| 433 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 434 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 435 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 436 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 437 | |
| 438 | mddev->flush_bio = NULL; |
| 439 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 440 | } |
| 441 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 442 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 443 | { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 444 | spin_lock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 445 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 446 | !mddev->flush_bio, |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 447 | mddev->lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 448 | mddev->flush_bio = bio; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 449 | spin_unlock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 450 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 451 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 452 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 453 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 454 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 455 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 456 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 457 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 458 | struct mddev *mddev = cb->data; |
| 459 | md_wakeup_thread(mddev->thread); |
| 460 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 461 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 462 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 463 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 464 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | { |
| 466 | atomic_inc(&mddev->active); |
| 467 | return mddev; |
| 468 | } |
| 469 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 470 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 471 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 472 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 474 | struct bio_set *bs = NULL; |
| 475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 477 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 478 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 479 | mddev->ctime == 0 && !mddev->hold_active) { |
| 480 | /* Array is not configured at all, and not held active, |
| 481 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 482 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 483 | bs = mddev->bio_set; |
| 484 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 485 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 486 | /* We did a probe so need to clean up. Call |
| 487 | * queue_work inside the spinlock so that |
| 488 | * flush_workqueue() after mddev_find will |
| 489 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 490 | */ |
| 491 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 492 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 493 | } else |
| 494 | kfree(mddev); |
| 495 | } |
| 496 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 497 | if (bs) |
| 498 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | } |
| 500 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 501 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 502 | { |
| 503 | mutex_init(&mddev->open_mutex); |
| 504 | mutex_init(&mddev->reconfig_mutex); |
| 505 | mutex_init(&mddev->bitmap_info.mutex); |
| 506 | INIT_LIST_HEAD(&mddev->disks); |
| 507 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 508 | init_timer(&mddev->safemode_timer); |
| 509 | atomic_set(&mddev->active, 1); |
| 510 | atomic_set(&mddev->openers, 0); |
| 511 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 512 | spin_lock_init(&mddev->lock); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 513 | atomic_set(&mddev->flush_pending, 0); |
| 514 | init_waitqueue_head(&mddev->sb_wait); |
| 515 | init_waitqueue_head(&mddev->recovery_wait); |
| 516 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 517 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 518 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 519 | mddev->resync_min = 0; |
| 520 | mddev->resync_max = MaxSector; |
| 521 | mddev->level = LEVEL_NONE; |
| 522 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 523 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 524 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 525 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 527 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 529 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 530 | unit &= ~((1<<MdpMinorShift)-1); |
| 531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | retry: |
| 533 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 535 | if (unit) { |
| 536 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 537 | if (mddev->unit == unit) { |
| 538 | mddev_get(mddev); |
| 539 | spin_unlock(&all_mddevs_lock); |
| 540 | kfree(new); |
| 541 | return mddev; |
| 542 | } |
| 543 | |
| 544 | if (new) { |
| 545 | list_add(&new->all_mddevs, &all_mddevs); |
| 546 | spin_unlock(&all_mddevs_lock); |
| 547 | new->hold_active = UNTIL_IOCTL; |
| 548 | return new; |
| 549 | } |
| 550 | } else if (new) { |
| 551 | /* find an unused unit number */ |
| 552 | static int next_minor = 512; |
| 553 | int start = next_minor; |
| 554 | int is_free = 0; |
| 555 | int dev = 0; |
| 556 | while (!is_free) { |
| 557 | dev = MKDEV(MD_MAJOR, next_minor); |
| 558 | next_minor++; |
| 559 | if (next_minor > MINORMASK) |
| 560 | next_minor = 0; |
| 561 | if (next_minor == start) { |
| 562 | /* Oh dear, all in use. */ |
| 563 | spin_unlock(&all_mddevs_lock); |
| 564 | kfree(new); |
| 565 | return NULL; |
| 566 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 567 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 568 | is_free = 1; |
| 569 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 570 | if (mddev->unit == dev) { |
| 571 | is_free = 0; |
| 572 | break; |
| 573 | } |
| 574 | } |
| 575 | new->unit = dev; |
| 576 | new->md_minor = MINOR(dev); |
| 577 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | list_add(&new->all_mddevs, &all_mddevs); |
| 579 | spin_unlock(&all_mddevs_lock); |
| 580 | return new; |
| 581 | } |
| 582 | spin_unlock(&all_mddevs_lock); |
| 583 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 584 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | if (!new) |
| 586 | return NULL; |
| 587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | new->unit = unit; |
| 589 | if (MAJOR(unit) == MD_MAJOR) |
| 590 | new->md_minor = MINOR(unit); |
| 591 | else |
| 592 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 593 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 594 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | goto retry; |
| 597 | } |
| 598 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 599 | static struct attribute_group md_redundancy_group; |
| 600 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 601 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 603 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 604 | /* These cannot be removed under reconfig_mutex as |
| 605 | * an access to the files will try to take reconfig_mutex |
| 606 | * while holding the file unremovable, which leads to |
| 607 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 608 | * So hold set sysfs_active while the remove in happeing, |
| 609 | * and anything else which might set ->to_remove or my |
| 610 | * otherwise change the sysfs namespace will fail with |
| 611 | * -EBUSY if sysfs_active is still set. |
| 612 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 613 | * test it under the same mutex to ensure its correct value |
| 614 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 615 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 616 | struct attribute_group *to_remove = mddev->to_remove; |
| 617 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 618 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 619 | mutex_unlock(&mddev->reconfig_mutex); |
| 620 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 621 | if (mddev->kobj.sd) { |
| 622 | if (to_remove != &md_redundancy_group) |
| 623 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 624 | if (mddev->pers == NULL || |
| 625 | mddev->pers->sync_request == NULL) { |
| 626 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 627 | if (mddev->sysfs_action) |
| 628 | sysfs_put(mddev->sysfs_action); |
| 629 | mddev->sysfs_action = NULL; |
| 630 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 631 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 632 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 633 | } else |
| 634 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 636 | /* As we've dropped the mutex we need a spinlock to |
| 637 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 638 | */ |
| 639 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 640 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 641 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 643 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 645 | static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr) |
| 646 | { |
| 647 | struct md_rdev *rdev; |
| 648 | |
| 649 | rdev_for_each_rcu(rdev, mddev) |
| 650 | if (rdev->desc_nr == nr) |
| 651 | return rdev; |
| 652 | |
| 653 | return NULL; |
| 654 | } |
| 655 | |
| 656 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 658 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 660 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | if (rdev->bdev->bd_dev == dev) |
| 662 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | return NULL; |
| 665 | } |
| 666 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 667 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 668 | { |
| 669 | struct md_rdev *rdev; |
| 670 | |
| 671 | rdev_for_each_rcu(rdev, mddev) |
| 672 | if (rdev->bdev->bd_dev == dev) |
| 673 | return rdev; |
| 674 | |
| 675 | return NULL; |
| 676 | } |
| 677 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 678 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 679 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 680 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 681 | list_for_each_entry(pers, &pers_list, list) { |
| 682 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 683 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 684 | if (strcmp(pers->name, clevel)==0) |
| 685 | return pers; |
| 686 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 687 | return NULL; |
| 688 | } |
| 689 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 690 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 691 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 693 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 694 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } |
| 696 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 697 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 700 | if (!rdev->sb_page) { |
| 701 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 702 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | return 0; |
| 706 | } |
| 707 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 708 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | { |
| 710 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 711 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | rdev->sb_loaded = 0; |
| 713 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 714 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 715 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 717 | if (rdev->bb_page) { |
| 718 | put_page(rdev->bb_page); |
| 719 | rdev->bb_page = NULL; |
| 720 | } |
NeilBrown | 4fa2f32 | 2012-05-22 13:55:01 +1000 | [diff] [blame] | 721 | kfree(rdev->badblocks.page); |
| 722 | rdev->badblocks.page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 724 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 726 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 727 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 728 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 729 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 730 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 731 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 732 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 733 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 734 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 735 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 736 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 737 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 738 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 739 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 740 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 741 | } |
| 742 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 743 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 744 | sector_t sector, int size, struct page *page) |
| 745 | { |
| 746 | /* write first size bytes of page to sector of rdev |
| 747 | * Increment mddev->pending_writes before returning |
| 748 | * and decrement it on completion, waking up sb_wait |
| 749 | * if zero is reached. |
| 750 | * If an error occurred, call md_error |
| 751 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 752 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 753 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 754 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 755 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 756 | bio_add_page(bio, page, size, 0); |
| 757 | bio->bi_private = rdev; |
| 758 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 759 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 760 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 761 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 762 | } |
| 763 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 764 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 765 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 766 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 767 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 768 | } |
| 769 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 770 | 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] | 771 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 773 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | int ret; |
| 775 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 776 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 777 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 778 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 779 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 780 | else if (rdev->mddev->reshape_position != MaxSector && |
| 781 | (rdev->mddev->reshape_backwards == |
| 782 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 783 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 784 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 785 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | bio_add_page(bio, page, size, 0); |
Kent Overstreet | c170bbb | 2013-11-24 16:32:22 -0700 | [diff] [blame] | 787 | submit_bio_wait(rw, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
| 789 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 790 | bio_put(bio); |
| 791 | return ret; |
| 792 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 793 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 795 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | { |
| 797 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | if (rdev->sb_loaded) |
| 800 | return 0; |
| 801 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 802 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | goto fail; |
| 804 | rdev->sb_loaded = 1; |
| 805 | return 0; |
| 806 | |
| 807 | fail: |
| 808 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 809 | bdevname(rdev->bdev,b)); |
| 810 | return -EINVAL; |
| 811 | } |
| 812 | |
| 813 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 814 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 815 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 816 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 817 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 818 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | } |
| 820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 822 | { |
| 823 | int ret; |
| 824 | mdp_super_t *tmp1, *tmp2; |
| 825 | |
| 826 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 827 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 828 | |
| 829 | if (!tmp1 || !tmp2) { |
| 830 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 831 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | goto abort; |
| 833 | } |
| 834 | |
| 835 | *tmp1 = *sb1; |
| 836 | *tmp2 = *sb2; |
| 837 | |
| 838 | /* |
| 839 | * nr_disks is not constant |
| 840 | */ |
| 841 | tmp1->nr_disks = 0; |
| 842 | tmp2->nr_disks = 0; |
| 843 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 844 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 846 | kfree(tmp1); |
| 847 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | return ret; |
| 849 | } |
| 850 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 851 | static u32 md_csum_fold(u32 csum) |
| 852 | { |
| 853 | csum = (csum & 0xffff) + (csum >> 16); |
| 854 | return (csum & 0xffff) + (csum >> 16); |
| 855 | } |
| 856 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 857 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 859 | u64 newcsum = 0; |
| 860 | u32 *sb32 = (u32*)sb; |
| 861 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | unsigned int disk_csum, csum; |
| 863 | |
| 864 | disk_csum = sb->sb_csum; |
| 865 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 866 | |
| 867 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 868 | newcsum += sb32[i]; |
| 869 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 870 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 871 | #ifdef CONFIG_ALPHA |
| 872 | /* This used to use csum_partial, which was wrong for several |
| 873 | * reasons including that different results are returned on |
| 874 | * different architectures. It isn't critical that we get exactly |
| 875 | * the same return value as before (we always csum_fold before |
| 876 | * testing, and that removes any differences). However as we |
| 877 | * know that csum_partial always returned a 16bit value on |
| 878 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 879 | */ |
| 880 | sb->sb_csum = md_csum_fold(disk_csum); |
| 881 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 883 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | return csum; |
| 885 | } |
| 886 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | /* |
| 888 | * Handle superblock details. |
| 889 | * We want to be able to handle multiple superblock formats |
| 890 | * so we have a common interface to them all, and an array of |
| 891 | * different handlers. |
| 892 | * We rely on user-space to write the initial superblock, and support |
| 893 | * reading and updating of superblocks. |
| 894 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 895 | * 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] | 896 | * loads and validates a superblock on dev. |
| 897 | * if refdev != NULL, compare superblocks on both devices |
| 898 | * Return: |
| 899 | * 0 - dev has a superblock that is compatible with refdev |
| 900 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 901 | * so dev should be used as the refdev in future |
| 902 | * -EINVAL superblock incompatible or invalid |
| 903 | * -othererror e.g. -EIO |
| 904 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 905 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | * Verify that dev is acceptable into mddev. |
| 907 | * The first time, mddev->raid_disks will be 0, and data from |
| 908 | * dev should be merged in. Subsequent calls check that dev |
| 909 | * is new enough. Return 0 or -EINVAL |
| 910 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 911 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | * Update the superblock for rdev with data in mddev |
| 913 | * This does not write to disc. |
| 914 | * |
| 915 | */ |
| 916 | |
| 917 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 918 | char *name; |
| 919 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 920 | int (*load_super)(struct md_rdev *rdev, |
| 921 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 922 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 923 | int (*validate_super)(struct mddev *mddev, |
| 924 | struct md_rdev *rdev); |
| 925 | void (*sync_super)(struct mddev *mddev, |
| 926 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 927 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 928 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 929 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 930 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | }; |
| 932 | |
| 933 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 934 | * Check that the given mddev has no bitmap. |
| 935 | * |
| 936 | * This function is called from the run method of all personalities that do not |
| 937 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 938 | * has a bitmap. Otherwise, it returns 0. |
| 939 | * |
| 940 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 941 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 942 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 943 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 944 | return 0; |
| 945 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 946 | mdname(mddev), mddev->pers->name); |
| 947 | return 1; |
| 948 | } |
| 949 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 950 | |
| 951 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 952 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 954 | 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] | 955 | { |
| 956 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 957 | mdp_super_t *sb; |
| 958 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | |
| 960 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 961 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | * it's at the end of the disk. |
| 963 | * |
| 964 | * It also happens to be a multiple of 4Kb. |
| 965 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 966 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 968 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | if (ret) return ret; |
| 970 | |
| 971 | ret = -EINVAL; |
| 972 | |
| 973 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 974 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | |
| 976 | if (sb->md_magic != MD_SB_MAGIC) { |
| 977 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 978 | b); |
| 979 | goto abort; |
| 980 | } |
| 981 | |
| 982 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 983 | sb->minor_version < 90 || |
| 984 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 986 | sb->major_version, sb->minor_version, |
| 987 | b); |
| 988 | goto abort; |
| 989 | } |
| 990 | |
| 991 | if (sb->raid_disks <= 0) |
| 992 | goto abort; |
| 993 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 994 | 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] | 995 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 996 | b); |
| 997 | goto abort; |
| 998 | } |
| 999 | |
| 1000 | rdev->preferred_minor = sb->md_minor; |
| 1001 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1002 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1003 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1004 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
| 1006 | if (sb->level == LEVEL_MULTIPATH) |
| 1007 | rdev->desc_nr = -1; |
| 1008 | else |
| 1009 | rdev->desc_nr = sb->this_disk.number; |
| 1010 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1011 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1013 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1015 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | if (!uuid_equal(refsb, sb)) { |
| 1017 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1018 | b, bdevname(refdev->bdev,b2)); |
| 1019 | goto abort; |
| 1020 | } |
| 1021 | if (!sb_equal(refsb, sb)) { |
| 1022 | printk(KERN_WARNING "md: %s has same UUID" |
| 1023 | " but different superblock to %s\n", |
| 1024 | b, bdevname(refdev->bdev, b2)); |
| 1025 | goto abort; |
| 1026 | } |
| 1027 | ev1 = md_event(sb); |
| 1028 | ev2 = md_event(refsb); |
| 1029 | if (ev1 > ev2) |
| 1030 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1031 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | ret = 0; |
| 1033 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1034 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1035 | /* Limit to 4TB as metadata cannot record more than that. |
| 1036 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1037 | * record this size) |
| 1038 | */ |
| 1039 | if (rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1040 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1042 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1043 | /* "this cannot possibly happen" ... */ |
| 1044 | ret = -EINVAL; |
| 1045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | abort: |
| 1047 | return ret; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * validate_super for 0.90.0 |
| 1052 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1053 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | { |
| 1055 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1056 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1057 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1059 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1060 | clear_bit(Faulty, &rdev->flags); |
| 1061 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1062 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1063 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | if (mddev->raid_disks == 0) { |
| 1066 | mddev->major_version = 0; |
| 1067 | mddev->minor_version = sb->minor_version; |
| 1068 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1069 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1070 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | mddev->ctime = sb->ctime; |
| 1072 | mddev->utime = sb->utime; |
| 1073 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1074 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | mddev->layout = sb->layout; |
| 1076 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1077 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1078 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1079 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1080 | mddev->bitmap_info.space = 0; |
| 1081 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1082 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1083 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1084 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1086 | if (mddev->minor_version >= 91) { |
| 1087 | mddev->reshape_position = sb->reshape_position; |
| 1088 | mddev->delta_disks = sb->delta_disks; |
| 1089 | mddev->new_level = sb->new_level; |
| 1090 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1091 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1092 | if (mddev->delta_disks < 0) |
| 1093 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1094 | } else { |
| 1095 | mddev->reshape_position = MaxSector; |
| 1096 | mddev->delta_disks = 0; |
| 1097 | mddev->new_level = mddev->level; |
| 1098 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1099 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1100 | } |
| 1101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1103 | mddev->recovery_cp = MaxSector; |
| 1104 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1105 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | sb->events_lo == sb->cp_events_lo) { |
| 1107 | mddev->recovery_cp = sb->recovery_cp; |
| 1108 | } else |
| 1109 | mddev->recovery_cp = 0; |
| 1110 | } |
| 1111 | |
| 1112 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1113 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1114 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1115 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1116 | |
| 1117 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1118 | |
| 1119 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1120 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1121 | mddev->bitmap_info.offset = |
| 1122 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1123 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1124 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1125 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1126 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1127 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1128 | /* Insist on good event counter while assembling, except |
| 1129 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1131 | if (sb->disks[rdev->desc_nr].state & ( |
| 1132 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1133 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1134 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1135 | } else if (mddev->bitmap) { |
| 1136 | /* if adding to array with a bitmap, then we can accept an |
| 1137 | * older device ... but not too old. |
| 1138 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1139 | if (ev1 < mddev->bitmap->events_cleared) |
| 1140 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1141 | if (ev1 < mddev->events) |
| 1142 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1143 | } else { |
| 1144 | if (ev1 < mddev->events) |
| 1145 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1146 | return 0; |
| 1147 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | desc = sb->disks + rdev->desc_nr; |
| 1151 | |
| 1152 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1153 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1154 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1155 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1156 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1158 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1159 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1160 | /* active but not in sync implies recovery up to |
| 1161 | * reshape position. We don't know exactly where |
| 1162 | * that is, so set to zero for now */ |
| 1163 | if (mddev->minor_version >= 91) { |
| 1164 | rdev->recovery_offset = 0; |
| 1165 | rdev->raid_disk = desc->raid_disk; |
| 1166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1168 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1169 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1170 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1171 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | return 0; |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * sync_super for 0.90.0 |
| 1177 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1178 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | { |
| 1180 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1181 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | /* make rdev->sb match mddev data.. |
| 1185 | * |
| 1186 | * 1/ zero out disks |
| 1187 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1188 | * 3/ any empty disks < next_spare become removed |
| 1189 | * |
| 1190 | * disks[0] gets initialised to REMOVED because |
| 1191 | * we cannot be sure from other fields if it has |
| 1192 | * been initialised or not. |
| 1193 | */ |
| 1194 | int i; |
| 1195 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1196 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1197 | rdev->sb_size = MD_SB_BYTES; |
| 1198 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1199 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | |
| 1201 | memset(sb, 0, sizeof(*sb)); |
| 1202 | |
| 1203 | sb->md_magic = MD_SB_MAGIC; |
| 1204 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | sb->patch_version = mddev->patch_version; |
| 1206 | sb->gvalid_words = 0; /* ignored */ |
| 1207 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1208 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1209 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1210 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1211 | |
| 1212 | sb->ctime = mddev->ctime; |
| 1213 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1214 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | sb->raid_disks = mddev->raid_disks; |
| 1216 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1217 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | sb->utime = mddev->utime; |
| 1219 | sb->state = 0; |
| 1220 | sb->events_hi = (mddev->events>>32); |
| 1221 | sb->events_lo = (u32)mddev->events; |
| 1222 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1223 | if (mddev->reshape_position == MaxSector) |
| 1224 | sb->minor_version = 90; |
| 1225 | else { |
| 1226 | sb->minor_version = 91; |
| 1227 | sb->reshape_position = mddev->reshape_position; |
| 1228 | sb->new_level = mddev->new_level; |
| 1229 | sb->delta_disks = mddev->delta_disks; |
| 1230 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1231 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1232 | } |
| 1233 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | if (mddev->in_sync) |
| 1235 | { |
| 1236 | sb->recovery_cp = mddev->recovery_cp; |
| 1237 | sb->cp_events_hi = (mddev->events>>32); |
| 1238 | sb->cp_events_lo = (u32)mddev->events; |
| 1239 | if (mddev->recovery_cp == MaxSector) |
| 1240 | sb->state = (1<< MD_SB_CLEAN); |
| 1241 | } else |
| 1242 | sb->recovery_cp = 0; |
| 1243 | |
| 1244 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1245 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1247 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1248 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1251 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1253 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1254 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1255 | |
| 1256 | if (rdev2->raid_disk >= 0 && |
| 1257 | sb->minor_version >= 91) |
| 1258 | /* we have nowhere to store the recovery_offset, |
| 1259 | * but if it is not below the reshape_position, |
| 1260 | * we can piggy-back on that. |
| 1261 | */ |
| 1262 | is_active = 1; |
| 1263 | if (rdev2->raid_disk < 0 || |
| 1264 | test_bit(Faulty, &rdev2->flags)) |
| 1265 | is_active = 0; |
| 1266 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1267 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1269 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1270 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | d = &sb->disks[rdev2->desc_nr]; |
| 1272 | nr_disks++; |
| 1273 | d->number = rdev2->desc_nr; |
| 1274 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1275 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1276 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | d->raid_disk = rdev2->raid_disk; |
| 1278 | else |
| 1279 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1280 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1282 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1284 | if (test_bit(In_sync, &rdev2->flags)) |
| 1285 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | active++; |
| 1287 | working++; |
| 1288 | } else { |
| 1289 | d->state = 0; |
| 1290 | spare++; |
| 1291 | working++; |
| 1292 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1293 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1294 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1297 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1298 | mdp_disk_t *d = &sb->disks[i]; |
| 1299 | if (d->state == 0 && d->number == 0) { |
| 1300 | d->number = i; |
| 1301 | d->raid_disk = i; |
| 1302 | d->state = (1<<MD_DISK_REMOVED); |
| 1303 | d->state |= (1<<MD_DISK_FAULTY); |
| 1304 | failed++; |
| 1305 | } |
| 1306 | } |
| 1307 | sb->nr_disks = nr_disks; |
| 1308 | sb->active_disks = active; |
| 1309 | sb->working_disks = working; |
| 1310 | sb->failed_disks = failed; |
| 1311 | sb->spare_disks = spare; |
| 1312 | |
| 1313 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1314 | sb->sb_csum = calc_sb_csum(sb); |
| 1315 | } |
| 1316 | |
| 1317 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1318 | * rdev_size_change for 0.90.0 |
| 1319 | */ |
| 1320 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1321 | 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] | 1322 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1323 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1324 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1325 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1326 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1327 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1328 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1329 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1330 | /* Limit to 4TB as metadata cannot record more than that. |
| 1331 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1332 | */ |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1333 | if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1334 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1335 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1336 | rdev->sb_page); |
| 1337 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1338 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1339 | } |
| 1340 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1341 | static int |
| 1342 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1343 | { |
| 1344 | /* non-zero offset changes not possible with v0.90 */ |
| 1345 | return new_offset == 0; |
| 1346 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1347 | |
| 1348 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | * version 1 superblock |
| 1350 | */ |
| 1351 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1352 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1354 | __le32 disk_csum; |
| 1355 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | unsigned long long newcsum; |
| 1357 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1358 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | |
| 1360 | disk_csum = sb->sb_csum; |
| 1361 | sb->sb_csum = 0; |
| 1362 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1363 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | newcsum += le32_to_cpu(*isuper++); |
| 1365 | |
| 1366 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1367 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | |
| 1369 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1370 | sb->sb_csum = disk_csum; |
| 1371 | return cpu_to_le32(csum); |
| 1372 | } |
| 1373 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1374 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1375 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1376 | 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] | 1377 | { |
| 1378 | struct mdp_superblock_1 *sb; |
| 1379 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1380 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1381 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1383 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | |
| 1385 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1386 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | * It is always aligned to a 4K boundary and |
| 1388 | * depeding on minor_version, it can be: |
| 1389 | * 0: At least 8K, but less than 12K, from end of device |
| 1390 | * 1: At start of device |
| 1391 | * 2: 4K from start of device. |
| 1392 | */ |
| 1393 | switch(minor_version) { |
| 1394 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1395 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1396 | sb_start -= 8*2; |
| 1397 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | break; |
| 1399 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1400 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | break; |
| 1402 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1403 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | break; |
| 1405 | default: |
| 1406 | return -EINVAL; |
| 1407 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1408 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1410 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1411 | * and it is safe to read 4k, so we do that |
| 1412 | */ |
| 1413 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | if (ret) return ret; |
| 1415 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1416 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | |
| 1418 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1419 | sb->major_version != cpu_to_le32(1) || |
| 1420 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1421 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1422 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | return -EINVAL; |
| 1424 | |
| 1425 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1426 | printk("md: invalid superblock checksum on %s\n", |
| 1427 | bdevname(rdev->bdev,b)); |
| 1428 | return -EINVAL; |
| 1429 | } |
| 1430 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1431 | printk("md: data_size too small on %s\n", |
| 1432 | bdevname(rdev->bdev,b)); |
| 1433 | return -EINVAL; |
| 1434 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1435 | if (sb->pad0 || |
| 1436 | sb->pad3[0] || |
| 1437 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1438 | /* Some padding is non-zero, might be a new feature */ |
| 1439 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | rdev->preferred_minor = 0xffff; |
| 1442 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1443 | rdev->new_data_offset = rdev->data_offset; |
| 1444 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1445 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1446 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1447 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1449 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1450 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1451 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1452 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1453 | |
| 1454 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1455 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1456 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1457 | if (minor_version |
| 1458 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1459 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1460 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1461 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1462 | rdev->desc_nr = -1; |
| 1463 | else |
| 1464 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1465 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1466 | if (!rdev->bb_page) { |
| 1467 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1468 | if (!rdev->bb_page) |
| 1469 | return -ENOMEM; |
| 1470 | } |
| 1471 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1472 | rdev->badblocks.count == 0) { |
| 1473 | /* need to load the bad block list. |
| 1474 | * Currently we limit it to one page. |
| 1475 | */ |
| 1476 | s32 offset; |
| 1477 | sector_t bb_sector; |
| 1478 | u64 *bbp; |
| 1479 | int i; |
| 1480 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1481 | if (sectors > (PAGE_SIZE / 512)) |
| 1482 | return -EINVAL; |
| 1483 | offset = le32_to_cpu(sb->bblog_offset); |
| 1484 | if (offset == 0) |
| 1485 | return -EINVAL; |
| 1486 | bb_sector = (long long)offset; |
| 1487 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1488 | rdev->bb_page, READ, true)) |
| 1489 | return -EIO; |
| 1490 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1491 | rdev->badblocks.shift = sb->bblog_shift; |
| 1492 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1493 | u64 bb = le64_to_cpu(*bbp); |
| 1494 | int count = bb & (0x3ff); |
| 1495 | u64 sector = bb >> 10; |
| 1496 | sector <<= sb->bblog_shift; |
| 1497 | count <<= sb->bblog_shift; |
| 1498 | if (bb + 1 == 0) |
| 1499 | break; |
| 1500 | if (md_set_badblocks(&rdev->badblocks, |
| 1501 | sector, count, 1) == 0) |
| 1502 | return -EINVAL; |
| 1503 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1504 | } else if (sb->bblog_offset != 0) |
| 1505 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1506 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1507 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1508 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1509 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1511 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | |
| 1513 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1514 | sb->level != refsb->level || |
| 1515 | sb->layout != refsb->layout || |
| 1516 | sb->chunksize != refsb->chunksize) { |
| 1517 | printk(KERN_WARNING "md: %s has strangely different" |
| 1518 | " superblock to %s\n", |
| 1519 | bdevname(rdev->bdev,b), |
| 1520 | bdevname(refdev->bdev,b2)); |
| 1521 | return -EINVAL; |
| 1522 | } |
| 1523 | ev1 = le64_to_cpu(sb->events); |
| 1524 | ev2 = le64_to_cpu(refsb->events); |
| 1525 | |
| 1526 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1527 | ret = 1; |
| 1528 | else |
| 1529 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1531 | if (minor_version) { |
| 1532 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1533 | sectors -= rdev->data_offset; |
| 1534 | } else |
| 1535 | sectors = rdev->sb_start; |
| 1536 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1538 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1539 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | } |
| 1541 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1542 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1544 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1545 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1547 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1548 | clear_bit(Faulty, &rdev->flags); |
| 1549 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1550 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1551 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | if (mddev->raid_disks == 0) { |
| 1554 | mddev->major_version = 1; |
| 1555 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1556 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1557 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1559 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1560 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1561 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | mddev->layout = le32_to_cpu(sb->layout); |
| 1563 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1564 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1565 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1566 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1567 | mddev->bitmap_info.space = 0; |
| 1568 | /* Default location for bitmap is 1K after superblock |
| 1569 | * using 3K - total of 4K |
| 1570 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1571 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1572 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1573 | mddev->reshape_backwards = 0; |
| 1574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1576 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1577 | |
| 1578 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1579 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1580 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1581 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1582 | mddev->bitmap_info.offset = |
| 1583 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1584 | /* Metadata doesn't record how much space is available. |
| 1585 | * For 1.0, we assume we can use up to the superblock |
| 1586 | * if before, else to 4K beyond superblock. |
| 1587 | * For others, assume no change is possible. |
| 1588 | */ |
| 1589 | if (mddev->minor_version > 0) |
| 1590 | mddev->bitmap_info.space = 0; |
| 1591 | else if (mddev->bitmap_info.offset > 0) |
| 1592 | mddev->bitmap_info.space = |
| 1593 | 8 - mddev->bitmap_info.offset; |
| 1594 | else |
| 1595 | mddev->bitmap_info.space = |
| 1596 | -mddev->bitmap_info.offset; |
| 1597 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1598 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1599 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1600 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1601 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1602 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1603 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1604 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1605 | if (mddev->delta_disks < 0 || |
| 1606 | (mddev->delta_disks == 0 && |
| 1607 | (le32_to_cpu(sb->feature_map) |
| 1608 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1609 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1610 | } else { |
| 1611 | mddev->reshape_position = MaxSector; |
| 1612 | mddev->delta_disks = 0; |
| 1613 | mddev->new_level = mddev->level; |
| 1614 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1615 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1616 | } |
| 1617 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1618 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1619 | /* Insist of good event counter while assembling, except for |
| 1620 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1622 | if (rdev->desc_nr >= 0 && |
| 1623 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1624 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1625 | if (ev1 < mddev->events) |
| 1626 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1627 | } else if (mddev->bitmap) { |
| 1628 | /* If adding to array with a bitmap, then we can accept an |
| 1629 | * older device, but not too old. |
| 1630 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1631 | if (ev1 < mddev->bitmap->events_cleared) |
| 1632 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1633 | if (ev1 < mddev->events) |
| 1634 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1635 | } else { |
| 1636 | if (ev1 < mddev->events) |
| 1637 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1638 | return 0; |
| 1639 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1641 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1642 | if (rdev->desc_nr < 0 || |
| 1643 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1644 | role = 0xffff; |
| 1645 | rdev->desc_nr = -1; |
| 1646 | } else |
| 1647 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | switch(role) { |
| 1649 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | break; |
| 1651 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1652 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | break; |
| 1654 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1655 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1656 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1657 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1658 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1659 | if (!(le32_to_cpu(sb->feature_map) & |
| 1660 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1661 | rdev->saved_raid_disk = -1; |
| 1662 | } else |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1663 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | rdev->raid_disk = role; |
| 1665 | break; |
| 1666 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1667 | if (sb->devflags & WriteMostly1) |
| 1668 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1669 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1670 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1671 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1672 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | return 0; |
| 1675 | } |
| 1676 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1677 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | { |
| 1679 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1680 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | int max_dev, i; |
| 1682 | /* make rdev->sb match mddev and rdev data. */ |
| 1683 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1684 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | |
| 1686 | sb->feature_map = 0; |
| 1687 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1688 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1690 | |
| 1691 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1692 | sb->events = cpu_to_le64(mddev->events); |
| 1693 | if (mddev->in_sync) |
| 1694 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1695 | else |
| 1696 | sb->resync_offset = cpu_to_le64(0); |
| 1697 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1698 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1699 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1700 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1701 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1702 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1703 | sb->level = cpu_to_le32(mddev->level); |
| 1704 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1705 | |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1706 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1707 | sb->devflags |= WriteMostly1; |
| 1708 | else |
| 1709 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1710 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1711 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1712 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1713 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1714 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1715 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1716 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1717 | |
| 1718 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1719 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1720 | sb->feature_map |= |
| 1721 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1722 | sb->recovery_offset = |
| 1723 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1724 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 1725 | sb->feature_map |= |
| 1726 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1727 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1728 | if (test_bit(Replacement, &rdev->flags)) |
| 1729 | sb->feature_map |= |
| 1730 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1731 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1732 | if (mddev->reshape_position != MaxSector) { |
| 1733 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1734 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1735 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1736 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1737 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1738 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1739 | if (mddev->delta_disks == 0 && |
| 1740 | mddev->reshape_backwards) |
| 1741 | sb->feature_map |
| 1742 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1743 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1744 | sb->feature_map |
| 1745 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1746 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1747 | - rdev->data_offset)); |
| 1748 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1749 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1750 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1751 | if (rdev->badblocks.count == 0) |
| 1752 | /* Nothing to do for bad blocks*/ ; |
| 1753 | else if (sb->bblog_offset == 0) |
| 1754 | /* Cannot record bad blocks on this device */ |
| 1755 | md_error(mddev, rdev); |
| 1756 | else { |
| 1757 | struct badblocks *bb = &rdev->badblocks; |
| 1758 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1759 | u64 *p = bb->page; |
| 1760 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1761 | if (bb->changed) { |
| 1762 | unsigned seq; |
| 1763 | |
| 1764 | retry: |
| 1765 | seq = read_seqbegin(&bb->lock); |
| 1766 | |
| 1767 | memset(bbp, 0xff, PAGE_SIZE); |
| 1768 | |
| 1769 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1770 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1771 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1772 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1773 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1774 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1775 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1776 | if (read_seqretry(&bb->lock, seq)) |
| 1777 | goto retry; |
| 1778 | |
| 1779 | bb->sector = (rdev->sb_start + |
| 1780 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1781 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1782 | } |
| 1783 | } |
| 1784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1786 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | if (rdev2->desc_nr+1 > max_dev) |
| 1788 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1789 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1790 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1791 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1792 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1793 | rdev->sb_size = max_dev * 2 + 256; |
| 1794 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1795 | if (rdev->sb_size & bmask) |
| 1796 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1797 | } else |
| 1798 | max_dev = le32_to_cpu(sb->max_dev); |
| 1799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | for (i=0; i<max_dev;i++) |
| 1801 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1802 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1803 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1805 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1807 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1809 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1810 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | else |
| 1812 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1813 | } |
| 1814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1816 | } |
| 1817 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1818 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1819 | 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] | 1820 | { |
| 1821 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1822 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1823 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1824 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1825 | if (rdev->data_offset != rdev->new_data_offset) |
| 1826 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1827 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1828 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1829 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1830 | max_sectors -= rdev->data_offset; |
| 1831 | if (!num_sectors || num_sectors > max_sectors) |
| 1832 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1833 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1834 | /* minor version 0 with bitmap we can't move */ |
| 1835 | return 0; |
| 1836 | } else { |
| 1837 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1838 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1839 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1840 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1841 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1842 | if (!num_sectors || num_sectors > max_sectors) |
| 1843 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1844 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1845 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1846 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1847 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1848 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1849 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1850 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1851 | rdev->sb_page); |
| 1852 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1853 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1854 | |
| 1855 | } |
| 1856 | |
| 1857 | static int |
| 1858 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1859 | unsigned long long new_offset) |
| 1860 | { |
| 1861 | /* All necessary checks on new >= old have been done */ |
| 1862 | struct bitmap *bitmap; |
| 1863 | if (new_offset >= rdev->data_offset) |
| 1864 | return 1; |
| 1865 | |
| 1866 | /* with 1.0 metadata, there is no metadata to tread on |
| 1867 | * so we can always move back */ |
| 1868 | if (rdev->mddev->minor_version == 0) |
| 1869 | return 1; |
| 1870 | |
| 1871 | /* otherwise we must be sure not to step on |
| 1872 | * any metadata, so stay: |
| 1873 | * 36K beyond start of superblock |
| 1874 | * beyond end of badblocks |
| 1875 | * beyond write-intent bitmap |
| 1876 | */ |
| 1877 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1878 | return 0; |
| 1879 | bitmap = rdev->mddev->bitmap; |
| 1880 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1881 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1882 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1883 | return 0; |
| 1884 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1885 | return 0; |
| 1886 | |
| 1887 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1888 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1890 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | [0] = { |
| 1892 | .name = "0.90.0", |
| 1893 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1894 | .load_super = super_90_load, |
| 1895 | .validate_super = super_90_validate, |
| 1896 | .sync_super = super_90_sync, |
| 1897 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1898 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | }, |
| 1900 | [1] = { |
| 1901 | .name = "md-1", |
| 1902 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1903 | .load_super = super_1_load, |
| 1904 | .validate_super = super_1_validate, |
| 1905 | .sync_super = super_1_sync, |
| 1906 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1907 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | }, |
| 1909 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1911 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1912 | { |
| 1913 | if (mddev->sync_super) { |
| 1914 | mddev->sync_super(mddev, rdev); |
| 1915 | return; |
| 1916 | } |
| 1917 | |
| 1918 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1919 | |
| 1920 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1921 | } |
| 1922 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1923 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1925 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1927 | rcu_read_lock(); |
| 1928 | rdev_for_each_rcu(rdev, mddev1) |
| 1929 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1930 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1931 | rdev2->bdev->bd_contains) { |
| 1932 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1933 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1934 | } |
| 1935 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | return 0; |
| 1937 | } |
| 1938 | |
| 1939 | static LIST_HEAD(pending_raid_disks); |
| 1940 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1941 | /* |
| 1942 | * Try to register data integrity profile for an mddev |
| 1943 | * |
| 1944 | * This is called when an array is started and after a disk has been kicked |
| 1945 | * from the array. It only succeeds if all working and active component devices |
| 1946 | * are integrity capable with matching profiles. |
| 1947 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1948 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1949 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1950 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1951 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1952 | if (list_empty(&mddev->disks)) |
| 1953 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1954 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1955 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1956 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1957 | /* skip spares and non-functional disks */ |
| 1958 | if (test_bit(Faulty, &rdev->flags)) |
| 1959 | continue; |
| 1960 | if (rdev->raid_disk < 0) |
| 1961 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1962 | if (!reference) { |
| 1963 | /* Use the first rdev as the reference */ |
| 1964 | reference = rdev; |
| 1965 | continue; |
| 1966 | } |
| 1967 | /* does this rdev's profile match the reference profile? */ |
| 1968 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1969 | rdev->bdev->bd_disk) < 0) |
| 1970 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1971 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1972 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1973 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1974 | /* |
| 1975 | * All component devices are integrity capable and have matching |
| 1976 | * profiles, register the common profile for the md device. |
| 1977 | */ |
| 1978 | if (blk_integrity_register(mddev->gendisk, |
| 1979 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1980 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1981 | mdname(mddev)); |
| 1982 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1983 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1984 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1985 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1986 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1987 | mdname(mddev)); |
| 1988 | return -EINVAL; |
| 1989 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1990 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1991 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1992 | EXPORT_SYMBOL(md_integrity_register); |
| 1993 | |
| 1994 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1995 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1996 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 1997 | struct blk_integrity *bi_rdev; |
| 1998 | struct blk_integrity *bi_mddev; |
| 1999 | |
| 2000 | if (!mddev->gendisk) |
| 2001 | return; |
| 2002 | |
| 2003 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2004 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2005 | |
| 2006 | if (!bi_mddev) /* nothing to do */ |
| 2007 | return; |
| 2008 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2009 | return; |
| 2010 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2011 | rdev->bdev->bd_disk) >= 0) |
| 2012 | return; |
| 2013 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2014 | blk_integrity_unregister(mddev->gendisk); |
| 2015 | } |
| 2016 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2017 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2018 | 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] | 2019 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2020 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2021 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2022 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2023 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2025 | /* prevent duplicates */ |
| 2026 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2027 | return -EEXIST; |
| 2028 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2029 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2030 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2031 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2032 | if (mddev->pers) { |
| 2033 | /* Cannot change size, so fail |
| 2034 | * If mddev->level <= 0, then we don't care |
| 2035 | * about aligning sizes (e.g. linear) |
| 2036 | */ |
| 2037 | if (mddev->level > 0) |
| 2038 | return -ENOSPC; |
| 2039 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2040 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2041 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | |
| 2043 | /* Verify rdev->desc_nr is unique. |
| 2044 | * If it is -1, assign a free number, else |
| 2045 | * check number is not in use |
| 2046 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2047 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | if (rdev->desc_nr < 0) { |
| 2049 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2050 | if (mddev->pers) |
| 2051 | choice = mddev->raid_disks; |
| 2052 | while (find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | choice++; |
| 2054 | rdev->desc_nr = choice; |
| 2055 | } else { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2056 | if (find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
| 2057 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2059 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2061 | rcu_read_unlock(); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2062 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2063 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2064 | mdname(mddev), mddev->max_disks); |
| 2065 | return -EBUSY; |
| 2066 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2067 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2068 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2069 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2072 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2073 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2074 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2075 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2076 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2077 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2078 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2079 | /* failure here is OK */; |
| 2080 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2081 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2082 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2083 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2084 | |
| 2085 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2086 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2089 | |
| 2090 | fail: |
| 2091 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2092 | b, mdname(mddev)); |
| 2093 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2096 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2097 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2098 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2099 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2100 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2101 | } |
| 2102 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2103 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | { |
| 2105 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2106 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2107 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2108 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2110 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2111 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2112 | sysfs_put(rdev->sysfs_state); |
| 2113 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2114 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2115 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2116 | * writing to 'remove' to "dev/state". We also need |
| 2117 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2118 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2119 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2120 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2121 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2122 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | /* |
| 2126 | * prevent the device from being mounted, repartitioned or |
| 2127 | * otherwise reused by a RAID array (or any other kernel |
| 2128 | * subsystem), by bd_claiming the device. |
| 2129 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2130 | 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] | 2131 | { |
| 2132 | int err = 0; |
| 2133 | struct block_device *bdev; |
| 2134 | char b[BDEVNAME_SIZE]; |
| 2135 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2136 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2137 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | if (IS_ERR(bdev)) { |
| 2139 | printk(KERN_ERR "md: could not open %s.\n", |
| 2140 | __bdevname(dev, b)); |
| 2141 | return PTR_ERR(bdev); |
| 2142 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | rdev->bdev = bdev; |
| 2144 | return err; |
| 2145 | } |
| 2146 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2147 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | { |
| 2149 | struct block_device *bdev = rdev->bdev; |
| 2150 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2151 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | void md_autodetect_dev(dev_t dev); |
| 2155 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2156 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | { |
| 2158 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2161 | bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2162 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2164 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2165 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | #endif |
| 2167 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2168 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | } |
| 2170 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2171 | static void kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | { |
| 2173 | unbind_rdev_from_array(rdev); |
| 2174 | export_rdev(rdev); |
| 2175 | } |
| 2176 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2177 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2179 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2181 | while (!list_empty(&mddev->disks)) { |
| 2182 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2183 | same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | kick_rdev_from_array(rdev); |
| 2185 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | mddev->raid_disks = 0; |
| 2187 | mddev->major_version = 0; |
| 2188 | } |
| 2189 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2190 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2192 | /* Update each superblock (in-memory image), but |
| 2193 | * if we are allowed to, skip spares which already |
| 2194 | * have the right event counter, or have one earlier |
| 2195 | * (which would mean they aren't being marked as dirty |
| 2196 | * with the rest of the array) |
| 2197 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2198 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2199 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2200 | if (rdev->sb_events == mddev->events || |
| 2201 | (nospares && |
| 2202 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2203 | rdev->sb_events+1 == mddev->events)) { |
| 2204 | /* Don't update this superblock */ |
| 2205 | rdev->sb_loaded = 2; |
| 2206 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2207 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2208 | rdev->sb_loaded = 1; |
| 2209 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | } |
| 2211 | } |
| 2212 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2213 | static void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2215 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2216 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2217 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2218 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2220 | if (mddev->ro) { |
| 2221 | if (force_change) |
| 2222 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2223 | return; |
| 2224 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2226 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2227 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2228 | if (rdev->raid_disk >= 0 && |
| 2229 | mddev->delta_disks >= 0 && |
| 2230 | !test_bit(In_sync, &rdev->flags) && |
| 2231 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2232 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2233 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2234 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2235 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2236 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2237 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2238 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2239 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2240 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2241 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2242 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2243 | md_ack_all_badblocks(&rdev->badblocks); |
| 2244 | md_error(mddev, rdev); |
| 2245 | } |
| 2246 | clear_bit(Blocked, &rdev->flags); |
| 2247 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2248 | wake_up(&rdev->blocked_wait); |
| 2249 | } |
| 2250 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2251 | wake_up(&mddev->sb_wait); |
| 2252 | return; |
| 2253 | } |
| 2254 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2255 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2256 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2257 | mddev->utime = get_seconds(); |
| 2258 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2259 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2260 | force_change = 1; |
| 2261 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2262 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2263 | * though if events isn't the right even/odd, we will have to do |
| 2264 | * spares after all |
| 2265 | */ |
| 2266 | nospares = 1; |
| 2267 | if (force_change) |
| 2268 | nospares = 0; |
| 2269 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2270 | /* If the array is degraded, then skipping spares is both |
| 2271 | * dangerous and fairly pointless. |
| 2272 | * Dangerous because a device that was removed from the array |
| 2273 | * might have a event_count that still looks up-to-date, |
| 2274 | * so it can be re-added without a resync. |
| 2275 | * Pointless because if there are any spares to skip, |
| 2276 | * then a recovery will happen and soon that array won't |
| 2277 | * be degraded any more and the spare can go back to sleep then. |
| 2278 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2279 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2280 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2281 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2282 | |
| 2283 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2284 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2285 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2286 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2287 | && mddev->can_decrease_events |
| 2288 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2289 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2290 | mddev->can_decrease_events = 0; |
| 2291 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2292 | /* otherwise we have to go forward and ... */ |
| 2293 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2294 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2295 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2297 | /* |
| 2298 | * This 64-bit counter should never wrap. |
| 2299 | * Either we are in around ~1 trillion A.C., assuming |
| 2300 | * 1 reboot per second, or we have a bug... |
| 2301 | */ |
| 2302 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2303 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2304 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2305 | if (rdev->badblocks.changed) |
| 2306 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2307 | if (test_bit(Faulty, &rdev->flags)) |
| 2308 | set_bit(FaultRecorded, &rdev->flags); |
| 2309 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2310 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2311 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2312 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2314 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2315 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2317 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2318 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2320 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2321 | if (rdev->sb_loaded != 1) |
| 2322 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2324 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2325 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2326 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2327 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2328 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2329 | bdevname(rdev->bdev, b), |
| 2330 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2331 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2332 | if (rdev->badblocks.size) { |
| 2333 | md_super_write(mddev, rdev, |
| 2334 | rdev->badblocks.sector, |
| 2335 | rdev->badblocks.size << 9, |
| 2336 | rdev->bb_page); |
| 2337 | rdev->badblocks.size = 0; |
| 2338 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2339 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2340 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2341 | pr_debug("md: %s (skipping faulty)\n", |
| 2342 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2343 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2344 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | /* only need to write one superblock... */ |
| 2346 | break; |
| 2347 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2348 | md_super_wait(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2349 | /* 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] | 2350 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2351 | spin_lock(&mddev->lock); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2352 | if (mddev->in_sync != sync_req || |
| 2353 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2354 | /* have to write it out again */ |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2355 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2356 | goto repeat; |
| 2357 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2358 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2359 | spin_unlock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2360 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2361 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2362 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2363 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2364 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2365 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2366 | clear_bit(Blocked, &rdev->flags); |
| 2367 | |
| 2368 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2369 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2370 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2371 | wake_up(&rdev->blocked_wait); |
| 2372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | } |
| 2374 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2375 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2376 | * We want to accept with case. For this we use cmd_match. |
| 2377 | */ |
| 2378 | static int cmd_match(const char *cmd, const char *str) |
| 2379 | { |
| 2380 | /* See if cmd, written into a sysfs file, matches |
| 2381 | * str. They must either be the same, or cmd can |
| 2382 | * have a trailing newline |
| 2383 | */ |
| 2384 | while (*cmd && *str && *cmd == *str) { |
| 2385 | cmd++; |
| 2386 | str++; |
| 2387 | } |
| 2388 | if (*cmd == '\n') |
| 2389 | cmd++; |
| 2390 | if (*str || *cmd) |
| 2391 | return 0; |
| 2392 | return 1; |
| 2393 | } |
| 2394 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2395 | struct rdev_sysfs_entry { |
| 2396 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2397 | ssize_t (*show)(struct md_rdev *, char *); |
| 2398 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2399 | }; |
| 2400 | |
| 2401 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2402 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2403 | { |
| 2404 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2405 | size_t len = 0; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2406 | unsigned long flags = ACCESS_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2407 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2408 | if (test_bit(Faulty, &flags) || |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2409 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2410 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2411 | sep = ","; |
| 2412 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2413 | if (test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2414 | len += sprintf(page+len, "%sin_sync",sep); |
| 2415 | sep = ","; |
| 2416 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2417 | if (test_bit(WriteMostly, &flags)) { |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2418 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2419 | sep = ","; |
| 2420 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2421 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2422 | (rdev->badblocks.unacked_exist |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2423 | && !test_bit(Faulty, &flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2424 | len += sprintf(page+len, "%sblocked", sep); |
| 2425 | sep = ","; |
| 2426 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2427 | if (!test_bit(Faulty, &flags) && |
| 2428 | !test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2429 | len += sprintf(page+len, "%sspare", sep); |
| 2430 | sep = ","; |
| 2431 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2432 | if (test_bit(WriteErrorSeen, &flags)) { |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2433 | len += sprintf(page+len, "%swrite_error", sep); |
| 2434 | sep = ","; |
| 2435 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2436 | if (test_bit(WantReplacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2437 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2438 | sep = ","; |
| 2439 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2440 | if (test_bit(Replacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2441 | len += sprintf(page+len, "%sreplacement", sep); |
| 2442 | sep = ","; |
| 2443 | } |
| 2444 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2445 | return len+sprintf(page+len, "\n"); |
| 2446 | } |
| 2447 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2448 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2449 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2450 | { |
| 2451 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2452 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2453 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2454 | * writemostly - sets write_mostly |
| 2455 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2456 | * blocked - sets the Blocked flags |
| 2457 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2458 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2459 | * -insync - clear Insync for a device with a slot assigned, |
| 2460 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2461 | * write_error - sets WriteErrorSeen |
| 2462 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2463 | */ |
| 2464 | int err = -EINVAL; |
| 2465 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2466 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2467 | if (test_bit(Faulty, &rdev->flags)) |
| 2468 | err = 0; |
| 2469 | else |
| 2470 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2471 | } else if (cmd_match(buf, "remove")) { |
| 2472 | if (rdev->raid_disk >= 0) |
| 2473 | err = -EBUSY; |
| 2474 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2475 | struct mddev *mddev = rdev->mddev; |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 2476 | if (mddev_is_clustered(mddev)) |
| 2477 | md_cluster_ops->metadata_update_start(mddev); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2478 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2479 | if (mddev->pers) |
| 2480 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2481 | md_new_event(mddev); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 2482 | if (mddev_is_clustered(mddev)) |
| 2483 | md_cluster_ops->metadata_update_finish(mddev); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2484 | err = 0; |
| 2485 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2486 | } else if (cmd_match(buf, "writemostly")) { |
| 2487 | set_bit(WriteMostly, &rdev->flags); |
| 2488 | err = 0; |
| 2489 | } else if (cmd_match(buf, "-writemostly")) { |
| 2490 | clear_bit(WriteMostly, &rdev->flags); |
| 2491 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2492 | } else if (cmd_match(buf, "blocked")) { |
| 2493 | set_bit(Blocked, &rdev->flags); |
| 2494 | err = 0; |
| 2495 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2496 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2497 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2498 | /* metadata handler doesn't understand badblocks, |
| 2499 | * so we need to fail the device |
| 2500 | */ |
| 2501 | md_error(rdev->mddev, rdev); |
| 2502 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2503 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2504 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2505 | wake_up(&rdev->blocked_wait); |
| 2506 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2507 | md_wakeup_thread(rdev->mddev->thread); |
| 2508 | |
| 2509 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2510 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2511 | set_bit(In_sync, &rdev->flags); |
| 2512 | err = 0; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2513 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 2514 | if (rdev->mddev->pers == NULL) { |
| 2515 | clear_bit(In_sync, &rdev->flags); |
| 2516 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2517 | rdev->raid_disk = -1; |
| 2518 | err = 0; |
| 2519 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2520 | } else if (cmd_match(buf, "write_error")) { |
| 2521 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2522 | err = 0; |
| 2523 | } else if (cmd_match(buf, "-write_error")) { |
| 2524 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2525 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2526 | } else if (cmd_match(buf, "want_replacement")) { |
| 2527 | /* Any non-spare device that is not a replacement can |
| 2528 | * become want_replacement at any time, but we then need to |
| 2529 | * check if recovery is needed. |
| 2530 | */ |
| 2531 | if (rdev->raid_disk >= 0 && |
| 2532 | !test_bit(Replacement, &rdev->flags)) |
| 2533 | set_bit(WantReplacement, &rdev->flags); |
| 2534 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2535 | md_wakeup_thread(rdev->mddev->thread); |
| 2536 | err = 0; |
| 2537 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2538 | /* Clearing 'want_replacement' is always allowed. |
| 2539 | * Once replacements starts it is too late though. |
| 2540 | */ |
| 2541 | err = 0; |
| 2542 | clear_bit(WantReplacement, &rdev->flags); |
| 2543 | } else if (cmd_match(buf, "replacement")) { |
| 2544 | /* Can only set a device as a replacement when array has not |
| 2545 | * yet been started. Once running, replacement is automatic |
| 2546 | * from spares, or by assigning 'slot'. |
| 2547 | */ |
| 2548 | if (rdev->mddev->pers) |
| 2549 | err = -EBUSY; |
| 2550 | else { |
| 2551 | set_bit(Replacement, &rdev->flags); |
| 2552 | err = 0; |
| 2553 | } |
| 2554 | } else if (cmd_match(buf, "-replacement")) { |
| 2555 | /* Similarly, can only clear Replacement before start */ |
| 2556 | if (rdev->mddev->pers) |
| 2557 | err = -EBUSY; |
| 2558 | else { |
| 2559 | clear_bit(Replacement, &rdev->flags); |
| 2560 | err = 0; |
| 2561 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2562 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2563 | if (!err) |
| 2564 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2565 | return err ? err : len; |
| 2566 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2567 | static struct rdev_sysfs_entry rdev_state = |
| 2568 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2569 | |
| 2570 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2571 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2572 | { |
| 2573 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2574 | } |
| 2575 | |
| 2576 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2577 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2578 | { |
| 2579 | char *e; |
| 2580 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2581 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2582 | atomic_set(&rdev->corrected_errors, n); |
| 2583 | return len; |
| 2584 | } |
| 2585 | return -EINVAL; |
| 2586 | } |
| 2587 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2588 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2589 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2590 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2591 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2592 | { |
| 2593 | if (rdev->raid_disk < 0) |
| 2594 | return sprintf(page, "none\n"); |
| 2595 | else |
| 2596 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2597 | } |
| 2598 | |
| 2599 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2600 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2601 | { |
| 2602 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2603 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2604 | int slot = simple_strtoul(buf, &e, 10); |
| 2605 | if (strncmp(buf, "none", 4)==0) |
| 2606 | slot = -1; |
| 2607 | else if (e==buf || (*e && *e!= '\n')) |
| 2608 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2609 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2610 | /* Setting 'slot' on an active array requires also |
| 2611 | * updating the 'rd%d' link, and communicating |
| 2612 | * with the personality with ->hot_*_disk. |
| 2613 | * For now we only support removing |
| 2614 | * failed/spare devices. This normally happens automatically, |
| 2615 | * but not when the metadata is externally managed. |
| 2616 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2617 | if (rdev->raid_disk == -1) |
| 2618 | return -EEXIST; |
| 2619 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2620 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2621 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2622 | clear_bit(Blocked, &rdev->flags); |
| 2623 | remove_and_add_spares(rdev->mddev, rdev); |
| 2624 | if (rdev->raid_disk >= 0) |
| 2625 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2626 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2627 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2628 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2629 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2630 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2631 | */ |
| 2632 | |
| 2633 | if (rdev->raid_disk != -1) |
| 2634 | return -EBUSY; |
| 2635 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2636 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2637 | return -EBUSY; |
| 2638 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2639 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2640 | return -EINVAL; |
| 2641 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2642 | if (slot >= rdev->mddev->raid_disks && |
| 2643 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2644 | return -ENOSPC; |
| 2645 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2646 | rdev->raid_disk = slot; |
| 2647 | if (test_bit(In_sync, &rdev->flags)) |
| 2648 | rdev->saved_raid_disk = slot; |
| 2649 | else |
| 2650 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2651 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 2652 | clear_bit(Bitmap_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2653 | err = rdev->mddev->pers-> |
| 2654 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2655 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2656 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2657 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2658 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2659 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2660 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2661 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2662 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2663 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2664 | if (slot >= rdev->mddev->raid_disks && |
| 2665 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2666 | return -ENOSPC; |
| 2667 | rdev->raid_disk = slot; |
| 2668 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2669 | clear_bit(Faulty, &rdev->flags); |
| 2670 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2671 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2672 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2673 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2674 | return len; |
| 2675 | } |
| 2676 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2677 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2678 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2679 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2680 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2681 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2682 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2683 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2687 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2688 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2689 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2690 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2691 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2692 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2693 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2694 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2695 | /* Must set offset before size, so overlap checks |
| 2696 | * can be sane */ |
| 2697 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2698 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2699 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2700 | return len; |
| 2701 | } |
| 2702 | |
| 2703 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2704 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2705 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2706 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2707 | { |
| 2708 | return sprintf(page, "%llu\n", |
| 2709 | (unsigned long long)rdev->new_data_offset); |
| 2710 | } |
| 2711 | |
| 2712 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2713 | const char *buf, size_t len) |
| 2714 | { |
| 2715 | unsigned long long new_offset; |
| 2716 | struct mddev *mddev = rdev->mddev; |
| 2717 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2718 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2719 | return -EINVAL; |
| 2720 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 2721 | if (mddev->sync_thread || |
| 2722 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2723 | return -EBUSY; |
| 2724 | if (new_offset == rdev->data_offset) |
| 2725 | /* reset is always permitted */ |
| 2726 | ; |
| 2727 | else if (new_offset > rdev->data_offset) { |
| 2728 | /* must not push array size beyond rdev_sectors */ |
| 2729 | if (new_offset - rdev->data_offset |
| 2730 | + mddev->dev_sectors > rdev->sectors) |
| 2731 | return -E2BIG; |
| 2732 | } |
| 2733 | /* Metadata worries about other space details. */ |
| 2734 | |
| 2735 | /* decreasing the offset is inconsistent with a backwards |
| 2736 | * reshape. |
| 2737 | */ |
| 2738 | if (new_offset < rdev->data_offset && |
| 2739 | mddev->reshape_backwards) |
| 2740 | return -EINVAL; |
| 2741 | /* Increasing offset is inconsistent with forwards |
| 2742 | * reshape. reshape_direction should be set to |
| 2743 | * 'backwards' first. |
| 2744 | */ |
| 2745 | if (new_offset > rdev->data_offset && |
| 2746 | !mddev->reshape_backwards) |
| 2747 | return -EINVAL; |
| 2748 | |
| 2749 | if (mddev->pers && mddev->persistent && |
| 2750 | !super_types[mddev->major_version] |
| 2751 | .allow_new_offset(rdev, new_offset)) |
| 2752 | return -E2BIG; |
| 2753 | rdev->new_data_offset = new_offset; |
| 2754 | if (new_offset > rdev->data_offset) |
| 2755 | mddev->reshape_backwards = 1; |
| 2756 | else if (new_offset < rdev->data_offset) |
| 2757 | mddev->reshape_backwards = 0; |
| 2758 | |
| 2759 | return len; |
| 2760 | } |
| 2761 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2762 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2763 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2764 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2765 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2766 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2767 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2768 | } |
| 2769 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2770 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2771 | { |
| 2772 | /* check if two start/length pairs overlap */ |
| 2773 | if (s1+l1 <= s2) |
| 2774 | return 0; |
| 2775 | if (s2+l2 <= s1) |
| 2776 | return 0; |
| 2777 | return 1; |
| 2778 | } |
| 2779 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2780 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2781 | { |
| 2782 | unsigned long long blocks; |
| 2783 | sector_t new; |
| 2784 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2785 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2786 | return -EINVAL; |
| 2787 | |
| 2788 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2789 | return -EINVAL; /* sector conversion overflow */ |
| 2790 | |
| 2791 | new = blocks * 2; |
| 2792 | if (new != blocks * 2) |
| 2793 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2794 | |
| 2795 | *sectors = new; |
| 2796 | return 0; |
| 2797 | } |
| 2798 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2799 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2800 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2801 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2802 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2803 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2804 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2805 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2806 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2807 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2808 | if (rdev->data_offset != rdev->new_data_offset) |
| 2809 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2810 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2811 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2812 | sectors = super_types[my_mddev->major_version]. |
| 2813 | rdev_size_change(rdev, sectors); |
| 2814 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2815 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2816 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2817 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2818 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 2819 | if (!my_mddev->pers->resize) |
| 2820 | /* Cannot change size for RAID0 or Linear etc */ |
| 2821 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2822 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2823 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2824 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2825 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2826 | rdev->sectors = sectors; |
| 2827 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2828 | /* Need to check that all other rdevs with the same |
| 2829 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 2830 | * the rdev lists safely. |
| 2831 | * This check does not provide a hard guarantee, it |
| 2832 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2833 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2834 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2835 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2836 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2837 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2838 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2839 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2840 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2841 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2842 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2843 | if (rdev->bdev == rdev2->bdev && |
| 2844 | rdev != rdev2 && |
| 2845 | overlaps(rdev->data_offset, rdev->sectors, |
| 2846 | rdev2->data_offset, |
| 2847 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2848 | overlap = 1; |
| 2849 | break; |
| 2850 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2851 | if (overlap) { |
| 2852 | mddev_put(mddev); |
| 2853 | break; |
| 2854 | } |
| 2855 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2856 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2857 | if (overlap) { |
| 2858 | /* Someone else could have slipped in a size |
| 2859 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2860 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2861 | * safe, and trust userspace not to race with |
| 2862 | * itself |
| 2863 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2864 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2865 | return -EBUSY; |
| 2866 | } |
| 2867 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2868 | return len; |
| 2869 | } |
| 2870 | |
| 2871 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2872 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2873 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2874 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2875 | { |
| 2876 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2877 | |
| 2878 | if (test_bit(In_sync, &rdev->flags) || |
| 2879 | recovery_start == MaxSector) |
| 2880 | return sprintf(page, "none\n"); |
| 2881 | |
| 2882 | return sprintf(page, "%llu\n", recovery_start); |
| 2883 | } |
| 2884 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2885 | 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] | 2886 | { |
| 2887 | unsigned long long recovery_start; |
| 2888 | |
| 2889 | if (cmd_match(buf, "none")) |
| 2890 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2891 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2892 | return -EINVAL; |
| 2893 | |
| 2894 | if (rdev->mddev->pers && |
| 2895 | rdev->raid_disk >= 0) |
| 2896 | return -EBUSY; |
| 2897 | |
| 2898 | rdev->recovery_offset = recovery_start; |
| 2899 | if (recovery_start == MaxSector) |
| 2900 | set_bit(In_sync, &rdev->flags); |
| 2901 | else |
| 2902 | clear_bit(In_sync, &rdev->flags); |
| 2903 | return len; |
| 2904 | } |
| 2905 | |
| 2906 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2907 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2908 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2909 | static ssize_t |
| 2910 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 2911 | static ssize_t |
| 2912 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 2913 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2914 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2915 | { |
| 2916 | return badblocks_show(&rdev->badblocks, page, 0); |
| 2917 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2918 | 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] | 2919 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2920 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 2921 | /* Maybe that ack was all we needed */ |
| 2922 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 2923 | wake_up(&rdev->blocked_wait); |
| 2924 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2925 | } |
| 2926 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 2927 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 2928 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2929 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2930 | { |
| 2931 | return badblocks_show(&rdev->badblocks, page, 1); |
| 2932 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2933 | 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] | 2934 | { |
| 2935 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 2936 | } |
| 2937 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 2938 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 2939 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2940 | static struct attribute *rdev_default_attrs[] = { |
| 2941 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2942 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2943 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2944 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2945 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2946 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2947 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2948 | &rdev_bad_blocks.attr, |
| 2949 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2950 | NULL, |
| 2951 | }; |
| 2952 | static ssize_t |
| 2953 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2954 | { |
| 2955 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2956 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2957 | |
| 2958 | if (!entry->show) |
| 2959 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2960 | if (!rdev->mddev) |
| 2961 | return -EBUSY; |
| 2962 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | static ssize_t |
| 2966 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2967 | const char *page, size_t length) |
| 2968 | { |
| 2969 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2970 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2971 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2972 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2973 | |
| 2974 | if (!entry->store) |
| 2975 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2976 | if (!capable(CAP_SYS_ADMIN)) |
| 2977 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2978 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2979 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2980 | if (rdev->mddev == NULL) |
| 2981 | rv = -EBUSY; |
| 2982 | else |
| 2983 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2984 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2985 | } |
| 2986 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | static void rdev_free(struct kobject *ko) |
| 2990 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2991 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2992 | kfree(rdev); |
| 2993 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2994 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2995 | .show = rdev_attr_show, |
| 2996 | .store = rdev_attr_store, |
| 2997 | }; |
| 2998 | static struct kobj_type rdev_ktype = { |
| 2999 | .release = rdev_free, |
| 3000 | .sysfs_ops = &rdev_sysfs_ops, |
| 3001 | .default_attrs = rdev_default_attrs, |
| 3002 | }; |
| 3003 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3004 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3005 | { |
| 3006 | rdev->desc_nr = -1; |
| 3007 | rdev->saved_raid_disk = -1; |
| 3008 | rdev->raid_disk = -1; |
| 3009 | rdev->flags = 0; |
| 3010 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3011 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3012 | rdev->sb_events = 0; |
| 3013 | rdev->last_read_error.tv_sec = 0; |
| 3014 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3015 | rdev->sb_loaded = 0; |
| 3016 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3017 | atomic_set(&rdev->nr_pending, 0); |
| 3018 | atomic_set(&rdev->read_errors, 0); |
| 3019 | atomic_set(&rdev->corrected_errors, 0); |
| 3020 | |
| 3021 | INIT_LIST_HEAD(&rdev->same_set); |
| 3022 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3023 | |
| 3024 | /* Add space to store bad block list. |
| 3025 | * This reserves the space even on arrays where it cannot |
| 3026 | * be used - I wonder if that matters |
| 3027 | */ |
| 3028 | rdev->badblocks.count = 0; |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 3029 | rdev->badblocks.shift = -1; /* disabled until explicitly enabled */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3030 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3031 | seqlock_init(&rdev->badblocks.lock); |
| 3032 | if (rdev->badblocks.page == NULL) |
| 3033 | return -ENOMEM; |
| 3034 | |
| 3035 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3036 | } |
| 3037 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | /* |
| 3039 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3040 | * |
| 3041 | * mark the device faulty if: |
| 3042 | * |
| 3043 | * - the device is nonexistent (zero size) |
| 3044 | * - the device has no valid superblock |
| 3045 | * |
| 3046 | * a faulty rdev _never_ has rdev->sb set. |
| 3047 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3048 | 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] | 3049 | { |
| 3050 | char b[BDEVNAME_SIZE]; |
| 3051 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3052 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | sector_t size; |
| 3054 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3055 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | if (!rdev) { |
| 3057 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3058 | return ERR_PTR(-ENOMEM); |
| 3059 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3061 | err = md_rdev_init(rdev); |
| 3062 | if (err) |
| 3063 | goto abort_free; |
| 3064 | err = alloc_disk_sb(rdev); |
| 3065 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | goto abort_free; |
| 3067 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3068 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | if (err) |
| 3070 | goto abort_free; |
| 3071 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3072 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3073 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3074 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3075 | if (!size) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3076 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3078 | bdevname(rdev->bdev,b)); |
| 3079 | err = -EINVAL; |
| 3080 | goto abort_free; |
| 3081 | } |
| 3082 | |
| 3083 | if (super_format >= 0) { |
| 3084 | err = super_types[super_format]. |
| 3085 | load_super(rdev, NULL, super_minor); |
| 3086 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3087 | printk(KERN_WARNING |
| 3088 | "md: %s does not have a valid v%d.%d " |
| 3089 | "superblock, not importing!\n", |
| 3090 | bdevname(rdev->bdev,b), |
| 3091 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | goto abort_free; |
| 3093 | } |
| 3094 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3095 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | "md: could not read %s's sb, not importing!\n", |
| 3097 | bdevname(rdev->bdev,b)); |
| 3098 | goto abort_free; |
| 3099 | } |
| 3100 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3102 | return rdev; |
| 3103 | |
| 3104 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3105 | if (rdev->bdev) |
| 3106 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3107 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | kfree(rdev); |
| 3109 | return ERR_PTR(err); |
| 3110 | } |
| 3111 | |
| 3112 | /* |
| 3113 | * Check a full RAID array for plausibility |
| 3114 | */ |
| 3115 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3116 | static void analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | { |
| 3118 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3119 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3120 | char b[BDEVNAME_SIZE]; |
| 3121 | |
| 3122 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3123 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3124 | switch (super_types[mddev->major_version]. |
| 3125 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3126 | case 1: |
| 3127 | freshest = rdev; |
| 3128 | break; |
| 3129 | case 0: |
| 3130 | break; |
| 3131 | default: |
| 3132 | printk( KERN_ERR \ |
| 3133 | "md: fatal superblock inconsistency in %s" |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3134 | " -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | bdevname(rdev->bdev,b)); |
| 3136 | kick_rdev_from_array(rdev); |
| 3137 | } |
| 3138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | super_types[mddev->major_version]. |
| 3140 | validate_super(mddev, freshest); |
| 3141 | |
| 3142 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3143 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3144 | if (mddev->max_disks && |
| 3145 | (rdev->desc_nr >= mddev->max_disks || |
| 3146 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3147 | printk(KERN_WARNING |
| 3148 | "md: %s: %s: only %d devices permitted\n", |
| 3149 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3150 | mddev->max_disks); |
| 3151 | kick_rdev_from_array(rdev); |
| 3152 | continue; |
| 3153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | if (rdev != freshest) |
| 3155 | if (super_types[mddev->major_version]. |
| 3156 | validate_super(mddev, rdev)) { |
| 3157 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3158 | " from array!\n", |
| 3159 | bdevname(rdev->bdev,b)); |
| 3160 | kick_rdev_from_array(rdev); |
| 3161 | continue; |
| 3162 | } |
| 3163 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3164 | rdev->desc_nr = i++; |
| 3165 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3166 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3167 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3168 | rdev->raid_disk = -1; |
| 3169 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | } |
| 3171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | } |
| 3173 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3174 | /* Read a fixed-point number. |
| 3175 | * Numbers in sysfs attributes should be in "standard" units where |
| 3176 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3177 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3178 | * milliseconds or jiffies. |
| 3179 | * This function takes a decimal number with a possible fractional |
| 3180 | * component, and produces an integer which is the result of |
| 3181 | * multiplying that number by 10^'scale'. |
| 3182 | * all without any floating-point arithmetic. |
| 3183 | */ |
| 3184 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3185 | { |
| 3186 | unsigned long result = 0; |
| 3187 | long decimals = -1; |
| 3188 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3189 | if (*cp == '.') |
| 3190 | decimals = 0; |
| 3191 | else if (decimals < scale) { |
| 3192 | unsigned int value; |
| 3193 | value = *cp - '0'; |
| 3194 | result = result * 10 + value; |
| 3195 | if (decimals >= 0) |
| 3196 | decimals++; |
| 3197 | } |
| 3198 | cp++; |
| 3199 | } |
| 3200 | if (*cp == '\n') |
| 3201 | cp++; |
| 3202 | if (*cp) |
| 3203 | return -EINVAL; |
| 3204 | if (decimals < 0) |
| 3205 | decimals = 0; |
| 3206 | while (decimals < scale) { |
| 3207 | result *= 10; |
| 3208 | decimals ++; |
| 3209 | } |
| 3210 | *res = result; |
| 3211 | return 0; |
| 3212 | } |
| 3213 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3214 | static void md_safemode_timeout(unsigned long data); |
| 3215 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3216 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3217 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3218 | { |
| 3219 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3220 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3221 | } |
| 3222 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3223 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3224 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3225 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3226 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3227 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3228 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3229 | if (msec == 0) |
| 3230 | mddev->safemode_delay = 0; |
| 3231 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3232 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3233 | unsigned long new_delay = (msec*HZ)/1000; |
| 3234 | |
| 3235 | if (new_delay == 0) |
| 3236 | new_delay = 1; |
| 3237 | mddev->safemode_delay = new_delay; |
| 3238 | if (new_delay < old_delay || old_delay == 0) |
| 3239 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3240 | } |
| 3241 | return len; |
| 3242 | } |
| 3243 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3244 | __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] | 3245 | |
| 3246 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3247 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3248 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3249 | struct md_personality *p; |
| 3250 | int ret; |
| 3251 | spin_lock(&mddev->lock); |
| 3252 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3253 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3254 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3255 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3256 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3257 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3258 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3259 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3260 | ret = 0; |
| 3261 | spin_unlock(&mddev->lock); |
| 3262 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3263 | } |
| 3264 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3265 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3266 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3267 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3268 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3269 | ssize_t rv; |
| 3270 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3271 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3272 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3273 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3274 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3275 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3276 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3277 | return -EINVAL; |
| 3278 | |
| 3279 | rv = mddev_lock(mddev); |
| 3280 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3281 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3282 | |
| 3283 | if (mddev->pers == NULL) { |
| 3284 | strncpy(mddev->clevel, buf, slen); |
| 3285 | if (mddev->clevel[slen-1] == '\n') |
| 3286 | slen--; |
| 3287 | mddev->clevel[slen] = 0; |
| 3288 | mddev->level = LEVEL_NONE; |
| 3289 | rv = len; |
| 3290 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3291 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3292 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3293 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3294 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3295 | |
| 3296 | /* request to change the personality. Need to ensure: |
| 3297 | * - array is not engaged in resync/recovery/reshape |
| 3298 | * - old personality can be suspended |
| 3299 | * - new personality will access other array. |
| 3300 | */ |
| 3301 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3302 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3303 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3304 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3305 | mddev->reshape_position != MaxSector || |
| 3306 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3307 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3308 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3309 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3310 | if (!mddev->pers->quiesce) { |
| 3311 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3312 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3313 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3314 | } |
| 3315 | |
| 3316 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3317 | strncpy(clevel, buf, slen); |
| 3318 | if (clevel[slen-1] == '\n') |
| 3319 | slen--; |
| 3320 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3321 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3322 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3323 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3324 | if (request_module("md-%s", clevel) != 0) |
| 3325 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3326 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3327 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3328 | if (!pers || !try_module_get(pers->owner)) { |
| 3329 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3330 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3331 | rv = -EINVAL; |
| 3332 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3333 | } |
| 3334 | spin_unlock(&pers_lock); |
| 3335 | |
| 3336 | if (pers == mddev->pers) { |
| 3337 | /* Nothing to do! */ |
| 3338 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3339 | rv = len; |
| 3340 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3341 | } |
| 3342 | if (!pers->takeover) { |
| 3343 | module_put(pers->owner); |
| 3344 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3345 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3346 | rv = -EINVAL; |
| 3347 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3348 | } |
| 3349 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3350 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3351 | rdev->new_raid_disk = rdev->raid_disk; |
| 3352 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3353 | /* ->takeover must set new_* and/or delta_disks |
| 3354 | * if it succeeds, and may set them when it fails. |
| 3355 | */ |
| 3356 | priv = pers->takeover(mddev); |
| 3357 | if (IS_ERR(priv)) { |
| 3358 | mddev->new_level = mddev->level; |
| 3359 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3360 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3361 | mddev->raid_disks -= mddev->delta_disks; |
| 3362 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3363 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3364 | module_put(pers->owner); |
| 3365 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3366 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3367 | rv = PTR_ERR(priv); |
| 3368 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | /* Looks like we have a winner */ |
| 3372 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 3373 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3374 | |
| 3375 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3376 | oldpers = mddev->pers; |
| 3377 | oldpriv = mddev->private; |
| 3378 | mddev->pers = pers; |
| 3379 | mddev->private = priv; |
| 3380 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3381 | mddev->level = mddev->new_level; |
| 3382 | mddev->layout = mddev->new_layout; |
| 3383 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 3384 | mddev->delta_disks = 0; |
| 3385 | mddev->reshape_backwards = 0; |
| 3386 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3387 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3388 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3389 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3390 | mddev->external) { |
| 3391 | /* We are converting from a no-redundancy array |
| 3392 | * to a redundancy array and metadata is managed |
| 3393 | * externally so we need to be sure that writes |
| 3394 | * won't block due to a need to transition |
| 3395 | * clean->dirty |
| 3396 | * until external management is started. |
| 3397 | */ |
| 3398 | mddev->in_sync = 0; |
| 3399 | mddev->safemode_delay = 0; |
| 3400 | mddev->safemode = 0; |
| 3401 | } |
| 3402 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3403 | oldpers->free(mddev, oldpriv); |
| 3404 | |
| 3405 | if (oldpers->sync_request == NULL && |
| 3406 | pers->sync_request != NULL) { |
| 3407 | /* need to add the md_redundancy_group */ |
| 3408 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3409 | printk(KERN_WARNING |
| 3410 | "md: cannot register extra attributes for %s\n", |
| 3411 | mdname(mddev)); |
| 3412 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3413 | } |
| 3414 | if (oldpers->sync_request != NULL && |
| 3415 | pers->sync_request == NULL) { |
| 3416 | /* need to remove the md_redundancy_group */ |
| 3417 | if (mddev->to_remove == NULL) |
| 3418 | mddev->to_remove = &md_redundancy_group; |
| 3419 | } |
| 3420 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3421 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3422 | if (rdev->raid_disk < 0) |
| 3423 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3424 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3425 | rdev->new_raid_disk = -1; |
| 3426 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3427 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3428 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3429 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3430 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3431 | if (rdev->raid_disk < 0) |
| 3432 | continue; |
| 3433 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3434 | continue; |
| 3435 | rdev->raid_disk = rdev->new_raid_disk; |
| 3436 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3437 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3438 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3439 | if (sysfs_link_rdev(mddev, rdev)) |
| 3440 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3441 | " for %s after level change\n", |
| 3442 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3443 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3444 | } |
| 3445 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3446 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3447 | /* this is now an array without redundancy, so |
| 3448 | * it must always be in_sync |
| 3449 | */ |
| 3450 | mddev->in_sync = 1; |
| 3451 | del_timer_sync(&mddev->safemode_timer); |
| 3452 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 3453 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3454 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3455 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3456 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 3457 | if (!mddev->thread) |
| 3458 | md_update_sb(mddev, 1); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3459 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3460 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3461 | rv = len; |
| 3462 | out_unlock: |
| 3463 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3464 | return rv; |
| 3465 | } |
| 3466 | |
| 3467 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3468 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3469 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3470 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3471 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3472 | { |
| 3473 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3474 | if (mddev->reshape_position != MaxSector && |
| 3475 | mddev->layout != mddev->new_layout) |
| 3476 | return sprintf(page, "%d (%d)\n", |
| 3477 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3478 | return sprintf(page, "%d\n", mddev->layout); |
| 3479 | } |
| 3480 | |
| 3481 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3482 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3483 | { |
| 3484 | char *e; |
| 3485 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3486 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3487 | |
| 3488 | if (!*buf || (*e && *e != '\n')) |
| 3489 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3490 | err = mddev_lock(mddev); |
| 3491 | if (err) |
| 3492 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3493 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3494 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3495 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3496 | err = -EBUSY; |
| 3497 | else if (mddev->ro) |
| 3498 | err = -EROFS; |
| 3499 | else { |
| 3500 | mddev->new_layout = n; |
| 3501 | err = mddev->pers->check_reshape(mddev); |
| 3502 | if (err) |
| 3503 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3504 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3505 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3506 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3507 | if (mddev->reshape_position == MaxSector) |
| 3508 | mddev->layout = n; |
| 3509 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3510 | mddev_unlock(mddev); |
| 3511 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3512 | } |
| 3513 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3514 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3515 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3516 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3517 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3518 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3519 | if (mddev->raid_disks == 0) |
| 3520 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3521 | if (mddev->reshape_position != MaxSector && |
| 3522 | mddev->delta_disks != 0) |
| 3523 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3524 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3525 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3526 | } |
| 3527 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3528 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3529 | |
| 3530 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3531 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3532 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3533 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3534 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3535 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3536 | |
| 3537 | if (!*buf || (*e && *e != '\n')) |
| 3538 | return -EINVAL; |
| 3539 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3540 | err = mddev_lock(mddev); |
| 3541 | if (err) |
| 3542 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3543 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3544 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3545 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3546 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3547 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3548 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3549 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3550 | rdev_for_each(rdev, mddev) { |
| 3551 | if (olddisks < n && |
| 3552 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3553 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3554 | if (olddisks > n && |
| 3555 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3556 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3557 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3558 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3559 | mddev->delta_disks = n - olddisks; |
| 3560 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3561 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3562 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3563 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3564 | out_unlock: |
| 3565 | mddev_unlock(mddev); |
| 3566 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3567 | } |
| 3568 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3569 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3570 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3571 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3572 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3573 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3574 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3575 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3576 | return sprintf(page, "%d (%d)\n", |
| 3577 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3578 | mddev->chunk_sectors << 9); |
| 3579 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3583 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3584 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3585 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3586 | char *e; |
| 3587 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3588 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3589 | if (!*buf || (*e && *e != '\n')) |
| 3590 | return -EINVAL; |
| 3591 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3592 | err = mddev_lock(mddev); |
| 3593 | if (err) |
| 3594 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3595 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3596 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3597 | err = -EBUSY; |
| 3598 | else if (mddev->ro) |
| 3599 | err = -EROFS; |
| 3600 | else { |
| 3601 | mddev->new_chunk_sectors = n >> 9; |
| 3602 | err = mddev->pers->check_reshape(mddev); |
| 3603 | if (err) |
| 3604 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3605 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3606 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3607 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3608 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3609 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3610 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3611 | mddev_unlock(mddev); |
| 3612 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3613 | } |
| 3614 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3615 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3616 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3617 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3618 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3619 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3620 | if (mddev->recovery_cp == MaxSector) |
| 3621 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3622 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3623 | } |
| 3624 | |
| 3625 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3626 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3627 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3628 | int err; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3629 | char *e; |
| 3630 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3631 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3632 | err = mddev_lock(mddev); |
| 3633 | if (err) |
| 3634 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3635 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3636 | err = -EBUSY; |
| 3637 | else if (cmd_match(buf, "none")) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3638 | n = MaxSector; |
| 3639 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3640 | err = -EINVAL; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3641 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3642 | if (!err) { |
| 3643 | mddev->recovery_cp = n; |
| 3644 | if (mddev->pers) |
| 3645 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3646 | } |
| 3647 | mddev_unlock(mddev); |
| 3648 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3649 | } |
| 3650 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3651 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3652 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3653 | /* |
| 3654 | * The array state can be: |
| 3655 | * |
| 3656 | * clear |
| 3657 | * No devices, no size, no level |
| 3658 | * Equivalent to STOP_ARRAY ioctl |
| 3659 | * inactive |
| 3660 | * May have some settings, but array is not active |
| 3661 | * all IO results in error |
| 3662 | * When written, doesn't tear down array, but just stops it |
| 3663 | * suspended (not supported yet) |
| 3664 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3665 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3666 | * readonly |
| 3667 | * no resync can happen. no superblocks get written. |
| 3668 | * write requests fail |
| 3669 | * read-auto |
| 3670 | * like readonly, but behaves like 'clean' on a write request. |
| 3671 | * |
| 3672 | * clean - no pending writes, but otherwise active. |
| 3673 | * When written to inactive array, starts without resync |
| 3674 | * If a write request arrives then |
| 3675 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3676 | * if not known, block and switch to write-pending |
| 3677 | * If written to an active array that has pending writes, then fails. |
| 3678 | * active |
| 3679 | * fully active: IO and resync can be happening. |
| 3680 | * When written to inactive array, starts with resync |
| 3681 | * |
| 3682 | * write-pending |
| 3683 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3684 | * |
| 3685 | * active-idle |
| 3686 | * like active, but no writes have been seen for a while (100msec). |
| 3687 | * |
| 3688 | */ |
| 3689 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3690 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3691 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3692 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3693 | "write-pending", "active-idle", NULL }; |
| 3694 | |
| 3695 | static int match_word(const char *word, char **list) |
| 3696 | { |
| 3697 | int n; |
| 3698 | for (n=0; list[n]; n++) |
| 3699 | if (cmd_match(word, list[n])) |
| 3700 | break; |
| 3701 | return n; |
| 3702 | } |
| 3703 | |
| 3704 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3705 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3706 | { |
| 3707 | enum array_state st = inactive; |
| 3708 | |
| 3709 | if (mddev->pers) |
| 3710 | switch(mddev->ro) { |
| 3711 | case 1: |
| 3712 | st = readonly; |
| 3713 | break; |
| 3714 | case 2: |
| 3715 | st = read_auto; |
| 3716 | break; |
| 3717 | case 0: |
| 3718 | if (mddev->in_sync) |
| 3719 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3720 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3721 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3722 | else if (mddev->safemode) |
| 3723 | st = active_idle; |
| 3724 | else |
| 3725 | st = active; |
| 3726 | } |
| 3727 | else { |
| 3728 | if (list_empty(&mddev->disks) && |
| 3729 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3730 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3731 | st = clear; |
| 3732 | else |
| 3733 | st = inactive; |
| 3734 | } |
| 3735 | return sprintf(page, "%s\n", array_states[st]); |
| 3736 | } |
| 3737 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3738 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 3739 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 3740 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3741 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3742 | |
| 3743 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3744 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3745 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3746 | int err; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3747 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3748 | |
| 3749 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 3750 | /* don't take reconfig_mutex when toggling between |
| 3751 | * clean and active |
| 3752 | */ |
| 3753 | spin_lock(&mddev->lock); |
| 3754 | if (st == active) { |
| 3755 | restart_array(mddev); |
| 3756 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 3757 | wake_up(&mddev->sb_wait); |
| 3758 | err = 0; |
| 3759 | } else /* st == clean */ { |
| 3760 | restart_array(mddev); |
| 3761 | if (atomic_read(&mddev->writes_pending) == 0) { |
| 3762 | if (mddev->in_sync == 0) { |
| 3763 | mddev->in_sync = 1; |
| 3764 | if (mddev->safemode == 1) |
| 3765 | mddev->safemode = 0; |
| 3766 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3767 | } |
| 3768 | err = 0; |
| 3769 | } else |
| 3770 | err = -EBUSY; |
| 3771 | } |
| 3772 | spin_unlock(&mddev->lock); |
| 3773 | return err; |
| 3774 | } |
| 3775 | err = mddev_lock(mddev); |
| 3776 | if (err) |
| 3777 | return err; |
| 3778 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3779 | switch(st) { |
| 3780 | case bad_word: |
| 3781 | break; |
| 3782 | case clear: |
| 3783 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3784 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3785 | break; |
| 3786 | case inactive: |
| 3787 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3788 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3789 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3790 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3791 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3792 | break; |
| 3793 | case suspended: |
| 3794 | break; /* not supported yet */ |
| 3795 | case readonly: |
| 3796 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3797 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3798 | else { |
| 3799 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3800 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3801 | err = do_md_run(mddev); |
| 3802 | } |
| 3803 | break; |
| 3804 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3805 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3806 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3807 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3808 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3809 | err = restart_array(mddev); |
| 3810 | if (err == 0) { |
| 3811 | mddev->ro = 2; |
| 3812 | set_disk_ro(mddev->gendisk, 0); |
| 3813 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3814 | } else { |
| 3815 | mddev->ro = 2; |
| 3816 | err = do_md_run(mddev); |
| 3817 | } |
| 3818 | break; |
| 3819 | case clean: |
| 3820 | if (mddev->pers) { |
| 3821 | restart_array(mddev); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3822 | spin_lock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3823 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3824 | if (mddev->in_sync == 0) { |
| 3825 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3826 | if (mddev->safemode == 1) |
| 3827 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3828 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3829 | } |
| 3830 | err = 0; |
| 3831 | } else |
| 3832 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3833 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3834 | } else |
| 3835 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3836 | break; |
| 3837 | case active: |
| 3838 | if (mddev->pers) { |
| 3839 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3840 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3841 | wake_up(&mddev->sb_wait); |
| 3842 | err = 0; |
| 3843 | } else { |
| 3844 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3845 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3846 | err = do_md_run(mddev); |
| 3847 | } |
| 3848 | break; |
| 3849 | case write_pending: |
| 3850 | case active_idle: |
| 3851 | /* these cannot be set */ |
| 3852 | break; |
| 3853 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3854 | |
| 3855 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 3856 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3857 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3858 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3859 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3860 | mddev_unlock(mddev); |
| 3861 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3862 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3863 | static struct md_sysfs_entry md_array_state = |
| 3864 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3865 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3866 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3867 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3868 | return sprintf(page, "%d\n", |
| 3869 | atomic_read(&mddev->max_corr_read_errors)); |
| 3870 | } |
| 3871 | |
| 3872 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3873 | 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] | 3874 | { |
| 3875 | char *e; |
| 3876 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3877 | |
| 3878 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3879 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3880 | return len; |
| 3881 | } |
| 3882 | return -EINVAL; |
| 3883 | } |
| 3884 | |
| 3885 | static struct md_sysfs_entry max_corr_read_errors = |
| 3886 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3887 | max_corrected_read_errors_store); |
| 3888 | |
| 3889 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3890 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3891 | { |
| 3892 | return -EINVAL; |
| 3893 | } |
| 3894 | |
| 3895 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3896 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3897 | { |
| 3898 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3899 | /* The new device is added to the array. |
| 3900 | * If the array has a persistent superblock, we read the |
| 3901 | * superblock to initialise info and check validity. |
| 3902 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3903 | * which mainly checks size. |
| 3904 | */ |
| 3905 | char *e; |
| 3906 | int major = simple_strtoul(buf, &e, 10); |
| 3907 | int minor; |
| 3908 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3909 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3910 | int err; |
| 3911 | |
| 3912 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3913 | return -EINVAL; |
| 3914 | minor = simple_strtoul(e+1, &e, 10); |
| 3915 | if (*e && *e != '\n') |
| 3916 | return -EINVAL; |
| 3917 | dev = MKDEV(major, minor); |
| 3918 | if (major != MAJOR(dev) || |
| 3919 | minor != MINOR(dev)) |
| 3920 | return -EOVERFLOW; |
| 3921 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3922 | flush_workqueue(md_misc_wq); |
| 3923 | |
| 3924 | err = mddev_lock(mddev); |
| 3925 | if (err) |
| 3926 | return err; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3927 | if (mddev->persistent) { |
| 3928 | rdev = md_import_device(dev, mddev->major_version, |
| 3929 | mddev->minor_version); |
| 3930 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3931 | struct md_rdev *rdev0 |
| 3932 | = list_entry(mddev->disks.next, |
| 3933 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3934 | err = super_types[mddev->major_version] |
| 3935 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3936 | if (err < 0) |
| 3937 | goto out; |
| 3938 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3939 | } else if (mddev->external) |
| 3940 | rdev = md_import_device(dev, -2, -1); |
| 3941 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3942 | rdev = md_import_device(dev, -1, -1); |
| 3943 | |
| 3944 | if (IS_ERR(rdev)) |
| 3945 | return PTR_ERR(rdev); |
| 3946 | err = bind_rdev_to_array(rdev, mddev); |
| 3947 | out: |
| 3948 | if (err) |
| 3949 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3950 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3951 | return err ? err : len; |
| 3952 | } |
| 3953 | |
| 3954 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3955 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3956 | |
| 3957 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3958 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3959 | { |
| 3960 | char *end; |
| 3961 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3962 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3963 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3964 | err = mddev_lock(mddev); |
| 3965 | if (err) |
| 3966 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3967 | if (!mddev->bitmap) |
| 3968 | goto out; |
| 3969 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3970 | while (*buf) { |
| 3971 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3972 | if (buf == end) break; |
| 3973 | if (*end == '-') { /* range */ |
| 3974 | buf = end + 1; |
| 3975 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3976 | if (buf == end) break; |
| 3977 | } |
| 3978 | if (*end && !isspace(*end)) break; |
| 3979 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3980 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3981 | } |
| 3982 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3983 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3984 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3985 | return len; |
| 3986 | } |
| 3987 | |
| 3988 | static struct md_sysfs_entry md_bitmap = |
| 3989 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3990 | |
| 3991 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3992 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3993 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3994 | return sprintf(page, "%llu\n", |
| 3995 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3996 | } |
| 3997 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3998 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3999 | |
| 4000 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4001 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4002 | { |
| 4003 | /* If array is inactive, we can reduce the component size, but |
| 4004 | * not increase it (except from 0). |
| 4005 | * If array is active, we can try an on-line resize |
| 4006 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4007 | sector_t sectors; |
| 4008 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4009 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4010 | if (err < 0) |
| 4011 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4012 | err = mddev_lock(mddev); |
| 4013 | if (err) |
| 4014 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4015 | if (mddev->pers) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 4016 | if (mddev_is_clustered(mddev)) |
| 4017 | md_cluster_ops->metadata_update_start(mddev); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4018 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4019 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 4020 | if (mddev_is_clustered(mddev)) |
| 4021 | md_cluster_ops->metadata_update_finish(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4022 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4023 | if (mddev->dev_sectors == 0 || |
| 4024 | mddev->dev_sectors > sectors) |
| 4025 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4026 | else |
| 4027 | err = -ENOSPC; |
| 4028 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4029 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4030 | return err ? err : len; |
| 4031 | } |
| 4032 | |
| 4033 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4034 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4035 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4036 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4037 | * This is one of |
| 4038 | * 'none' for arrays with no metadata (good luck...) |
| 4039 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4040 | * or N.M for internally known formats |
| 4041 | */ |
| 4042 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4043 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4044 | { |
| 4045 | if (mddev->persistent) |
| 4046 | return sprintf(page, "%d.%d\n", |
| 4047 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4048 | else if (mddev->external) |
| 4049 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4050 | else |
| 4051 | return sprintf(page, "none\n"); |
| 4052 | } |
| 4053 | |
| 4054 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4055 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4056 | { |
| 4057 | int major, minor; |
| 4058 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4059 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4060 | /* Changing the details of 'external' metadata is |
| 4061 | * always permitted. Otherwise there must be |
| 4062 | * no devices attached to the array. |
| 4063 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4064 | |
| 4065 | err = mddev_lock(mddev); |
| 4066 | if (err) |
| 4067 | return err; |
| 4068 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4069 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4070 | ; |
| 4071 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4072 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4073 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4074 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4075 | if (cmd_match(buf, "none")) { |
| 4076 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4077 | mddev->external = 0; |
| 4078 | mddev->major_version = 0; |
| 4079 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4080 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4081 | } |
| 4082 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4083 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4084 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4085 | namelen = sizeof(mddev->metadata_type)-1; |
| 4086 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4087 | mddev->metadata_type[namelen] = 0; |
| 4088 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4089 | mddev->metadata_type[--namelen] = 0; |
| 4090 | mddev->persistent = 0; |
| 4091 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4092 | mddev->major_version = 0; |
| 4093 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4094 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4095 | } |
| 4096 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4097 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4098 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4099 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4100 | buf = e+1; |
| 4101 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4102 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4103 | goto out_unlock; |
| 4104 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4105 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4106 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4107 | mddev->major_version = major; |
| 4108 | mddev->minor_version = minor; |
| 4109 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4110 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4111 | err = 0; |
| 4112 | out_unlock: |
| 4113 | mddev_unlock(mddev); |
| 4114 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4118 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4119 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4120 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4121 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4122 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4123 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4124 | unsigned long recovery = mddev->recovery; |
| 4125 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4126 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4127 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4128 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4129 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4130 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4131 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4132 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4133 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4134 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4135 | type = "check"; |
| 4136 | else |
| 4137 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4138 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4139 | type = "recover"; |
| 4140 | } |
| 4141 | return sprintf(page, "%s\n", type); |
| 4142 | } |
| 4143 | |
| 4144 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4145 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4146 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4147 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4148 | return -EINVAL; |
| 4149 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4150 | if (cmd_match(page, "frozen")) |
| 4151 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4152 | else |
| 4153 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4154 | |
| 4155 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 4156 | flush_workqueue(md_misc_wq); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4157 | if (mddev->sync_thread) { |
| 4158 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4159 | if (mddev_lock(mddev) == 0) { |
| 4160 | md_reap_sync_thread(mddev); |
| 4161 | mddev_unlock(mddev); |
| 4162 | } |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4163 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4164 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4165 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4166 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4167 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4168 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4169 | else if (cmd_match(page, "recover")) { |
| 4170 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4171 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4172 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4173 | int err; |
| 4174 | if (mddev->pers->start_reshape == NULL) |
| 4175 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4176 | err = mddev_lock(mddev); |
| 4177 | if (!err) { |
| 4178 | err = mddev->pers->start_reshape(mddev); |
| 4179 | mddev_unlock(mddev); |
| 4180 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4181 | if (err) |
| 4182 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4183 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4184 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4185 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4186 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4187 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4188 | return -EINVAL; |
| 4189 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4190 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4191 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4192 | if (mddev->ro == 2) { |
| 4193 | /* A write to sync_action is enough to justify |
| 4194 | * canceling read-auto mode |
| 4195 | */ |
| 4196 | mddev->ro = 0; |
| 4197 | md_wakeup_thread(mddev->sync_thread); |
| 4198 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4199 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4200 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4201 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4202 | return len; |
| 4203 | } |
| 4204 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4205 | static struct md_sysfs_entry md_scan_mode = |
| 4206 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
| 4207 | |
| 4208 | static ssize_t |
| 4209 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4210 | { |
| 4211 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4212 | } |
| 4213 | |
| 4214 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4215 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4216 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4217 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4218 | { |
| 4219 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4220 | (unsigned long long) |
| 4221 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4222 | } |
| 4223 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4224 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4225 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4226 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4227 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4228 | { |
| 4229 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4230 | mddev->sync_speed_min ? "local": "system"); |
| 4231 | } |
| 4232 | |
| 4233 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4234 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4235 | { |
| 4236 | int min; |
| 4237 | char *e; |
| 4238 | if (strncmp(buf, "system", 6)==0) { |
| 4239 | mddev->sync_speed_min = 0; |
| 4240 | return len; |
| 4241 | } |
| 4242 | min = simple_strtoul(buf, &e, 10); |
| 4243 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4244 | return -EINVAL; |
| 4245 | mddev->sync_speed_min = min; |
| 4246 | return len; |
| 4247 | } |
| 4248 | |
| 4249 | static struct md_sysfs_entry md_sync_min = |
| 4250 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4251 | |
| 4252 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4253 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4254 | { |
| 4255 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4256 | mddev->sync_speed_max ? "local": "system"); |
| 4257 | } |
| 4258 | |
| 4259 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4260 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4261 | { |
| 4262 | int max; |
| 4263 | char *e; |
| 4264 | if (strncmp(buf, "system", 6)==0) { |
| 4265 | mddev->sync_speed_max = 0; |
| 4266 | return len; |
| 4267 | } |
| 4268 | max = simple_strtoul(buf, &e, 10); |
| 4269 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4270 | return -EINVAL; |
| 4271 | mddev->sync_speed_max = max; |
| 4272 | return len; |
| 4273 | } |
| 4274 | |
| 4275 | static struct md_sysfs_entry md_sync_max = |
| 4276 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4277 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4278 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4279 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4280 | { |
| 4281 | return sprintf(page, "%d\n", mddev->degraded); |
| 4282 | } |
| 4283 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4284 | |
| 4285 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4286 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4287 | { |
| 4288 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4289 | } |
| 4290 | |
| 4291 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4292 | 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] | 4293 | { |
| 4294 | long n; |
| 4295 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4296 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4297 | return -EINVAL; |
| 4298 | |
| 4299 | if (n != 0 && n != 1) |
| 4300 | return -EINVAL; |
| 4301 | |
| 4302 | mddev->parallel_resync = n; |
| 4303 | |
| 4304 | if (mddev->sync_thread) |
| 4305 | wake_up(&resync_wait); |
| 4306 | |
| 4307 | return len; |
| 4308 | } |
| 4309 | |
| 4310 | /* force parallel resync, even with shared block devices */ |
| 4311 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4312 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4313 | sync_force_parallel_show, sync_force_parallel_store); |
| 4314 | |
| 4315 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4316 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4317 | { |
| 4318 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4319 | if (mddev->curr_resync == 0) |
| 4320 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4321 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4322 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4323 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4324 | db = resync - mddev->resync_mark_cnt; |
| 4325 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4326 | } |
| 4327 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4328 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4329 | |
| 4330 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4331 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4332 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4333 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4334 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4335 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4336 | return sprintf(page, "none\n"); |
| 4337 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4338 | if (mddev->curr_resync == 1 || |
| 4339 | mddev->curr_resync == 2) |
| 4340 | return sprintf(page, "delayed\n"); |
| 4341 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4342 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4343 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4344 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4345 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4346 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4347 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4348 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4349 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4350 | } |
| 4351 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4352 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4353 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4354 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4355 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4356 | { |
| 4357 | return sprintf(page, "%llu\n", |
| 4358 | (unsigned long long)mddev->resync_min); |
| 4359 | } |
| 4360 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4361 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4362 | { |
| 4363 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4364 | int err; |
| 4365 | int chunk; |
| 4366 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4367 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4368 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4369 | |
| 4370 | spin_lock(&mddev->lock); |
| 4371 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4372 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4373 | goto out_unlock; |
| 4374 | |
| 4375 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4376 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4377 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4378 | |
| 4379 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4380 | chunk = mddev->chunk_sectors; |
| 4381 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4382 | sector_t temp = min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4383 | |
| 4384 | err = -EINVAL; |
| 4385 | if (sector_div(temp, chunk)) |
| 4386 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4387 | } |
| 4388 | mddev->resync_min = min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4389 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4390 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4391 | out_unlock: |
| 4392 | spin_unlock(&mddev->lock); |
| 4393 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | static struct md_sysfs_entry md_min_sync = |
| 4397 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4398 | |
| 4399 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4400 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4401 | { |
| 4402 | if (mddev->resync_max == MaxSector) |
| 4403 | return sprintf(page, "max\n"); |
| 4404 | else |
| 4405 | return sprintf(page, "%llu\n", |
| 4406 | (unsigned long long)mddev->resync_max); |
| 4407 | } |
| 4408 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4409 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4410 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4411 | int err; |
| 4412 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4413 | if (strncmp(buf, "max", 3) == 0) |
| 4414 | mddev->resync_max = MaxSector; |
| 4415 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4416 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4417 | int chunk; |
| 4418 | |
| 4419 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4420 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4421 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4422 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4423 | goto out_unlock; |
| 4424 | |
| 4425 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4426 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4427 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4428 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4429 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4430 | |
| 4431 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4432 | chunk = mddev->chunk_sectors; |
| 4433 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4434 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4435 | |
| 4436 | err = -EINVAL; |
| 4437 | if (sector_div(temp, chunk)) |
| 4438 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4439 | } |
| 4440 | mddev->resync_max = max; |
| 4441 | } |
| 4442 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4443 | err = 0; |
| 4444 | out_unlock: |
| 4445 | spin_unlock(&mddev->lock); |
| 4446 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4447 | } |
| 4448 | |
| 4449 | static struct md_sysfs_entry md_max_sync = |
| 4450 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4451 | |
| 4452 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4453 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4454 | { |
| 4455 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4456 | } |
| 4457 | |
| 4458 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4459 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4460 | { |
| 4461 | char *e; |
| 4462 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4463 | unsigned long long old; |
| 4464 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4465 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4466 | if (buf == e || (*e && *e != '\n')) |
| 4467 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4468 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4469 | err = mddev_lock(mddev); |
| 4470 | if (err) |
| 4471 | return err; |
| 4472 | err = -EINVAL; |
| 4473 | if (mddev->pers == NULL || |
| 4474 | mddev->pers->quiesce == NULL) |
| 4475 | goto unlock; |
| 4476 | old = mddev->suspend_lo; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4477 | mddev->suspend_lo = new; |
| 4478 | if (new >= old) |
| 4479 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4480 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4481 | else { |
| 4482 | /* Expanding suspended region - need to wait */ |
| 4483 | mddev->pers->quiesce(mddev, 1); |
| 4484 | mddev->pers->quiesce(mddev, 0); |
| 4485 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4486 | err = 0; |
| 4487 | unlock: |
| 4488 | mddev_unlock(mddev); |
| 4489 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4490 | } |
| 4491 | static struct md_sysfs_entry md_suspend_lo = |
| 4492 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4493 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4494 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4495 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4496 | { |
| 4497 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4498 | } |
| 4499 | |
| 4500 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4501 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4502 | { |
| 4503 | char *e; |
| 4504 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4505 | unsigned long long old; |
| 4506 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4507 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4508 | if (buf == e || (*e && *e != '\n')) |
| 4509 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4510 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4511 | err = mddev_lock(mddev); |
| 4512 | if (err) |
| 4513 | return err; |
| 4514 | err = -EINVAL; |
| 4515 | if (mddev->pers == NULL || |
| 4516 | mddev->pers->quiesce == NULL) |
| 4517 | goto unlock; |
| 4518 | old = mddev->suspend_hi; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4519 | mddev->suspend_hi = new; |
| 4520 | if (new <= old) |
| 4521 | /* Shrinking suspended region */ |
| 4522 | mddev->pers->quiesce(mddev, 2); |
| 4523 | else { |
| 4524 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4525 | mddev->pers->quiesce(mddev, 1); |
| 4526 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4527 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4528 | err = 0; |
| 4529 | unlock: |
| 4530 | mddev_unlock(mddev); |
| 4531 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4532 | } |
| 4533 | static struct md_sysfs_entry md_suspend_hi = |
| 4534 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4535 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4536 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4537 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4538 | { |
| 4539 | if (mddev->reshape_position != MaxSector) |
| 4540 | return sprintf(page, "%llu\n", |
| 4541 | (unsigned long long)mddev->reshape_position); |
| 4542 | strcpy(page, "none\n"); |
| 4543 | return 5; |
| 4544 | } |
| 4545 | |
| 4546 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4547 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4548 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4549 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4550 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4551 | int err; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4552 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4553 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4554 | if (buf == e || (*e && *e != '\n')) |
| 4555 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4556 | err = mddev_lock(mddev); |
| 4557 | if (err) |
| 4558 | return err; |
| 4559 | err = -EBUSY; |
| 4560 | if (mddev->pers) |
| 4561 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4562 | mddev->reshape_position = new; |
| 4563 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4564 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4565 | mddev->new_level = mddev->level; |
| 4566 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4567 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4568 | rdev_for_each(rdev, mddev) |
| 4569 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4570 | err = 0; |
| 4571 | unlock: |
| 4572 | mddev_unlock(mddev); |
| 4573 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4574 | } |
| 4575 | |
| 4576 | static struct md_sysfs_entry md_reshape_position = |
| 4577 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4578 | reshape_position_store); |
| 4579 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4580 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4581 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4582 | { |
| 4583 | return sprintf(page, "%s\n", |
| 4584 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4585 | } |
| 4586 | |
| 4587 | static ssize_t |
| 4588 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4589 | { |
| 4590 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4591 | int err; |
| 4592 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4593 | if (cmd_match(buf, "forwards")) |
| 4594 | backwards = 0; |
| 4595 | else if (cmd_match(buf, "backwards")) |
| 4596 | backwards = 1; |
| 4597 | else |
| 4598 | return -EINVAL; |
| 4599 | if (mddev->reshape_backwards == backwards) |
| 4600 | return len; |
| 4601 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4602 | err = mddev_lock(mddev); |
| 4603 | if (err) |
| 4604 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4605 | /* check if we are allowed to change */ |
| 4606 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4607 | err = -EBUSY; |
| 4608 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4609 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4610 | err = -EINVAL; |
| 4611 | else |
| 4612 | mddev->reshape_backwards = backwards; |
| 4613 | mddev_unlock(mddev); |
| 4614 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | static struct md_sysfs_entry md_reshape_direction = |
| 4618 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4619 | reshape_direction_store); |
| 4620 | |
| 4621 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4622 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4623 | { |
| 4624 | if (mddev->external_size) |
| 4625 | return sprintf(page, "%llu\n", |
| 4626 | (unsigned long long)mddev->array_sectors/2); |
| 4627 | else |
| 4628 | return sprintf(page, "default\n"); |
| 4629 | } |
| 4630 | |
| 4631 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4632 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4633 | { |
| 4634 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4635 | int err; |
| 4636 | |
| 4637 | err = mddev_lock(mddev); |
| 4638 | if (err) |
| 4639 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4640 | |
| 4641 | if (strncmp(buf, "default", 7) == 0) { |
| 4642 | if (mddev->pers) |
| 4643 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4644 | else |
| 4645 | sectors = mddev->array_sectors; |
| 4646 | |
| 4647 | mddev->external_size = 0; |
| 4648 | } else { |
| 4649 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4650 | err = -EINVAL; |
| 4651 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 4652 | err = -E2BIG; |
| 4653 | else |
| 4654 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4655 | } |
| 4656 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4657 | if (!err) { |
| 4658 | mddev->array_sectors = sectors; |
| 4659 | if (mddev->pers) { |
| 4660 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4661 | revalidate_disk(mddev->gendisk); |
| 4662 | } |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4663 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4664 | mddev_unlock(mddev); |
| 4665 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4666 | } |
| 4667 | |
| 4668 | static struct md_sysfs_entry md_array_size = |
| 4669 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4670 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4671 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4672 | static struct attribute *md_default_attrs[] = { |
| 4673 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4674 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4675 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4676 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4677 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4678 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4679 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4680 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4681 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4682 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4683 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4684 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4685 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4686 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4687 | NULL, |
| 4688 | }; |
| 4689 | |
| 4690 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4691 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4692 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4693 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4694 | &md_sync_min.attr, |
| 4695 | &md_sync_max.attr, |
| 4696 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4697 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4698 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4699 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4700 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4701 | &md_suspend_lo.attr, |
| 4702 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4703 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4704 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4705 | NULL, |
| 4706 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4707 | static struct attribute_group md_redundancy_group = { |
| 4708 | .name = NULL, |
| 4709 | .attrs = md_redundancy_attrs, |
| 4710 | }; |
| 4711 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4712 | static ssize_t |
| 4713 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4714 | { |
| 4715 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4716 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4717 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4718 | |
| 4719 | if (!entry->show) |
| 4720 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4721 | spin_lock(&all_mddevs_lock); |
| 4722 | if (list_empty(&mddev->all_mddevs)) { |
| 4723 | spin_unlock(&all_mddevs_lock); |
| 4724 | return -EBUSY; |
| 4725 | } |
| 4726 | mddev_get(mddev); |
| 4727 | spin_unlock(&all_mddevs_lock); |
| 4728 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4729 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4730 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4731 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4732 | } |
| 4733 | |
| 4734 | static ssize_t |
| 4735 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4736 | const char *page, size_t length) |
| 4737 | { |
| 4738 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4739 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4740 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4741 | |
| 4742 | if (!entry->store) |
| 4743 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4744 | if (!capable(CAP_SYS_ADMIN)) |
| 4745 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4746 | spin_lock(&all_mddevs_lock); |
| 4747 | if (list_empty(&mddev->all_mddevs)) { |
| 4748 | spin_unlock(&all_mddevs_lock); |
| 4749 | return -EBUSY; |
| 4750 | } |
| 4751 | mddev_get(mddev); |
| 4752 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4753 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4754 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4755 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | static void md_free(struct kobject *ko) |
| 4759 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4760 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4761 | |
| 4762 | if (mddev->sysfs_state) |
| 4763 | sysfs_put(mddev->sysfs_state); |
| 4764 | |
| 4765 | if (mddev->gendisk) { |
| 4766 | del_gendisk(mddev->gendisk); |
| 4767 | put_disk(mddev->gendisk); |
| 4768 | } |
| 4769 | if (mddev->queue) |
| 4770 | blk_cleanup_queue(mddev->queue); |
| 4771 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4772 | kfree(mddev); |
| 4773 | } |
| 4774 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4775 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4776 | .show = md_attr_show, |
| 4777 | .store = md_attr_store, |
| 4778 | }; |
| 4779 | static struct kobj_type md_ktype = { |
| 4780 | .release = md_free, |
| 4781 | .sysfs_ops = &md_sysfs_ops, |
| 4782 | .default_attrs = md_default_attrs, |
| 4783 | }; |
| 4784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4785 | int mdp_major = 0; |
| 4786 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4787 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4788 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4789 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4790 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4791 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4792 | kobject_del(&mddev->kobj); |
| 4793 | kobject_put(&mddev->kobj); |
| 4794 | } |
| 4795 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4796 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4797 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4798 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4799 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4800 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4801 | int partitioned; |
| 4802 | int shift; |
| 4803 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4804 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | |
| 4806 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4807 | return -ENODEV; |
| 4808 | |
| 4809 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4810 | shift = partitioned ? MdpMinorShift : 0; |
| 4811 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4813 | /* wait for any previous instance of this device to be |
| 4814 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4815 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4816 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4817 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4818 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4819 | error = -EEXIST; |
| 4820 | if (mddev->gendisk) |
| 4821 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4822 | |
| 4823 | if (name) { |
| 4824 | /* Need to ensure that 'name' is not a duplicate. |
| 4825 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4826 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4827 | spin_lock(&all_mddevs_lock); |
| 4828 | |
| 4829 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4830 | if (mddev2->gendisk && |
| 4831 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4832 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4833 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4834 | } |
| 4835 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4837 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4838 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4839 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4840 | if (!mddev->queue) |
| 4841 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4842 | mddev->queue->queuedata = mddev; |
| 4843 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4844 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 4845 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | disk = alloc_disk(1 << shift); |
| 4848 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4849 | blk_cleanup_queue(mddev->queue); |
| 4850 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4851 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4853 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4854 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4855 | if (name) |
| 4856 | strcpy(disk->disk_name, name); |
| 4857 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4859 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4860 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | disk->fops = &md_fops; |
| 4862 | disk->private_data = mddev; |
| 4863 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4864 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4865 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4866 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4867 | * remove it now. |
| 4868 | */ |
| 4869 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4870 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4871 | /* As soon as we call add_disk(), another thread could get |
| 4872 | * through to md_open, so make sure it doesn't get too far |
| 4873 | */ |
| 4874 | mutex_lock(&mddev->open_mutex); |
| 4875 | add_disk(disk); |
| 4876 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4877 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4878 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4879 | if (error) { |
| 4880 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4881 | * __must_check, we must do something with the result |
| 4882 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4883 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4884 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4885 | error = 0; |
| 4886 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4887 | if (mddev->kobj.sd && |
| 4888 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4889 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4890 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4891 | abort: |
| 4892 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4893 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4894 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4895 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4896 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4897 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4898 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4899 | } |
| 4900 | |
| 4901 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4902 | { |
| 4903 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | return NULL; |
| 4905 | } |
| 4906 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4907 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4908 | { |
| 4909 | /* val must be "md_*" where * is not all digits. |
| 4910 | * We allocate an array with a large free minor number, and |
| 4911 | * set the name to val. val must not already be an active name. |
| 4912 | */ |
| 4913 | int len = strlen(val); |
| 4914 | char buf[DISK_NAME_LEN]; |
| 4915 | |
| 4916 | while (len && val[len-1] == '\n') |
| 4917 | len--; |
| 4918 | if (len >= DISK_NAME_LEN) |
| 4919 | return -E2BIG; |
| 4920 | strlcpy(buf, val, len+1); |
| 4921 | if (strncmp(buf, "md_", 3) != 0) |
| 4922 | return -EINVAL; |
| 4923 | return md_alloc(0, buf); |
| 4924 | } |
| 4925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | static void md_safemode_timeout(unsigned long data) |
| 4927 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4928 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4929 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4930 | if (!atomic_read(&mddev->writes_pending)) { |
| 4931 | mddev->safemode = 1; |
| 4932 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4933 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4934 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | md_wakeup_thread(mddev->thread); |
| 4936 | } |
| 4937 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4938 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4939 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4940 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4942 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4943 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4944 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4945 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4946 | if (list_empty(&mddev->disks)) |
| 4947 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | |
| 4950 | if (mddev->pers) |
| 4951 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4952 | /* Cannot run until previous stop completes properly */ |
| 4953 | if (mddev->sysfs_active) |
| 4954 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4956 | /* |
| 4957 | * Analyze all RAID superblock(s) |
| 4958 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4959 | if (!mddev->raid_disks) { |
| 4960 | if (!mddev->persistent) |
| 4961 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4962 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4964 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4965 | if (mddev->level != LEVEL_NONE) |
| 4966 | request_module("md-level-%d", mddev->level); |
| 4967 | else if (mddev->clevel[0]) |
| 4968 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | |
| 4970 | /* |
| 4971 | * Drop all container device buffers, from now on |
| 4972 | * the only valid external interface is through the md |
| 4973 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4975 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4976 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | continue; |
| 4978 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4979 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4980 | |
| 4981 | /* perform some consistency tests on the device. |
| 4982 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4983 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4984 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4985 | if (rdev->meta_bdev) { |
| 4986 | /* Nothing to check */; |
| 4987 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4988 | if (mddev->dev_sectors && |
| 4989 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4990 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4991 | printk("md: %s: data overlaps metadata\n", |
| 4992 | mdname(mddev)); |
| 4993 | return -EINVAL; |
| 4994 | } |
| 4995 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4996 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4997 | > rdev->data_offset) { |
| 4998 | printk("md: %s: metadata overlaps data\n", |
| 4999 | mdname(mddev)); |
| 5000 | return -EINVAL; |
| 5001 | } |
| 5002 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5003 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5004 | } |
| 5005 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5006 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 5007 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5009 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5010 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5011 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5013 | if (mddev->level != LEVEL_NONE) |
| 5014 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5015 | mddev->level); |
| 5016 | else |
| 5017 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5018 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | return -EINVAL; |
| 5020 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5021 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5022 | if (mddev->level != pers->level) { |
| 5023 | mddev->level = pers->level; |
| 5024 | mddev->new_level = pers->level; |
| 5025 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5026 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5028 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5029 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5030 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5031 | module_put(pers->owner); |
| 5032 | return -EINVAL; |
| 5033 | } |
| 5034 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5035 | if (pers->sync_request) { |
| 5036 | /* Warn if this is a potentially silly |
| 5037 | * configuration. |
| 5038 | */ |
| 5039 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5040 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5041 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5042 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5043 | rdev_for_each(rdev, mddev) |
| 5044 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5045 | if (rdev < rdev2 && |
| 5046 | rdev->bdev->bd_contains == |
| 5047 | rdev2->bdev->bd_contains) { |
| 5048 | printk(KERN_WARNING |
| 5049 | "%s: WARNING: %s appears to be" |
| 5050 | " on the same physical disk as" |
| 5051 | " %s.\n", |
| 5052 | mdname(mddev), |
| 5053 | bdevname(rdev->bdev,b), |
| 5054 | bdevname(rdev2->bdev,b2)); |
| 5055 | warned = 1; |
| 5056 | } |
| 5057 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5058 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5059 | if (warned) |
| 5060 | printk(KERN_WARNING |
| 5061 | "True protection against single-disk" |
| 5062 | " failure might be compromised.\n"); |
| 5063 | } |
| 5064 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5065 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5066 | /* may be over-ridden by personality */ |
| 5067 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5068 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5069 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5071 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5072 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5073 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5074 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5075 | if (err) |
| 5076 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5077 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5078 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5079 | " but 'external_size' not in effect?\n", __func__); |
| 5080 | printk(KERN_ERR |
| 5081 | "md: invalid array_size %llu > default size %llu\n", |
| 5082 | (unsigned long long)mddev->array_sectors / 2, |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5083 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5084 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5085 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5086 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5087 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5088 | struct bitmap *bitmap; |
| 5089 | |
| 5090 | bitmap = bitmap_create(mddev, -1); |
| 5091 | if (IS_ERR(bitmap)) { |
| 5092 | err = PTR_ERR(bitmap); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5093 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5094 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5095 | } else |
| 5096 | mddev->bitmap = bitmap; |
| 5097 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5098 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5099 | if (err) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5100 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5101 | pers->free(mddev, mddev->private); |
| 5102 | module_put(pers->owner); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5103 | bitmap_destroy(mddev); |
| 5104 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5106 | if (mddev->queue) { |
| 5107 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5108 | mddev->queue->backing_dev_info.congested_fn = md_congested; |
NeilBrown | 64590f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5109 | blk_queue_merge_bvec(mddev->queue, md_mergeable_bvec); |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5110 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5111 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5112 | if (mddev->kobj.sd && |
| 5113 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5114 | printk(KERN_WARNING |
| 5115 | "md: cannot register extra attributes for %s\n", |
| 5116 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5117 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5118 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5119 | mddev->ro = 0; |
| 5120 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5121 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5122 | atomic_set(&mddev->max_corr_read_errors, |
| 5123 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5124 | mddev->safemode = 0; |
| 5125 | mddev->safemode_timer.function = md_safemode_timeout; |
| 5126 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5127 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5129 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5130 | spin_lock(&mddev->lock); |
| 5131 | mddev->pers = pers; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5132 | mddev->ready = 1; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5133 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5134 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5135 | if (rdev->raid_disk >= 0) |
| 5136 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5137 | /* failure here is OK */; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5139 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5140 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5141 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5142 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5144 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5145 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5146 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5147 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | return 0; |
| 5149 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5150 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5152 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5153 | { |
| 5154 | int err; |
| 5155 | |
| 5156 | err = md_run(mddev); |
| 5157 | if (err) |
| 5158 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5159 | err = bitmap_load(mddev); |
| 5160 | if (err) { |
| 5161 | bitmap_destroy(mddev); |
| 5162 | goto out; |
| 5163 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5164 | |
| 5165 | md_wakeup_thread(mddev->thread); |
| 5166 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5167 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5168 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5169 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5170 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5171 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5172 | out: |
| 5173 | return err; |
| 5174 | } |
| 5175 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5176 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5177 | { |
| 5178 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5180 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5182 | return -ENXIO; |
| 5183 | if (!mddev->pers) |
| 5184 | return -EINVAL; |
| 5185 | if (!mddev->ro) |
| 5186 | return -EBUSY; |
| 5187 | mddev->safemode = 0; |
| 5188 | mddev->ro = 0; |
| 5189 | set_disk_ro(disk, 0); |
| 5190 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5191 | mdname(mddev)); |
| 5192 | /* Kick recovery or resync if necessary */ |
| 5193 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5194 | md_wakeup_thread(mddev->thread); |
| 5195 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5196 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5197 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5198 | } |
| 5199 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5200 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5201 | { |
| 5202 | mddev->array_sectors = 0; |
| 5203 | mddev->external_size = 0; |
| 5204 | mddev->dev_sectors = 0; |
| 5205 | mddev->raid_disks = 0; |
| 5206 | mddev->recovery_cp = 0; |
| 5207 | mddev->resync_min = 0; |
| 5208 | mddev->resync_max = MaxSector; |
| 5209 | mddev->reshape_position = MaxSector; |
| 5210 | mddev->external = 0; |
| 5211 | mddev->persistent = 0; |
| 5212 | mddev->level = LEVEL_NONE; |
| 5213 | mddev->clevel[0] = 0; |
| 5214 | mddev->flags = 0; |
| 5215 | mddev->ro = 0; |
| 5216 | mddev->metadata_type[0] = 0; |
| 5217 | mddev->chunk_sectors = 0; |
| 5218 | mddev->ctime = mddev->utime = 0; |
| 5219 | mddev->layout = 0; |
| 5220 | mddev->max_disks = 0; |
| 5221 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5222 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5223 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5224 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5225 | mddev->new_level = LEVEL_NONE; |
| 5226 | mddev->new_layout = 0; |
| 5227 | mddev->new_chunk_sectors = 0; |
| 5228 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5229 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5230 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5231 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5232 | mddev->recovery = 0; |
| 5233 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5234 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5235 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5236 | mddev->safemode = 0; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5237 | mddev->merge_check_needed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5238 | mddev->bitmap_info.offset = 0; |
| 5239 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5240 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5241 | mddev->bitmap_info.chunksize = 0; |
| 5242 | mddev->bitmap_info.daemon_sleep = 0; |
| 5243 | mddev->bitmap_info.max_write_behind = 0; |
| 5244 | } |
| 5245 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5246 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5247 | { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 5248 | if (mddev_is_clustered(mddev)) |
| 5249 | md_cluster_ops->metadata_update_start(mddev); |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 5250 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5251 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5252 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5253 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5254 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5255 | } |
| 5256 | |
| 5257 | del_timer_sync(&mddev->safemode_timer); |
| 5258 | |
| 5259 | bitmap_flush(mddev); |
| 5260 | md_super_wait(mddev); |
| 5261 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5262 | if (mddev->ro == 0 && |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5263 | (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5264 | /* mark array as shutdown cleanly */ |
| 5265 | mddev->in_sync = 1; |
| 5266 | md_update_sb(mddev, 1); |
| 5267 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 5268 | if (mddev_is_clustered(mddev)) |
| 5269 | md_cluster_ops->metadata_update_finish(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5270 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5271 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5272 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5273 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 5274 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5275 | __md_stop_writes(mddev); |
| 5276 | mddev_unlock(mddev); |
| 5277 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5278 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5279 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5280 | static void mddev_detach(struct mddev *mddev) |
| 5281 | { |
| 5282 | struct bitmap *bitmap = mddev->bitmap; |
| 5283 | /* wait for behind writes to complete */ |
| 5284 | if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { |
| 5285 | printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n", |
| 5286 | mdname(mddev)); |
| 5287 | /* need to kick something here to make sure I/O goes? */ |
| 5288 | wait_event(bitmap->behind_wait, |
| 5289 | atomic_read(&bitmap->behind_writes) == 0); |
| 5290 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5291 | if (mddev->pers && mddev->pers->quiesce) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5292 | mddev->pers->quiesce(mddev, 1); |
| 5293 | mddev->pers->quiesce(mddev, 0); |
| 5294 | } |
| 5295 | md_unregister_thread(&mddev->thread); |
| 5296 | if (mddev->queue) |
| 5297 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 5298 | } |
| 5299 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5300 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5301 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5302 | struct md_personality *pers = mddev->pers; |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5303 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5304 | spin_lock(&mddev->lock); |
| 5305 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5306 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5307 | spin_unlock(&mddev->lock); |
| 5308 | pers->free(mddev, mddev->private); |
| 5309 | if (pers->sync_request && mddev->to_remove == NULL) |
| 5310 | mddev->to_remove = &md_redundancy_group; |
| 5311 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5312 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5313 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5314 | |
| 5315 | void md_stop(struct mddev *mddev) |
| 5316 | { |
| 5317 | /* stop the array and free an attached data structures. |
| 5318 | * This is called from dm-raid |
| 5319 | */ |
| 5320 | __md_stop(mddev); |
| 5321 | bitmap_destroy(mddev); |
| 5322 | if (mddev->bio_set) |
| 5323 | bioset_free(mddev->bio_set); |
| 5324 | } |
| 5325 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5326 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5327 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5328 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5329 | { |
| 5330 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5331 | int did_freeze = 0; |
| 5332 | |
| 5333 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5334 | did_freeze = 1; |
| 5335 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5336 | md_wakeup_thread(mddev->thread); |
| 5337 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5338 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5339 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5340 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5341 | /* Thread might be blocked waiting for metadata update |
| 5342 | * which will now never happen */ |
| 5343 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5344 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5345 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5346 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 5347 | &mddev->recovery)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5348 | mddev_lock_nointr(mddev); |
| 5349 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5350 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5351 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5352 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5353 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5354 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5355 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5356 | if (did_freeze) { |
| 5357 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5358 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5359 | md_wakeup_thread(mddev->thread); |
| 5360 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5361 | err = -EBUSY; |
| 5362 | goto out; |
| 5363 | } |
| 5364 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5365 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5366 | |
| 5367 | err = -ENXIO; |
| 5368 | if (mddev->ro==1) |
| 5369 | goto out; |
| 5370 | mddev->ro = 1; |
| 5371 | set_disk_ro(mddev->gendisk, 1); |
| 5372 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5373 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5374 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5375 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5376 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5377 | } |
| 5378 | out: |
| 5379 | mutex_unlock(&mddev->open_mutex); |
| 5380 | return err; |
| 5381 | } |
| 5382 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5383 | /* mode: |
| 5384 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5385 | * 2 - stop but do not disassemble array |
| 5386 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5387 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5388 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5389 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5390 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5391 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5392 | int did_freeze = 0; |
| 5393 | |
| 5394 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5395 | did_freeze = 1; |
| 5396 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5397 | md_wakeup_thread(mddev->thread); |
| 5398 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5399 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5400 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5401 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5402 | /* Thread might be blocked waiting for metadata update |
| 5403 | * which will now never happen */ |
| 5404 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5405 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5406 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5407 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 5408 | !test_bit(MD_RECOVERY_RUNNING, |
| 5409 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5410 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5412 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5413 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5414 | mddev->sysfs_active || |
| 5415 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5416 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5417 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5418 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5419 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5420 | if (did_freeze) { |
| 5421 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5422 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5423 | md_wakeup_thread(mddev->thread); |
| 5424 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 5425 | return -EBUSY; |
| 5426 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5427 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5428 | if (mddev->ro) |
| 5429 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5430 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5431 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5432 | __md_stop(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5433 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5434 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5435 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5436 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5437 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5438 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5439 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5440 | if (rdev->raid_disk >= 0) |
| 5441 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5442 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5443 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5444 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5445 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5446 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5447 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5448 | if (mddev->ro) |
| 5449 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5450 | } else |
| 5451 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | /* |
| 5453 | * Free resources if final stop |
| 5454 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5455 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5457 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5458 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5459 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5460 | struct file *f = mddev->bitmap_info.file; |
| 5461 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5462 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5463 | spin_unlock(&mddev->lock); |
| 5464 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5465 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5466 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5468 | export_array(mddev); |
| 5469 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5470 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5471 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5472 | if (mddev->hold_active == UNTIL_STOP) |
| 5473 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5474 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5475 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5476 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5477 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5478 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | } |
| 5480 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5481 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5482 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5484 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | int err; |
| 5486 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5487 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5489 | |
| 5490 | printk(KERN_INFO "md: running: "); |
| 5491 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5492 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | char b[BDEVNAME_SIZE]; |
| 5494 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5495 | } |
| 5496 | printk("\n"); |
| 5497 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5498 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | if (err) { |
| 5500 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5501 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | } |
| 5503 | } |
| 5504 | |
| 5505 | /* |
| 5506 | * lets try to run arrays based on all disks that have arrived |
| 5507 | * until now. (those are in pending_raid_disks) |
| 5508 | * |
| 5509 | * the method: pick the first pending disk, collect all disks with |
| 5510 | * the same UUID, remove all from the pending list and put them into |
| 5511 | * the 'same_array' list. Then order this list based on superblock |
| 5512 | * update time (freshest comes first), kick out 'old' disks and |
| 5513 | * compare superblocks. If everything's fine then run it. |
| 5514 | * |
| 5515 | * If "unit" is allocated, then bump its reference count |
| 5516 | */ |
| 5517 | static void autorun_devices(int part) |
| 5518 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5519 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5520 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5521 | char b[BDEVNAME_SIZE]; |
| 5522 | |
| 5523 | printk(KERN_INFO "md: autorun ...\n"); |
| 5524 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5525 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5527 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5529 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5530 | |
| 5531 | printk(KERN_INFO "md: considering %s ...\n", |
| 5532 | bdevname(rdev0->bdev,b)); |
| 5533 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5534 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5536 | printk(KERN_INFO "md: adding %s ...\n", |
| 5537 | bdevname(rdev->bdev,b)); |
| 5538 | list_move(&rdev->same_set, &candidates); |
| 5539 | } |
| 5540 | /* |
| 5541 | * now we have a set of devices, with all of them having |
| 5542 | * mostly sane superblocks. It's time to allocate the |
| 5543 | * mddev. |
| 5544 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5545 | if (part) { |
| 5546 | dev = MKDEV(mdp_major, |
| 5547 | rdev0->preferred_minor << MdpMinorShift); |
| 5548 | unit = MINOR(dev) >> MdpMinorShift; |
| 5549 | } else { |
| 5550 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5551 | unit = MINOR(dev); |
| 5552 | } |
| 5553 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5554 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5555 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5556 | break; |
| 5557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5558 | |
| 5559 | md_probe(dev, NULL, NULL); |
| 5560 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5561 | if (!mddev || !mddev->gendisk) { |
| 5562 | if (mddev) |
| 5563 | mddev_put(mddev); |
| 5564 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5565 | "md: cannot allocate memory for md drive.\n"); |
| 5566 | break; |
| 5567 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5568 | if (mddev_lock(mddev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5570 | mdname(mddev)); |
| 5571 | else if (mddev->raid_disks || mddev->major_version |
| 5572 | || !list_empty(&mddev->disks)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5573 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5574 | "md: %s already running, cannot run %s\n", |
| 5575 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5576 | mddev_unlock(mddev); |
| 5577 | } else { |
| 5578 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5579 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5580 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5581 | list_del_init(&rdev->same_set); |
| 5582 | if (bind_rdev_to_array(rdev, mddev)) |
| 5583 | export_rdev(rdev); |
| 5584 | } |
| 5585 | autorun_array(mddev); |
| 5586 | mddev_unlock(mddev); |
| 5587 | } |
| 5588 | /* on success, candidates will be empty, on error |
| 5589 | * it won't... |
| 5590 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5591 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5592 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5593 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5594 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5595 | mddev_put(mddev); |
| 5596 | } |
| 5597 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5598 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5599 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5601 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5602 | { |
| 5603 | mdu_version_t ver; |
| 5604 | |
| 5605 | ver.major = MD_MAJOR_VERSION; |
| 5606 | ver.minor = MD_MINOR_VERSION; |
| 5607 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5608 | |
| 5609 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5610 | return -EFAULT; |
| 5611 | |
| 5612 | return 0; |
| 5613 | } |
| 5614 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5615 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5616 | { |
| 5617 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5618 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5619 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5621 | nr = working = insync = failed = spare = 0; |
| 5622 | rcu_read_lock(); |
| 5623 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5625 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5626 | failed++; |
| 5627 | else { |
| 5628 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5629 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5630 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | else |
| 5632 | spare++; |
| 5633 | } |
| 5634 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5635 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5636 | |
| 5637 | info.major_version = mddev->major_version; |
| 5638 | info.minor_version = mddev->minor_version; |
| 5639 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5640 | info.ctime = mddev->ctime; |
| 5641 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5642 | info.size = mddev->dev_sectors / 2; |
| 5643 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5644 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5645 | info.nr_disks = nr; |
| 5646 | info.raid_disks = mddev->raid_disks; |
| 5647 | info.md_minor = mddev->md_minor; |
| 5648 | info.not_persistent= !mddev->persistent; |
| 5649 | |
| 5650 | info.utime = mddev->utime; |
| 5651 | info.state = 0; |
| 5652 | if (mddev->in_sync) |
| 5653 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5654 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 5655 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 5656 | if (mddev_is_clustered(mddev)) |
| 5657 | info.state |= (1<<MD_SB_CLUSTERED); |
| 5658 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5659 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | info.working_disks = working; |
| 5661 | info.failed_disks = failed; |
| 5662 | info.spare_disks = spare; |
| 5663 | |
| 5664 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5665 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | |
| 5667 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5668 | return -EFAULT; |
| 5669 | |
| 5670 | return 0; |
| 5671 | } |
| 5672 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5673 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5674 | { |
| 5675 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5676 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5677 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5678 | |
NeilBrown | 60559da | 2013-07-16 16:44:29 +1000 | [diff] [blame] | 5679 | file = kmalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5680 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5681 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5682 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5683 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5684 | spin_lock(&mddev->lock); |
| 5685 | /* bitmap disabled, zero the first byte and copy out */ |
| 5686 | if (!mddev->bitmap_info.file) |
| 5687 | file->pathname[0] = '\0'; |
| 5688 | else if ((ptr = d_path(&mddev->bitmap_info.file->f_path, |
| 5689 | file->pathname, sizeof(file->pathname))), |
| 5690 | IS_ERR(ptr)) |
| 5691 | err = PTR_ERR(ptr); |
| 5692 | else |
| 5693 | memmove(file->pathname, ptr, |
| 5694 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 5695 | spin_unlock(&mddev->lock); |
| 5696 | |
| 5697 | if (err == 0 && |
| 5698 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5699 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5700 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5701 | kfree(file); |
| 5702 | return err; |
| 5703 | } |
| 5704 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5705 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | { |
| 5707 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5708 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | |
| 5710 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5711 | return -EFAULT; |
| 5712 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5713 | rcu_read_lock(); |
| 5714 | rdev = find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | if (rdev) { |
| 5716 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5717 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5718 | info.raid_disk = rdev->raid_disk; |
| 5719 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5720 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5721 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5722 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5724 | info.state |= (1<<MD_DISK_SYNC); |
| 5725 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5726 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5727 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | } else { |
| 5729 | info.major = info.minor = 0; |
| 5730 | info.raid_disk = -1; |
| 5731 | info.state = (1<<MD_DISK_REMOVED); |
| 5732 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5733 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5734 | |
| 5735 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5736 | return -EFAULT; |
| 5737 | |
| 5738 | return 0; |
| 5739 | } |
| 5740 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5741 | 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] | 5742 | { |
| 5743 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5744 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5745 | dev_t dev = MKDEV(info->major,info->minor); |
| 5746 | |
| 5747 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5748 | return -EOVERFLOW; |
| 5749 | |
| 5750 | if (!mddev->raid_disks) { |
| 5751 | int err; |
| 5752 | /* expecting a device which has a superblock */ |
| 5753 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5754 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5755 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | "md: md_import_device returned %ld\n", |
| 5757 | PTR_ERR(rdev)); |
| 5758 | return PTR_ERR(rdev); |
| 5759 | } |
| 5760 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5761 | struct md_rdev *rdev0 |
| 5762 | = list_entry(mddev->disks.next, |
| 5763 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5764 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5765 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5766 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5767 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5768 | "md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5769 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5770 | bdevname(rdev0->bdev,b2)); |
| 5771 | export_rdev(rdev); |
| 5772 | return -EINVAL; |
| 5773 | } |
| 5774 | } |
| 5775 | err = bind_rdev_to_array(rdev, mddev); |
| 5776 | if (err) |
| 5777 | export_rdev(rdev); |
| 5778 | return err; |
| 5779 | } |
| 5780 | |
| 5781 | /* |
| 5782 | * add_new_disk can be used once the array is assembled |
| 5783 | * to add "hot spares". They must already have a superblock |
| 5784 | * written |
| 5785 | */ |
| 5786 | if (mddev->pers) { |
| 5787 | int err; |
| 5788 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5789 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5790 | "%s: personality does not support diskops!\n", |
| 5791 | mdname(mddev)); |
| 5792 | return -EINVAL; |
| 5793 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5794 | if (mddev->persistent) |
| 5795 | rdev = md_import_device(dev, mddev->major_version, |
| 5796 | mddev->minor_version); |
| 5797 | else |
| 5798 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5799 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5800 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5801 | "md: md_import_device returned %ld\n", |
| 5802 | PTR_ERR(rdev)); |
| 5803 | return PTR_ERR(rdev); |
| 5804 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5805 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5806 | if (!mddev->persistent) { |
| 5807 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5808 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5809 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5810 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 5811 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5812 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5813 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 5814 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5815 | } else |
| 5816 | super_types[mddev->major_version]. |
| 5817 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5818 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 5819 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5820 | /* This was a hot-add request, but events doesn't |
| 5821 | * match, so reject it. |
| 5822 | */ |
| 5823 | export_rdev(rdev); |
| 5824 | return -EINVAL; |
| 5825 | } |
| 5826 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5827 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5828 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5829 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5830 | else |
| 5831 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | rdev->raid_disk = -1; |
| 5834 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5835 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5836 | /* If there is hot_add_disk but no hot_remove_disk |
| 5837 | * then added disks for geometry changes, |
| 5838 | * and should be added immediately. |
| 5839 | */ |
| 5840 | super_types[mddev->major_version]. |
| 5841 | validate_super(mddev, rdev); |
| 5842 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5843 | if (err) |
| 5844 | unbind_rdev_from_array(rdev); |
| 5845 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5846 | if (err) |
| 5847 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5848 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5849 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5850 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5851 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5852 | if (mddev->degraded) |
| 5853 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5854 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5855 | if (!err) |
| 5856 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5857 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | return err; |
| 5859 | } |
| 5860 | |
| 5861 | /* otherwise, add_new_disk is only allowed |
| 5862 | * for major_version==0 superblocks |
| 5863 | */ |
| 5864 | if (mddev->major_version != 0) { |
| 5865 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5866 | mdname(mddev)); |
| 5867 | return -EINVAL; |
| 5868 | } |
| 5869 | |
| 5870 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5871 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5872 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5874 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5875 | "md: error, md_import_device() returned %ld\n", |
| 5876 | PTR_ERR(rdev)); |
| 5877 | return PTR_ERR(rdev); |
| 5878 | } |
| 5879 | rdev->desc_nr = info->number; |
| 5880 | if (info->raid_disk < mddev->raid_disks) |
| 5881 | rdev->raid_disk = info->raid_disk; |
| 5882 | else |
| 5883 | rdev->raid_disk = -1; |
| 5884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5886 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5887 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5888 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5889 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5890 | set_bit(WriteMostly, &rdev->flags); |
| 5891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5892 | if (!mddev->persistent) { |
| 5893 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5894 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5895 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5896 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5897 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5899 | err = bind_rdev_to_array(rdev, mddev); |
| 5900 | if (err) { |
| 5901 | export_rdev(rdev); |
| 5902 | return err; |
| 5903 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | } |
| 5905 | |
| 5906 | return 0; |
| 5907 | } |
| 5908 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5909 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | { |
| 5911 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5912 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5913 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | rdev = find_rdev(mddev, dev); |
| 5915 | if (!rdev) |
| 5916 | return -ENXIO; |
| 5917 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 5918 | if (mddev_is_clustered(mddev)) |
| 5919 | md_cluster_ops->metadata_update_start(mddev); |
| 5920 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 5921 | clear_bit(Blocked, &rdev->flags); |
| 5922 | remove_and_add_spares(mddev, rdev); |
| 5923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | if (rdev->raid_disk >= 0) |
| 5925 | goto busy; |
| 5926 | |
| 5927 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5928 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5929 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5930 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 5931 | if (mddev_is_clustered(mddev)) |
| 5932 | md_cluster_ops->metadata_update_finish(mddev); |
| 5933 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | return 0; |
| 5935 | busy: |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 5936 | if (mddev_is_clustered(mddev)) |
| 5937 | md_cluster_ops->metadata_update_cancel(mddev); |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5938 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5940 | return -EBUSY; |
| 5941 | } |
| 5942 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5943 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5944 | { |
| 5945 | char b[BDEVNAME_SIZE]; |
| 5946 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5947 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | |
| 5949 | if (!mddev->pers) |
| 5950 | return -ENODEV; |
| 5951 | |
| 5952 | if (mddev->major_version != 0) { |
| 5953 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5954 | " version-0 superblocks.\n", |
| 5955 | mdname(mddev)); |
| 5956 | return -EINVAL; |
| 5957 | } |
| 5958 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5959 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | "%s: personality does not support diskops!\n", |
| 5961 | mdname(mddev)); |
| 5962 | return -EINVAL; |
| 5963 | } |
| 5964 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5965 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5966 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5967 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | "md: error, md_import_device() returned %ld\n", |
| 5969 | PTR_ERR(rdev)); |
| 5970 | return -EINVAL; |
| 5971 | } |
| 5972 | |
| 5973 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5974 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5975 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5976 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5977 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5978 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5979 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5980 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5981 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5982 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5983 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5984 | err = -EINVAL; |
| 5985 | goto abort_export; |
| 5986 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 5987 | |
| 5988 | if (mddev_is_clustered(mddev)) |
| 5989 | md_cluster_ops->metadata_update_start(mddev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5990 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5992 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5993 | err = bind_rdev_to_array(rdev, mddev); |
| 5994 | if (err) |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 5995 | goto abort_clustered; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5996 | |
| 5997 | /* |
| 5998 | * The rest should better be atomic, we can have disk failures |
| 5999 | * noticed in interrupt contexts ... |
| 6000 | */ |
| 6001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6002 | rdev->raid_disk = -1; |
| 6003 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6004 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6005 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6006 | if (mddev_is_clustered(mddev)) |
| 6007 | md_cluster_ops->metadata_update_finish(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6008 | /* |
| 6009 | * Kick recovery, maybe this spare has to be added to the |
| 6010 | * array immediately. |
| 6011 | */ |
| 6012 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6013 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6014 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6015 | return 0; |
| 6016 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6017 | abort_clustered: |
| 6018 | if (mddev_is_clustered(mddev)) |
| 6019 | md_cluster_ops->metadata_update_cancel(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | abort_export: |
| 6021 | export_rdev(rdev); |
| 6022 | return err; |
| 6023 | } |
| 6024 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6025 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6026 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6027 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6028 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6029 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 6030 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6031 | return -EBUSY; |
| 6032 | if (mddev->recovery || mddev->sync_thread) |
| 6033 | return -EBUSY; |
| 6034 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6035 | } |
| 6036 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6037 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6038 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6039 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6040 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6041 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 6042 | return -EEXIST; /* cannot add when bitmap is present */ |
| 6043 | f = fget(fd); |
| 6044 | |
| 6045 | if (f == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6046 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 6047 | mdname(mddev)); |
| 6048 | return -EBADF; |
| 6049 | } |
| 6050 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6051 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6052 | if (!S_ISREG(inode->i_mode)) { |
| 6053 | printk(KERN_ERR "%s: error: bitmap file must be a regular file\n", |
| 6054 | mdname(mddev)); |
| 6055 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6056 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6057 | printk(KERN_ERR "%s: error: bitmap file must open for write\n", |
| 6058 | mdname(mddev)); |
| 6059 | err = -EBADF; |
| 6060 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6061 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 6062 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6063 | err = -EBUSY; |
| 6064 | } |
| 6065 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6066 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6067 | return err; |
| 6068 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6069 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6070 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6071 | } else if (mddev->bitmap == NULL) |
| 6072 | return -ENOENT; /* cannot remove what isn't there */ |
| 6073 | err = 0; |
| 6074 | if (mddev->pers) { |
| 6075 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6076 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6077 | struct bitmap *bitmap; |
| 6078 | |
| 6079 | bitmap = bitmap_create(mddev, -1); |
| 6080 | if (!IS_ERR(bitmap)) { |
| 6081 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6082 | err = bitmap_load(mddev); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6083 | } |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6084 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6085 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6086 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6087 | fd = -1; /* make sure to put the file */ |
| 6088 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6089 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6090 | } |
| 6091 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6092 | struct file *f = mddev->bitmap_info.file; |
| 6093 | if (f) { |
| 6094 | spin_lock(&mddev->lock); |
| 6095 | mddev->bitmap_info.file = NULL; |
| 6096 | spin_unlock(&mddev->lock); |
| 6097 | fput(f); |
| 6098 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6099 | } |
| 6100 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6101 | return err; |
| 6102 | } |
| 6103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | /* |
| 6105 | * set_array_info is used two different ways |
| 6106 | * The original usage is when creating a new array. |
| 6107 | * In this usage, raid_disks is > 0 and it together with |
| 6108 | * level, size, not_persistent,layout,chunksize determine the |
| 6109 | * shape of the array. |
| 6110 | * This will always create an array with a type-0.90.0 superblock. |
| 6111 | * The newer usage is when assembling an array. |
| 6112 | * In this case raid_disks will be 0, and the major_version field is |
| 6113 | * use to determine which style super-blocks are to be found on the devices. |
| 6114 | * The minor and patch _version numbers are also kept incase the |
| 6115 | * super_block handler wishes to interpret them. |
| 6116 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6117 | 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] | 6118 | { |
| 6119 | |
| 6120 | if (info->raid_disks == 0) { |
| 6121 | /* just setting version number for superblock loading */ |
| 6122 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6123 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6124 | super_types[info->major_version].name == NULL) { |
| 6125 | /* maybe try to auto-load a module? */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6126 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6127 | "md: superblock version %d not known\n", |
| 6128 | info->major_version); |
| 6129 | return -EINVAL; |
| 6130 | } |
| 6131 | mddev->major_version = info->major_version; |
| 6132 | mddev->minor_version = info->minor_version; |
| 6133 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6134 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6135 | /* ensure mddev_put doesn't delete this now that there |
| 6136 | * is some minimal configuration. |
| 6137 | */ |
| 6138 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | return 0; |
| 6140 | } |
| 6141 | mddev->major_version = MD_MAJOR_VERSION; |
| 6142 | mddev->minor_version = MD_MINOR_VERSION; |
| 6143 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 6144 | mddev->ctime = get_seconds(); |
| 6145 | |
| 6146 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6147 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6148 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6149 | mddev->raid_disks = info->raid_disks; |
| 6150 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6151 | * openned |
| 6152 | */ |
| 6153 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6154 | mddev->recovery_cp = MaxSector; |
| 6155 | else |
| 6156 | mddev->recovery_cp = 0; |
| 6157 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6158 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6159 | |
| 6160 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6161 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6162 | |
| 6163 | mddev->max_disks = MD_SB_DISKS; |
| 6164 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6165 | if (mddev->persistent) |
| 6166 | mddev->flags = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6167 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6168 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6169 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6170 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6171 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6172 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6173 | mddev->reshape_position = MaxSector; |
| 6174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6175 | /* |
| 6176 | * Generate a 128 bit UUID |
| 6177 | */ |
| 6178 | get_random_bytes(mddev->uuid, 16); |
| 6179 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6180 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6181 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6182 | mddev->new_layout = mddev->layout; |
| 6183 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6184 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6186 | return 0; |
| 6187 | } |
| 6188 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6189 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6190 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6191 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6192 | |
| 6193 | if (mddev->external_size) |
| 6194 | return; |
| 6195 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6196 | mddev->array_sectors = array_sectors; |
| 6197 | } |
| 6198 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6199 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6200 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6201 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6202 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6203 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6204 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6205 | |
| 6206 | if (mddev->pers->resize == NULL) |
| 6207 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6208 | /* The "num_sectors" is the number of sectors of each device that |
| 6209 | * is used. This can only make sense for arrays with redundancy. |
| 6210 | * linear and raid0 always use whatever space is available. We can only |
| 6211 | * consider changing this number if no resync or reconstruction is |
| 6212 | * 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] | 6213 | * 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] | 6214 | * of each device. If num_sectors is zero, we find the largest size |
| 6215 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6216 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6217 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6218 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6219 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6220 | if (mddev->ro) |
| 6221 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6222 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6223 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6224 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6225 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6226 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6227 | num_sectors = avail; |
| 6228 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6229 | return -ENOSPC; |
| 6230 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6231 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6232 | if (!rv) |
| 6233 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6234 | return rv; |
| 6235 | } |
| 6236 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6237 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6238 | { |
| 6239 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6240 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6241 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6242 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6243 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6244 | if (mddev->ro) |
| 6245 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6246 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6247 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6248 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6249 | if (mddev->sync_thread || |
| 6250 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6251 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6252 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6253 | |
| 6254 | rdev_for_each(rdev, mddev) { |
| 6255 | if (mddev->raid_disks < raid_disks && |
| 6256 | rdev->data_offset < rdev->new_data_offset) |
| 6257 | return -EINVAL; |
| 6258 | if (mddev->raid_disks > raid_disks && |
| 6259 | rdev->data_offset > rdev->new_data_offset) |
| 6260 | return -EINVAL; |
| 6261 | } |
| 6262 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6263 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6264 | if (mddev->delta_disks < 0) |
| 6265 | mddev->reshape_backwards = 1; |
| 6266 | else if (mddev->delta_disks > 0) |
| 6267 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6268 | |
| 6269 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6270 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6271 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6272 | mddev->reshape_backwards = 0; |
| 6273 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6274 | return rv; |
| 6275 | } |
| 6276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6277 | /* |
| 6278 | * update_array_info is used to change the configuration of an |
| 6279 | * on-line array. |
| 6280 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6281 | * fields in the info are checked against the array. |
| 6282 | * Any differences that cannot be handled will cause an error. |
| 6283 | * Normally, only one change can be managed at a time. |
| 6284 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6285 | 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] | 6286 | { |
| 6287 | int rv = 0; |
| 6288 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6289 | int state = 0; |
| 6290 | |
| 6291 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6292 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6293 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6294 | |
| 6295 | if (mddev->major_version != info->major_version || |
| 6296 | mddev->minor_version != info->minor_version || |
| 6297 | /* mddev->patch_version != info->patch_version || */ |
| 6298 | mddev->ctime != info->ctime || |
| 6299 | mddev->level != info->level || |
| 6300 | /* mddev->layout != info->layout || */ |
| 6301 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6302 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6303 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6304 | ((state^info->state) & 0xfffffe00) |
| 6305 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6306 | return -EINVAL; |
| 6307 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6308 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6309 | cnt++; |
| 6310 | if (mddev->raid_disks != info->raid_disks) |
| 6311 | cnt++; |
| 6312 | if (mddev->layout != info->layout) |
| 6313 | cnt++; |
| 6314 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6315 | cnt++; |
| 6316 | if (cnt == 0) |
| 6317 | return 0; |
| 6318 | if (cnt > 1) |
| 6319 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6320 | |
| 6321 | if (mddev->layout != info->layout) { |
| 6322 | /* Change layout |
| 6323 | * we don't need to do anything at the md level, the |
| 6324 | * personality will take care of it all. |
| 6325 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6326 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6327 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6328 | else { |
| 6329 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6330 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6331 | if (rv) |
| 6332 | mddev->new_layout = mddev->layout; |
| 6333 | return rv; |
| 6334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6335 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6336 | if (mddev_is_clustered(mddev)) |
| 6337 | md_cluster_ops->metadata_update_start(mddev); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6338 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6339 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6340 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6341 | if (mddev->raid_disks != info->raid_disks) |
| 6342 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6343 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6344 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6345 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 6346 | rv = -EINVAL; |
| 6347 | goto err; |
| 6348 | } |
| 6349 | if (mddev->recovery || mddev->sync_thread) { |
| 6350 | rv = -EBUSY; |
| 6351 | goto err; |
| 6352 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6353 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6354 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6355 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6356 | if (mddev->bitmap) { |
| 6357 | rv = -EEXIST; |
| 6358 | goto err; |
| 6359 | } |
| 6360 | if (mddev->bitmap_info.default_offset == 0) { |
| 6361 | rv = -EINVAL; |
| 6362 | goto err; |
| 6363 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6364 | mddev->bitmap_info.offset = |
| 6365 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6366 | mddev->bitmap_info.space = |
| 6367 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6368 | mddev->pers->quiesce(mddev, 1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6369 | bitmap = bitmap_create(mddev, -1); |
| 6370 | if (!IS_ERR(bitmap)) { |
| 6371 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6372 | rv = bitmap_load(mddev); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6373 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6374 | if (rv) |
| 6375 | bitmap_destroy(mddev); |
| 6376 | mddev->pers->quiesce(mddev, 0); |
| 6377 | } else { |
| 6378 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6379 | if (!mddev->bitmap) { |
| 6380 | rv = -ENOENT; |
| 6381 | goto err; |
| 6382 | } |
| 6383 | if (mddev->bitmap->storage.file) { |
| 6384 | rv = -EINVAL; |
| 6385 | goto err; |
| 6386 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6387 | mddev->pers->quiesce(mddev, 1); |
| 6388 | bitmap_destroy(mddev); |
| 6389 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6390 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6391 | } |
| 6392 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6393 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6394 | if (mddev_is_clustered(mddev)) |
| 6395 | md_cluster_ops->metadata_update_finish(mddev); |
| 6396 | return rv; |
| 6397 | err: |
| 6398 | if (mddev_is_clustered(mddev)) |
| 6399 | md_cluster_ops->metadata_update_cancel(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6400 | return rv; |
| 6401 | } |
| 6402 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6403 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6404 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6405 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6406 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6407 | |
| 6408 | if (mddev->pers == NULL) |
| 6409 | return -ENODEV; |
| 6410 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6411 | rcu_read_lock(); |
| 6412 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6413 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6414 | err = -ENODEV; |
| 6415 | else { |
| 6416 | md_error(mddev, rdev); |
| 6417 | if (!test_bit(Faulty, &rdev->flags)) |
| 6418 | err = -EBUSY; |
| 6419 | } |
| 6420 | rcu_read_unlock(); |
| 6421 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6422 | } |
| 6423 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6424 | /* |
| 6425 | * We have a problem here : there is no easy way to give a CHS |
| 6426 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6427 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6428 | * dosfs just mad... ;-) |
| 6429 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6430 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6431 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6432 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6433 | |
| 6434 | geo->heads = 2; |
| 6435 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6436 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6437 | return 0; |
| 6438 | } |
| 6439 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6440 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 6441 | { |
| 6442 | switch (cmd) { |
| 6443 | case ADD_NEW_DISK: |
| 6444 | case BLKROSET: |
| 6445 | case GET_ARRAY_INFO: |
| 6446 | case GET_BITMAP_FILE: |
| 6447 | case GET_DISK_INFO: |
| 6448 | case HOT_ADD_DISK: |
| 6449 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6450 | case RAID_AUTORUN: |
| 6451 | case RAID_VERSION: |
| 6452 | case RESTART_ARRAY_RW: |
| 6453 | case RUN_ARRAY: |
| 6454 | case SET_ARRAY_INFO: |
| 6455 | case SET_BITMAP_FILE: |
| 6456 | case SET_DISK_FAULTY: |
| 6457 | case STOP_ARRAY: |
| 6458 | case STOP_ARRAY_RO: |
| 6459 | return true; |
| 6460 | default: |
| 6461 | return false; |
| 6462 | } |
| 6463 | } |
| 6464 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6465 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6466 | unsigned int cmd, unsigned long arg) |
| 6467 | { |
| 6468 | int err = 0; |
| 6469 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6470 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6471 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6473 | if (!md_ioctl_valid(cmd)) |
| 6474 | return -ENOTTY; |
| 6475 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6476 | switch (cmd) { |
| 6477 | case RAID_VERSION: |
| 6478 | case GET_ARRAY_INFO: |
| 6479 | case GET_DISK_INFO: |
| 6480 | break; |
| 6481 | default: |
| 6482 | if (!capable(CAP_SYS_ADMIN)) |
| 6483 | return -EACCES; |
| 6484 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6485 | |
| 6486 | /* |
| 6487 | * Commands dealing with the RAID driver but not any |
| 6488 | * particular array: |
| 6489 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6490 | switch (cmd) { |
| 6491 | case RAID_VERSION: |
| 6492 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6493 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6496 | case RAID_AUTORUN: |
| 6497 | err = 0; |
| 6498 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6499 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6501 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6502 | } |
| 6503 | |
| 6504 | /* |
| 6505 | * Commands creating/starting a new array: |
| 6506 | */ |
| 6507 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6508 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6509 | |
| 6510 | if (!mddev) { |
| 6511 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6512 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6513 | } |
| 6514 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6515 | /* Some actions do not requires the mutex */ |
| 6516 | switch (cmd) { |
| 6517 | case GET_ARRAY_INFO: |
| 6518 | if (!mddev->raid_disks && !mddev->external) |
| 6519 | err = -ENODEV; |
| 6520 | else |
| 6521 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6522 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6523 | |
| 6524 | case GET_DISK_INFO: |
| 6525 | if (!mddev->raid_disks && !mddev->external) |
| 6526 | err = -ENODEV; |
| 6527 | else |
| 6528 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6529 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6530 | |
| 6531 | case SET_DISK_FAULTY: |
| 6532 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6533 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6534 | |
| 6535 | case GET_BITMAP_FILE: |
| 6536 | err = get_bitmap_file(mddev, argp); |
| 6537 | goto out; |
| 6538 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6539 | } |
| 6540 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 6541 | if (cmd == ADD_NEW_DISK) |
| 6542 | /* need to ensure md_delayed_delete() has completed */ |
| 6543 | flush_workqueue(md_misc_wq); |
| 6544 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 6545 | if (cmd == HOT_REMOVE_DISK) |
| 6546 | /* need to ensure recovery thread has run */ |
| 6547 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 6548 | !test_bit(MD_RECOVERY_NEEDED, |
| 6549 | &mddev->flags), |
| 6550 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6551 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 6552 | /* Need to flush page cache, and ensure no-one else opens |
| 6553 | * and writes |
| 6554 | */ |
| 6555 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6556 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6557 | mutex_unlock(&mddev->open_mutex); |
| 6558 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6559 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6560 | } |
| 6561 | set_bit(MD_STILL_CLOSED, &mddev->flags); |
| 6562 | mutex_unlock(&mddev->open_mutex); |
| 6563 | sync_blockdev(bdev); |
| 6564 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6565 | err = mddev_lock(mddev); |
| 6566 | if (err) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6567 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6568 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6569 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6570 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6571 | } |
| 6572 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6573 | if (cmd == SET_ARRAY_INFO) { |
| 6574 | mdu_array_info_t info; |
| 6575 | if (!arg) |
| 6576 | memset(&info, 0, sizeof(info)); |
| 6577 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6578 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6579 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6580 | } |
| 6581 | if (mddev->pers) { |
| 6582 | err = update_array_info(mddev, &info); |
| 6583 | if (err) { |
| 6584 | printk(KERN_WARNING "md: couldn't update" |
| 6585 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6586 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6587 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6588 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6589 | } |
| 6590 | if (!list_empty(&mddev->disks)) { |
| 6591 | printk(KERN_WARNING |
| 6592 | "md: array %s already has disks!\n", |
| 6593 | mdname(mddev)); |
| 6594 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6595 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6596 | } |
| 6597 | if (mddev->raid_disks) { |
| 6598 | printk(KERN_WARNING |
| 6599 | "md: array %s already initialised!\n", |
| 6600 | mdname(mddev)); |
| 6601 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6602 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6603 | } |
| 6604 | err = set_array_info(mddev, &info); |
| 6605 | if (err) { |
| 6606 | printk(KERN_WARNING "md: couldn't set" |
| 6607 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6608 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6609 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6610 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6611 | } |
| 6612 | |
| 6613 | /* |
| 6614 | * Commands querying/configuring an existing array: |
| 6615 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6616 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6617 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6618 | if ((!mddev->raid_disks && !mddev->external) |
| 6619 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6620 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6621 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6622 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6623 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6624 | } |
| 6625 | |
| 6626 | /* |
| 6627 | * Commands even a read-only array can execute: |
| 6628 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6629 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6630 | case RESTART_ARRAY_RW: |
| 6631 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6632 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6633 | |
| 6634 | case STOP_ARRAY: |
| 6635 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6636 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6637 | |
| 6638 | case STOP_ARRAY_RO: |
| 6639 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6640 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6641 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6642 | case HOT_REMOVE_DISK: |
| 6643 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6644 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6645 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6646 | case ADD_NEW_DISK: |
| 6647 | /* We can support ADD_NEW_DISK on read-only arrays |
| 6648 | * on if we are re-adding a preexisting device. |
| 6649 | * So require mddev->pers and MD_DISK_SYNC. |
| 6650 | */ |
| 6651 | if (mddev->pers) { |
| 6652 | mdu_disk_info_t info; |
| 6653 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6654 | err = -EFAULT; |
| 6655 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 6656 | /* Need to clear read-only for this */ |
| 6657 | break; |
| 6658 | else |
| 6659 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6660 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6661 | } |
| 6662 | break; |
| 6663 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6664 | case BLKROSET: |
| 6665 | if (get_user(ro, (int __user *)(arg))) { |
| 6666 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6667 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6668 | } |
| 6669 | err = -EINVAL; |
| 6670 | |
| 6671 | /* if the bdev is going readonly the value of mddev->ro |
| 6672 | * does not matter, no writes are coming |
| 6673 | */ |
| 6674 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6675 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6676 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6677 | /* are we are already prepared for writes? */ |
| 6678 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6679 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6680 | |
| 6681 | /* transitioning to readauto need only happen for |
| 6682 | * arrays that call md_write_start |
| 6683 | */ |
| 6684 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6685 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6686 | if (err == 0) { |
| 6687 | mddev->ro = 2; |
| 6688 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6689 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6690 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6691 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6692 | } |
| 6693 | |
| 6694 | /* |
| 6695 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6696 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6697 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 6698 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6699 | if (mddev->ro == 2) { |
| 6700 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6701 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6702 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6703 | /* mddev_unlock will wake thread */ |
| 6704 | /* If a device failed while we were read-only, we |
| 6705 | * need to make sure the metadata is updated now. |
| 6706 | */ |
| 6707 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
| 6708 | mddev_unlock(mddev); |
| 6709 | wait_event(mddev->sb_wait, |
| 6710 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && |
| 6711 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6712 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6713 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6714 | } else { |
| 6715 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6716 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6718 | } |
| 6719 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6720 | switch (cmd) { |
| 6721 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6722 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6723 | mdu_disk_info_t info; |
| 6724 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6725 | err = -EFAULT; |
| 6726 | else |
| 6727 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6728 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6729 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6730 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6731 | case HOT_ADD_DISK: |
| 6732 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6733 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6734 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6735 | case RUN_ARRAY: |
| 6736 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6737 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6738 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6739 | case SET_BITMAP_FILE: |
| 6740 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6741 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6742 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6743 | default: |
| 6744 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6745 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6746 | } |
| 6747 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6748 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6749 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6750 | err != -EINVAL) |
| 6751 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6753 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6754 | return err; |
| 6755 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6756 | #ifdef CONFIG_COMPAT |
| 6757 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6758 | unsigned int cmd, unsigned long arg) |
| 6759 | { |
| 6760 | switch (cmd) { |
| 6761 | case HOT_REMOVE_DISK: |
| 6762 | case HOT_ADD_DISK: |
| 6763 | case SET_DISK_FAULTY: |
| 6764 | case SET_BITMAP_FILE: |
| 6765 | /* These take in integer arg, do not convert */ |
| 6766 | break; |
| 6767 | default: |
| 6768 | arg = (unsigned long)compat_ptr(arg); |
| 6769 | break; |
| 6770 | } |
| 6771 | |
| 6772 | return md_ioctl(bdev, mode, cmd, arg); |
| 6773 | } |
| 6774 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6775 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6776 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6777 | { |
| 6778 | /* |
| 6779 | * Succeed if we can lock the mddev, which confirms that |
| 6780 | * it isn't being stopped right now. |
| 6781 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6782 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6783 | int err; |
| 6784 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 6785 | if (!mddev) |
| 6786 | return -ENODEV; |
| 6787 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6788 | if (mddev->gendisk != bdev->bd_disk) { |
| 6789 | /* we are racing with mddev_put which is discarding this |
| 6790 | * bd_disk. |
| 6791 | */ |
| 6792 | mddev_put(mddev); |
| 6793 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6794 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6795 | /* Then retry the open from the top */ |
| 6796 | return -ERESTARTSYS; |
| 6797 | } |
| 6798 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6799 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6800 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6801 | goto out; |
| 6802 | |
| 6803 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6804 | atomic_inc(&mddev->openers); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6805 | clear_bit(MD_STILL_CLOSED, &mddev->flags); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6806 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6807 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6808 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6809 | out: |
| 6810 | return err; |
| 6811 | } |
| 6812 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 6813 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6814 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6815 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6816 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6817 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6818 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6819 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6820 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6821 | |
| 6822 | static int md_media_changed(struct gendisk *disk) |
| 6823 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6824 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6825 | |
| 6826 | return mddev->changed; |
| 6827 | } |
| 6828 | |
| 6829 | static int md_revalidate(struct gendisk *disk) |
| 6830 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6831 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6832 | |
| 6833 | mddev->changed = 0; |
| 6834 | return 0; |
| 6835 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6836 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6837 | { |
| 6838 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6839 | .open = md_open, |
| 6840 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6841 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6842 | #ifdef CONFIG_COMPAT |
| 6843 | .compat_ioctl = md_compat_ioctl, |
| 6844 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6845 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6846 | .media_changed = md_media_changed, |
| 6847 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6848 | }; |
| 6849 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6850 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6851 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6852 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6854 | /* |
| 6855 | * md_thread is a 'system-thread', it's priority should be very |
| 6856 | * high. We avoid resource deadlocks individually in each |
| 6857 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6858 | * the very same RT priority as kswapd, thus we will never get |
| 6859 | * into a priority inversion deadlock. |
| 6860 | * |
| 6861 | * we definitely have to have equal or higher priority than |
| 6862 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6863 | * many dirty RAID5 blocks. |
| 6864 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6865 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6866 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6867 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6868 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6869 | /* We need to wait INTERRUPTIBLE so that |
| 6870 | * we don't add to the load-average. |
| 6871 | * That means we need to be sure no signals are |
| 6872 | * pending |
| 6873 | */ |
| 6874 | if (signal_pending(current)) |
| 6875 | flush_signals(current); |
| 6876 | |
| 6877 | wait_event_interruptible_timeout |
| 6878 | (thread->wqueue, |
| 6879 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6880 | || kthread_should_stop(), |
| 6881 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6882 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6883 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6884 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6885 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6886 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6888 | return 0; |
| 6889 | } |
| 6890 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6891 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | { |
| 6893 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6894 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6895 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6896 | wake_up(&thread->wqueue); |
| 6897 | } |
| 6898 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 6899 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6900 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6901 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 6902 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6903 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6904 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6905 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6906 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6907 | if (!thread) |
| 6908 | return NULL; |
| 6909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6910 | init_waitqueue_head(&thread->wqueue); |
| 6911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6912 | thread->run = run; |
| 6913 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6914 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6915 | thread->tsk = kthread_run(md_thread, thread, |
| 6916 | "%s_%s", |
| 6917 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 6918 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6919 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6920 | kfree(thread); |
| 6921 | return NULL; |
| 6922 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6923 | return thread; |
| 6924 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 6925 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6926 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6927 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6928 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6929 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6930 | if (!thread) |
| 6931 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6932 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6933 | /* Locking ensures that mddev_unlock does not wake_up a |
| 6934 | * non-existent thread |
| 6935 | */ |
| 6936 | spin_lock(&pers_lock); |
| 6937 | *threadp = NULL; |
| 6938 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6939 | |
| 6940 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6941 | kfree(thread); |
| 6942 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 6943 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6944 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6945 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6946 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6947 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6948 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6949 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6950 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6951 | return; |
| 6952 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6953 | if (mddev->degraded) |
| 6954 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6955 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6956 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6957 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6958 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6959 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6960 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6961 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6962 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 6963 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6964 | |
| 6965 | /* seq_file implementation /proc/mdstat */ |
| 6966 | |
| 6967 | static void status_unused(struct seq_file *seq) |
| 6968 | { |
| 6969 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6970 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6971 | |
| 6972 | seq_printf(seq, "unused devices: "); |
| 6973 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6974 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6975 | char b[BDEVNAME_SIZE]; |
| 6976 | i++; |
| 6977 | seq_printf(seq, "%s ", |
| 6978 | bdevname(rdev->bdev,b)); |
| 6979 | } |
| 6980 | if (!i) |
| 6981 | seq_printf(seq, "<none>"); |
| 6982 | |
| 6983 | seq_printf(seq, "\n"); |
| 6984 | } |
| 6985 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6986 | static void status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6987 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6988 | sector_t max_sectors, resync, res; |
| 6989 | unsigned long dt, db; |
| 6990 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6991 | int scale; |
| 6992 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6993 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 6994 | if (mddev->curr_resync <= 3) |
| 6995 | resync = 0; |
| 6996 | else |
| 6997 | resync = mddev->curr_resync |
| 6998 | - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6999 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7000 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 7001 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7002 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7003 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7004 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7005 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 7006 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7007 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7008 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7009 | * u32, as those are the requirements for sector_div. |
| 7010 | * Thus 'scale' must be at least 10 |
| 7011 | */ |
| 7012 | scale = 10; |
| 7013 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7014 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7015 | scale++; |
| 7016 | } |
| 7017 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7018 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7019 | |
| 7020 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7021 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7022 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7023 | seq_printf(seq, "["); |
| 7024 | for (i = 0; i < x; i++) |
| 7025 | seq_printf(seq, "="); |
| 7026 | seq_printf(seq, ">"); |
| 7027 | for (i = 0; i < y; i++) |
| 7028 | seq_printf(seq, "."); |
| 7029 | seq_printf(seq, "] "); |
| 7030 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7031 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7032 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 7033 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7034 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 7035 | "check" : |
| 7036 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 7037 | "resync" : "recovery"))), |
| 7038 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7039 | (unsigned long long) resync/2, |
| 7040 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7041 | |
| 7042 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7043 | * dt: time from mark until now |
| 7044 | * db: blocks written from mark until now |
| 7045 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7046 | * |
| 7047 | * rt is a sector_t, so could be 32bit or 64bit. |
| 7048 | * So we divide before multiply in case it is 32bit and close |
| 7049 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 7050 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7051 | * near the end of resync when the number of remaining sectors |
| 7052 | * is close to 'db'. |
| 7053 | * We then divide rt by 32 after multiplying by db to compensate. |
| 7054 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7055 | */ |
| 7056 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 7057 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7058 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 7059 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7060 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7061 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 7062 | sector_div(rt, db/32+1); |
| 7063 | rt *= dt; |
| 7064 | rt >>= 5; |
| 7065 | |
| 7066 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 7067 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7068 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7069 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7070 | } |
| 7071 | |
| 7072 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 7073 | { |
| 7074 | struct list_head *tmp; |
| 7075 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7076 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7077 | |
| 7078 | if (l >= 0x10000) |
| 7079 | return NULL; |
| 7080 | if (!l--) |
| 7081 | /* header */ |
| 7082 | return (void*)1; |
| 7083 | |
| 7084 | spin_lock(&all_mddevs_lock); |
| 7085 | list_for_each(tmp,&all_mddevs) |
| 7086 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7087 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7088 | mddev_get(mddev); |
| 7089 | spin_unlock(&all_mddevs_lock); |
| 7090 | return mddev; |
| 7091 | } |
| 7092 | spin_unlock(&all_mddevs_lock); |
| 7093 | if (!l--) |
| 7094 | return (void*)2;/* tail */ |
| 7095 | return NULL; |
| 7096 | } |
| 7097 | |
| 7098 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 7099 | { |
| 7100 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7101 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7103 | ++*pos; |
| 7104 | if (v == (void*)2) |
| 7105 | return NULL; |
| 7106 | |
| 7107 | spin_lock(&all_mddevs_lock); |
| 7108 | if (v == (void*)1) |
| 7109 | tmp = all_mddevs.next; |
| 7110 | else |
| 7111 | tmp = mddev->all_mddevs.next; |
| 7112 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7113 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7114 | else { |
| 7115 | next_mddev = (void*)2; |
| 7116 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7117 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7118 | spin_unlock(&all_mddevs_lock); |
| 7119 | |
| 7120 | if (v != (void*)1) |
| 7121 | mddev_put(mddev); |
| 7122 | return next_mddev; |
| 7123 | |
| 7124 | } |
| 7125 | |
| 7126 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 7127 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7128 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7129 | |
| 7130 | if (mddev && v != (void*)1 && v != (void*)2) |
| 7131 | mddev_put(mddev); |
| 7132 | } |
| 7133 | |
| 7134 | static int md_seq_show(struct seq_file *seq, void *v) |
| 7135 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7136 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7137 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7138 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7139 | |
| 7140 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7141 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7142 | seq_printf(seq, "Personalities : "); |
| 7143 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7144 | list_for_each_entry(pers, &pers_list, list) |
| 7145 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7146 | |
| 7147 | spin_unlock(&pers_lock); |
| 7148 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7149 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7150 | return 0; |
| 7151 | } |
| 7152 | if (v == (void*)2) { |
| 7153 | status_unused(seq); |
| 7154 | return 0; |
| 7155 | } |
| 7156 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7157 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7158 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7159 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7160 | mddev->pers ? "" : "in"); |
| 7161 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7162 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7163 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7164 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7165 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7166 | seq_printf(seq, " %s", mddev->pers->name); |
| 7167 | } |
| 7168 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7169 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7170 | rcu_read_lock(); |
| 7171 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7172 | char b[BDEVNAME_SIZE]; |
| 7173 | seq_printf(seq, " %s[%d]", |
| 7174 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7175 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7176 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7177 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7178 | seq_printf(seq, "(F)"); |
| 7179 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7180 | } |
| 7181 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7182 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7183 | if (test_bit(Replacement, &rdev->flags)) |
| 7184 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7185 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7186 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7187 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7188 | |
| 7189 | if (!list_empty(&mddev->disks)) { |
| 7190 | if (mddev->pers) |
| 7191 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7192 | (unsigned long long) |
| 7193 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7194 | else |
| 7195 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7196 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7197 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7198 | if (mddev->persistent) { |
| 7199 | if (mddev->major_version != 0 || |
| 7200 | mddev->minor_version != 90) { |
| 7201 | seq_printf(seq," super %d.%d", |
| 7202 | mddev->major_version, |
| 7203 | mddev->minor_version); |
| 7204 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7205 | } else if (mddev->external) |
| 7206 | seq_printf(seq, " super external:%s", |
| 7207 | mddev->metadata_type); |
| 7208 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7209 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7210 | |
| 7211 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7212 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7213 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7214 | if (mddev->pers->sync_request) { |
| 7215 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7216 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7217 | seq_printf(seq, "\n "); |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7218 | } else if (mddev->curr_resync >= 1) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7219 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 7220 | else if (mddev->recovery_cp < MaxSector) |
| 7221 | seq_printf(seq, "\tresync=PENDING\n "); |
| 7222 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7223 | } else |
| 7224 | seq_printf(seq, "\n "); |
| 7225 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7226 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7227 | |
| 7228 | seq_printf(seq, "\n"); |
| 7229 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7230 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7232 | return 0; |
| 7233 | } |
| 7234 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7235 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7236 | .start = md_seq_start, |
| 7237 | .next = md_seq_next, |
| 7238 | .stop = md_seq_stop, |
| 7239 | .show = md_seq_show, |
| 7240 | }; |
| 7241 | |
| 7242 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7243 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7244 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7245 | int error; |
| 7246 | |
| 7247 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7248 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7249 | return error; |
| 7250 | |
| 7251 | seq = file->private_data; |
| 7252 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7253 | return error; |
| 7254 | } |
| 7255 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7256 | static int md_unloading; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7257 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7258 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7259 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7260 | int mask; |
| 7261 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7262 | if (md_unloading) |
kbuild test robot | 7d7e64f | 2014-12-03 16:07:59 +1100 | [diff] [blame] | 7263 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7264 | poll_wait(filp, &md_event_waiters, wait); |
| 7265 | |
| 7266 | /* always allow read */ |
| 7267 | mask = POLLIN | POLLRDNORM; |
| 7268 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7269 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7270 | mask |= POLLERR | POLLPRI; |
| 7271 | return mask; |
| 7272 | } |
| 7273 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7274 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7275 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7276 | .open = md_seq_open, |
| 7277 | .read = seq_read, |
| 7278 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7279 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7280 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7281 | }; |
| 7282 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7283 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7284 | { |
Chao Yu | 50bd377 | 2014-09-25 15:28:34 +0800 | [diff] [blame] | 7285 | printk(KERN_INFO "md: %s personality registered for level %d\n", |
| 7286 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7287 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7288 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7289 | spin_unlock(&pers_lock); |
| 7290 | return 0; |
| 7291 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7292 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7293 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7294 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7295 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7296 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7297 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7298 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7299 | spin_unlock(&pers_lock); |
| 7300 | return 0; |
| 7301 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7302 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7303 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7304 | int register_md_cluster_operations(struct md_cluster_operations *ops, struct module *module) |
| 7305 | { |
| 7306 | if (md_cluster_ops != NULL) |
| 7307 | return -EALREADY; |
| 7308 | spin_lock(&pers_lock); |
| 7309 | md_cluster_ops = ops; |
| 7310 | md_cluster_mod = module; |
| 7311 | spin_unlock(&pers_lock); |
| 7312 | return 0; |
| 7313 | } |
| 7314 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 7315 | |
| 7316 | int unregister_md_cluster_operations(void) |
| 7317 | { |
| 7318 | spin_lock(&pers_lock); |
| 7319 | md_cluster_ops = NULL; |
| 7320 | spin_unlock(&pers_lock); |
| 7321 | return 0; |
| 7322 | } |
| 7323 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 7324 | |
| 7325 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 7326 | { |
| 7327 | int err; |
| 7328 | |
| 7329 | err = request_module("md-cluster"); |
| 7330 | if (err) { |
| 7331 | pr_err("md-cluster module not found.\n"); |
| 7332 | return err; |
| 7333 | } |
| 7334 | |
| 7335 | spin_lock(&pers_lock); |
| 7336 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
| 7337 | spin_unlock(&pers_lock); |
| 7338 | return -ENOENT; |
| 7339 | } |
| 7340 | spin_unlock(&pers_lock); |
| 7341 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 7342 | return md_cluster_ops->join(mddev, nodes); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7343 | } |
| 7344 | |
| 7345 | void md_cluster_stop(struct mddev *mddev) |
| 7346 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 7347 | if (!md_cluster_ops) |
| 7348 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7349 | md_cluster_ops->leave(mddev); |
| 7350 | module_put(md_cluster_mod); |
| 7351 | } |
| 7352 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7353 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7354 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7355 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7356 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7357 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7358 | |
| 7359 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7360 | rcu_read_lock(); |
| 7361 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7362 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7363 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7364 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7365 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7366 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7367 | * as sync_io is counted when a request starts, and |
| 7368 | * disk_stats is counted when it completes. |
| 7369 | * So resync activity will cause curr_events to be smaller than |
| 7370 | * when there was no such activity. |
| 7371 | * non-sync IO will cause disk_stat to increase without |
| 7372 | * increasing sync_io so curr_events will (eventually) |
| 7373 | * be larger than it was before. Once it becomes |
| 7374 | * substantially larger, the test below will cause |
| 7375 | * the array to appear non-idle, and resync will slow |
| 7376 | * down. |
| 7377 | * If there is a lot of outstanding resync activity when |
| 7378 | * we set last_event to curr_events, then all that activity |
| 7379 | * completing might cause the array to appear non-idle |
| 7380 | * and resync will be slowed down even though there might |
| 7381 | * not have been non-resync activity. This will only |
| 7382 | * happen once though. 'last_events' will soon reflect |
| 7383 | * the state where there is little or no outstanding |
| 7384 | * resync requests, and further resync activity will |
| 7385 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7386 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7387 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7388 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7389 | rdev->last_events = curr_events; |
| 7390 | idle = 0; |
| 7391 | } |
| 7392 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7393 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7394 | return idle; |
| 7395 | } |
| 7396 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7397 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7398 | { |
| 7399 | /* another "blocks" (512byte) blocks have been synced */ |
| 7400 | atomic_sub(blocks, &mddev->recovery_active); |
| 7401 | wake_up(&mddev->recovery_wait); |
| 7402 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7403 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7404 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7405 | md_wakeup_thread(mddev->thread); |
| 7406 | // stop recovery, signal do_sync .... |
| 7407 | } |
| 7408 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7409 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7410 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7411 | /* md_write_start(mddev, bi) |
| 7412 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7413 | * in superblock) before writing, schedule a superblock update |
| 7414 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7415 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7416 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7417 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7418 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7419 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7420 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7421 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7422 | BUG_ON(mddev->ro == 1); |
| 7423 | if (mddev->ro == 2) { |
| 7424 | /* need to switch to read/write */ |
| 7425 | mddev->ro = 0; |
| 7426 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7427 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7428 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7429 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7430 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7431 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7432 | if (mddev->safemode == 1) |
| 7433 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7434 | if (mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7435 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7436 | if (mddev->in_sync) { |
| 7437 | mddev->in_sync = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7438 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7439 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7440 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7441 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7442 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7443 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7444 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7445 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7446 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7447 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7448 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7449 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7450 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7451 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7452 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7453 | { |
| 7454 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7455 | if (mddev->safemode == 2) |
| 7456 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7457 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7458 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7459 | } |
| 7460 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7461 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7462 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7463 | /* md_allow_write(mddev) |
| 7464 | * Calling this ensures that the array is marked 'active' so that writes |
| 7465 | * may proceed without blocking. It is important to call this before |
| 7466 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7467 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7468 | * |
| 7469 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7470 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7471 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7472 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7473 | { |
| 7474 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7475 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7476 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7477 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7478 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7479 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7480 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7481 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7482 | if (mddev->in_sync) { |
| 7483 | mddev->in_sync = 0; |
| 7484 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7485 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7486 | if (mddev->safemode_delay && |
| 7487 | mddev->safemode == 0) |
| 7488 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7489 | spin_unlock(&mddev->lock); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7490 | if (mddev_is_clustered(mddev)) |
| 7491 | md_cluster_ops->metadata_update_start(mddev); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7492 | md_update_sb(mddev, 0); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7493 | if (mddev_is_clustered(mddev)) |
| 7494 | md_cluster_ops->metadata_update_finish(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7495 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7496 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7497 | spin_unlock(&mddev->lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7498 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7499 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7500 | return -EAGAIN; |
| 7501 | else |
| 7502 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7503 | } |
| 7504 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7506 | #define SYNC_MARKS 10 |
| 7507 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7508 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7509 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7510 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7511 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7512 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7513 | unsigned int currspeed = 0, |
| 7514 | window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7515 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7516 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7517 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7518 | sector_t mark_cnt[SYNC_MARKS]; |
| 7519 | int last_mark,m; |
| 7520 | struct list_head *tmp; |
| 7521 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7522 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7523 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7524 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7525 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7526 | |
| 7527 | /* just incase thread restarts... */ |
| 7528 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7529 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7530 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 7531 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7532 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7533 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7534 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7535 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7536 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7537 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7538 | action = "check"; |
| 7539 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7540 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7541 | action = "repair"; |
| 7542 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7543 | desc = "resync"; |
| 7544 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7545 | desc = "reshape"; |
| 7546 | else |
| 7547 | desc = "recovery"; |
| 7548 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7549 | mddev->last_sync_action = action ?: desc; |
| 7550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7551 | /* we overload curr_resync somewhat here. |
| 7552 | * 0 == not engaged in resync at all |
| 7553 | * 2 == checking that there is no conflict with another sync |
| 7554 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7555 | * commense |
| 7556 | * other == active in resync - this many blocks |
| 7557 | * |
| 7558 | * Before starting a resync we must have set curr_resync to |
| 7559 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7560 | * less than ours. When we find one that is the same or higher |
| 7561 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7562 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7563 | * This will mean we have to start checking from the beginning again. |
| 7564 | * |
| 7565 | */ |
| 7566 | |
| 7567 | do { |
| 7568 | mddev->curr_resync = 2; |
| 7569 | |
| 7570 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7571 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7572 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7573 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7574 | if (mddev2 == mddev) |
| 7575 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7576 | if (!mddev->parallel_resync |
| 7577 | && mddev2->curr_resync |
| 7578 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7579 | DEFINE_WAIT(wq); |
| 7580 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7581 | /* arbitrarily yield */ |
| 7582 | mddev->curr_resync = 1; |
| 7583 | wake_up(&resync_wait); |
| 7584 | } |
| 7585 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7586 | /* no need to wait here, we can wait the next |
| 7587 | * time 'round when curr_resync == 2 |
| 7588 | */ |
| 7589 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7590 | /* We need to wait 'interruptible' so as not to |
| 7591 | * contribute to the load average, and not to |
| 7592 | * be caught by 'softlockup' |
| 7593 | */ |
| 7594 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7595 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7596 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7597 | printk(KERN_INFO "md: delaying %s of %s" |
| 7598 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7599 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7600 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7601 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7602 | if (signal_pending(current)) |
| 7603 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7604 | schedule(); |
| 7605 | finish_wait(&resync_wait, &wq); |
| 7606 | goto try_again; |
| 7607 | } |
| 7608 | finish_wait(&resync_wait, &wq); |
| 7609 | } |
| 7610 | } |
| 7611 | } while (mddev->curr_resync < 2); |
| 7612 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7613 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7614 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7615 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7616 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7617 | */ |
| 7618 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 7619 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7620 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7621 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7622 | j = mddev->resync_min; |
| 7623 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7624 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7625 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7626 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7627 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7628 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7629 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7630 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7631 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7632 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7633 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7634 | if (rdev->raid_disk >= 0 && |
| 7635 | !test_bit(Faulty, &rdev->flags) && |
| 7636 | !test_bit(In_sync, &rdev->flags) && |
| 7637 | rdev->recovery_offset < j) |
| 7638 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7639 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 7640 | |
| 7641 | /* If there is a bitmap, we need to make sure all |
| 7642 | * writes that started before we added a spare |
| 7643 | * complete before we start doing a recovery. |
| 7644 | * Otherwise the write might complete and (via |
| 7645 | * bitmap_endwrite) set a bit in the bitmap after the |
| 7646 | * recovery has checked that bit and skipped that |
| 7647 | * region. |
| 7648 | */ |
| 7649 | if (mddev->bitmap) { |
| 7650 | mddev->pers->quiesce(mddev, 1); |
| 7651 | mddev->pers->quiesce(mddev, 0); |
| 7652 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7653 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7654 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7655 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7656 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7657 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7658 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7659 | "(but not more than %d KB/sec) for %s.\n", |
| 7660 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7661 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7662 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7663 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7664 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7665 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7666 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7667 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7668 | } |
| 7669 | last_mark = 0; |
| 7670 | mddev->resync_mark = mark[last_mark]; |
| 7671 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7672 | |
| 7673 | /* |
| 7674 | * Tune reconstruction: |
| 7675 | */ |
| 7676 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450c | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7677 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7678 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7679 | |
| 7680 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7681 | last_check = 0; |
| 7682 | |
| 7683 | if (j>2) { |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7684 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7685 | "md: resuming %s of %s from checkpoint.\n", |
| 7686 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7687 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7688 | } else |
| 7689 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7690 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7691 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 7692 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7693 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7694 | |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 7695 | if (mddev_is_clustered(mddev)) |
Goldwyn Rodrigues | 965400e | 2014-06-07 02:16:58 -0500 | [diff] [blame] | 7696 | md_cluster_ops->resync_start(mddev, j, max_sectors); |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 7697 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7698 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7699 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7700 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7701 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7702 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7703 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7704 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7705 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7706 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7707 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7708 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7709 | (j - mddev->curr_resync_completed)*2 |
| 7710 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7711 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7712 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7713 | wait_event(mddev->recovery_wait, |
| 7714 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7715 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7716 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 7717 | j > mddev->recovery_cp) |
| 7718 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7719 | update_time = jiffies; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7720 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7721 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7722 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7723 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7724 | while (j >= mddev->resync_max && |
| 7725 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7726 | /* As this condition is controlled by user-space, |
| 7727 | * we can block indefinitely, so use '_interruptible' |
| 7728 | * to avoid triggering warnings. |
| 7729 | */ |
| 7730 | flush_signals(current); /* just in case */ |
| 7731 | wait_event_interruptible(mddev->recovery_wait, |
| 7732 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7733 | || test_bit(MD_RECOVERY_INTR, |
| 7734 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7735 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7736 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7737 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7738 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7739 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7740 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7741 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7742 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7743 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7744 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7745 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7746 | |
| 7747 | if (!skipped) { /* actual IO requested */ |
| 7748 | io_sectors += sectors; |
| 7749 | atomic_add(sectors, &mddev->recovery_active); |
| 7750 | } |
| 7751 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7752 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7753 | break; |
| 7754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7755 | j += sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7756 | if (j > 2) |
| 7757 | mddev->curr_resync = j; |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 7758 | if (mddev_is_clustered(mddev)) |
| 7759 | md_cluster_ops->resync_info_update(mddev, j, max_sectors); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7760 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7761 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7762 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7763 | * visible in /proc/mdstat |
| 7764 | */ |
| 7765 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7766 | |
| 7767 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7768 | continue; |
| 7769 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7770 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7771 | repeat: |
| 7772 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7773 | /* step marks */ |
| 7774 | int next = (last_mark+1) % SYNC_MARKS; |
| 7775 | |
| 7776 | mddev->resync_mark = mark[next]; |
| 7777 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7778 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7779 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7780 | last_mark = next; |
| 7781 | } |
| 7782 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7783 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7784 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7785 | |
| 7786 | /* |
| 7787 | * this loop exits only if either when we are slower than |
| 7788 | * the 'hard' speed limit, or the system was IO-idle for |
| 7789 | * a jiffy. |
| 7790 | * the system might be non-idle CPU-wise, but we only care |
| 7791 | * about not overloading the IO subsystem. (things like an |
| 7792 | * e2fsck being done on the RAID array should execute fast) |
| 7793 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7794 | cond_resched(); |
| 7795 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7796 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 7797 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7798 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7799 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7800 | if (currspeed > speed_min(mddev)) { |
| 7801 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7802 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7803 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7804 | goto repeat; |
| 7805 | } |
| 7806 | } |
| 7807 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7808 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
| 7809 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 7810 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7811 | /* |
| 7812 | * this also signals 'finished resyncing' to md_stop |
| 7813 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7814 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7815 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7816 | |
| 7817 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7818 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7819 | |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 7820 | if (mddev_is_clustered(mddev)) |
Goldwyn Rodrigues | 965400e | 2014-06-07 02:16:58 -0500 | [diff] [blame] | 7821 | md_cluster_ops->resync_finish(mddev); |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 7822 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7823 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7824 | mddev->curr_resync > 2) { |
| 7825 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7826 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7827 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7828 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7829 | "md: checkpointing %s of %s.\n", |
| 7830 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7831 | if (test_bit(MD_RECOVERY_ERROR, |
| 7832 | &mddev->recovery)) |
| 7833 | mddev->recovery_cp = |
| 7834 | mddev->curr_resync_completed; |
| 7835 | else |
| 7836 | mddev->recovery_cp = |
| 7837 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7838 | } |
| 7839 | } else |
| 7840 | mddev->recovery_cp = MaxSector; |
| 7841 | } else { |
| 7842 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7843 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7844 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7845 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7846 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7847 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7848 | !test_bit(Faulty, &rdev->flags) && |
| 7849 | !test_bit(In_sync, &rdev->flags) && |
| 7850 | rdev->recovery_offset < mddev->curr_resync) |
| 7851 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7852 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7853 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7854 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7855 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7856 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7857 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 7858 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7859 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7860 | /* We completed so min/max setting can be forgotten if used. */ |
| 7861 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7862 | mddev->resync_min = 0; |
| 7863 | mddev->resync_max = MaxSector; |
| 7864 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7865 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7866 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 7867 | spin_unlock(&mddev->lock); |
| 7868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7869 | wake_up(&resync_wait); |
| 7870 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7871 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7872 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7873 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7874 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7875 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7876 | static int remove_and_add_spares(struct mddev *mddev, |
| 7877 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7878 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7879 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7880 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7881 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7882 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7883 | rdev_for_each(rdev, mddev) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7884 | if ((this == NULL || rdev == this) && |
| 7885 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7886 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7887 | (test_bit(Faulty, &rdev->flags) || |
| 7888 | ! test_bit(In_sync, &rdev->flags)) && |
| 7889 | atomic_read(&rdev->nr_pending)==0) { |
| 7890 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7891 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7892 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7893 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7894 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7895 | } |
| 7896 | } |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 7897 | if (removed && mddev->kobj.sd) |
| 7898 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7899 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7900 | if (this) |
| 7901 | goto no_add; |
| 7902 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7903 | rdev_for_each(rdev, mddev) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7904 | if (rdev->raid_disk >= 0 && |
| 7905 | !test_bit(In_sync, &rdev->flags) && |
| 7906 | !test_bit(Faulty, &rdev->flags)) |
| 7907 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7908 | if (rdev->raid_disk >= 0) |
| 7909 | continue; |
| 7910 | if (test_bit(Faulty, &rdev->flags)) |
| 7911 | continue; |
| 7912 | if (mddev->ro && |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 7913 | ! (rdev->saved_raid_disk >= 0 && |
| 7914 | !test_bit(Bitmap_sync, &rdev->flags))) |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7915 | continue; |
| 7916 | |
NeilBrown | 7eb4188 | 2014-01-14 15:55:14 +1100 | [diff] [blame] | 7917 | if (rdev->saved_raid_disk < 0) |
| 7918 | rdev->recovery_offset = 0; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7919 | if (mddev->pers-> |
| 7920 | hot_add_disk(mddev, rdev) == 0) { |
| 7921 | if (sysfs_link_rdev(mddev, rdev)) |
| 7922 | /* failure here is OK */; |
| 7923 | spares++; |
| 7924 | md_new_event(mddev); |
| 7925 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7926 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7927 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7928 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 7929 | if (removed) |
| 7930 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7931 | return spares; |
| 7932 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7933 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7934 | static void md_start_sync(struct work_struct *ws) |
| 7935 | { |
| 7936 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
| 7937 | |
| 7938 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7939 | mddev, |
| 7940 | "resync"); |
| 7941 | if (!mddev->sync_thread) { |
| 7942 | printk(KERN_ERR "%s: could not start resync" |
| 7943 | " thread...\n", |
| 7944 | mdname(mddev)); |
| 7945 | /* leave the spares where they are, it shouldn't hurt */ |
| 7946 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7947 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7948 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7949 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7950 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7951 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7952 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7953 | &mddev->recovery)) |
| 7954 | if (mddev->sysfs_action) |
| 7955 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7956 | } else |
| 7957 | md_wakeup_thread(mddev->sync_thread); |
| 7958 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7959 | md_new_event(mddev); |
| 7960 | } |
| 7961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7962 | /* |
| 7963 | * This routine is regularly called by all per-raid-array threads to |
| 7964 | * deal with generic issues like resync and super-block update. |
| 7965 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7966 | * need this as they never do any recovery or update the superblock. |
| 7967 | * |
| 7968 | * It does not do any resync itself, but rather "forks" off other threads |
| 7969 | * to do that as needed. |
| 7970 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7971 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7972 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7973 | * and wakeups up this thread which will reap the thread and finish up. |
| 7974 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7975 | * |
| 7976 | * The overall approach is: |
| 7977 | * 1/ if the superblock needs updating, update it. |
| 7978 | * 2/ If a recovery thread is running, don't do anything else. |
| 7979 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7980 | * 4/ If there are any faulty devices, remove them. |
| 7981 | * 5/ If array is degraded, try to add spares devices |
| 7982 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7983 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7984 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7985 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7986 | if (mddev->suspended) |
| 7987 | return; |
| 7988 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7989 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7990 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7991 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7992 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7993 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7994 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7995 | mdname(mddev)); |
| 7996 | mddev->safemode = 2; |
| 7997 | } |
| 7998 | flush_signals(current); |
| 7999 | } |
| 8000 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8001 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 8002 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8003 | if ( ! ( |
NeilBrown | 142d44c | 2013-11-28 10:34:18 +1100 | [diff] [blame] | 8004 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8005 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8006 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8007 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8008 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 8009 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8010 | )) |
| 8011 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8012 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 8013 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8014 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8015 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8016 | if (mddev->ro) { |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8017 | /* On a read-only array we can: |
| 8018 | * - remove failed devices |
| 8019 | * - add already-in_sync devices if the array itself |
| 8020 | * is in-sync. |
| 8021 | * As we only add devices that are already in-sync, |
| 8022 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8023 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8024 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8025 | /* There is no thread, but we need to call |
| 8026 | * ->spare_active and clear saved_raid_disk |
| 8027 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 8028 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8029 | md_reap_sync_thread(mddev); |
| 8030 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8031 | goto unlock; |
| 8032 | } |
| 8033 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8034 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8035 | int did_change = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8036 | spin_lock(&mddev->lock); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8037 | if (mddev->safemode && |
| 8038 | !atomic_read(&mddev->writes_pending) && |
| 8039 | !mddev->in_sync && |
| 8040 | mddev->recovery_cp == MaxSector) { |
| 8041 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8042 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 8043 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8044 | } |
| 8045 | if (mddev->safemode == 1) |
| 8046 | mddev->safemode = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8047 | spin_unlock(&mddev->lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8048 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8049 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8050 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8051 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 8052 | if (mddev->flags & MD_UPDATE_SB_FLAGS) { |
| 8053 | if (mddev_is_clustered(mddev)) |
| 8054 | md_cluster_ops->metadata_update_start(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 8055 | md_update_sb(mddev, 0); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 8056 | if (mddev_is_clustered(mddev)) |
| 8057 | md_cluster_ops->metadata_update_finish(mddev); |
| 8058 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8060 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 8061 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 8062 | /* resync/recovery still happening */ |
| 8063 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8064 | goto unlock; |
| 8065 | } |
| 8066 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8067 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8068 | goto unlock; |
| 8069 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8070 | /* Set RUNNING before clearing NEEDED to avoid |
| 8071 | * any transients in the value of "sync_action". |
| 8072 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8073 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8074 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8075 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8076 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8077 | /* Clear some bits that don't mean anything, but |
| 8078 | * might be left set |
| 8079 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8080 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 8081 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8082 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 8083 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 8084 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8085 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8086 | /* no recovery is running. |
| 8087 | * remove any failed drives, then |
| 8088 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8089 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8090 | * the personality to fail the re-add. |
| 8091 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8092 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8093 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8094 | if (mddev->pers->check_reshape == NULL || |
| 8095 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8096 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8097 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8098 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8099 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8100 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8101 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8102 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 8103 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8104 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8105 | } else if (mddev->recovery_cp < MaxSector) { |
| 8106 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8107 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8108 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 8109 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8110 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8112 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 8113 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 8114 | /* We are adding a device or devices to an array |
| 8115 | * which has the bitmap stored on all devices. |
| 8116 | * So make sure all bitmap pages get written |
| 8117 | */ |
| 8118 | bitmap_write_all(mddev->bitmap); |
| 8119 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8120 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 8121 | queue_work(md_misc_wq, &mddev->del_work); |
| 8122 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8123 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8124 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8125 | if (!mddev->sync_thread) { |
| 8126 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8127 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8128 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8129 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 8130 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8131 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8132 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8133 | unlock: |
| 8134 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8135 | mddev_unlock(mddev); |
| 8136 | } |
| 8137 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8138 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8139 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8140 | void md_reap_sync_thread(struct mddev *mddev) |
| 8141 | { |
| 8142 | struct md_rdev *rdev; |
| 8143 | |
| 8144 | /* resync has finished, collect result */ |
| 8145 | md_unregister_thread(&mddev->sync_thread); |
| 8146 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8147 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 8148 | /* success...*/ |
| 8149 | /* activate any spares */ |
| 8150 | if (mddev->pers->spare_active(mddev)) { |
| 8151 | sysfs_notify(&mddev->kobj, NULL, |
| 8152 | "degraded"); |
| 8153 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 8154 | } |
| 8155 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 8156 | if (mddev_is_clustered(mddev)) |
| 8157 | md_cluster_ops->metadata_update_start(mddev); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8158 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8159 | mddev->pers->finish_reshape) |
| 8160 | mddev->pers->finish_reshape(mddev); |
| 8161 | |
| 8162 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8163 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8164 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8165 | if (!mddev->degraded) |
| 8166 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8167 | rdev->saved_raid_disk = -1; |
| 8168 | |
| 8169 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 8170 | if (mddev_is_clustered(mddev)) |
| 8171 | md_cluster_ops->metadata_update_finish(mddev); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8172 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 8173 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8174 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8175 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8176 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8177 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8178 | /* flag recovery needed just to double check */ |
| 8179 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8180 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8181 | md_new_event(mddev); |
| 8182 | if (mddev->event_work.func) |
| 8183 | queue_work(md_misc_wq, &mddev->event_work); |
| 8184 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8185 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8186 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8187 | 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] | 8188 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8189 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8190 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8191 | !test_bit(Blocked, &rdev->flags) && |
| 8192 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8193 | msecs_to_jiffies(5000)); |
| 8194 | rdev_dec_pending(rdev, mddev); |
| 8195 | } |
| 8196 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 8197 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8198 | void md_finish_reshape(struct mddev *mddev) |
| 8199 | { |
| 8200 | /* called be personality module when reshape completes. */ |
| 8201 | struct md_rdev *rdev; |
| 8202 | |
| 8203 | rdev_for_each(rdev, mddev) { |
| 8204 | if (rdev->data_offset > rdev->new_data_offset) |
| 8205 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 8206 | else |
| 8207 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 8208 | rdev->data_offset = rdev->new_data_offset; |
| 8209 | } |
| 8210 | } |
| 8211 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8212 | |
| 8213 | /* Bad block management. |
| 8214 | * We can record which blocks on each device are 'bad' and so just |
| 8215 | * fail those blocks, or that stripe, rather than the whole device. |
| 8216 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 8217 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 8218 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 8219 | * A 'shift' can be set so that larger blocks are tracked and |
| 8220 | * consequently larger devices can be covered. |
| 8221 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 8222 | * |
| 8223 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 8224 | * might need to retry if it is very unlucky. |
| 8225 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 8226 | * so we use the write_seqlock_irq variant. |
| 8227 | * |
| 8228 | * When looking for a bad block we specify a range and want to |
| 8229 | * know if any block in the range is bad. So we binary-search |
| 8230 | * to the last range that starts at-or-before the given endpoint, |
| 8231 | * (or "before the sector after the target range") |
| 8232 | * then see if it ends after the given start. |
| 8233 | * We return |
| 8234 | * 0 if there are no known bad blocks in the range |
| 8235 | * 1 if there are known bad block which are all acknowledged |
| 8236 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 8237 | * plus the start/length of the first bad section we overlap. |
| 8238 | */ |
| 8239 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 8240 | sector_t *first_bad, int *bad_sectors) |
| 8241 | { |
| 8242 | int hi; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8243 | int lo; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8244 | u64 *p = bb->page; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8245 | int rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8246 | sector_t target = s + sectors; |
| 8247 | unsigned seq; |
| 8248 | |
| 8249 | if (bb->shift > 0) { |
| 8250 | /* round the start down, and the end up */ |
| 8251 | s >>= bb->shift; |
| 8252 | target += (1<<bb->shift) - 1; |
| 8253 | target >>= bb->shift; |
| 8254 | sectors = target - s; |
| 8255 | } |
| 8256 | /* 'target' is now the first block after the bad range */ |
| 8257 | |
| 8258 | retry: |
| 8259 | seq = read_seqbegin(&bb->lock); |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8260 | lo = 0; |
| 8261 | rv = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8262 | hi = bb->count; |
| 8263 | |
| 8264 | /* Binary search between lo and hi for 'target' |
| 8265 | * i.e. for the last range that starts before 'target' |
| 8266 | */ |
| 8267 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 8268 | * are known not to be the last range before target. |
| 8269 | * VARIANT: hi-lo is the number of possible |
| 8270 | * ranges, and decreases until it reaches 1 |
| 8271 | */ |
| 8272 | while (hi - lo > 1) { |
| 8273 | int mid = (lo + hi) / 2; |
| 8274 | sector_t a = BB_OFFSET(p[mid]); |
| 8275 | if (a < target) |
| 8276 | /* This could still be the one, earlier ranges |
| 8277 | * could not. */ |
| 8278 | lo = mid; |
| 8279 | else |
| 8280 | /* This and later ranges are definitely out. */ |
| 8281 | hi = mid; |
| 8282 | } |
| 8283 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 8284 | if (hi > lo) { |
| 8285 | /* need to check all range that end after 's' to see if |
| 8286 | * any are unacknowledged. |
| 8287 | */ |
| 8288 | while (lo >= 0 && |
| 8289 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8290 | if (BB_OFFSET(p[lo]) < target) { |
| 8291 | /* starts before the end, and finishes after |
| 8292 | * the start, so they must overlap |
| 8293 | */ |
| 8294 | if (rv != -1 && BB_ACK(p[lo])) |
| 8295 | rv = 1; |
| 8296 | else |
| 8297 | rv = -1; |
| 8298 | *first_bad = BB_OFFSET(p[lo]); |
| 8299 | *bad_sectors = BB_LEN(p[lo]); |
| 8300 | } |
| 8301 | lo--; |
| 8302 | } |
| 8303 | } |
| 8304 | |
| 8305 | if (read_seqretry(&bb->lock, seq)) |
| 8306 | goto retry; |
| 8307 | |
| 8308 | return rv; |
| 8309 | } |
| 8310 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 8311 | |
| 8312 | /* |
| 8313 | * Add a range of bad blocks to the table. |
| 8314 | * This might extend the table, or might contract it |
| 8315 | * if two adjacent ranges can be merged. |
| 8316 | * We binary-search to find the 'insertion' point, then |
| 8317 | * decide how best to handle it. |
| 8318 | */ |
| 8319 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 8320 | int acknowledged) |
| 8321 | { |
| 8322 | u64 *p; |
| 8323 | int lo, hi; |
| 8324 | int rv = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8325 | unsigned long flags; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8326 | |
| 8327 | if (bb->shift < 0) |
| 8328 | /* badblocks are disabled */ |
| 8329 | return 0; |
| 8330 | |
| 8331 | if (bb->shift) { |
| 8332 | /* round the start down, and the end up */ |
| 8333 | sector_t next = s + sectors; |
| 8334 | s >>= bb->shift; |
| 8335 | next += (1<<bb->shift) - 1; |
| 8336 | next >>= bb->shift; |
| 8337 | sectors = next - s; |
| 8338 | } |
| 8339 | |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8340 | write_seqlock_irqsave(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8341 | |
| 8342 | p = bb->page; |
| 8343 | lo = 0; |
| 8344 | hi = bb->count; |
| 8345 | /* Find the last range that starts at-or-before 's' */ |
| 8346 | while (hi - lo > 1) { |
| 8347 | int mid = (lo + hi) / 2; |
| 8348 | sector_t a = BB_OFFSET(p[mid]); |
| 8349 | if (a <= s) |
| 8350 | lo = mid; |
| 8351 | else |
| 8352 | hi = mid; |
| 8353 | } |
| 8354 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 8355 | hi = lo; |
| 8356 | |
| 8357 | if (hi > lo) { |
| 8358 | /* we found a range that might merge with the start |
| 8359 | * of our new range |
| 8360 | */ |
| 8361 | sector_t a = BB_OFFSET(p[lo]); |
| 8362 | sector_t e = a + BB_LEN(p[lo]); |
| 8363 | int ack = BB_ACK(p[lo]); |
| 8364 | if (e >= s) { |
| 8365 | /* Yes, we can merge with a previous range */ |
| 8366 | if (s == a && s + sectors >= e) |
| 8367 | /* new range covers old */ |
| 8368 | ack = acknowledged; |
| 8369 | else |
| 8370 | ack = ack && acknowledged; |
| 8371 | |
| 8372 | if (e < s + sectors) |
| 8373 | e = s + sectors; |
| 8374 | if (e - a <= BB_MAX_LEN) { |
| 8375 | p[lo] = BB_MAKE(a, e-a, ack); |
| 8376 | s = e; |
| 8377 | } else { |
| 8378 | /* does not all fit in one range, |
| 8379 | * make p[lo] maximal |
| 8380 | */ |
| 8381 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8382 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8383 | s = a + BB_MAX_LEN; |
| 8384 | } |
| 8385 | sectors = e - s; |
| 8386 | } |
| 8387 | } |
| 8388 | if (sectors && hi < bb->count) { |
| 8389 | /* 'hi' points to the first range that starts after 's'. |
| 8390 | * Maybe we can merge with the start of that range */ |
| 8391 | sector_t a = BB_OFFSET(p[hi]); |
| 8392 | sector_t e = a + BB_LEN(p[hi]); |
| 8393 | int ack = BB_ACK(p[hi]); |
| 8394 | if (a <= s + sectors) { |
| 8395 | /* merging is possible */ |
| 8396 | if (e <= s + sectors) { |
| 8397 | /* full overlap */ |
| 8398 | e = s + sectors; |
| 8399 | ack = acknowledged; |
| 8400 | } else |
| 8401 | ack = ack && acknowledged; |
| 8402 | |
| 8403 | a = s; |
| 8404 | if (e - a <= BB_MAX_LEN) { |
| 8405 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8406 | s = e; |
| 8407 | } else { |
| 8408 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8409 | s = a + BB_MAX_LEN; |
| 8410 | } |
| 8411 | sectors = e - s; |
| 8412 | lo = hi; |
| 8413 | hi++; |
| 8414 | } |
| 8415 | } |
| 8416 | if (sectors == 0 && hi < bb->count) { |
| 8417 | /* we might be able to combine lo and hi */ |
| 8418 | /* Note: 's' is at the end of 'lo' */ |
| 8419 | sector_t a = BB_OFFSET(p[hi]); |
| 8420 | int lolen = BB_LEN(p[lo]); |
| 8421 | int hilen = BB_LEN(p[hi]); |
| 8422 | int newlen = lolen + hilen - (s - a); |
| 8423 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8424 | /* yes, we can combine them */ |
| 8425 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8426 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8427 | memmove(p + hi, p + hi + 1, |
| 8428 | (bb->count - hi - 1) * 8); |
| 8429 | bb->count--; |
| 8430 | } |
| 8431 | } |
| 8432 | while (sectors) { |
| 8433 | /* didn't merge (it all). |
| 8434 | * Need to add a range just before 'hi' */ |
| 8435 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8436 | /* No room for more */ |
| 8437 | rv = 0; |
| 8438 | break; |
| 8439 | } else { |
| 8440 | int this_sectors = sectors; |
| 8441 | memmove(p + hi + 1, p + hi, |
| 8442 | (bb->count - hi) * 8); |
| 8443 | bb->count++; |
| 8444 | |
| 8445 | if (this_sectors > BB_MAX_LEN) |
| 8446 | this_sectors = BB_MAX_LEN; |
| 8447 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8448 | sectors -= this_sectors; |
| 8449 | s += this_sectors; |
| 8450 | } |
| 8451 | } |
| 8452 | |
| 8453 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8454 | if (!acknowledged) |
| 8455 | bb->unacked_exist = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8456 | write_sequnlock_irqrestore(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8457 | |
| 8458 | return rv; |
| 8459 | } |
| 8460 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8461 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8462 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8463 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8464 | int rv; |
| 8465 | if (is_new) |
| 8466 | s += rdev->new_data_offset; |
| 8467 | else |
| 8468 | s += rdev->data_offset; |
| 8469 | rv = md_set_badblocks(&rdev->badblocks, |
| 8470 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8471 | if (rv) { |
| 8472 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8473 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8474 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 8475 | md_wakeup_thread(rdev->mddev->thread); |
| 8476 | } |
| 8477 | return rv; |
| 8478 | } |
| 8479 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8480 | |
| 8481 | /* |
| 8482 | * Remove a range of bad blocks from the table. |
| 8483 | * This may involve extending the table if we spilt a region, |
| 8484 | * but it must not fail. So if the table becomes full, we just |
| 8485 | * drop the remove request. |
| 8486 | */ |
| 8487 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8488 | { |
| 8489 | u64 *p; |
| 8490 | int lo, hi; |
| 8491 | sector_t target = s + sectors; |
| 8492 | int rv = 0; |
| 8493 | |
| 8494 | if (bb->shift > 0) { |
| 8495 | /* When clearing we round the start up and the end down. |
| 8496 | * This should not matter as the shift should align with |
| 8497 | * the block size and no rounding should ever be needed. |
| 8498 | * However it is better the think a block is bad when it |
| 8499 | * isn't than to think a block is not bad when it is. |
| 8500 | */ |
| 8501 | s += (1<<bb->shift) - 1; |
| 8502 | s >>= bb->shift; |
| 8503 | target >>= bb->shift; |
| 8504 | sectors = target - s; |
| 8505 | } |
| 8506 | |
| 8507 | write_seqlock_irq(&bb->lock); |
| 8508 | |
| 8509 | p = bb->page; |
| 8510 | lo = 0; |
| 8511 | hi = bb->count; |
| 8512 | /* Find the last range that starts before 'target' */ |
| 8513 | while (hi - lo > 1) { |
| 8514 | int mid = (lo + hi) / 2; |
| 8515 | sector_t a = BB_OFFSET(p[mid]); |
| 8516 | if (a < target) |
| 8517 | lo = mid; |
| 8518 | else |
| 8519 | hi = mid; |
| 8520 | } |
| 8521 | if (hi > lo) { |
| 8522 | /* p[lo] is the last range that could overlap the |
| 8523 | * current range. Earlier ranges could also overlap, |
| 8524 | * but only this one can overlap the end of the range. |
| 8525 | */ |
| 8526 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8527 | /* Partial overlap, leave the tail of this range */ |
| 8528 | int ack = BB_ACK(p[lo]); |
| 8529 | sector_t a = BB_OFFSET(p[lo]); |
| 8530 | sector_t end = a + BB_LEN(p[lo]); |
| 8531 | |
| 8532 | if (a < s) { |
| 8533 | /* we need to split this range */ |
| 8534 | if (bb->count >= MD_MAX_BADBLOCKS) { |
NeilBrown | 8b32bf5 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 8535 | rv = -ENOSPC; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8536 | goto out; |
| 8537 | } |
| 8538 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8539 | bb->count++; |
| 8540 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8541 | lo++; |
| 8542 | } |
| 8543 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8544 | /* there is no longer an overlap */ |
| 8545 | hi = lo; |
| 8546 | lo--; |
| 8547 | } |
| 8548 | while (lo >= 0 && |
| 8549 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8550 | /* This range does overlap */ |
| 8551 | if (BB_OFFSET(p[lo]) < s) { |
| 8552 | /* Keep the early parts of this range. */ |
| 8553 | int ack = BB_ACK(p[lo]); |
| 8554 | sector_t start = BB_OFFSET(p[lo]); |
| 8555 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8556 | /* now low doesn't overlap, so.. */ |
| 8557 | break; |
| 8558 | } |
| 8559 | lo--; |
| 8560 | } |
| 8561 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8562 | * anything between needs to be discarded |
| 8563 | */ |
| 8564 | if (hi - lo > 1) { |
| 8565 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8566 | bb->count -= (hi - lo - 1); |
| 8567 | } |
| 8568 | } |
| 8569 | |
| 8570 | bb->changed = 1; |
| 8571 | out: |
| 8572 | write_sequnlock_irq(&bb->lock); |
| 8573 | return rv; |
| 8574 | } |
| 8575 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8576 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8577 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8578 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8579 | if (is_new) |
| 8580 | s += rdev->new_data_offset; |
| 8581 | else |
| 8582 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8583 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8584 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8585 | } |
| 8586 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8587 | |
| 8588 | /* |
| 8589 | * Acknowledge all bad blocks in a list. |
| 8590 | * This only succeeds if ->changed is clear. It is used by |
| 8591 | * in-kernel metadata updates |
| 8592 | */ |
| 8593 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8594 | { |
| 8595 | if (bb->page == NULL || bb->changed) |
| 8596 | /* no point even trying */ |
| 8597 | return; |
| 8598 | write_seqlock_irq(&bb->lock); |
| 8599 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8600 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8601 | u64 *p = bb->page; |
| 8602 | int i; |
| 8603 | for (i = 0; i < bb->count ; i++) { |
| 8604 | if (!BB_ACK(p[i])) { |
| 8605 | sector_t start = BB_OFFSET(p[i]); |
| 8606 | int len = BB_LEN(p[i]); |
| 8607 | p[i] = BB_MAKE(start, len, 1); |
| 8608 | } |
| 8609 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8610 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8611 | } |
| 8612 | write_sequnlock_irq(&bb->lock); |
| 8613 | } |
| 8614 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8615 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8616 | /* sysfs access to bad-blocks list. |
| 8617 | * We present two files. |
| 8618 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8619 | * are recorded as bad. The list is truncated to fit within |
| 8620 | * the one-page limit of sysfs. |
| 8621 | * Writing "sector length" to this file adds an acknowledged |
| 8622 | * bad block list. |
| 8623 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8624 | * been acknowledged. Writing to this file adds bad blocks |
| 8625 | * without acknowledging them. This is largely for testing. |
| 8626 | */ |
| 8627 | |
| 8628 | static ssize_t |
| 8629 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8630 | { |
| 8631 | size_t len; |
| 8632 | int i; |
| 8633 | u64 *p = bb->page; |
| 8634 | unsigned seq; |
| 8635 | |
| 8636 | if (bb->shift < 0) |
| 8637 | return 0; |
| 8638 | |
| 8639 | retry: |
| 8640 | seq = read_seqbegin(&bb->lock); |
| 8641 | |
| 8642 | len = 0; |
| 8643 | i = 0; |
| 8644 | |
| 8645 | while (len < PAGE_SIZE && i < bb->count) { |
| 8646 | sector_t s = BB_OFFSET(p[i]); |
| 8647 | unsigned int length = BB_LEN(p[i]); |
| 8648 | int ack = BB_ACK(p[i]); |
| 8649 | i++; |
| 8650 | |
| 8651 | if (unack && ack) |
| 8652 | continue; |
| 8653 | |
| 8654 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8655 | (unsigned long long)s << bb->shift, |
| 8656 | length << bb->shift); |
| 8657 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8658 | if (unack && len == 0) |
| 8659 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8660 | |
| 8661 | if (read_seqretry(&bb->lock, seq)) |
| 8662 | goto retry; |
| 8663 | |
| 8664 | return len; |
| 8665 | } |
| 8666 | |
| 8667 | #define DO_DEBUG 1 |
| 8668 | |
| 8669 | static ssize_t |
| 8670 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8671 | { |
| 8672 | unsigned long long sector; |
| 8673 | int length; |
| 8674 | char newline; |
| 8675 | #ifdef DO_DEBUG |
| 8676 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8677 | * Normally only a successful write may clear a badblock |
| 8678 | */ |
| 8679 | int clear = 0; |
| 8680 | if (page[0] == '-') { |
| 8681 | clear = 1; |
| 8682 | page++; |
| 8683 | } |
| 8684 | #endif /* DO_DEBUG */ |
| 8685 | |
| 8686 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8687 | case 3: |
| 8688 | if (newline != '\n') |
| 8689 | return -EINVAL; |
| 8690 | case 2: |
| 8691 | if (length <= 0) |
| 8692 | return -EINVAL; |
| 8693 | break; |
| 8694 | default: |
| 8695 | return -EINVAL; |
| 8696 | } |
| 8697 | |
| 8698 | #ifdef DO_DEBUG |
| 8699 | if (clear) { |
| 8700 | md_clear_badblocks(bb, sector, length); |
| 8701 | return len; |
| 8702 | } |
| 8703 | #endif /* DO_DEBUG */ |
| 8704 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8705 | return len; |
| 8706 | else |
| 8707 | return -ENOSPC; |
| 8708 | } |
| 8709 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8710 | static int md_notify_reboot(struct notifier_block *this, |
| 8711 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8712 | { |
| 8713 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8714 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8715 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8716 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8717 | for_each_mddev(mddev, tmp) { |
| 8718 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8719 | if (mddev->pers) |
| 8720 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 8721 | if (mddev->persistent) |
| 8722 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8723 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8724 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8725 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8726 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8727 | /* |
| 8728 | * certain more exotic SCSI devices are known to be |
| 8729 | * volatile wrt too early system reboots. While the |
| 8730 | * right place to handle this issue is the given |
| 8731 | * driver, we do want to have a safe RAID driver ... |
| 8732 | */ |
| 8733 | if (need_delay) |
| 8734 | mdelay(1000*1); |
| 8735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8736 | return NOTIFY_DONE; |
| 8737 | } |
| 8738 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8739 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8740 | .notifier_call = md_notify_reboot, |
| 8741 | .next = NULL, |
| 8742 | .priority = INT_MAX, /* before any real devices */ |
| 8743 | }; |
| 8744 | |
| 8745 | static void md_geninit(void) |
| 8746 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8747 | 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] | 8748 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8749 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8750 | } |
| 8751 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8752 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8753 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8754 | int ret = -ENOMEM; |
| 8755 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8756 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8757 | if (!md_wq) |
| 8758 | goto err_wq; |
| 8759 | |
| 8760 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8761 | if (!md_misc_wq) |
| 8762 | goto err_misc_wq; |
| 8763 | |
| 8764 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8765 | goto err_md; |
| 8766 | |
| 8767 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8768 | goto err_mdp; |
| 8769 | mdp_major = ret; |
| 8770 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8771 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8772 | md_probe, NULL, NULL); |
| 8773 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8774 | md_probe, NULL, NULL); |
| 8775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8776 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8777 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8778 | |
| 8779 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8780 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8781 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8782 | err_mdp: |
| 8783 | unregister_blkdev(MD_MAJOR, "md"); |
| 8784 | err_md: |
| 8785 | destroy_workqueue(md_misc_wq); |
| 8786 | err_misc_wq: |
| 8787 | destroy_workqueue(md_wq); |
| 8788 | err_wq: |
| 8789 | return ret; |
| 8790 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8791 | |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8792 | void md_reload_sb(struct mddev *mddev) |
| 8793 | { |
| 8794 | struct md_rdev *rdev, *tmp; |
| 8795 | |
| 8796 | rdev_for_each_safe(rdev, tmp, mddev) { |
| 8797 | rdev->sb_loaded = 0; |
| 8798 | ClearPageUptodate(rdev->sb_page); |
| 8799 | } |
| 8800 | mddev->raid_disks = 0; |
| 8801 | analyze_sbs(mddev); |
| 8802 | rdev_for_each_safe(rdev, tmp, mddev) { |
| 8803 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 8804 | /* since we don't write to faulty devices, we figure out if the |
| 8805 | * disk is faulty by comparing events |
| 8806 | */ |
| 8807 | if (mddev->events > sb->events) |
| 8808 | set_bit(Faulty, &rdev->flags); |
| 8809 | } |
| 8810 | |
| 8811 | } |
| 8812 | EXPORT_SYMBOL(md_reload_sb); |
| 8813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8814 | #ifndef MODULE |
| 8815 | |
| 8816 | /* |
| 8817 | * Searches all registered partitions for autorun RAID arrays |
| 8818 | * at boot time. |
| 8819 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8820 | |
| 8821 | static LIST_HEAD(all_detected_devices); |
| 8822 | struct detected_devices_node { |
| 8823 | struct list_head list; |
| 8824 | dev_t dev; |
| 8825 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8826 | |
| 8827 | void md_autodetect_dev(dev_t dev) |
| 8828 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8829 | struct detected_devices_node *node_detected_dev; |
| 8830 | |
| 8831 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8832 | if (node_detected_dev) { |
| 8833 | node_detected_dev->dev = dev; |
| 8834 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8835 | } else { |
| 8836 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8837 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8838 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8839 | } |
| 8840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8841 | static void autostart_arrays(int part) |
| 8842 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8843 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8844 | struct detected_devices_node *node_detected_dev; |
| 8845 | dev_t dev; |
| 8846 | int i_scanned, i_passed; |
| 8847 | |
| 8848 | i_scanned = 0; |
| 8849 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8850 | |
| 8851 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8852 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8853 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8854 | i_scanned++; |
| 8855 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8856 | struct detected_devices_node, list); |
| 8857 | list_del(&node_detected_dev->list); |
| 8858 | dev = node_detected_dev->dev; |
| 8859 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8860 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8861 | if (IS_ERR(rdev)) |
| 8862 | continue; |
| 8863 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8864 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8865 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8866 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8867 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8868 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8869 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8870 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8871 | |
| 8872 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8873 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8874 | |
| 8875 | autorun_devices(part); |
| 8876 | } |
| 8877 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8878 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8879 | |
| 8880 | static __exit void md_exit(void) |
| 8881 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8882 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8883 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8884 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8885 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8886 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8887 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8888 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8889 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8890 | unregister_blkdev(mdp_major, "mdp"); |
| 8891 | unregister_reboot_notifier(&md_notifier); |
| 8892 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8893 | |
| 8894 | /* We cannot unload the modules while some process is |
| 8895 | * waiting for us in select() or poll() - wake them up |
| 8896 | */ |
| 8897 | md_unloading = 1; |
| 8898 | while (waitqueue_active(&md_event_waiters)) { |
| 8899 | /* not safe to leave yet */ |
| 8900 | wake_up(&md_event_waiters); |
| 8901 | msleep(delay); |
| 8902 | delay += delay; |
| 8903 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8904 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8905 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8906 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8907 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8908 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8909 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8910 | destroy_workqueue(md_misc_wq); |
| 8911 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8912 | } |
| 8913 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8914 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8915 | module_exit(md_exit) |
| 8916 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8917 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8918 | { |
| 8919 | return sprintf(buffer, "%d", start_readonly); |
| 8920 | } |
| 8921 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8922 | { |
| 8923 | char *e; |
| 8924 | int num = simple_strtoul(val, &e, 10); |
| 8925 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8926 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8927 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8928 | } |
| 8929 | return -EINVAL; |
| 8930 | } |
| 8931 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8932 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8933 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8934 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8936 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8937 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8938 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8939 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |