Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 39 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 40 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 41 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 42 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 43 | #include <linux/hdreg.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/random.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 46 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 47 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 49 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 50 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/raid/md_p.h> |
| 52 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 56 | #include "md-cluster.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 59 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 62 | /* pers_list is a list of registered personalities protected |
| 63 | * by pers_lock. |
| 64 | * pers_lock does extra service to protect accesses to |
| 65 | * mddev->thread when the mutex cannot be held. |
| 66 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 67 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static DEFINE_SPINLOCK(pers_lock); |
| 69 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 70 | struct md_cluster_operations *md_cluster_ops; |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame] | 71 | EXPORT_SYMBOL(md_cluster_ops); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 72 | struct module *md_cluster_mod; |
| 73 | EXPORT_SYMBOL(md_cluster_mod); |
| 74 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 75 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 76 | static struct workqueue_struct *md_wq; |
| 77 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 78 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 79 | static int remove_and_add_spares(struct mddev *mddev, |
| 80 | struct md_rdev *this); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 81 | static void mddev_detach(struct mddev *mddev); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 84 | * Default number of read corrections we'll attempt on an rdev |
| 85 | * before ejecting it from the array. We divide the read error |
| 86 | * count by 2 for every hour elapsed between read errors. |
| 87 | */ |
| 88 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 89 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 91 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 92 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 93 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 95 | * speed limit - in case reconstruction slows down your system despite |
| 96 | * idle IO detection. |
| 97 | * |
| 98 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 99 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | */ |
| 101 | |
| 102 | static int sysctl_speed_limit_min = 1000; |
| 103 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 104 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 105 | { |
| 106 | return mddev->sync_speed_min ? |
| 107 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 108 | } |
| 109 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 110 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 111 | { |
| 112 | return mddev->sync_speed_max ? |
| 113 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | static struct ctl_table_header *raid_table_header; |
| 117 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 118 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | .procname = "speed_limit_min", |
| 121 | .data = &sysctl_speed_limit_min, |
| 122 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 123 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 124 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | }, |
| 126 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | .procname = "speed_limit_max", |
| 128 | .data = &sysctl_speed_limit_max, |
| 129 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 130 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 131 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 133 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 136 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | .procname = "raid", |
| 139 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 140 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | .child = raid_table, |
| 142 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 143 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 146 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | .procname = "dev", |
| 149 | .maxlen = 0, |
| 150 | .mode = 0555, |
| 151 | .child = raid_dir_table, |
| 152 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 153 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 156 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 158 | static int start_readonly; |
| 159 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 160 | /* bio_clone_mddev |
| 161 | * like bio_clone, but with a local bio set |
| 162 | */ |
| 163 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 164 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 165 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 166 | { |
| 167 | struct bio *b; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 168 | |
| 169 | if (!mddev || !mddev->bio_set) |
| 170 | return bio_alloc(gfp_mask, nr_iovecs); |
| 171 | |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 172 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 173 | if (!b) |
| 174 | return NULL; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 175 | return b; |
| 176 | } |
| 177 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 178 | |
| 179 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 180 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 181 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 182 | if (!mddev || !mddev->bio_set) |
| 183 | return bio_clone(bio, gfp_mask); |
| 184 | |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 185 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 186 | } |
| 187 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 190 | * We have a system wide 'event count' that is incremented |
| 191 | * on any 'interesting' event, and readers of /proc/mdstat |
| 192 | * can use 'poll' or 'select' to find out when the event |
| 193 | * count increases. |
| 194 | * |
| 195 | * Events are: |
| 196 | * start array, stop array, error, add device, remove device, |
| 197 | * start build, activate spare |
| 198 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 199 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 200 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 201 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 202 | { |
| 203 | atomic_inc(&md_event_count); |
| 204 | wake_up(&md_event_waiters); |
| 205 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 206 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 207 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 208 | /* Alternate version that can be called from interrupts |
| 209 | * when calling sysfs_notify isn't needed. |
| 210 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 211 | static void md_new_event_inintr(struct mddev *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 212 | { |
| 213 | atomic_inc(&md_event_count); |
| 214 | wake_up(&md_event_waiters); |
| 215 | } |
| 216 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 217 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | * Enables to iterate over all existing md arrays |
| 219 | * all_mddevs_lock protects this list. |
| 220 | */ |
| 221 | static LIST_HEAD(all_mddevs); |
| 222 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | /* |
| 225 | * iterates through all used mddevs in the system. |
| 226 | * We take care to grab the all_mddevs_lock whenever navigating |
| 227 | * the list, and to always hold a refcount when unlocked. |
| 228 | * Any code which breaks out of this loop while own |
| 229 | * a reference to the current mddev and must mddev_put it. |
| 230 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 231 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | \ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 233 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 234 | _tmp = all_mddevs.next; \ |
| 235 | _mddev = NULL;}); \ |
| 236 | ({ if (_tmp != &all_mddevs) \ |
| 237 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 239 | if (_mddev) mddev_put(_mddev); \ |
| 240 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 241 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 243 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | ) |
| 245 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 246 | /* Rather than calling directly into the personality make_request function, |
| 247 | * IO requests come here first so that we can check if the device is |
| 248 | * being suspended pending a reconfiguration. |
| 249 | * We hold a refcount over the call to ->make_request. By the time that |
| 250 | * call has finished, the bio has been linked into some internal structure |
| 251 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 252 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 253 | static void md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 255 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 256 | struct mddev *mddev = q->queuedata; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 257 | unsigned int sectors; |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 258 | int cpu; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 259 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 260 | if (mddev == NULL || mddev->pers == NULL |
| 261 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 262 | bio_io_error(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 263 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 264 | } |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 265 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
| 266 | bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS); |
| 267 | return; |
| 268 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 269 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 270 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 271 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 272 | DEFINE_WAIT(__wait); |
| 273 | for (;;) { |
| 274 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 275 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 276 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 277 | break; |
| 278 | rcu_read_unlock(); |
| 279 | schedule(); |
| 280 | rcu_read_lock(); |
| 281 | } |
| 282 | finish_wait(&mddev->sb_wait, &__wait); |
| 283 | } |
| 284 | atomic_inc(&mddev->active_io); |
| 285 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 286 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 287 | /* |
| 288 | * save the sectors now since our bio can |
| 289 | * go away inside make_request |
| 290 | */ |
| 291 | sectors = bio_sectors(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 292 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 293 | |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 294 | cpu = part_stat_lock(); |
| 295 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 296 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
| 297 | part_stat_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 298 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 299 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 300 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 303 | /* mddev_suspend makes sure no new requests are submitted |
| 304 | * to the device, and that any requests that have been submitted |
| 305 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 306 | * Once mddev_detach() is called and completes, the module will be |
| 307 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 308 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 309 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 310 | { |
| 311 | BUG_ON(mddev->suspended); |
| 312 | mddev->suspended = 1; |
| 313 | synchronize_rcu(); |
| 314 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 315 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 316 | |
| 317 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 318 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 319 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 320 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 321 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 322 | { |
| 323 | mddev->suspended = 0; |
| 324 | wake_up(&mddev->sb_wait); |
| 325 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 326 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 327 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 328 | md_wakeup_thread(mddev->thread); |
| 329 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 330 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 331 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 332 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 333 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 334 | { |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 335 | struct md_personality *pers = mddev->pers; |
| 336 | int ret = 0; |
| 337 | |
| 338 | rcu_read_lock(); |
| 339 | if (mddev->suspended) |
| 340 | ret = 1; |
| 341 | else if (pers && pers->congested) |
| 342 | ret = pers->congested(mddev, bits); |
| 343 | rcu_read_unlock(); |
| 344 | return ret; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 345 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 346 | EXPORT_SYMBOL_GPL(mddev_congested); |
| 347 | static int md_congested(void *data, int bits) |
| 348 | { |
| 349 | struct mddev *mddev = data; |
| 350 | return mddev_congested(mddev, bits); |
| 351 | } |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 352 | |
NeilBrown | 64590f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 353 | static int md_mergeable_bvec(struct request_queue *q, |
| 354 | struct bvec_merge_data *bvm, |
| 355 | struct bio_vec *biovec) |
| 356 | { |
| 357 | struct mddev *mddev = q->queuedata; |
| 358 | int ret; |
| 359 | rcu_read_lock(); |
| 360 | if (mddev->suspended) { |
| 361 | /* Must always allow one vec */ |
| 362 | if (bvm->bi_size == 0) |
| 363 | ret = biovec->bv_len; |
| 364 | else |
| 365 | ret = 0; |
| 366 | } else { |
| 367 | struct md_personality *pers = mddev->pers; |
| 368 | if (pers && pers->mergeable_bvec) |
| 369 | ret = pers->mergeable_bvec(mddev, bvm, biovec); |
| 370 | else |
| 371 | ret = biovec->bv_len; |
| 372 | } |
| 373 | rcu_read_unlock(); |
| 374 | return ret; |
| 375 | } |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 376 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 377 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 378 | */ |
| 379 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 380 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 381 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 382 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 383 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 384 | |
| 385 | rdev_dec_pending(rdev, mddev); |
| 386 | |
| 387 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 388 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 389 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 390 | } |
| 391 | bio_put(bio); |
| 392 | } |
| 393 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 394 | static void md_submit_flush_data(struct work_struct *ws); |
| 395 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 396 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 397 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 398 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 399 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 400 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 401 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 402 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 403 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 404 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 405 | if (rdev->raid_disk >= 0 && |
| 406 | !test_bit(Faulty, &rdev->flags)) { |
| 407 | /* Take two references, one is dropped |
| 408 | * when request finishes, one after |
| 409 | * we reclaim rcu_read_lock |
| 410 | */ |
| 411 | struct bio *bi; |
| 412 | atomic_inc(&rdev->nr_pending); |
| 413 | atomic_inc(&rdev->nr_pending); |
| 414 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 415 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 416 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 417 | bi->bi_private = rdev; |
| 418 | bi->bi_bdev = rdev->bdev; |
| 419 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 420 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 421 | rcu_read_lock(); |
| 422 | rdev_dec_pending(rdev, mddev); |
| 423 | } |
| 424 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 425 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 426 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 427 | } |
| 428 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 429 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 430 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 431 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 432 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 433 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 434 | if (bio->bi_iter.bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 435 | /* an empty barrier - all done */ |
| 436 | bio_endio(bio, 0); |
| 437 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 438 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 439 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 440 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 441 | |
| 442 | mddev->flush_bio = NULL; |
| 443 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 444 | } |
| 445 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 446 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 447 | { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 448 | spin_lock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 449 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 450 | !mddev->flush_bio, |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 451 | mddev->lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 452 | mddev->flush_bio = bio; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 453 | spin_unlock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 454 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 455 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 456 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 457 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 458 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 459 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 460 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 461 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 462 | struct mddev *mddev = cb->data; |
| 463 | md_wakeup_thread(mddev->thread); |
| 464 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 465 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 466 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 467 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 468 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
| 470 | atomic_inc(&mddev->active); |
| 471 | return mddev; |
| 472 | } |
| 473 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 474 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 475 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 476 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 478 | struct bio_set *bs = NULL; |
| 479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 481 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 482 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 483 | mddev->ctime == 0 && !mddev->hold_active) { |
| 484 | /* Array is not configured at all, and not held active, |
| 485 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 486 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 487 | bs = mddev->bio_set; |
| 488 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 489 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 490 | /* We did a probe so need to clean up. Call |
| 491 | * queue_work inside the spinlock so that |
| 492 | * flush_workqueue() after mddev_find will |
| 493 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 494 | */ |
| 495 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 496 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 497 | } else |
| 498 | kfree(mddev); |
| 499 | } |
| 500 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 501 | if (bs) |
| 502 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
| 504 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 505 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 506 | { |
| 507 | mutex_init(&mddev->open_mutex); |
| 508 | mutex_init(&mddev->reconfig_mutex); |
| 509 | mutex_init(&mddev->bitmap_info.mutex); |
| 510 | INIT_LIST_HEAD(&mddev->disks); |
| 511 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 512 | init_timer(&mddev->safemode_timer); |
| 513 | atomic_set(&mddev->active, 1); |
| 514 | atomic_set(&mddev->openers, 0); |
| 515 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 516 | spin_lock_init(&mddev->lock); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 517 | atomic_set(&mddev->flush_pending, 0); |
| 518 | init_waitqueue_head(&mddev->sb_wait); |
| 519 | init_waitqueue_head(&mddev->recovery_wait); |
| 520 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 521 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 522 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 523 | mddev->resync_min = 0; |
| 524 | mddev->resync_max = MaxSector; |
| 525 | mddev->level = LEVEL_NONE; |
| 526 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 527 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 528 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 529 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 531 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 533 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 534 | unit &= ~((1<<MdpMinorShift)-1); |
| 535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | retry: |
| 537 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 539 | if (unit) { |
| 540 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 541 | if (mddev->unit == unit) { |
| 542 | mddev_get(mddev); |
| 543 | spin_unlock(&all_mddevs_lock); |
| 544 | kfree(new); |
| 545 | return mddev; |
| 546 | } |
| 547 | |
| 548 | if (new) { |
| 549 | list_add(&new->all_mddevs, &all_mddevs); |
| 550 | spin_unlock(&all_mddevs_lock); |
| 551 | new->hold_active = UNTIL_IOCTL; |
| 552 | return new; |
| 553 | } |
| 554 | } else if (new) { |
| 555 | /* find an unused unit number */ |
| 556 | static int next_minor = 512; |
| 557 | int start = next_minor; |
| 558 | int is_free = 0; |
| 559 | int dev = 0; |
| 560 | while (!is_free) { |
| 561 | dev = MKDEV(MD_MAJOR, next_minor); |
| 562 | next_minor++; |
| 563 | if (next_minor > MINORMASK) |
| 564 | next_minor = 0; |
| 565 | if (next_minor == start) { |
| 566 | /* Oh dear, all in use. */ |
| 567 | spin_unlock(&all_mddevs_lock); |
| 568 | kfree(new); |
| 569 | return NULL; |
| 570 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 571 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 572 | is_free = 1; |
| 573 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 574 | if (mddev->unit == dev) { |
| 575 | is_free = 0; |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | new->unit = dev; |
| 580 | new->md_minor = MINOR(dev); |
| 581 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | list_add(&new->all_mddevs, &all_mddevs); |
| 583 | spin_unlock(&all_mddevs_lock); |
| 584 | return new; |
| 585 | } |
| 586 | spin_unlock(&all_mddevs_lock); |
| 587 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 588 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | if (!new) |
| 590 | return NULL; |
| 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | new->unit = unit; |
| 593 | if (MAJOR(unit) == MD_MAJOR) |
| 594 | new->md_minor = MINOR(unit); |
| 595 | else |
| 596 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 597 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 598 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | goto retry; |
| 601 | } |
| 602 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 603 | static struct attribute_group md_redundancy_group; |
| 604 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 605 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 607 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 608 | /* These cannot be removed under reconfig_mutex as |
| 609 | * an access to the files will try to take reconfig_mutex |
| 610 | * while holding the file unremovable, which leads to |
| 611 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 612 | * So hold set sysfs_active while the remove in happeing, |
| 613 | * and anything else which might set ->to_remove or my |
| 614 | * otherwise change the sysfs namespace will fail with |
| 615 | * -EBUSY if sysfs_active is still set. |
| 616 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 617 | * test it under the same mutex to ensure its correct value |
| 618 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 619 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 620 | struct attribute_group *to_remove = mddev->to_remove; |
| 621 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 622 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 623 | mutex_unlock(&mddev->reconfig_mutex); |
| 624 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 625 | if (mddev->kobj.sd) { |
| 626 | if (to_remove != &md_redundancy_group) |
| 627 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 628 | if (mddev->pers == NULL || |
| 629 | mddev->pers->sync_request == NULL) { |
| 630 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 631 | if (mddev->sysfs_action) |
| 632 | sysfs_put(mddev->sysfs_action); |
| 633 | mddev->sysfs_action = NULL; |
| 634 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 635 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 636 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 637 | } else |
| 638 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 640 | /* As we've dropped the mutex we need a spinlock to |
| 641 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 642 | */ |
| 643 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 644 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 645 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 647 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 649 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 650 | { |
| 651 | struct md_rdev *rdev; |
| 652 | |
| 653 | rdev_for_each_rcu(rdev, mddev) |
| 654 | if (rdev->desc_nr == nr) |
| 655 | return rdev; |
| 656 | |
| 657 | return NULL; |
| 658 | } |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 659 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 660 | |
| 661 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 663 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 665 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | if (rdev->bdev->bd_dev == dev) |
| 667 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | return NULL; |
| 670 | } |
| 671 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 672 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 673 | { |
| 674 | struct md_rdev *rdev; |
| 675 | |
| 676 | rdev_for_each_rcu(rdev, mddev) |
| 677 | if (rdev->bdev->bd_dev == dev) |
| 678 | return rdev; |
| 679 | |
| 680 | return NULL; |
| 681 | } |
| 682 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 683 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 684 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 685 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 686 | list_for_each_entry(pers, &pers_list, list) { |
| 687 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 688 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 689 | if (strcmp(pers->name, clevel)==0) |
| 690 | return pers; |
| 691 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 692 | return NULL; |
| 693 | } |
| 694 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 695 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 696 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 698 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 699 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | } |
| 701 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 702 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 705 | if (!rdev->sb_page) { |
| 706 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 707 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | return 0; |
| 711 | } |
| 712 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 713 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | { |
| 715 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 716 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | rdev->sb_loaded = 0; |
| 718 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 719 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 720 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 722 | if (rdev->bb_page) { |
| 723 | put_page(rdev->bb_page); |
| 724 | rdev->bb_page = NULL; |
| 725 | } |
NeilBrown | 4fa2f32 | 2012-05-22 13:55:01 +1000 | [diff] [blame] | 726 | kfree(rdev->badblocks.page); |
| 727 | rdev->badblocks.page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 729 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 731 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 732 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 733 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 734 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 735 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 736 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 737 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 738 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 739 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 740 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 741 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 742 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 743 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 744 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 745 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 746 | } |
| 747 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 748 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 749 | sector_t sector, int size, struct page *page) |
| 750 | { |
| 751 | /* write first size bytes of page to sector of rdev |
| 752 | * Increment mddev->pending_writes before returning |
| 753 | * and decrement it on completion, waking up sb_wait |
| 754 | * if zero is reached. |
| 755 | * If an error occurred, call md_error |
| 756 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 757 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 758 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 759 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 760 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 761 | bio_add_page(bio, page, size, 0); |
| 762 | bio->bi_private = rdev; |
| 763 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 764 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 765 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 766 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 767 | } |
| 768 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 769 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 770 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 771 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 772 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 773 | } |
| 774 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 775 | 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] | 776 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 778 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | int ret; |
| 780 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 781 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 782 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 783 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 784 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 785 | else if (rdev->mddev->reshape_position != MaxSector && |
| 786 | (rdev->mddev->reshape_backwards == |
| 787 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 788 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 789 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 790 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | bio_add_page(bio, page, size, 0); |
Kent Overstreet | c170bbb | 2013-11-24 16:32:22 -0700 | [diff] [blame] | 792 | submit_bio_wait(rw, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
| 794 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 795 | bio_put(bio); |
| 796 | return ret; |
| 797 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 798 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 800 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | { |
| 802 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | if (rdev->sb_loaded) |
| 805 | return 0; |
| 806 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 807 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | goto fail; |
| 809 | rdev->sb_loaded = 1; |
| 810 | return 0; |
| 811 | |
| 812 | fail: |
| 813 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 814 | bdevname(rdev->bdev,b)); |
| 815 | return -EINVAL; |
| 816 | } |
| 817 | |
| 818 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 819 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 820 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 821 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 822 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 823 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 827 | { |
| 828 | int ret; |
| 829 | mdp_super_t *tmp1, *tmp2; |
| 830 | |
| 831 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 832 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 833 | |
| 834 | if (!tmp1 || !tmp2) { |
| 835 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 836 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | goto abort; |
| 838 | } |
| 839 | |
| 840 | *tmp1 = *sb1; |
| 841 | *tmp2 = *sb2; |
| 842 | |
| 843 | /* |
| 844 | * nr_disks is not constant |
| 845 | */ |
| 846 | tmp1->nr_disks = 0; |
| 847 | tmp2->nr_disks = 0; |
| 848 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 849 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 851 | kfree(tmp1); |
| 852 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | return ret; |
| 854 | } |
| 855 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 856 | static u32 md_csum_fold(u32 csum) |
| 857 | { |
| 858 | csum = (csum & 0xffff) + (csum >> 16); |
| 859 | return (csum & 0xffff) + (csum >> 16); |
| 860 | } |
| 861 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 862 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 864 | u64 newcsum = 0; |
| 865 | u32 *sb32 = (u32*)sb; |
| 866 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | unsigned int disk_csum, csum; |
| 868 | |
| 869 | disk_csum = sb->sb_csum; |
| 870 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 871 | |
| 872 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 873 | newcsum += sb32[i]; |
| 874 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 875 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 876 | #ifdef CONFIG_ALPHA |
| 877 | /* This used to use csum_partial, which was wrong for several |
| 878 | * reasons including that different results are returned on |
| 879 | * different architectures. It isn't critical that we get exactly |
| 880 | * the same return value as before (we always csum_fold before |
| 881 | * testing, and that removes any differences). However as we |
| 882 | * know that csum_partial always returned a 16bit value on |
| 883 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 884 | */ |
| 885 | sb->sb_csum = md_csum_fold(disk_csum); |
| 886 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 888 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | return csum; |
| 890 | } |
| 891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | /* |
| 893 | * Handle superblock details. |
| 894 | * We want to be able to handle multiple superblock formats |
| 895 | * so we have a common interface to them all, and an array of |
| 896 | * different handlers. |
| 897 | * We rely on user-space to write the initial superblock, and support |
| 898 | * reading and updating of superblocks. |
| 899 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 900 | * 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] | 901 | * loads and validates a superblock on dev. |
| 902 | * if refdev != NULL, compare superblocks on both devices |
| 903 | * Return: |
| 904 | * 0 - dev has a superblock that is compatible with refdev |
| 905 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 906 | * so dev should be used as the refdev in future |
| 907 | * -EINVAL superblock incompatible or invalid |
| 908 | * -othererror e.g. -EIO |
| 909 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 910 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | * Verify that dev is acceptable into mddev. |
| 912 | * The first time, mddev->raid_disks will be 0, and data from |
| 913 | * dev should be merged in. Subsequent calls check that dev |
| 914 | * is new enough. Return 0 or -EINVAL |
| 915 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 916 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | * Update the superblock for rdev with data in mddev |
| 918 | * This does not write to disc. |
| 919 | * |
| 920 | */ |
| 921 | |
| 922 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 923 | char *name; |
| 924 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 925 | int (*load_super)(struct md_rdev *rdev, |
| 926 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 927 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 928 | int (*validate_super)(struct mddev *mddev, |
| 929 | struct md_rdev *rdev); |
| 930 | void (*sync_super)(struct mddev *mddev, |
| 931 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 932 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 933 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 934 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 935 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | }; |
| 937 | |
| 938 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 939 | * Check that the given mddev has no bitmap. |
| 940 | * |
| 941 | * This function is called from the run method of all personalities that do not |
| 942 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 943 | * has a bitmap. Otherwise, it returns 0. |
| 944 | * |
| 945 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 946 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 947 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 948 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 949 | return 0; |
| 950 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 951 | mdname(mddev), mddev->pers->name); |
| 952 | return 1; |
| 953 | } |
| 954 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 955 | |
| 956 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 957 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 959 | 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] | 960 | { |
| 961 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 962 | mdp_super_t *sb; |
| 963 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
| 965 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 966 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | * it's at the end of the disk. |
| 968 | * |
| 969 | * It also happens to be a multiple of 4Kb. |
| 970 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 971 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 973 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | if (ret) return ret; |
| 975 | |
| 976 | ret = -EINVAL; |
| 977 | |
| 978 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 979 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
| 981 | if (sb->md_magic != MD_SB_MAGIC) { |
| 982 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 983 | b); |
| 984 | goto abort; |
| 985 | } |
| 986 | |
| 987 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 988 | sb->minor_version < 90 || |
| 989 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 991 | sb->major_version, sb->minor_version, |
| 992 | b); |
| 993 | goto abort; |
| 994 | } |
| 995 | |
| 996 | if (sb->raid_disks <= 0) |
| 997 | goto abort; |
| 998 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 999 | 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] | 1000 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1001 | b); |
| 1002 | goto abort; |
| 1003 | } |
| 1004 | |
| 1005 | rdev->preferred_minor = sb->md_minor; |
| 1006 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1007 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1008 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1009 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | |
| 1011 | if (sb->level == LEVEL_MULTIPATH) |
| 1012 | rdev->desc_nr = -1; |
| 1013 | else |
| 1014 | rdev->desc_nr = sb->this_disk.number; |
| 1015 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1016 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1018 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1020 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | if (!uuid_equal(refsb, sb)) { |
| 1022 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1023 | b, bdevname(refdev->bdev,b2)); |
| 1024 | goto abort; |
| 1025 | } |
| 1026 | if (!sb_equal(refsb, sb)) { |
| 1027 | printk(KERN_WARNING "md: %s has same UUID" |
| 1028 | " but different superblock to %s\n", |
| 1029 | b, bdevname(refdev->bdev, b2)); |
| 1030 | goto abort; |
| 1031 | } |
| 1032 | ev1 = md_event(sb); |
| 1033 | ev2 = md_event(refsb); |
| 1034 | if (ev1 > ev2) |
| 1035 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1036 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | ret = 0; |
| 1038 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1039 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1040 | /* Limit to 4TB as metadata cannot record more than that. |
| 1041 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1042 | * record this size) |
| 1043 | */ |
| 1044 | if (rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1045 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1047 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1048 | /* "this cannot possibly happen" ... */ |
| 1049 | ret = -EINVAL; |
| 1050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | abort: |
| 1052 | return ret; |
| 1053 | } |
| 1054 | |
| 1055 | /* |
| 1056 | * validate_super for 0.90.0 |
| 1057 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1058 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | { |
| 1060 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1061 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1062 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1064 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1065 | clear_bit(Faulty, &rdev->flags); |
| 1066 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1067 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1068 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1069 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | if (mddev->raid_disks == 0) { |
| 1071 | mddev->major_version = 0; |
| 1072 | mddev->minor_version = sb->minor_version; |
| 1073 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1074 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1075 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | mddev->ctime = sb->ctime; |
| 1077 | mddev->utime = sb->utime; |
| 1078 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1079 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | mddev->layout = sb->layout; |
| 1081 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1082 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1083 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1084 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1085 | mddev->bitmap_info.space = 0; |
| 1086 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1087 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1088 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1089 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1091 | if (mddev->minor_version >= 91) { |
| 1092 | mddev->reshape_position = sb->reshape_position; |
| 1093 | mddev->delta_disks = sb->delta_disks; |
| 1094 | mddev->new_level = sb->new_level; |
| 1095 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1096 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1097 | if (mddev->delta_disks < 0) |
| 1098 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1099 | } else { |
| 1100 | mddev->reshape_position = MaxSector; |
| 1101 | mddev->delta_disks = 0; |
| 1102 | mddev->new_level = mddev->level; |
| 1103 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1104 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1105 | } |
| 1106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1108 | mddev->recovery_cp = MaxSector; |
| 1109 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1110 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | sb->events_lo == sb->cp_events_lo) { |
| 1112 | mddev->recovery_cp = sb->recovery_cp; |
| 1113 | } else |
| 1114 | mddev->recovery_cp = 0; |
| 1115 | } |
| 1116 | |
| 1117 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1118 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1119 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1120 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1121 | |
| 1122 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1123 | |
| 1124 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1125 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1126 | mddev->bitmap_info.offset = |
| 1127 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1128 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1129 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1130 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1131 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1132 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1133 | /* Insist on good event counter while assembling, except |
| 1134 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1136 | if (sb->disks[rdev->desc_nr].state & ( |
| 1137 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1138 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1139 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1140 | } else if (mddev->bitmap) { |
| 1141 | /* if adding to array with a bitmap, then we can accept an |
| 1142 | * older device ... but not too old. |
| 1143 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1144 | if (ev1 < mddev->bitmap->events_cleared) |
| 1145 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1146 | if (ev1 < mddev->events) |
| 1147 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1148 | } else { |
| 1149 | if (ev1 < mddev->events) |
| 1150 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1151 | return 0; |
| 1152 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | desc = sb->disks + rdev->desc_nr; |
| 1156 | |
| 1157 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1158 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1159 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1160 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1161 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1163 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1164 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1165 | /* active but not in sync implies recovery up to |
| 1166 | * reshape position. We don't know exactly where |
| 1167 | * that is, so set to zero for now */ |
| 1168 | if (mddev->minor_version >= 91) { |
| 1169 | rdev->recovery_offset = 0; |
| 1170 | rdev->raid_disk = desc->raid_disk; |
| 1171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1173 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1174 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1175 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1176 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | return 0; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * sync_super for 0.90.0 |
| 1182 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1183 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | { |
| 1185 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1186 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | /* make rdev->sb match mddev data.. |
| 1190 | * |
| 1191 | * 1/ zero out disks |
| 1192 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1193 | * 3/ any empty disks < next_spare become removed |
| 1194 | * |
| 1195 | * disks[0] gets initialised to REMOVED because |
| 1196 | * we cannot be sure from other fields if it has |
| 1197 | * been initialised or not. |
| 1198 | */ |
| 1199 | int i; |
| 1200 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1201 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1202 | rdev->sb_size = MD_SB_BYTES; |
| 1203 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1204 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | |
| 1206 | memset(sb, 0, sizeof(*sb)); |
| 1207 | |
| 1208 | sb->md_magic = MD_SB_MAGIC; |
| 1209 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | sb->patch_version = mddev->patch_version; |
| 1211 | sb->gvalid_words = 0; /* ignored */ |
| 1212 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1213 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1214 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1215 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1216 | |
| 1217 | sb->ctime = mddev->ctime; |
| 1218 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1219 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | sb->raid_disks = mddev->raid_disks; |
| 1221 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1222 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | sb->utime = mddev->utime; |
| 1224 | sb->state = 0; |
| 1225 | sb->events_hi = (mddev->events>>32); |
| 1226 | sb->events_lo = (u32)mddev->events; |
| 1227 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1228 | if (mddev->reshape_position == MaxSector) |
| 1229 | sb->minor_version = 90; |
| 1230 | else { |
| 1231 | sb->minor_version = 91; |
| 1232 | sb->reshape_position = mddev->reshape_position; |
| 1233 | sb->new_level = mddev->new_level; |
| 1234 | sb->delta_disks = mddev->delta_disks; |
| 1235 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1236 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1237 | } |
| 1238 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | if (mddev->in_sync) |
| 1240 | { |
| 1241 | sb->recovery_cp = mddev->recovery_cp; |
| 1242 | sb->cp_events_hi = (mddev->events>>32); |
| 1243 | sb->cp_events_lo = (u32)mddev->events; |
| 1244 | if (mddev->recovery_cp == MaxSector) |
| 1245 | sb->state = (1<< MD_SB_CLEAN); |
| 1246 | } else |
| 1247 | sb->recovery_cp = 0; |
| 1248 | |
| 1249 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1250 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1252 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1253 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1256 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1258 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1259 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1260 | |
| 1261 | if (rdev2->raid_disk >= 0 && |
| 1262 | sb->minor_version >= 91) |
| 1263 | /* we have nowhere to store the recovery_offset, |
| 1264 | * but if it is not below the reshape_position, |
| 1265 | * we can piggy-back on that. |
| 1266 | */ |
| 1267 | is_active = 1; |
| 1268 | if (rdev2->raid_disk < 0 || |
| 1269 | test_bit(Faulty, &rdev2->flags)) |
| 1270 | is_active = 0; |
| 1271 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1272 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1274 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1275 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | d = &sb->disks[rdev2->desc_nr]; |
| 1277 | nr_disks++; |
| 1278 | d->number = rdev2->desc_nr; |
| 1279 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1280 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1281 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | d->raid_disk = rdev2->raid_disk; |
| 1283 | else |
| 1284 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1285 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1287 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1289 | if (test_bit(In_sync, &rdev2->flags)) |
| 1290 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | active++; |
| 1292 | working++; |
| 1293 | } else { |
| 1294 | d->state = 0; |
| 1295 | spare++; |
| 1296 | working++; |
| 1297 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1298 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1299 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1302 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1303 | mdp_disk_t *d = &sb->disks[i]; |
| 1304 | if (d->state == 0 && d->number == 0) { |
| 1305 | d->number = i; |
| 1306 | d->raid_disk = i; |
| 1307 | d->state = (1<<MD_DISK_REMOVED); |
| 1308 | d->state |= (1<<MD_DISK_FAULTY); |
| 1309 | failed++; |
| 1310 | } |
| 1311 | } |
| 1312 | sb->nr_disks = nr_disks; |
| 1313 | sb->active_disks = active; |
| 1314 | sb->working_disks = working; |
| 1315 | sb->failed_disks = failed; |
| 1316 | sb->spare_disks = spare; |
| 1317 | |
| 1318 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1319 | sb->sb_csum = calc_sb_csum(sb); |
| 1320 | } |
| 1321 | |
| 1322 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1323 | * rdev_size_change for 0.90.0 |
| 1324 | */ |
| 1325 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1326 | 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] | 1327 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1328 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1329 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1330 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1331 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1332 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1333 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1334 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1335 | /* Limit to 4TB as metadata cannot record more than that. |
| 1336 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1337 | */ |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1338 | if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1339 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1340 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1341 | rdev->sb_page); |
| 1342 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1343 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1344 | } |
| 1345 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1346 | static int |
| 1347 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1348 | { |
| 1349 | /* non-zero offset changes not possible with v0.90 */ |
| 1350 | return new_offset == 0; |
| 1351 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1352 | |
| 1353 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | * version 1 superblock |
| 1355 | */ |
| 1356 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1357 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1359 | __le32 disk_csum; |
| 1360 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | unsigned long long newcsum; |
| 1362 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1363 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | |
| 1365 | disk_csum = sb->sb_csum; |
| 1366 | sb->sb_csum = 0; |
| 1367 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1368 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | newcsum += le32_to_cpu(*isuper++); |
| 1370 | |
| 1371 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1372 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
| 1374 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1375 | sb->sb_csum = disk_csum; |
| 1376 | return cpu_to_le32(csum); |
| 1377 | } |
| 1378 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1379 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1380 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1381 | 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] | 1382 | { |
| 1383 | struct mdp_superblock_1 *sb; |
| 1384 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1385 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1386 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1388 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | |
| 1390 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1391 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | * It is always aligned to a 4K boundary and |
| 1393 | * depeding on minor_version, it can be: |
| 1394 | * 0: At least 8K, but less than 12K, from end of device |
| 1395 | * 1: At start of device |
| 1396 | * 2: 4K from start of device. |
| 1397 | */ |
| 1398 | switch(minor_version) { |
| 1399 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1400 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1401 | sb_start -= 8*2; |
| 1402 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | break; |
| 1404 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1405 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | break; |
| 1407 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1408 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | break; |
| 1410 | default: |
| 1411 | return -EINVAL; |
| 1412 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1413 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1415 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1416 | * and it is safe to read 4k, so we do that |
| 1417 | */ |
| 1418 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | if (ret) return ret; |
| 1420 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1421 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | |
| 1423 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1424 | sb->major_version != cpu_to_le32(1) || |
| 1425 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1426 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1427 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | return -EINVAL; |
| 1429 | |
| 1430 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1431 | printk("md: invalid superblock checksum on %s\n", |
| 1432 | bdevname(rdev->bdev,b)); |
| 1433 | return -EINVAL; |
| 1434 | } |
| 1435 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1436 | printk("md: data_size too small on %s\n", |
| 1437 | bdevname(rdev->bdev,b)); |
| 1438 | return -EINVAL; |
| 1439 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1440 | if (sb->pad0 || |
| 1441 | sb->pad3[0] || |
| 1442 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1443 | /* Some padding is non-zero, might be a new feature */ |
| 1444 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | rdev->preferred_minor = 0xffff; |
| 1447 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1448 | rdev->new_data_offset = rdev->data_offset; |
| 1449 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1450 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1451 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1452 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1454 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1455 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1456 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1457 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1458 | |
| 1459 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1460 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1461 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1462 | if (minor_version |
| 1463 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1464 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1465 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1466 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1467 | rdev->desc_nr = -1; |
| 1468 | else |
| 1469 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1470 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1471 | if (!rdev->bb_page) { |
| 1472 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1473 | if (!rdev->bb_page) |
| 1474 | return -ENOMEM; |
| 1475 | } |
| 1476 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1477 | rdev->badblocks.count == 0) { |
| 1478 | /* need to load the bad block list. |
| 1479 | * Currently we limit it to one page. |
| 1480 | */ |
| 1481 | s32 offset; |
| 1482 | sector_t bb_sector; |
| 1483 | u64 *bbp; |
| 1484 | int i; |
| 1485 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1486 | if (sectors > (PAGE_SIZE / 512)) |
| 1487 | return -EINVAL; |
| 1488 | offset = le32_to_cpu(sb->bblog_offset); |
| 1489 | if (offset == 0) |
| 1490 | return -EINVAL; |
| 1491 | bb_sector = (long long)offset; |
| 1492 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1493 | rdev->bb_page, READ, true)) |
| 1494 | return -EIO; |
| 1495 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1496 | rdev->badblocks.shift = sb->bblog_shift; |
| 1497 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1498 | u64 bb = le64_to_cpu(*bbp); |
| 1499 | int count = bb & (0x3ff); |
| 1500 | u64 sector = bb >> 10; |
| 1501 | sector <<= sb->bblog_shift; |
| 1502 | count <<= sb->bblog_shift; |
| 1503 | if (bb + 1 == 0) |
| 1504 | break; |
| 1505 | if (md_set_badblocks(&rdev->badblocks, |
| 1506 | sector, count, 1) == 0) |
| 1507 | return -EINVAL; |
| 1508 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1509 | } else if (sb->bblog_offset != 0) |
| 1510 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1511 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1512 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1513 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1514 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1516 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | |
| 1518 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1519 | sb->level != refsb->level || |
| 1520 | sb->layout != refsb->layout || |
| 1521 | sb->chunksize != refsb->chunksize) { |
| 1522 | printk(KERN_WARNING "md: %s has strangely different" |
| 1523 | " superblock to %s\n", |
| 1524 | bdevname(rdev->bdev,b), |
| 1525 | bdevname(refdev->bdev,b2)); |
| 1526 | return -EINVAL; |
| 1527 | } |
| 1528 | ev1 = le64_to_cpu(sb->events); |
| 1529 | ev2 = le64_to_cpu(refsb->events); |
| 1530 | |
| 1531 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1532 | ret = 1; |
| 1533 | else |
| 1534 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1536 | if (minor_version) { |
| 1537 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1538 | sectors -= rdev->data_offset; |
| 1539 | } else |
| 1540 | sectors = rdev->sb_start; |
| 1541 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1543 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1544 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1547 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1549 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1550 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1552 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1553 | clear_bit(Faulty, &rdev->flags); |
| 1554 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1555 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1556 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | if (mddev->raid_disks == 0) { |
| 1559 | mddev->major_version = 1; |
| 1560 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1561 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1562 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1564 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1565 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1566 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | mddev->layout = le32_to_cpu(sb->layout); |
| 1568 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1569 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1570 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1571 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1572 | mddev->bitmap_info.space = 0; |
| 1573 | /* Default location for bitmap is 1K after superblock |
| 1574 | * using 3K - total of 4K |
| 1575 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1576 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1577 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1578 | mddev->reshape_backwards = 0; |
| 1579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1581 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1582 | |
| 1583 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1584 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1585 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1586 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1587 | mddev->bitmap_info.offset = |
| 1588 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1589 | /* Metadata doesn't record how much space is available. |
| 1590 | * For 1.0, we assume we can use up to the superblock |
| 1591 | * if before, else to 4K beyond superblock. |
| 1592 | * For others, assume no change is possible. |
| 1593 | */ |
| 1594 | if (mddev->minor_version > 0) |
| 1595 | mddev->bitmap_info.space = 0; |
| 1596 | else if (mddev->bitmap_info.offset > 0) |
| 1597 | mddev->bitmap_info.space = |
| 1598 | 8 - mddev->bitmap_info.offset; |
| 1599 | else |
| 1600 | mddev->bitmap_info.space = |
| 1601 | -mddev->bitmap_info.offset; |
| 1602 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1603 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1604 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1605 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1606 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1607 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1608 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1609 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1610 | if (mddev->delta_disks < 0 || |
| 1611 | (mddev->delta_disks == 0 && |
| 1612 | (le32_to_cpu(sb->feature_map) |
| 1613 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1614 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1615 | } else { |
| 1616 | mddev->reshape_position = MaxSector; |
| 1617 | mddev->delta_disks = 0; |
| 1618 | mddev->new_level = mddev->level; |
| 1619 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1620 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1621 | } |
| 1622 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1623 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1624 | /* Insist of good event counter while assembling, except for |
| 1625 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1627 | if (rdev->desc_nr >= 0 && |
| 1628 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1629 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1630 | if (ev1 < mddev->events) |
| 1631 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1632 | } else if (mddev->bitmap) { |
| 1633 | /* If adding to array with a bitmap, then we can accept an |
| 1634 | * older device, but not too old. |
| 1635 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1636 | if (ev1 < mddev->bitmap->events_cleared) |
| 1637 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1638 | if (ev1 < mddev->events) |
| 1639 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1640 | } else { |
| 1641 | if (ev1 < mddev->events) |
| 1642 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1643 | return 0; |
| 1644 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1646 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1647 | if (rdev->desc_nr < 0 || |
| 1648 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1649 | role = 0xffff; |
| 1650 | rdev->desc_nr = -1; |
| 1651 | } else |
| 1652 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | switch(role) { |
| 1654 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | break; |
| 1656 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1657 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | break; |
| 1659 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1660 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1661 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1662 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1663 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1664 | if (!(le32_to_cpu(sb->feature_map) & |
| 1665 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1666 | rdev->saved_raid_disk = -1; |
| 1667 | } else |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1668 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | rdev->raid_disk = role; |
| 1670 | break; |
| 1671 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1672 | if (sb->devflags & WriteMostly1) |
| 1673 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1674 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1675 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1676 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1677 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | return 0; |
| 1680 | } |
| 1681 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1682 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | { |
| 1684 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1685 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | int max_dev, i; |
| 1687 | /* make rdev->sb match mddev and rdev data. */ |
| 1688 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1689 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | |
| 1691 | sb->feature_map = 0; |
| 1692 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1693 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1695 | |
| 1696 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1697 | sb->events = cpu_to_le64(mddev->events); |
| 1698 | if (mddev->in_sync) |
| 1699 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1700 | else |
| 1701 | sb->resync_offset = cpu_to_le64(0); |
| 1702 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1703 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1704 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1705 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1706 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1707 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1708 | sb->level = cpu_to_le32(mddev->level); |
| 1709 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1710 | |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1711 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1712 | sb->devflags |= WriteMostly1; |
| 1713 | else |
| 1714 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1715 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1716 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1717 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1718 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1719 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1720 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1721 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1722 | |
| 1723 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1724 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1725 | sb->feature_map |= |
| 1726 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1727 | sb->recovery_offset = |
| 1728 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1729 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 1730 | sb->feature_map |= |
| 1731 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1732 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1733 | if (test_bit(Replacement, &rdev->flags)) |
| 1734 | sb->feature_map |= |
| 1735 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1736 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1737 | if (mddev->reshape_position != MaxSector) { |
| 1738 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1739 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1740 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1741 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1742 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1743 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1744 | if (mddev->delta_disks == 0 && |
| 1745 | mddev->reshape_backwards) |
| 1746 | sb->feature_map |
| 1747 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1748 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1749 | sb->feature_map |
| 1750 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1751 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1752 | - rdev->data_offset)); |
| 1753 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1754 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1755 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1756 | if (rdev->badblocks.count == 0) |
| 1757 | /* Nothing to do for bad blocks*/ ; |
| 1758 | else if (sb->bblog_offset == 0) |
| 1759 | /* Cannot record bad blocks on this device */ |
| 1760 | md_error(mddev, rdev); |
| 1761 | else { |
| 1762 | struct badblocks *bb = &rdev->badblocks; |
| 1763 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1764 | u64 *p = bb->page; |
| 1765 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1766 | if (bb->changed) { |
| 1767 | unsigned seq; |
| 1768 | |
| 1769 | retry: |
| 1770 | seq = read_seqbegin(&bb->lock); |
| 1771 | |
| 1772 | memset(bbp, 0xff, PAGE_SIZE); |
| 1773 | |
| 1774 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1775 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1776 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1777 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1778 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1779 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1780 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1781 | if (read_seqretry(&bb->lock, seq)) |
| 1782 | goto retry; |
| 1783 | |
| 1784 | bb->sector = (rdev->sb_start + |
| 1785 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1786 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1787 | } |
| 1788 | } |
| 1789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1791 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | if (rdev2->desc_nr+1 > max_dev) |
| 1793 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1794 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1795 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1796 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1797 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1798 | rdev->sb_size = max_dev * 2 + 256; |
| 1799 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1800 | if (rdev->sb_size & bmask) |
| 1801 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1802 | } else |
| 1803 | max_dev = le32_to_cpu(sb->max_dev); |
| 1804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | for (i=0; i<max_dev;i++) |
| 1806 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1807 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1808 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1810 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1812 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1814 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1815 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | else |
| 1817 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1818 | } |
| 1819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1821 | } |
| 1822 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1823 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1824 | 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] | 1825 | { |
| 1826 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1827 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1828 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1829 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1830 | if (rdev->data_offset != rdev->new_data_offset) |
| 1831 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1832 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1833 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1834 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1835 | max_sectors -= rdev->data_offset; |
| 1836 | if (!num_sectors || num_sectors > max_sectors) |
| 1837 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1838 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1839 | /* minor version 0 with bitmap we can't move */ |
| 1840 | return 0; |
| 1841 | } else { |
| 1842 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1843 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1844 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1845 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1846 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1847 | if (!num_sectors || num_sectors > max_sectors) |
| 1848 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1849 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1850 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1851 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1852 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1853 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1854 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1855 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1856 | rdev->sb_page); |
| 1857 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1858 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1859 | |
| 1860 | } |
| 1861 | |
| 1862 | static int |
| 1863 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1864 | unsigned long long new_offset) |
| 1865 | { |
| 1866 | /* All necessary checks on new >= old have been done */ |
| 1867 | struct bitmap *bitmap; |
| 1868 | if (new_offset >= rdev->data_offset) |
| 1869 | return 1; |
| 1870 | |
| 1871 | /* with 1.0 metadata, there is no metadata to tread on |
| 1872 | * so we can always move back */ |
| 1873 | if (rdev->mddev->minor_version == 0) |
| 1874 | return 1; |
| 1875 | |
| 1876 | /* otherwise we must be sure not to step on |
| 1877 | * any metadata, so stay: |
| 1878 | * 36K beyond start of superblock |
| 1879 | * beyond end of badblocks |
| 1880 | * beyond write-intent bitmap |
| 1881 | */ |
| 1882 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1883 | return 0; |
| 1884 | bitmap = rdev->mddev->bitmap; |
| 1885 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1886 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1887 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1888 | return 0; |
| 1889 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1890 | return 0; |
| 1891 | |
| 1892 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1893 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1895 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | [0] = { |
| 1897 | .name = "0.90.0", |
| 1898 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1899 | .load_super = super_90_load, |
| 1900 | .validate_super = super_90_validate, |
| 1901 | .sync_super = super_90_sync, |
| 1902 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1903 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | }, |
| 1905 | [1] = { |
| 1906 | .name = "md-1", |
| 1907 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1908 | .load_super = super_1_load, |
| 1909 | .validate_super = super_1_validate, |
| 1910 | .sync_super = super_1_sync, |
| 1911 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1912 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | }, |
| 1914 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1916 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1917 | { |
| 1918 | if (mddev->sync_super) { |
| 1919 | mddev->sync_super(mddev, rdev); |
| 1920 | return; |
| 1921 | } |
| 1922 | |
| 1923 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1924 | |
| 1925 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1926 | } |
| 1927 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1928 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1930 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1932 | rcu_read_lock(); |
| 1933 | rdev_for_each_rcu(rdev, mddev1) |
| 1934 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1935 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1936 | rdev2->bdev->bd_contains) { |
| 1937 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1938 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1939 | } |
| 1940 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | return 0; |
| 1942 | } |
| 1943 | |
| 1944 | static LIST_HEAD(pending_raid_disks); |
| 1945 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1946 | /* |
| 1947 | * Try to register data integrity profile for an mddev |
| 1948 | * |
| 1949 | * This is called when an array is started and after a disk has been kicked |
| 1950 | * from the array. It only succeeds if all working and active component devices |
| 1951 | * are integrity capable with matching profiles. |
| 1952 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1953 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1954 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1955 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1956 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1957 | if (list_empty(&mddev->disks)) |
| 1958 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1959 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1960 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1961 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1962 | /* skip spares and non-functional disks */ |
| 1963 | if (test_bit(Faulty, &rdev->flags)) |
| 1964 | continue; |
| 1965 | if (rdev->raid_disk < 0) |
| 1966 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1967 | if (!reference) { |
| 1968 | /* Use the first rdev as the reference */ |
| 1969 | reference = rdev; |
| 1970 | continue; |
| 1971 | } |
| 1972 | /* does this rdev's profile match the reference profile? */ |
| 1973 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1974 | rdev->bdev->bd_disk) < 0) |
| 1975 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1976 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1977 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1978 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1979 | /* |
| 1980 | * All component devices are integrity capable and have matching |
| 1981 | * profiles, register the common profile for the md device. |
| 1982 | */ |
| 1983 | if (blk_integrity_register(mddev->gendisk, |
| 1984 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1985 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1986 | mdname(mddev)); |
| 1987 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1988 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1989 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1990 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1991 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1992 | mdname(mddev)); |
| 1993 | return -EINVAL; |
| 1994 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1995 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1996 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1997 | EXPORT_SYMBOL(md_integrity_register); |
| 1998 | |
| 1999 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2000 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2001 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2002 | struct blk_integrity *bi_rdev; |
| 2003 | struct blk_integrity *bi_mddev; |
| 2004 | |
| 2005 | if (!mddev->gendisk) |
| 2006 | return; |
| 2007 | |
| 2008 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2009 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2010 | |
| 2011 | if (!bi_mddev) /* nothing to do */ |
| 2012 | return; |
| 2013 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2014 | return; |
| 2015 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2016 | rdev->bdev->bd_disk) >= 0) |
| 2017 | return; |
| 2018 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2019 | blk_integrity_unregister(mddev->gendisk); |
| 2020 | } |
| 2021 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2022 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2023 | 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] | 2024 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2025 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2026 | struct kobject *ko; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2027 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2029 | /* prevent duplicates */ |
| 2030 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2031 | return -EEXIST; |
| 2032 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2033 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2034 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2035 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2036 | if (mddev->pers) { |
| 2037 | /* Cannot change size, so fail |
| 2038 | * If mddev->level <= 0, then we don't care |
| 2039 | * about aligning sizes (e.g. linear) |
| 2040 | */ |
| 2041 | if (mddev->level > 0) |
| 2042 | return -ENOSPC; |
| 2043 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2044 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2045 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | |
| 2047 | /* Verify rdev->desc_nr is unique. |
| 2048 | * If it is -1, assign a free number, else |
| 2049 | * check number is not in use |
| 2050 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2051 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | if (rdev->desc_nr < 0) { |
| 2053 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2054 | if (mddev->pers) |
| 2055 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2056 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | choice++; |
| 2058 | rdev->desc_nr = choice; |
| 2059 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2060 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2061 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2063 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2065 | rcu_read_unlock(); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2066 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2067 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2068 | mdname(mddev), mddev->max_disks); |
| 2069 | return -EBUSY; |
| 2070 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2071 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2072 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2075 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2076 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2077 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2078 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2079 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2080 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2081 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2082 | /* failure here is OK */; |
| 2083 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2084 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2085 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2086 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2087 | |
| 2088 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2089 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2092 | |
| 2093 | fail: |
| 2094 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2095 | b, mdname(mddev)); |
| 2096 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | } |
| 2098 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2099 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2100 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2101 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2102 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2103 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2104 | } |
| 2105 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2106 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | { |
| 2108 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2109 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2110 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2111 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2113 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2114 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2115 | sysfs_put(rdev->sysfs_state); |
| 2116 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2117 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2118 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2119 | * writing to 'remove' to "dev/state". We also need |
| 2120 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2121 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2122 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2123 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2124 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2125 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | /* |
| 2129 | * prevent the device from being mounted, repartitioned or |
| 2130 | * otherwise reused by a RAID array (or any other kernel |
| 2131 | * subsystem), by bd_claiming the device. |
| 2132 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2133 | 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] | 2134 | { |
| 2135 | int err = 0; |
| 2136 | struct block_device *bdev; |
| 2137 | char b[BDEVNAME_SIZE]; |
| 2138 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2139 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2140 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | if (IS_ERR(bdev)) { |
| 2142 | printk(KERN_ERR "md: could not open %s.\n", |
| 2143 | __bdevname(dev, b)); |
| 2144 | return PTR_ERR(bdev); |
| 2145 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | rdev->bdev = bdev; |
| 2147 | return err; |
| 2148 | } |
| 2149 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2150 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | { |
| 2152 | struct block_device *bdev = rdev->bdev; |
| 2153 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2154 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | void md_autodetect_dev(dev_t dev); |
| 2158 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2159 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | { |
| 2161 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2164 | bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2165 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2167 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2168 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | #endif |
| 2170 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2171 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2174 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | { |
| 2176 | unbind_rdev_from_array(rdev); |
| 2177 | export_rdev(rdev); |
| 2178 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2179 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2181 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2183 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2185 | while (!list_empty(&mddev->disks)) { |
| 2186 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2187 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2188 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | mddev->raid_disks = 0; |
| 2191 | mddev->major_version = 0; |
| 2192 | } |
| 2193 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2194 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2196 | /* Update each superblock (in-memory image), but |
| 2197 | * if we are allowed to, skip spares which already |
| 2198 | * have the right event counter, or have one earlier |
| 2199 | * (which would mean they aren't being marked as dirty |
| 2200 | * with the rest of the array) |
| 2201 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2202 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2203 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2204 | if (rdev->sb_events == mddev->events || |
| 2205 | (nospares && |
| 2206 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2207 | rdev->sb_events+1 == mddev->events)) { |
| 2208 | /* Don't update this superblock */ |
| 2209 | rdev->sb_loaded = 2; |
| 2210 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2211 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2212 | rdev->sb_loaded = 1; |
| 2213 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | } |
| 2215 | } |
| 2216 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2217 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2219 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2220 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2221 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2222 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2224 | if (mddev->ro) { |
| 2225 | if (force_change) |
| 2226 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2227 | return; |
| 2228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2230 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2231 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2232 | if (rdev->raid_disk >= 0 && |
| 2233 | mddev->delta_disks >= 0 && |
| 2234 | !test_bit(In_sync, &rdev->flags) && |
| 2235 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2236 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2237 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2238 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2239 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2240 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2241 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2242 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2243 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2244 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2245 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2246 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2247 | md_ack_all_badblocks(&rdev->badblocks); |
| 2248 | md_error(mddev, rdev); |
| 2249 | } |
| 2250 | clear_bit(Blocked, &rdev->flags); |
| 2251 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2252 | wake_up(&rdev->blocked_wait); |
| 2253 | } |
| 2254 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2255 | wake_up(&mddev->sb_wait); |
| 2256 | return; |
| 2257 | } |
| 2258 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2259 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2260 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2261 | mddev->utime = get_seconds(); |
| 2262 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2263 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2264 | force_change = 1; |
| 2265 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2266 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2267 | * though if events isn't the right even/odd, we will have to do |
| 2268 | * spares after all |
| 2269 | */ |
| 2270 | nospares = 1; |
| 2271 | if (force_change) |
| 2272 | nospares = 0; |
| 2273 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2274 | /* If the array is degraded, then skipping spares is both |
| 2275 | * dangerous and fairly pointless. |
| 2276 | * Dangerous because a device that was removed from the array |
| 2277 | * might have a event_count that still looks up-to-date, |
| 2278 | * so it can be re-added without a resync. |
| 2279 | * Pointless because if there are any spares to skip, |
| 2280 | * then a recovery will happen and soon that array won't |
| 2281 | * be degraded any more and the spare can go back to sleep then. |
| 2282 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2283 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2284 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2285 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2286 | |
| 2287 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2288 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2289 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2290 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2291 | && mddev->can_decrease_events |
| 2292 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2293 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2294 | mddev->can_decrease_events = 0; |
| 2295 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2296 | /* otherwise we have to go forward and ... */ |
| 2297 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2298 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2301 | /* |
| 2302 | * This 64-bit counter should never wrap. |
| 2303 | * Either we are in around ~1 trillion A.C., assuming |
| 2304 | * 1 reboot per second, or we have a bug... |
| 2305 | */ |
| 2306 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2307 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2308 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2309 | if (rdev->badblocks.changed) |
| 2310 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2311 | if (test_bit(Faulty, &rdev->flags)) |
| 2312 | set_bit(FaultRecorded, &rdev->flags); |
| 2313 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2314 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2315 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2316 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2318 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2319 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2321 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2322 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2324 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2325 | if (rdev->sb_loaded != 1) |
| 2326 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2328 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2329 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2330 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2331 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2332 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2333 | bdevname(rdev->bdev, b), |
| 2334 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2335 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2336 | if (rdev->badblocks.size) { |
| 2337 | md_super_write(mddev, rdev, |
| 2338 | rdev->badblocks.sector, |
| 2339 | rdev->badblocks.size << 9, |
| 2340 | rdev->bb_page); |
| 2341 | rdev->badblocks.size = 0; |
| 2342 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2343 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2344 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2345 | pr_debug("md: %s (skipping faulty)\n", |
| 2346 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2347 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2348 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | /* only need to write one superblock... */ |
| 2350 | break; |
| 2351 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2352 | md_super_wait(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2353 | /* 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] | 2354 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2355 | spin_lock(&mddev->lock); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2356 | if (mddev->in_sync != sync_req || |
| 2357 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2358 | /* have to write it out again */ |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2359 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2360 | goto repeat; |
| 2361 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2362 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2363 | spin_unlock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2364 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2365 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2366 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2367 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2368 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2369 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2370 | clear_bit(Blocked, &rdev->flags); |
| 2371 | |
| 2372 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2373 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2374 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2375 | wake_up(&rdev->blocked_wait); |
| 2376 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2378 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2380 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2381 | { |
| 2382 | struct mddev *mddev = rdev->mddev; |
| 2383 | int err = 0; |
| 2384 | |
| 2385 | if (!mddev->pers->hot_remove_disk) { |
| 2386 | /* If there is hot_add_disk but no hot_remove_disk |
| 2387 | * then added disks for geometry changes, |
| 2388 | * and should be added immediately. |
| 2389 | */ |
| 2390 | super_types[mddev->major_version]. |
| 2391 | validate_super(mddev, rdev); |
| 2392 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 2393 | if (err) { |
| 2394 | unbind_rdev_from_array(rdev); |
| 2395 | export_rdev(rdev); |
| 2396 | return err; |
| 2397 | } |
| 2398 | } |
| 2399 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2400 | |
| 2401 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2402 | if (mddev->degraded) |
| 2403 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2404 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2405 | md_new_event(mddev); |
| 2406 | md_wakeup_thread(mddev->thread); |
| 2407 | return 0; |
| 2408 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2410 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2411 | * We want to accept with case. For this we use cmd_match. |
| 2412 | */ |
| 2413 | static int cmd_match(const char *cmd, const char *str) |
| 2414 | { |
| 2415 | /* See if cmd, written into a sysfs file, matches |
| 2416 | * str. They must either be the same, or cmd can |
| 2417 | * have a trailing newline |
| 2418 | */ |
| 2419 | while (*cmd && *str && *cmd == *str) { |
| 2420 | cmd++; |
| 2421 | str++; |
| 2422 | } |
| 2423 | if (*cmd == '\n') |
| 2424 | cmd++; |
| 2425 | if (*str || *cmd) |
| 2426 | return 0; |
| 2427 | return 1; |
| 2428 | } |
| 2429 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2430 | struct rdev_sysfs_entry { |
| 2431 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2432 | ssize_t (*show)(struct md_rdev *, char *); |
| 2433 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2434 | }; |
| 2435 | |
| 2436 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2437 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2438 | { |
| 2439 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2440 | size_t len = 0; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2441 | unsigned long flags = ACCESS_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2442 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2443 | if (test_bit(Faulty, &flags) || |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2444 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2445 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2446 | sep = ","; |
| 2447 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2448 | if (test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2449 | len += sprintf(page+len, "%sin_sync",sep); |
| 2450 | sep = ","; |
| 2451 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2452 | if (test_bit(WriteMostly, &flags)) { |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2453 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2454 | sep = ","; |
| 2455 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2456 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2457 | (rdev->badblocks.unacked_exist |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2458 | && !test_bit(Faulty, &flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2459 | len += sprintf(page+len, "%sblocked", sep); |
| 2460 | sep = ","; |
| 2461 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2462 | if (!test_bit(Faulty, &flags) && |
| 2463 | !test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2464 | len += sprintf(page+len, "%sspare", sep); |
| 2465 | sep = ","; |
| 2466 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2467 | if (test_bit(WriteErrorSeen, &flags)) { |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2468 | len += sprintf(page+len, "%swrite_error", sep); |
| 2469 | sep = ","; |
| 2470 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2471 | if (test_bit(WantReplacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2472 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2473 | sep = ","; |
| 2474 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2475 | if (test_bit(Replacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2476 | len += sprintf(page+len, "%sreplacement", sep); |
| 2477 | sep = ","; |
| 2478 | } |
| 2479 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2480 | return len+sprintf(page+len, "\n"); |
| 2481 | } |
| 2482 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2483 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2484 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2485 | { |
| 2486 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2487 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2488 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2489 | * writemostly - sets write_mostly |
| 2490 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2491 | * blocked - sets the Blocked flags |
| 2492 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2493 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2494 | * -insync - clear Insync for a device with a slot assigned, |
| 2495 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2496 | * write_error - sets WriteErrorSeen |
| 2497 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2498 | */ |
| 2499 | int err = -EINVAL; |
| 2500 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2501 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2502 | if (test_bit(Faulty, &rdev->flags)) |
| 2503 | err = 0; |
| 2504 | else |
| 2505 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2506 | } else if (cmd_match(buf, "remove")) { |
| 2507 | if (rdev->raid_disk >= 0) |
| 2508 | err = -EBUSY; |
| 2509 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2510 | struct mddev *mddev = rdev->mddev; |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 2511 | if (mddev_is_clustered(mddev)) |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 2512 | md_cluster_ops->remove_disk(mddev, rdev); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2513 | md_kick_rdev_from_array(rdev); |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 2514 | if (mddev_is_clustered(mddev)) |
| 2515 | md_cluster_ops->metadata_update_start(mddev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2516 | if (mddev->pers) |
| 2517 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2518 | md_new_event(mddev); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 2519 | if (mddev_is_clustered(mddev)) |
| 2520 | md_cluster_ops->metadata_update_finish(mddev); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2521 | err = 0; |
| 2522 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2523 | } else if (cmd_match(buf, "writemostly")) { |
| 2524 | set_bit(WriteMostly, &rdev->flags); |
| 2525 | err = 0; |
| 2526 | } else if (cmd_match(buf, "-writemostly")) { |
| 2527 | clear_bit(WriteMostly, &rdev->flags); |
| 2528 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2529 | } else if (cmd_match(buf, "blocked")) { |
| 2530 | set_bit(Blocked, &rdev->flags); |
| 2531 | err = 0; |
| 2532 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2533 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2534 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2535 | /* metadata handler doesn't understand badblocks, |
| 2536 | * so we need to fail the device |
| 2537 | */ |
| 2538 | md_error(rdev->mddev, rdev); |
| 2539 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2540 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2541 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2542 | wake_up(&rdev->blocked_wait); |
| 2543 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2544 | md_wakeup_thread(rdev->mddev->thread); |
| 2545 | |
| 2546 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2547 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2548 | set_bit(In_sync, &rdev->flags); |
| 2549 | err = 0; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2550 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 2551 | if (rdev->mddev->pers == NULL) { |
| 2552 | clear_bit(In_sync, &rdev->flags); |
| 2553 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2554 | rdev->raid_disk = -1; |
| 2555 | err = 0; |
| 2556 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2557 | } else if (cmd_match(buf, "write_error")) { |
| 2558 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2559 | err = 0; |
| 2560 | } else if (cmd_match(buf, "-write_error")) { |
| 2561 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2562 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2563 | } else if (cmd_match(buf, "want_replacement")) { |
| 2564 | /* Any non-spare device that is not a replacement can |
| 2565 | * become want_replacement at any time, but we then need to |
| 2566 | * check if recovery is needed. |
| 2567 | */ |
| 2568 | if (rdev->raid_disk >= 0 && |
| 2569 | !test_bit(Replacement, &rdev->flags)) |
| 2570 | set_bit(WantReplacement, &rdev->flags); |
| 2571 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2572 | md_wakeup_thread(rdev->mddev->thread); |
| 2573 | err = 0; |
| 2574 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2575 | /* Clearing 'want_replacement' is always allowed. |
| 2576 | * Once replacements starts it is too late though. |
| 2577 | */ |
| 2578 | err = 0; |
| 2579 | clear_bit(WantReplacement, &rdev->flags); |
| 2580 | } else if (cmd_match(buf, "replacement")) { |
| 2581 | /* Can only set a device as a replacement when array has not |
| 2582 | * yet been started. Once running, replacement is automatic |
| 2583 | * from spares, or by assigning 'slot'. |
| 2584 | */ |
| 2585 | if (rdev->mddev->pers) |
| 2586 | err = -EBUSY; |
| 2587 | else { |
| 2588 | set_bit(Replacement, &rdev->flags); |
| 2589 | err = 0; |
| 2590 | } |
| 2591 | } else if (cmd_match(buf, "-replacement")) { |
| 2592 | /* Similarly, can only clear Replacement before start */ |
| 2593 | if (rdev->mddev->pers) |
| 2594 | err = -EBUSY; |
| 2595 | else { |
| 2596 | clear_bit(Replacement, &rdev->flags); |
| 2597 | err = 0; |
| 2598 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2599 | } else if (cmd_match(buf, "re-add")) { |
| 2600 | if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 2601 | /* clear_bit is performed _after_ all the devices |
| 2602 | * have their local Faulty bit cleared. If any writes |
| 2603 | * happen in the meantime in the local node, they |
| 2604 | * will land in the local bitmap, which will be synced |
| 2605 | * by this node eventually |
| 2606 | */ |
| 2607 | if (!mddev_is_clustered(rdev->mddev) || |
| 2608 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 2609 | clear_bit(Faulty, &rdev->flags); |
| 2610 | err = add_bound_rdev(rdev); |
| 2611 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2612 | } else |
| 2613 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2614 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2615 | if (!err) |
| 2616 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2617 | return err ? err : len; |
| 2618 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2619 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 2620 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2621 | |
| 2622 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2623 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2624 | { |
| 2625 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2626 | } |
| 2627 | |
| 2628 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2629 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2630 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2631 | unsigned int n; |
| 2632 | int rv; |
| 2633 | |
| 2634 | rv = kstrtouint(buf, 10, &n); |
| 2635 | if (rv < 0) |
| 2636 | return rv; |
| 2637 | atomic_set(&rdev->corrected_errors, n); |
| 2638 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2639 | } |
| 2640 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2641 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2642 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2643 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2644 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2645 | { |
| 2646 | if (rdev->raid_disk < 0) |
| 2647 | return sprintf(page, "none\n"); |
| 2648 | else |
| 2649 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2650 | } |
| 2651 | |
| 2652 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2653 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2654 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2655 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2656 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2657 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2658 | if (strncmp(buf, "none", 4)==0) |
| 2659 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2660 | else { |
| 2661 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 2662 | if (err < 0) |
| 2663 | return err; |
| 2664 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2665 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2666 | /* Setting 'slot' on an active array requires also |
| 2667 | * updating the 'rd%d' link, and communicating |
| 2668 | * with the personality with ->hot_*_disk. |
| 2669 | * For now we only support removing |
| 2670 | * failed/spare devices. This normally happens automatically, |
| 2671 | * but not when the metadata is externally managed. |
| 2672 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2673 | if (rdev->raid_disk == -1) |
| 2674 | return -EEXIST; |
| 2675 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2676 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2677 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2678 | clear_bit(Blocked, &rdev->flags); |
| 2679 | remove_and_add_spares(rdev->mddev, rdev); |
| 2680 | if (rdev->raid_disk >= 0) |
| 2681 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2682 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2683 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2684 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2685 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2686 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2687 | */ |
| 2688 | |
| 2689 | if (rdev->raid_disk != -1) |
| 2690 | return -EBUSY; |
| 2691 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2692 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2693 | return -EBUSY; |
| 2694 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2695 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2696 | return -EINVAL; |
| 2697 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2698 | if (slot >= rdev->mddev->raid_disks && |
| 2699 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2700 | return -ENOSPC; |
| 2701 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2702 | rdev->raid_disk = slot; |
| 2703 | if (test_bit(In_sync, &rdev->flags)) |
| 2704 | rdev->saved_raid_disk = slot; |
| 2705 | else |
| 2706 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2707 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 2708 | clear_bit(Bitmap_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2709 | err = rdev->mddev->pers-> |
| 2710 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2711 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2712 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2713 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2714 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2715 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2716 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2717 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2718 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2719 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2720 | if (slot >= rdev->mddev->raid_disks && |
| 2721 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2722 | return -ENOSPC; |
| 2723 | rdev->raid_disk = slot; |
| 2724 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2725 | clear_bit(Faulty, &rdev->flags); |
| 2726 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2727 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2728 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2729 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2730 | return len; |
| 2731 | } |
| 2732 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2733 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2734 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2735 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2736 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2737 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2738 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2739 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2743 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2744 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2745 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2746 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2747 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2748 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2749 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2750 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2751 | /* Must set offset before size, so overlap checks |
| 2752 | * can be sane */ |
| 2753 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2754 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2755 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2756 | return len; |
| 2757 | } |
| 2758 | |
| 2759 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2760 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2761 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2762 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2763 | { |
| 2764 | return sprintf(page, "%llu\n", |
| 2765 | (unsigned long long)rdev->new_data_offset); |
| 2766 | } |
| 2767 | |
| 2768 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2769 | const char *buf, size_t len) |
| 2770 | { |
| 2771 | unsigned long long new_offset; |
| 2772 | struct mddev *mddev = rdev->mddev; |
| 2773 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2774 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2775 | return -EINVAL; |
| 2776 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 2777 | if (mddev->sync_thread || |
| 2778 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2779 | return -EBUSY; |
| 2780 | if (new_offset == rdev->data_offset) |
| 2781 | /* reset is always permitted */ |
| 2782 | ; |
| 2783 | else if (new_offset > rdev->data_offset) { |
| 2784 | /* must not push array size beyond rdev_sectors */ |
| 2785 | if (new_offset - rdev->data_offset |
| 2786 | + mddev->dev_sectors > rdev->sectors) |
| 2787 | return -E2BIG; |
| 2788 | } |
| 2789 | /* Metadata worries about other space details. */ |
| 2790 | |
| 2791 | /* decreasing the offset is inconsistent with a backwards |
| 2792 | * reshape. |
| 2793 | */ |
| 2794 | if (new_offset < rdev->data_offset && |
| 2795 | mddev->reshape_backwards) |
| 2796 | return -EINVAL; |
| 2797 | /* Increasing offset is inconsistent with forwards |
| 2798 | * reshape. reshape_direction should be set to |
| 2799 | * 'backwards' first. |
| 2800 | */ |
| 2801 | if (new_offset > rdev->data_offset && |
| 2802 | !mddev->reshape_backwards) |
| 2803 | return -EINVAL; |
| 2804 | |
| 2805 | if (mddev->pers && mddev->persistent && |
| 2806 | !super_types[mddev->major_version] |
| 2807 | .allow_new_offset(rdev, new_offset)) |
| 2808 | return -E2BIG; |
| 2809 | rdev->new_data_offset = new_offset; |
| 2810 | if (new_offset > rdev->data_offset) |
| 2811 | mddev->reshape_backwards = 1; |
| 2812 | else if (new_offset < rdev->data_offset) |
| 2813 | mddev->reshape_backwards = 0; |
| 2814 | |
| 2815 | return len; |
| 2816 | } |
| 2817 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2818 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2819 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2820 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2821 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2822 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2823 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2824 | } |
| 2825 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2826 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2827 | { |
| 2828 | /* check if two start/length pairs overlap */ |
| 2829 | if (s1+l1 <= s2) |
| 2830 | return 0; |
| 2831 | if (s2+l2 <= s1) |
| 2832 | return 0; |
| 2833 | return 1; |
| 2834 | } |
| 2835 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2836 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2837 | { |
| 2838 | unsigned long long blocks; |
| 2839 | sector_t new; |
| 2840 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2841 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2842 | return -EINVAL; |
| 2843 | |
| 2844 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2845 | return -EINVAL; /* sector conversion overflow */ |
| 2846 | |
| 2847 | new = blocks * 2; |
| 2848 | if (new != blocks * 2) |
| 2849 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2850 | |
| 2851 | *sectors = new; |
| 2852 | return 0; |
| 2853 | } |
| 2854 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2855 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2856 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2857 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2858 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2859 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2860 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2861 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2862 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2863 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2864 | if (rdev->data_offset != rdev->new_data_offset) |
| 2865 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2866 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2867 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2868 | sectors = super_types[my_mddev->major_version]. |
| 2869 | rdev_size_change(rdev, sectors); |
| 2870 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2871 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2872 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2873 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2874 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 2875 | if (!my_mddev->pers->resize) |
| 2876 | /* Cannot change size for RAID0 or Linear etc */ |
| 2877 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2878 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2879 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2880 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2881 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2882 | rdev->sectors = sectors; |
| 2883 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2884 | /* Need to check that all other rdevs with the same |
| 2885 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 2886 | * the rdev lists safely. |
| 2887 | * This check does not provide a hard guarantee, it |
| 2888 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2889 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2890 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2891 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2892 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2893 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2894 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2895 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2896 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2897 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2898 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2899 | if (rdev->bdev == rdev2->bdev && |
| 2900 | rdev != rdev2 && |
| 2901 | overlaps(rdev->data_offset, rdev->sectors, |
| 2902 | rdev2->data_offset, |
| 2903 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2904 | overlap = 1; |
| 2905 | break; |
| 2906 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2907 | if (overlap) { |
| 2908 | mddev_put(mddev); |
| 2909 | break; |
| 2910 | } |
| 2911 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2912 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2913 | if (overlap) { |
| 2914 | /* Someone else could have slipped in a size |
| 2915 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2916 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2917 | * safe, and trust userspace not to race with |
| 2918 | * itself |
| 2919 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2920 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2921 | return -EBUSY; |
| 2922 | } |
| 2923 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2924 | return len; |
| 2925 | } |
| 2926 | |
| 2927 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2928 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2929 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2930 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2931 | { |
| 2932 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2933 | |
| 2934 | if (test_bit(In_sync, &rdev->flags) || |
| 2935 | recovery_start == MaxSector) |
| 2936 | return sprintf(page, "none\n"); |
| 2937 | |
| 2938 | return sprintf(page, "%llu\n", recovery_start); |
| 2939 | } |
| 2940 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2941 | 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] | 2942 | { |
| 2943 | unsigned long long recovery_start; |
| 2944 | |
| 2945 | if (cmd_match(buf, "none")) |
| 2946 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2947 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2948 | return -EINVAL; |
| 2949 | |
| 2950 | if (rdev->mddev->pers && |
| 2951 | rdev->raid_disk >= 0) |
| 2952 | return -EBUSY; |
| 2953 | |
| 2954 | rdev->recovery_offset = recovery_start; |
| 2955 | if (recovery_start == MaxSector) |
| 2956 | set_bit(In_sync, &rdev->flags); |
| 2957 | else |
| 2958 | clear_bit(In_sync, &rdev->flags); |
| 2959 | return len; |
| 2960 | } |
| 2961 | |
| 2962 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2963 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2964 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2965 | static ssize_t |
| 2966 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 2967 | static ssize_t |
| 2968 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 2969 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2970 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2971 | { |
| 2972 | return badblocks_show(&rdev->badblocks, page, 0); |
| 2973 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2974 | 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] | 2975 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2976 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 2977 | /* Maybe that ack was all we needed */ |
| 2978 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 2979 | wake_up(&rdev->blocked_wait); |
| 2980 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2981 | } |
| 2982 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 2983 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 2984 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2985 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2986 | { |
| 2987 | return badblocks_show(&rdev->badblocks, page, 1); |
| 2988 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2989 | 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] | 2990 | { |
| 2991 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 2992 | } |
| 2993 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 2994 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 2995 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2996 | static struct attribute *rdev_default_attrs[] = { |
| 2997 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2998 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2999 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3000 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3001 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3002 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3003 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3004 | &rdev_bad_blocks.attr, |
| 3005 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3006 | NULL, |
| 3007 | }; |
| 3008 | static ssize_t |
| 3009 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3010 | { |
| 3011 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3012 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3013 | |
| 3014 | if (!entry->show) |
| 3015 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3016 | if (!rdev->mddev) |
| 3017 | return -EBUSY; |
| 3018 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | static ssize_t |
| 3022 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3023 | const char *page, size_t length) |
| 3024 | { |
| 3025 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3026 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3027 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3028 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3029 | |
| 3030 | if (!entry->store) |
| 3031 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3032 | if (!capable(CAP_SYS_ADMIN)) |
| 3033 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3034 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3035 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3036 | if (rdev->mddev == NULL) |
| 3037 | rv = -EBUSY; |
| 3038 | else |
| 3039 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3040 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3041 | } |
| 3042 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | static void rdev_free(struct kobject *ko) |
| 3046 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3047 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3048 | kfree(rdev); |
| 3049 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3050 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3051 | .show = rdev_attr_show, |
| 3052 | .store = rdev_attr_store, |
| 3053 | }; |
| 3054 | static struct kobj_type rdev_ktype = { |
| 3055 | .release = rdev_free, |
| 3056 | .sysfs_ops = &rdev_sysfs_ops, |
| 3057 | .default_attrs = rdev_default_attrs, |
| 3058 | }; |
| 3059 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3060 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3061 | { |
| 3062 | rdev->desc_nr = -1; |
| 3063 | rdev->saved_raid_disk = -1; |
| 3064 | rdev->raid_disk = -1; |
| 3065 | rdev->flags = 0; |
| 3066 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3067 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3068 | rdev->sb_events = 0; |
| 3069 | rdev->last_read_error.tv_sec = 0; |
| 3070 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3071 | rdev->sb_loaded = 0; |
| 3072 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3073 | atomic_set(&rdev->nr_pending, 0); |
| 3074 | atomic_set(&rdev->read_errors, 0); |
| 3075 | atomic_set(&rdev->corrected_errors, 0); |
| 3076 | |
| 3077 | INIT_LIST_HEAD(&rdev->same_set); |
| 3078 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3079 | |
| 3080 | /* Add space to store bad block list. |
| 3081 | * This reserves the space even on arrays where it cannot |
| 3082 | * be used - I wonder if that matters |
| 3083 | */ |
| 3084 | rdev->badblocks.count = 0; |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 3085 | rdev->badblocks.shift = -1; /* disabled until explicitly enabled */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3086 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3087 | seqlock_init(&rdev->badblocks.lock); |
| 3088 | if (rdev->badblocks.page == NULL) |
| 3089 | return -ENOMEM; |
| 3090 | |
| 3091 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3092 | } |
| 3093 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | /* |
| 3095 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3096 | * |
| 3097 | * mark the device faulty if: |
| 3098 | * |
| 3099 | * - the device is nonexistent (zero size) |
| 3100 | * - the device has no valid superblock |
| 3101 | * |
| 3102 | * a faulty rdev _never_ has rdev->sb set. |
| 3103 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3104 | 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] | 3105 | { |
| 3106 | char b[BDEVNAME_SIZE]; |
| 3107 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3108 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3109 | sector_t size; |
| 3110 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3111 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | if (!rdev) { |
| 3113 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3114 | return ERR_PTR(-ENOMEM); |
| 3115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3117 | err = md_rdev_init(rdev); |
| 3118 | if (err) |
| 3119 | goto abort_free; |
| 3120 | err = alloc_disk_sb(rdev); |
| 3121 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | goto abort_free; |
| 3123 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3124 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | if (err) |
| 3126 | goto abort_free; |
| 3127 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3128 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3129 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3130 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | if (!size) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3132 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3134 | bdevname(rdev->bdev,b)); |
| 3135 | err = -EINVAL; |
| 3136 | goto abort_free; |
| 3137 | } |
| 3138 | |
| 3139 | if (super_format >= 0) { |
| 3140 | err = super_types[super_format]. |
| 3141 | load_super(rdev, NULL, super_minor); |
| 3142 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3143 | printk(KERN_WARNING |
| 3144 | "md: %s does not have a valid v%d.%d " |
| 3145 | "superblock, not importing!\n", |
| 3146 | bdevname(rdev->bdev,b), |
| 3147 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | goto abort_free; |
| 3149 | } |
| 3150 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3151 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | "md: could not read %s's sb, not importing!\n", |
| 3153 | bdevname(rdev->bdev,b)); |
| 3154 | goto abort_free; |
| 3155 | } |
| 3156 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3158 | return rdev; |
| 3159 | |
| 3160 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3161 | if (rdev->bdev) |
| 3162 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3163 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | kfree(rdev); |
| 3165 | return ERR_PTR(err); |
| 3166 | } |
| 3167 | |
| 3168 | /* |
| 3169 | * Check a full RAID array for plausibility |
| 3170 | */ |
| 3171 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3172 | static void analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3173 | { |
| 3174 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3175 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | char b[BDEVNAME_SIZE]; |
| 3177 | |
| 3178 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3179 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3180 | switch (super_types[mddev->major_version]. |
| 3181 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3182 | case 1: |
| 3183 | freshest = rdev; |
| 3184 | break; |
| 3185 | case 0: |
| 3186 | break; |
| 3187 | default: |
| 3188 | printk( KERN_ERR \ |
| 3189 | "md: fatal superblock inconsistency in %s" |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3190 | " -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3192 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | } |
| 3194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | super_types[mddev->major_version]. |
| 3196 | validate_super(mddev, freshest); |
| 3197 | |
| 3198 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3199 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3200 | if (mddev->max_disks && |
| 3201 | (rdev->desc_nr >= mddev->max_disks || |
| 3202 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3203 | printk(KERN_WARNING |
| 3204 | "md: %s: %s: only %d devices permitted\n", |
| 3205 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3206 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3207 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3208 | continue; |
| 3209 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3210 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3211 | if (super_types[mddev->major_version]. |
| 3212 | validate_super(mddev, rdev)) { |
| 3213 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3214 | " from array!\n", |
| 3215 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3216 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | continue; |
| 3218 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3219 | /* No device should have a Candidate flag |
| 3220 | * when reading devices |
| 3221 | */ |
| 3222 | if (test_bit(Candidate, &rdev->flags)) { |
| 3223 | pr_info("md: kicking Cluster Candidate %s from array!\n", |
| 3224 | bdevname(rdev->bdev, b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3225 | md_kick_rdev_from_array(rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3226 | } |
| 3227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3228 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3229 | rdev->desc_nr = i++; |
| 3230 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3231 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3232 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3233 | rdev->raid_disk = -1; |
| 3234 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | } |
| 3236 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3237 | } |
| 3238 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3239 | /* Read a fixed-point number. |
| 3240 | * Numbers in sysfs attributes should be in "standard" units where |
| 3241 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3242 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3243 | * milliseconds or jiffies. |
| 3244 | * This function takes a decimal number with a possible fractional |
| 3245 | * component, and produces an integer which is the result of |
| 3246 | * multiplying that number by 10^'scale'. |
| 3247 | * all without any floating-point arithmetic. |
| 3248 | */ |
| 3249 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3250 | { |
| 3251 | unsigned long result = 0; |
| 3252 | long decimals = -1; |
| 3253 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3254 | if (*cp == '.') |
| 3255 | decimals = 0; |
| 3256 | else if (decimals < scale) { |
| 3257 | unsigned int value; |
| 3258 | value = *cp - '0'; |
| 3259 | result = result * 10 + value; |
| 3260 | if (decimals >= 0) |
| 3261 | decimals++; |
| 3262 | } |
| 3263 | cp++; |
| 3264 | } |
| 3265 | if (*cp == '\n') |
| 3266 | cp++; |
| 3267 | if (*cp) |
| 3268 | return -EINVAL; |
| 3269 | if (decimals < 0) |
| 3270 | decimals = 0; |
| 3271 | while (decimals < scale) { |
| 3272 | result *= 10; |
| 3273 | decimals ++; |
| 3274 | } |
| 3275 | *res = result; |
| 3276 | return 0; |
| 3277 | } |
| 3278 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3279 | static void md_safemode_timeout(unsigned long data); |
| 3280 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3281 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3282 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3283 | { |
| 3284 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3285 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3286 | } |
| 3287 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3288 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3289 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3290 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3291 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3292 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3293 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3294 | if (msec == 0) |
| 3295 | mddev->safemode_delay = 0; |
| 3296 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3297 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3298 | unsigned long new_delay = (msec*HZ)/1000; |
| 3299 | |
| 3300 | if (new_delay == 0) |
| 3301 | new_delay = 1; |
| 3302 | mddev->safemode_delay = new_delay; |
| 3303 | if (new_delay < old_delay || old_delay == 0) |
| 3304 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3305 | } |
| 3306 | return len; |
| 3307 | } |
| 3308 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3309 | __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] | 3310 | |
| 3311 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3312 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3313 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3314 | struct md_personality *p; |
| 3315 | int ret; |
| 3316 | spin_lock(&mddev->lock); |
| 3317 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3318 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3319 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3320 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3321 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3322 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3323 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3324 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3325 | ret = 0; |
| 3326 | spin_unlock(&mddev->lock); |
| 3327 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3328 | } |
| 3329 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3330 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3331 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3332 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3333 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3334 | ssize_t rv; |
| 3335 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3336 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3337 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3338 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3339 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3340 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3341 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3342 | return -EINVAL; |
| 3343 | |
| 3344 | rv = mddev_lock(mddev); |
| 3345 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3346 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3347 | |
| 3348 | if (mddev->pers == NULL) { |
| 3349 | strncpy(mddev->clevel, buf, slen); |
| 3350 | if (mddev->clevel[slen-1] == '\n') |
| 3351 | slen--; |
| 3352 | mddev->clevel[slen] = 0; |
| 3353 | mddev->level = LEVEL_NONE; |
| 3354 | rv = len; |
| 3355 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3356 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3357 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3358 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3359 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3360 | |
| 3361 | /* request to change the personality. Need to ensure: |
| 3362 | * - array is not engaged in resync/recovery/reshape |
| 3363 | * - old personality can be suspended |
| 3364 | * - new personality will access other array. |
| 3365 | */ |
| 3366 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3367 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3368 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3369 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3370 | mddev->reshape_position != MaxSector || |
| 3371 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3372 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3373 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3374 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3375 | if (!mddev->pers->quiesce) { |
| 3376 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3377 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3378 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3382 | strncpy(clevel, buf, slen); |
| 3383 | if (clevel[slen-1] == '\n') |
| 3384 | slen--; |
| 3385 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3386 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3387 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3388 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3389 | if (request_module("md-%s", clevel) != 0) |
| 3390 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3391 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3392 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3393 | if (!pers || !try_module_get(pers->owner)) { |
| 3394 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3395 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3396 | rv = -EINVAL; |
| 3397 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3398 | } |
| 3399 | spin_unlock(&pers_lock); |
| 3400 | |
| 3401 | if (pers == mddev->pers) { |
| 3402 | /* Nothing to do! */ |
| 3403 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3404 | rv = len; |
| 3405 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3406 | } |
| 3407 | if (!pers->takeover) { |
| 3408 | module_put(pers->owner); |
| 3409 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3410 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3411 | rv = -EINVAL; |
| 3412 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3413 | } |
| 3414 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3415 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3416 | rdev->new_raid_disk = rdev->raid_disk; |
| 3417 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3418 | /* ->takeover must set new_* and/or delta_disks |
| 3419 | * if it succeeds, and may set them when it fails. |
| 3420 | */ |
| 3421 | priv = pers->takeover(mddev); |
| 3422 | if (IS_ERR(priv)) { |
| 3423 | mddev->new_level = mddev->level; |
| 3424 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3425 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3426 | mddev->raid_disks -= mddev->delta_disks; |
| 3427 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3428 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3429 | module_put(pers->owner); |
| 3430 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3431 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3432 | rv = PTR_ERR(priv); |
| 3433 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | /* Looks like we have a winner */ |
| 3437 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 3438 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3439 | |
| 3440 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3441 | oldpers = mddev->pers; |
| 3442 | oldpriv = mddev->private; |
| 3443 | mddev->pers = pers; |
| 3444 | mddev->private = priv; |
| 3445 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3446 | mddev->level = mddev->new_level; |
| 3447 | mddev->layout = mddev->new_layout; |
| 3448 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 3449 | mddev->delta_disks = 0; |
| 3450 | mddev->reshape_backwards = 0; |
| 3451 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3452 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3453 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3454 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3455 | mddev->external) { |
| 3456 | /* We are converting from a no-redundancy array |
| 3457 | * to a redundancy array and metadata is managed |
| 3458 | * externally so we need to be sure that writes |
| 3459 | * won't block due to a need to transition |
| 3460 | * clean->dirty |
| 3461 | * until external management is started. |
| 3462 | */ |
| 3463 | mddev->in_sync = 0; |
| 3464 | mddev->safemode_delay = 0; |
| 3465 | mddev->safemode = 0; |
| 3466 | } |
| 3467 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3468 | oldpers->free(mddev, oldpriv); |
| 3469 | |
| 3470 | if (oldpers->sync_request == NULL && |
| 3471 | pers->sync_request != NULL) { |
| 3472 | /* need to add the md_redundancy_group */ |
| 3473 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3474 | printk(KERN_WARNING |
| 3475 | "md: cannot register extra attributes for %s\n", |
| 3476 | mdname(mddev)); |
| 3477 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3478 | } |
| 3479 | if (oldpers->sync_request != NULL && |
| 3480 | pers->sync_request == NULL) { |
| 3481 | /* need to remove the md_redundancy_group */ |
| 3482 | if (mddev->to_remove == NULL) |
| 3483 | mddev->to_remove = &md_redundancy_group; |
| 3484 | } |
| 3485 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3486 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3487 | if (rdev->raid_disk < 0) |
| 3488 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3489 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3490 | rdev->new_raid_disk = -1; |
| 3491 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3492 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3493 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3494 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3495 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3496 | if (rdev->raid_disk < 0) |
| 3497 | continue; |
| 3498 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3499 | continue; |
| 3500 | rdev->raid_disk = rdev->new_raid_disk; |
| 3501 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3502 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3503 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3504 | if (sysfs_link_rdev(mddev, rdev)) |
| 3505 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3506 | " for %s after level change\n", |
| 3507 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3508 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3509 | } |
| 3510 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3511 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3512 | /* this is now an array without redundancy, so |
| 3513 | * it must always be in_sync |
| 3514 | */ |
| 3515 | mddev->in_sync = 1; |
| 3516 | del_timer_sync(&mddev->safemode_timer); |
| 3517 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 3518 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3519 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3520 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3521 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 3522 | if (!mddev->thread) |
| 3523 | md_update_sb(mddev, 1); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3524 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3525 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3526 | rv = len; |
| 3527 | out_unlock: |
| 3528 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3529 | return rv; |
| 3530 | } |
| 3531 | |
| 3532 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3533 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3534 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3535 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3536 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3537 | { |
| 3538 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3539 | if (mddev->reshape_position != MaxSector && |
| 3540 | mddev->layout != mddev->new_layout) |
| 3541 | return sprintf(page, "%d (%d)\n", |
| 3542 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3543 | return sprintf(page, "%d\n", mddev->layout); |
| 3544 | } |
| 3545 | |
| 3546 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3547 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3548 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3549 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3550 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3551 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3552 | err = kstrtouint(buf, 10, &n); |
| 3553 | if (err < 0) |
| 3554 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3555 | err = mddev_lock(mddev); |
| 3556 | if (err) |
| 3557 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3558 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3559 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3560 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3561 | err = -EBUSY; |
| 3562 | else if (mddev->ro) |
| 3563 | err = -EROFS; |
| 3564 | else { |
| 3565 | mddev->new_layout = n; |
| 3566 | err = mddev->pers->check_reshape(mddev); |
| 3567 | if (err) |
| 3568 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3569 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3570 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3571 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3572 | if (mddev->reshape_position == MaxSector) |
| 3573 | mddev->layout = n; |
| 3574 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3575 | mddev_unlock(mddev); |
| 3576 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3577 | } |
| 3578 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3579 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3580 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3581 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3582 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3583 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3584 | if (mddev->raid_disks == 0) |
| 3585 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3586 | if (mddev->reshape_position != MaxSector && |
| 3587 | mddev->delta_disks != 0) |
| 3588 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3589 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3590 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3591 | } |
| 3592 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3593 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3594 | |
| 3595 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3596 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3597 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3598 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3599 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3600 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3601 | err = kstrtouint(buf, 10, &n); |
| 3602 | if (err < 0) |
| 3603 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3604 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3605 | err = mddev_lock(mddev); |
| 3606 | if (err) |
| 3607 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3608 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3609 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3610 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3611 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3612 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3613 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3614 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3615 | rdev_for_each(rdev, mddev) { |
| 3616 | if (olddisks < n && |
| 3617 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3618 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3619 | if (olddisks > n && |
| 3620 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3621 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3622 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3623 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3624 | mddev->delta_disks = n - olddisks; |
| 3625 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3626 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3627 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3628 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3629 | out_unlock: |
| 3630 | mddev_unlock(mddev); |
| 3631 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3632 | } |
| 3633 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3634 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3635 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3636 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3637 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3638 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3639 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3640 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3641 | return sprintf(page, "%d (%d)\n", |
| 3642 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3643 | mddev->chunk_sectors << 9); |
| 3644 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3648 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3649 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3650 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3651 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3652 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3653 | err = kstrtoul(buf, 10, &n); |
| 3654 | if (err < 0) |
| 3655 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3656 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3657 | err = mddev_lock(mddev); |
| 3658 | if (err) |
| 3659 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3660 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3661 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3662 | err = -EBUSY; |
| 3663 | else if (mddev->ro) |
| 3664 | err = -EROFS; |
| 3665 | else { |
| 3666 | mddev->new_chunk_sectors = n >> 9; |
| 3667 | err = mddev->pers->check_reshape(mddev); |
| 3668 | if (err) |
| 3669 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3670 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3671 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3672 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3673 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3674 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3675 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3676 | mddev_unlock(mddev); |
| 3677 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3678 | } |
| 3679 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3680 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3681 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3682 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3683 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3684 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3685 | if (mddev->recovery_cp == MaxSector) |
| 3686 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3687 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3688 | } |
| 3689 | |
| 3690 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3691 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3692 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3693 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3694 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3695 | |
| 3696 | if (cmd_match(buf, "none")) |
| 3697 | n = MaxSector; |
| 3698 | else { |
| 3699 | err = kstrtoull(buf, 10, &n); |
| 3700 | if (err < 0) |
| 3701 | return err; |
| 3702 | if (n != (sector_t)n) |
| 3703 | return -EINVAL; |
| 3704 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3705 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3706 | err = mddev_lock(mddev); |
| 3707 | if (err) |
| 3708 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3709 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3710 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3711 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3712 | if (!err) { |
| 3713 | mddev->recovery_cp = n; |
| 3714 | if (mddev->pers) |
| 3715 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3716 | } |
| 3717 | mddev_unlock(mddev); |
| 3718 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3719 | } |
| 3720 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3721 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 3722 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3723 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3724 | /* |
| 3725 | * The array state can be: |
| 3726 | * |
| 3727 | * clear |
| 3728 | * No devices, no size, no level |
| 3729 | * Equivalent to STOP_ARRAY ioctl |
| 3730 | * inactive |
| 3731 | * May have some settings, but array is not active |
| 3732 | * all IO results in error |
| 3733 | * When written, doesn't tear down array, but just stops it |
| 3734 | * suspended (not supported yet) |
| 3735 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3736 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3737 | * readonly |
| 3738 | * no resync can happen. no superblocks get written. |
| 3739 | * write requests fail |
| 3740 | * read-auto |
| 3741 | * like readonly, but behaves like 'clean' on a write request. |
| 3742 | * |
| 3743 | * clean - no pending writes, but otherwise active. |
| 3744 | * When written to inactive array, starts without resync |
| 3745 | * If a write request arrives then |
| 3746 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3747 | * if not known, block and switch to write-pending |
| 3748 | * If written to an active array that has pending writes, then fails. |
| 3749 | * active |
| 3750 | * fully active: IO and resync can be happening. |
| 3751 | * When written to inactive array, starts with resync |
| 3752 | * |
| 3753 | * write-pending |
| 3754 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3755 | * |
| 3756 | * active-idle |
| 3757 | * like active, but no writes have been seen for a while (100msec). |
| 3758 | * |
| 3759 | */ |
| 3760 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3761 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3762 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3763 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3764 | "write-pending", "active-idle", NULL }; |
| 3765 | |
| 3766 | static int match_word(const char *word, char **list) |
| 3767 | { |
| 3768 | int n; |
| 3769 | for (n=0; list[n]; n++) |
| 3770 | if (cmd_match(word, list[n])) |
| 3771 | break; |
| 3772 | return n; |
| 3773 | } |
| 3774 | |
| 3775 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3776 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3777 | { |
| 3778 | enum array_state st = inactive; |
| 3779 | |
| 3780 | if (mddev->pers) |
| 3781 | switch(mddev->ro) { |
| 3782 | case 1: |
| 3783 | st = readonly; |
| 3784 | break; |
| 3785 | case 2: |
| 3786 | st = read_auto; |
| 3787 | break; |
| 3788 | case 0: |
| 3789 | if (mddev->in_sync) |
| 3790 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3791 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3792 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3793 | else if (mddev->safemode) |
| 3794 | st = active_idle; |
| 3795 | else |
| 3796 | st = active; |
| 3797 | } |
| 3798 | else { |
| 3799 | if (list_empty(&mddev->disks) && |
| 3800 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3801 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3802 | st = clear; |
| 3803 | else |
| 3804 | st = inactive; |
| 3805 | } |
| 3806 | return sprintf(page, "%s\n", array_states[st]); |
| 3807 | } |
| 3808 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3809 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 3810 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 3811 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3812 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3813 | |
| 3814 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3815 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3816 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3817 | int err; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3818 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3819 | |
| 3820 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 3821 | /* don't take reconfig_mutex when toggling between |
| 3822 | * clean and active |
| 3823 | */ |
| 3824 | spin_lock(&mddev->lock); |
| 3825 | if (st == active) { |
| 3826 | restart_array(mddev); |
| 3827 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 3828 | wake_up(&mddev->sb_wait); |
| 3829 | err = 0; |
| 3830 | } else /* st == clean */ { |
| 3831 | restart_array(mddev); |
| 3832 | if (atomic_read(&mddev->writes_pending) == 0) { |
| 3833 | if (mddev->in_sync == 0) { |
| 3834 | mddev->in_sync = 1; |
| 3835 | if (mddev->safemode == 1) |
| 3836 | mddev->safemode = 0; |
| 3837 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3838 | } |
| 3839 | err = 0; |
| 3840 | } else |
| 3841 | err = -EBUSY; |
| 3842 | } |
| 3843 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 3844 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3845 | } |
| 3846 | err = mddev_lock(mddev); |
| 3847 | if (err) |
| 3848 | return err; |
| 3849 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3850 | switch(st) { |
| 3851 | case bad_word: |
| 3852 | break; |
| 3853 | case clear: |
| 3854 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3855 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3856 | break; |
| 3857 | case inactive: |
| 3858 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3859 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3860 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3861 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3862 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3863 | break; |
| 3864 | case suspended: |
| 3865 | break; /* not supported yet */ |
| 3866 | case readonly: |
| 3867 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3868 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3869 | else { |
| 3870 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3871 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3872 | err = do_md_run(mddev); |
| 3873 | } |
| 3874 | break; |
| 3875 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3876 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3877 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3878 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3879 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3880 | err = restart_array(mddev); |
| 3881 | if (err == 0) { |
| 3882 | mddev->ro = 2; |
| 3883 | set_disk_ro(mddev->gendisk, 0); |
| 3884 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3885 | } else { |
| 3886 | mddev->ro = 2; |
| 3887 | err = do_md_run(mddev); |
| 3888 | } |
| 3889 | break; |
| 3890 | case clean: |
| 3891 | if (mddev->pers) { |
| 3892 | restart_array(mddev); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3893 | spin_lock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3894 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3895 | if (mddev->in_sync == 0) { |
| 3896 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3897 | if (mddev->safemode == 1) |
| 3898 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3899 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3900 | } |
| 3901 | err = 0; |
| 3902 | } else |
| 3903 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3904 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3905 | } else |
| 3906 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3907 | break; |
| 3908 | case active: |
| 3909 | if (mddev->pers) { |
| 3910 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3911 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3912 | wake_up(&mddev->sb_wait); |
| 3913 | err = 0; |
| 3914 | } else { |
| 3915 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3916 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3917 | err = do_md_run(mddev); |
| 3918 | } |
| 3919 | break; |
| 3920 | case write_pending: |
| 3921 | case active_idle: |
| 3922 | /* these cannot be set */ |
| 3923 | break; |
| 3924 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3925 | |
| 3926 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 3927 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3928 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3929 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3930 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3931 | mddev_unlock(mddev); |
| 3932 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3933 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3934 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3935 | __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3936 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3937 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3938 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3939 | return sprintf(page, "%d\n", |
| 3940 | atomic_read(&mddev->max_corr_read_errors)); |
| 3941 | } |
| 3942 | |
| 3943 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3944 | 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] | 3945 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3946 | unsigned int n; |
| 3947 | int rv; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3948 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3949 | rv = kstrtouint(buf, 10, &n); |
| 3950 | if (rv < 0) |
| 3951 | return rv; |
| 3952 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3953 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3954 | } |
| 3955 | |
| 3956 | static struct md_sysfs_entry max_corr_read_errors = |
| 3957 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3958 | max_corrected_read_errors_store); |
| 3959 | |
| 3960 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3961 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3962 | { |
| 3963 | return -EINVAL; |
| 3964 | } |
| 3965 | |
| 3966 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3967 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3968 | { |
| 3969 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3970 | /* The new device is added to the array. |
| 3971 | * If the array has a persistent superblock, we read the |
| 3972 | * superblock to initialise info and check validity. |
| 3973 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3974 | * which mainly checks size. |
| 3975 | */ |
| 3976 | char *e; |
| 3977 | int major = simple_strtoul(buf, &e, 10); |
| 3978 | int minor; |
| 3979 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3980 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3981 | int err; |
| 3982 | |
| 3983 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3984 | return -EINVAL; |
| 3985 | minor = simple_strtoul(e+1, &e, 10); |
| 3986 | if (*e && *e != '\n') |
| 3987 | return -EINVAL; |
| 3988 | dev = MKDEV(major, minor); |
| 3989 | if (major != MAJOR(dev) || |
| 3990 | minor != MINOR(dev)) |
| 3991 | return -EOVERFLOW; |
| 3992 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3993 | flush_workqueue(md_misc_wq); |
| 3994 | |
| 3995 | err = mddev_lock(mddev); |
| 3996 | if (err) |
| 3997 | return err; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3998 | if (mddev->persistent) { |
| 3999 | rdev = md_import_device(dev, mddev->major_version, |
| 4000 | mddev->minor_version); |
| 4001 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4002 | struct md_rdev *rdev0 |
| 4003 | = list_entry(mddev->disks.next, |
| 4004 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4005 | err = super_types[mddev->major_version] |
| 4006 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4007 | if (err < 0) |
| 4008 | goto out; |
| 4009 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4010 | } else if (mddev->external) |
| 4011 | rdev = md_import_device(dev, -2, -1); |
| 4012 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4013 | rdev = md_import_device(dev, -1, -1); |
| 4014 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4015 | if (IS_ERR(rdev)) { |
| 4016 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4017 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4018 | } |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4019 | err = bind_rdev_to_array(rdev, mddev); |
| 4020 | out: |
| 4021 | if (err) |
| 4022 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4023 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4024 | return err ? err : len; |
| 4025 | } |
| 4026 | |
| 4027 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4028 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4029 | |
| 4030 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4031 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4032 | { |
| 4033 | char *end; |
| 4034 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4035 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4036 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4037 | err = mddev_lock(mddev); |
| 4038 | if (err) |
| 4039 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4040 | if (!mddev->bitmap) |
| 4041 | goto out; |
| 4042 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4043 | while (*buf) { |
| 4044 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4045 | if (buf == end) break; |
| 4046 | if (*end == '-') { /* range */ |
| 4047 | buf = end + 1; |
| 4048 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4049 | if (buf == end) break; |
| 4050 | } |
| 4051 | if (*end && !isspace(*end)) break; |
| 4052 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4053 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4054 | } |
| 4055 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4056 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4057 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4058 | return len; |
| 4059 | } |
| 4060 | |
| 4061 | static struct md_sysfs_entry md_bitmap = |
| 4062 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4063 | |
| 4064 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4065 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4066 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4067 | return sprintf(page, "%llu\n", |
| 4068 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4069 | } |
| 4070 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4071 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4072 | |
| 4073 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4074 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4075 | { |
| 4076 | /* If array is inactive, we can reduce the component size, but |
| 4077 | * not increase it (except from 0). |
| 4078 | * If array is active, we can try an on-line resize |
| 4079 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4080 | sector_t sectors; |
| 4081 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4082 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4083 | if (err < 0) |
| 4084 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4085 | err = mddev_lock(mddev); |
| 4086 | if (err) |
| 4087 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4088 | if (mddev->pers) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 4089 | if (mddev_is_clustered(mddev)) |
| 4090 | md_cluster_ops->metadata_update_start(mddev); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4091 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4092 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 4093 | if (mddev_is_clustered(mddev)) |
| 4094 | md_cluster_ops->metadata_update_finish(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4095 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4096 | if (mddev->dev_sectors == 0 || |
| 4097 | mddev->dev_sectors > sectors) |
| 4098 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4099 | else |
| 4100 | err = -ENOSPC; |
| 4101 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4102 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4103 | return err ? err : len; |
| 4104 | } |
| 4105 | |
| 4106 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4107 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4108 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4109 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4110 | * This is one of |
| 4111 | * 'none' for arrays with no metadata (good luck...) |
| 4112 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4113 | * or N.M for internally known formats |
| 4114 | */ |
| 4115 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4116 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4117 | { |
| 4118 | if (mddev->persistent) |
| 4119 | return sprintf(page, "%d.%d\n", |
| 4120 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4121 | else if (mddev->external) |
| 4122 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4123 | else |
| 4124 | return sprintf(page, "none\n"); |
| 4125 | } |
| 4126 | |
| 4127 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4128 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4129 | { |
| 4130 | int major, minor; |
| 4131 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4132 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4133 | /* Changing the details of 'external' metadata is |
| 4134 | * always permitted. Otherwise there must be |
| 4135 | * no devices attached to the array. |
| 4136 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4137 | |
| 4138 | err = mddev_lock(mddev); |
| 4139 | if (err) |
| 4140 | return err; |
| 4141 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4142 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4143 | ; |
| 4144 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4145 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4146 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4147 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4148 | if (cmd_match(buf, "none")) { |
| 4149 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4150 | mddev->external = 0; |
| 4151 | mddev->major_version = 0; |
| 4152 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4153 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4154 | } |
| 4155 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4156 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4157 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4158 | namelen = sizeof(mddev->metadata_type)-1; |
| 4159 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4160 | mddev->metadata_type[namelen] = 0; |
| 4161 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4162 | mddev->metadata_type[--namelen] = 0; |
| 4163 | mddev->persistent = 0; |
| 4164 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4165 | mddev->major_version = 0; |
| 4166 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4167 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4168 | } |
| 4169 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4170 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4171 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4172 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4173 | buf = e+1; |
| 4174 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4175 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4176 | goto out_unlock; |
| 4177 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4178 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4179 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4180 | mddev->major_version = major; |
| 4181 | mddev->minor_version = minor; |
| 4182 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4183 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4184 | err = 0; |
| 4185 | out_unlock: |
| 4186 | mddev_unlock(mddev); |
| 4187 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4188 | } |
| 4189 | |
| 4190 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4191 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4192 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4193 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4194 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4195 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4196 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4197 | unsigned long recovery = mddev->recovery; |
| 4198 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4199 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4200 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4201 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4202 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4203 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4204 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4205 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4206 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4207 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4208 | type = "check"; |
| 4209 | else |
| 4210 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4211 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4212 | type = "recover"; |
| 4213 | } |
| 4214 | return sprintf(page, "%s\n", type); |
| 4215 | } |
| 4216 | |
| 4217 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4218 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4219 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4220 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4221 | return -EINVAL; |
| 4222 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4223 | |
| 4224 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4225 | if (cmd_match(page, "frozen")) |
| 4226 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4227 | else |
| 4228 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4229 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4230 | mddev_lock(mddev) == 0) { |
| 4231 | flush_workqueue(md_misc_wq); |
| 4232 | if (mddev->sync_thread) { |
| 4233 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4234 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4235 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4236 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4237 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4238 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4239 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4240 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4241 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4242 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4243 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4244 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4245 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4246 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4247 | int err; |
| 4248 | if (mddev->pers->start_reshape == NULL) |
| 4249 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4250 | err = mddev_lock(mddev); |
| 4251 | if (!err) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4252 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4253 | err = mddev->pers->start_reshape(mddev); |
| 4254 | mddev_unlock(mddev); |
| 4255 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4256 | if (err) |
| 4257 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4258 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4259 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4260 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4261 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4262 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4263 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4264 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4265 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4266 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4267 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4268 | if (mddev->ro == 2) { |
| 4269 | /* A write to sync_action is enough to justify |
| 4270 | * canceling read-auto mode |
| 4271 | */ |
| 4272 | mddev->ro = 0; |
| 4273 | md_wakeup_thread(mddev->sync_thread); |
| 4274 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4275 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4276 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4277 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4278 | return len; |
| 4279 | } |
| 4280 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4281 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4282 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4283 | |
| 4284 | static ssize_t |
| 4285 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4286 | { |
| 4287 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4288 | } |
| 4289 | |
| 4290 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4291 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4292 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4293 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4294 | { |
| 4295 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4296 | (unsigned long long) |
| 4297 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4298 | } |
| 4299 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4300 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4301 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4302 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4303 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4304 | { |
| 4305 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4306 | mddev->sync_speed_min ? "local": "system"); |
| 4307 | } |
| 4308 | |
| 4309 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4310 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4311 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4312 | unsigned int min; |
| 4313 | int rv; |
| 4314 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4315 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4316 | min = 0; |
| 4317 | } else { |
| 4318 | rv = kstrtouint(buf, 10, &min); |
| 4319 | if (rv < 0) |
| 4320 | return rv; |
| 4321 | if (min == 0) |
| 4322 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4323 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4324 | mddev->sync_speed_min = min; |
| 4325 | return len; |
| 4326 | } |
| 4327 | |
| 4328 | static struct md_sysfs_entry md_sync_min = |
| 4329 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4330 | |
| 4331 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4332 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4333 | { |
| 4334 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4335 | mddev->sync_speed_max ? "local": "system"); |
| 4336 | } |
| 4337 | |
| 4338 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4339 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4340 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4341 | unsigned int max; |
| 4342 | int rv; |
| 4343 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4344 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4345 | max = 0; |
| 4346 | } else { |
| 4347 | rv = kstrtouint(buf, 10, &max); |
| 4348 | if (rv < 0) |
| 4349 | return rv; |
| 4350 | if (max == 0) |
| 4351 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4352 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4353 | mddev->sync_speed_max = max; |
| 4354 | return len; |
| 4355 | } |
| 4356 | |
| 4357 | static struct md_sysfs_entry md_sync_max = |
| 4358 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4359 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4360 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4361 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4362 | { |
| 4363 | return sprintf(page, "%d\n", mddev->degraded); |
| 4364 | } |
| 4365 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4366 | |
| 4367 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4368 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4369 | { |
| 4370 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4371 | } |
| 4372 | |
| 4373 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4374 | 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] | 4375 | { |
| 4376 | long n; |
| 4377 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4378 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4379 | return -EINVAL; |
| 4380 | |
| 4381 | if (n != 0 && n != 1) |
| 4382 | return -EINVAL; |
| 4383 | |
| 4384 | mddev->parallel_resync = n; |
| 4385 | |
| 4386 | if (mddev->sync_thread) |
| 4387 | wake_up(&resync_wait); |
| 4388 | |
| 4389 | return len; |
| 4390 | } |
| 4391 | |
| 4392 | /* force parallel resync, even with shared block devices */ |
| 4393 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4394 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4395 | sync_force_parallel_show, sync_force_parallel_store); |
| 4396 | |
| 4397 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4398 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4399 | { |
| 4400 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4401 | if (mddev->curr_resync == 0) |
| 4402 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4403 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4404 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4405 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4406 | db = resync - mddev->resync_mark_cnt; |
| 4407 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4408 | } |
| 4409 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4410 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4411 | |
| 4412 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4413 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4414 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4415 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4416 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4417 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4418 | return sprintf(page, "none\n"); |
| 4419 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4420 | if (mddev->curr_resync == 1 || |
| 4421 | mddev->curr_resync == 2) |
| 4422 | return sprintf(page, "delayed\n"); |
| 4423 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4424 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4425 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4426 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4427 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4428 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4429 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4430 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4431 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4432 | } |
| 4433 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4434 | static struct md_sysfs_entry md_sync_completed = |
| 4435 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4436 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4437 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4438 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4439 | { |
| 4440 | return sprintf(page, "%llu\n", |
| 4441 | (unsigned long long)mddev->resync_min); |
| 4442 | } |
| 4443 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4444 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4445 | { |
| 4446 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4447 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4448 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4449 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4450 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4451 | |
| 4452 | spin_lock(&mddev->lock); |
| 4453 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4454 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4455 | goto out_unlock; |
| 4456 | |
| 4457 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4458 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4459 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4460 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 4461 | /* Round down to multiple of 4K for safety */ |
| 4462 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4463 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4464 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4465 | out_unlock: |
| 4466 | spin_unlock(&mddev->lock); |
| 4467 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4468 | } |
| 4469 | |
| 4470 | static struct md_sysfs_entry md_min_sync = |
| 4471 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4472 | |
| 4473 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4474 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4475 | { |
| 4476 | if (mddev->resync_max == MaxSector) |
| 4477 | return sprintf(page, "max\n"); |
| 4478 | else |
| 4479 | return sprintf(page, "%llu\n", |
| 4480 | (unsigned long long)mddev->resync_max); |
| 4481 | } |
| 4482 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4483 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4484 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4485 | int err; |
| 4486 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4487 | if (strncmp(buf, "max", 3) == 0) |
| 4488 | mddev->resync_max = MaxSector; |
| 4489 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4490 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4491 | int chunk; |
| 4492 | |
| 4493 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4494 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4495 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4496 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4497 | goto out_unlock; |
| 4498 | |
| 4499 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4500 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4501 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4502 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4503 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4504 | |
| 4505 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4506 | chunk = mddev->chunk_sectors; |
| 4507 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4508 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4509 | |
| 4510 | err = -EINVAL; |
| 4511 | if (sector_div(temp, chunk)) |
| 4512 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4513 | } |
| 4514 | mddev->resync_max = max; |
| 4515 | } |
| 4516 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4517 | err = 0; |
| 4518 | out_unlock: |
| 4519 | spin_unlock(&mddev->lock); |
| 4520 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | static struct md_sysfs_entry md_max_sync = |
| 4524 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4525 | |
| 4526 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4527 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4528 | { |
| 4529 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4530 | } |
| 4531 | |
| 4532 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4533 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4534 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4535 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4536 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4537 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4538 | err = kstrtoull(buf, 10, &new); |
| 4539 | if (err < 0) |
| 4540 | return err; |
| 4541 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4542 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4543 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4544 | err = mddev_lock(mddev); |
| 4545 | if (err) |
| 4546 | return err; |
| 4547 | err = -EINVAL; |
| 4548 | if (mddev->pers == NULL || |
| 4549 | mddev->pers->quiesce == NULL) |
| 4550 | goto unlock; |
| 4551 | old = mddev->suspend_lo; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4552 | mddev->suspend_lo = new; |
| 4553 | if (new >= old) |
| 4554 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4555 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4556 | else { |
| 4557 | /* Expanding suspended region - need to wait */ |
| 4558 | mddev->pers->quiesce(mddev, 1); |
| 4559 | mddev->pers->quiesce(mddev, 0); |
| 4560 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4561 | err = 0; |
| 4562 | unlock: |
| 4563 | mddev_unlock(mddev); |
| 4564 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4565 | } |
| 4566 | static struct md_sysfs_entry md_suspend_lo = |
| 4567 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4568 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4569 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4570 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4571 | { |
| 4572 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4573 | } |
| 4574 | |
| 4575 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4576 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4577 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4578 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4579 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4580 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4581 | err = kstrtoull(buf, 10, &new); |
| 4582 | if (err < 0) |
| 4583 | return err; |
| 4584 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4585 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4586 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4587 | err = mddev_lock(mddev); |
| 4588 | if (err) |
| 4589 | return err; |
| 4590 | err = -EINVAL; |
| 4591 | if (mddev->pers == NULL || |
| 4592 | mddev->pers->quiesce == NULL) |
| 4593 | goto unlock; |
| 4594 | old = mddev->suspend_hi; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4595 | mddev->suspend_hi = new; |
| 4596 | if (new <= old) |
| 4597 | /* Shrinking suspended region */ |
| 4598 | mddev->pers->quiesce(mddev, 2); |
| 4599 | else { |
| 4600 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4601 | mddev->pers->quiesce(mddev, 1); |
| 4602 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4603 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4604 | err = 0; |
| 4605 | unlock: |
| 4606 | mddev_unlock(mddev); |
| 4607 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4608 | } |
| 4609 | static struct md_sysfs_entry md_suspend_hi = |
| 4610 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4611 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4612 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4613 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4614 | { |
| 4615 | if (mddev->reshape_position != MaxSector) |
| 4616 | return sprintf(page, "%llu\n", |
| 4617 | (unsigned long long)mddev->reshape_position); |
| 4618 | strcpy(page, "none\n"); |
| 4619 | return 5; |
| 4620 | } |
| 4621 | |
| 4622 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4623 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4624 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4625 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4626 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4627 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4628 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4629 | err = kstrtoull(buf, 10, &new); |
| 4630 | if (err < 0) |
| 4631 | return err; |
| 4632 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4633 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4634 | err = mddev_lock(mddev); |
| 4635 | if (err) |
| 4636 | return err; |
| 4637 | err = -EBUSY; |
| 4638 | if (mddev->pers) |
| 4639 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4640 | mddev->reshape_position = new; |
| 4641 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4642 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4643 | mddev->new_level = mddev->level; |
| 4644 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4645 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4646 | rdev_for_each(rdev, mddev) |
| 4647 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4648 | err = 0; |
| 4649 | unlock: |
| 4650 | mddev_unlock(mddev); |
| 4651 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | static struct md_sysfs_entry md_reshape_position = |
| 4655 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4656 | reshape_position_store); |
| 4657 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4658 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4659 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4660 | { |
| 4661 | return sprintf(page, "%s\n", |
| 4662 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4663 | } |
| 4664 | |
| 4665 | static ssize_t |
| 4666 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4667 | { |
| 4668 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4669 | int err; |
| 4670 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4671 | if (cmd_match(buf, "forwards")) |
| 4672 | backwards = 0; |
| 4673 | else if (cmd_match(buf, "backwards")) |
| 4674 | backwards = 1; |
| 4675 | else |
| 4676 | return -EINVAL; |
| 4677 | if (mddev->reshape_backwards == backwards) |
| 4678 | return len; |
| 4679 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4680 | err = mddev_lock(mddev); |
| 4681 | if (err) |
| 4682 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4683 | /* check if we are allowed to change */ |
| 4684 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4685 | err = -EBUSY; |
| 4686 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4687 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4688 | err = -EINVAL; |
| 4689 | else |
| 4690 | mddev->reshape_backwards = backwards; |
| 4691 | mddev_unlock(mddev); |
| 4692 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4693 | } |
| 4694 | |
| 4695 | static struct md_sysfs_entry md_reshape_direction = |
| 4696 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4697 | reshape_direction_store); |
| 4698 | |
| 4699 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4700 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4701 | { |
| 4702 | if (mddev->external_size) |
| 4703 | return sprintf(page, "%llu\n", |
| 4704 | (unsigned long long)mddev->array_sectors/2); |
| 4705 | else |
| 4706 | return sprintf(page, "default\n"); |
| 4707 | } |
| 4708 | |
| 4709 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4710 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4711 | { |
| 4712 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4713 | int err; |
| 4714 | |
| 4715 | err = mddev_lock(mddev); |
| 4716 | if (err) |
| 4717 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4718 | |
| 4719 | if (strncmp(buf, "default", 7) == 0) { |
| 4720 | if (mddev->pers) |
| 4721 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4722 | else |
| 4723 | sectors = mddev->array_sectors; |
| 4724 | |
| 4725 | mddev->external_size = 0; |
| 4726 | } else { |
| 4727 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4728 | err = -EINVAL; |
| 4729 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 4730 | err = -E2BIG; |
| 4731 | else |
| 4732 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4733 | } |
| 4734 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4735 | if (!err) { |
| 4736 | mddev->array_sectors = sectors; |
| 4737 | if (mddev->pers) { |
| 4738 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4739 | revalidate_disk(mddev->gendisk); |
| 4740 | } |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4741 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4742 | mddev_unlock(mddev); |
| 4743 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4744 | } |
| 4745 | |
| 4746 | static struct md_sysfs_entry md_array_size = |
| 4747 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4748 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4749 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4750 | static struct attribute *md_default_attrs[] = { |
| 4751 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4752 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4753 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4754 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4755 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4756 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4757 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4758 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4759 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4760 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4761 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4762 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4763 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4764 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4765 | NULL, |
| 4766 | }; |
| 4767 | |
| 4768 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4769 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4770 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4771 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4772 | &md_sync_min.attr, |
| 4773 | &md_sync_max.attr, |
| 4774 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4775 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4776 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4777 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4778 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4779 | &md_suspend_lo.attr, |
| 4780 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4781 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4782 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4783 | NULL, |
| 4784 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4785 | static struct attribute_group md_redundancy_group = { |
| 4786 | .name = NULL, |
| 4787 | .attrs = md_redundancy_attrs, |
| 4788 | }; |
| 4789 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4790 | static ssize_t |
| 4791 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4792 | { |
| 4793 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4794 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4795 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4796 | |
| 4797 | if (!entry->show) |
| 4798 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4799 | spin_lock(&all_mddevs_lock); |
| 4800 | if (list_empty(&mddev->all_mddevs)) { |
| 4801 | spin_unlock(&all_mddevs_lock); |
| 4802 | return -EBUSY; |
| 4803 | } |
| 4804 | mddev_get(mddev); |
| 4805 | spin_unlock(&all_mddevs_lock); |
| 4806 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4807 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4808 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4809 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | static ssize_t |
| 4813 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4814 | const char *page, size_t length) |
| 4815 | { |
| 4816 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4817 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4818 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4819 | |
| 4820 | if (!entry->store) |
| 4821 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4822 | if (!capable(CAP_SYS_ADMIN)) |
| 4823 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4824 | spin_lock(&all_mddevs_lock); |
| 4825 | if (list_empty(&mddev->all_mddevs)) { |
| 4826 | spin_unlock(&all_mddevs_lock); |
| 4827 | return -EBUSY; |
| 4828 | } |
| 4829 | mddev_get(mddev); |
| 4830 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4831 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4832 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4833 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4834 | } |
| 4835 | |
| 4836 | static void md_free(struct kobject *ko) |
| 4837 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4838 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4839 | |
| 4840 | if (mddev->sysfs_state) |
| 4841 | sysfs_put(mddev->sysfs_state); |
| 4842 | |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 4843 | if (mddev->queue) |
| 4844 | blk_cleanup_queue(mddev->queue); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4845 | if (mddev->gendisk) { |
| 4846 | del_gendisk(mddev->gendisk); |
| 4847 | put_disk(mddev->gendisk); |
| 4848 | } |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4849 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4850 | kfree(mddev); |
| 4851 | } |
| 4852 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4853 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4854 | .show = md_attr_show, |
| 4855 | .store = md_attr_store, |
| 4856 | }; |
| 4857 | static struct kobj_type md_ktype = { |
| 4858 | .release = md_free, |
| 4859 | .sysfs_ops = &md_sysfs_ops, |
| 4860 | .default_attrs = md_default_attrs, |
| 4861 | }; |
| 4862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | int mdp_major = 0; |
| 4864 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4865 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4866 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4867 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4868 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4869 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4870 | kobject_del(&mddev->kobj); |
| 4871 | kobject_put(&mddev->kobj); |
| 4872 | } |
| 4873 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4874 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4876 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4877 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4878 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4879 | int partitioned; |
| 4880 | int shift; |
| 4881 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4882 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | |
| 4884 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4885 | return -ENODEV; |
| 4886 | |
| 4887 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4888 | shift = partitioned ? MdpMinorShift : 0; |
| 4889 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4891 | /* wait for any previous instance of this device to be |
| 4892 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4893 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4894 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4895 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4896 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4897 | error = -EEXIST; |
| 4898 | if (mddev->gendisk) |
| 4899 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4900 | |
| 4901 | if (name) { |
| 4902 | /* Need to ensure that 'name' is not a duplicate. |
| 4903 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4904 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4905 | spin_lock(&all_mddevs_lock); |
| 4906 | |
| 4907 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4908 | if (mddev2->gendisk && |
| 4909 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4910 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4911 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4912 | } |
| 4913 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4915 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4916 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4917 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4918 | if (!mddev->queue) |
| 4919 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4920 | mddev->queue->queuedata = mddev; |
| 4921 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4922 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 4923 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | disk = alloc_disk(1 << shift); |
| 4926 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4927 | blk_cleanup_queue(mddev->queue); |
| 4928 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4929 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4931 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4933 | if (name) |
| 4934 | strcpy(disk->disk_name, name); |
| 4935 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4937 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4938 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4939 | disk->fops = &md_fops; |
| 4940 | disk->private_data = mddev; |
| 4941 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4942 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4943 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4944 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4945 | * remove it now. |
| 4946 | */ |
| 4947 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4949 | /* As soon as we call add_disk(), another thread could get |
| 4950 | * through to md_open, so make sure it doesn't get too far |
| 4951 | */ |
| 4952 | mutex_lock(&mddev->open_mutex); |
| 4953 | add_disk(disk); |
| 4954 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4955 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4956 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4957 | if (error) { |
| 4958 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4959 | * __must_check, we must do something with the result |
| 4960 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4961 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4962 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4963 | error = 0; |
| 4964 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4965 | if (mddev->kobj.sd && |
| 4966 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4967 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4968 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4969 | abort: |
| 4970 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4971 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4972 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4973 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4974 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4975 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4976 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4977 | } |
| 4978 | |
| 4979 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4980 | { |
| 4981 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | return NULL; |
| 4983 | } |
| 4984 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4985 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4986 | { |
| 4987 | /* val must be "md_*" where * is not all digits. |
| 4988 | * We allocate an array with a large free minor number, and |
| 4989 | * set the name to val. val must not already be an active name. |
| 4990 | */ |
| 4991 | int len = strlen(val); |
| 4992 | char buf[DISK_NAME_LEN]; |
| 4993 | |
| 4994 | while (len && val[len-1] == '\n') |
| 4995 | len--; |
| 4996 | if (len >= DISK_NAME_LEN) |
| 4997 | return -E2BIG; |
| 4998 | strlcpy(buf, val, len+1); |
| 4999 | if (strncmp(buf, "md_", 3) != 0) |
| 5000 | return -EINVAL; |
| 5001 | return md_alloc(0, buf); |
| 5002 | } |
| 5003 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5004 | static void md_safemode_timeout(unsigned long data) |
| 5005 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5006 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5007 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5008 | if (!atomic_read(&mddev->writes_pending)) { |
| 5009 | mddev->safemode = 1; |
| 5010 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5011 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5012 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | md_wakeup_thread(mddev->thread); |
| 5014 | } |
| 5015 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5016 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5017 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5018 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5020 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5021 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5022 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5024 | if (list_empty(&mddev->disks)) |
| 5025 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5026 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | |
| 5028 | if (mddev->pers) |
| 5029 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5030 | /* Cannot run until previous stop completes properly */ |
| 5031 | if (mddev->sysfs_active) |
| 5032 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | /* |
| 5035 | * Analyze all RAID superblock(s) |
| 5036 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5037 | if (!mddev->raid_disks) { |
| 5038 | if (!mddev->persistent) |
| 5039 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5040 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5041 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5042 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5043 | if (mddev->level != LEVEL_NONE) |
| 5044 | request_module("md-level-%d", mddev->level); |
| 5045 | else if (mddev->clevel[0]) |
| 5046 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5047 | |
| 5048 | /* |
| 5049 | * Drop all container device buffers, from now on |
| 5050 | * the only valid external interface is through the md |
| 5051 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5052 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5053 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5054 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5055 | continue; |
| 5056 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5057 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5058 | |
| 5059 | /* perform some consistency tests on the device. |
| 5060 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5061 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5062 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5063 | if (rdev->meta_bdev) { |
| 5064 | /* Nothing to check */; |
| 5065 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5066 | if (mddev->dev_sectors && |
| 5067 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5068 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5069 | printk("md: %s: data overlaps metadata\n", |
| 5070 | mdname(mddev)); |
| 5071 | return -EINVAL; |
| 5072 | } |
| 5073 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5074 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5075 | > rdev->data_offset) { |
| 5076 | printk("md: %s: metadata overlaps data\n", |
| 5077 | mdname(mddev)); |
| 5078 | return -EINVAL; |
| 5079 | } |
| 5080 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5081 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5082 | } |
| 5083 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5084 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 5085 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5087 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5088 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5089 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5090 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5091 | if (mddev->level != LEVEL_NONE) |
| 5092 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5093 | mddev->level); |
| 5094 | else |
| 5095 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5096 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5097 | return -EINVAL; |
| 5098 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5099 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5100 | if (mddev->level != pers->level) { |
| 5101 | mddev->level = pers->level; |
| 5102 | mddev->new_level = pers->level; |
| 5103 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5104 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5106 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5107 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5108 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5109 | module_put(pers->owner); |
| 5110 | return -EINVAL; |
| 5111 | } |
| 5112 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5113 | if (pers->sync_request) { |
| 5114 | /* Warn if this is a potentially silly |
| 5115 | * configuration. |
| 5116 | */ |
| 5117 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5118 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5119 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5120 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5121 | rdev_for_each(rdev, mddev) |
| 5122 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5123 | if (rdev < rdev2 && |
| 5124 | rdev->bdev->bd_contains == |
| 5125 | rdev2->bdev->bd_contains) { |
| 5126 | printk(KERN_WARNING |
| 5127 | "%s: WARNING: %s appears to be" |
| 5128 | " on the same physical disk as" |
| 5129 | " %s.\n", |
| 5130 | mdname(mddev), |
| 5131 | bdevname(rdev->bdev,b), |
| 5132 | bdevname(rdev2->bdev,b2)); |
| 5133 | warned = 1; |
| 5134 | } |
| 5135 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5136 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5137 | if (warned) |
| 5138 | printk(KERN_WARNING |
| 5139 | "True protection against single-disk" |
| 5140 | " failure might be compromised.\n"); |
| 5141 | } |
| 5142 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5143 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5144 | /* may be over-ridden by personality */ |
| 5145 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5146 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5147 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5149 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5150 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5151 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5152 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5153 | if (err) |
| 5154 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5155 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5156 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5157 | " but 'external_size' not in effect?\n", __func__); |
| 5158 | printk(KERN_ERR |
| 5159 | "md: invalid array_size %llu > default size %llu\n", |
| 5160 | (unsigned long long)mddev->array_sectors / 2, |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5161 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5162 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5163 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5164 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5165 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5166 | struct bitmap *bitmap; |
| 5167 | |
| 5168 | bitmap = bitmap_create(mddev, -1); |
| 5169 | if (IS_ERR(bitmap)) { |
| 5170 | err = PTR_ERR(bitmap); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5171 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5172 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5173 | } else |
| 5174 | mddev->bitmap = bitmap; |
| 5175 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5177 | if (err) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5178 | mddev_detach(mddev); |
NeilBrown | 0c35bd4 | 2015-03-13 11:51:18 +1100 | [diff] [blame] | 5179 | if (mddev->private) |
| 5180 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5181 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5182 | module_put(pers->owner); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5183 | bitmap_destroy(mddev); |
| 5184 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5185 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5186 | if (mddev->queue) { |
| 5187 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5188 | mddev->queue->backing_dev_info.congested_fn = md_congested; |
NeilBrown | 64590f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5189 | blk_queue_merge_bvec(mddev->queue, md_mergeable_bvec); |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5190 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5191 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5192 | if (mddev->kobj.sd && |
| 5193 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5194 | printk(KERN_WARNING |
| 5195 | "md: cannot register extra attributes for %s\n", |
| 5196 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5197 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5198 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5199 | mddev->ro = 0; |
| 5200 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5201 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5202 | atomic_set(&mddev->max_corr_read_errors, |
| 5203 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | mddev->safemode = 0; |
| 5205 | mddev->safemode_timer.function = md_safemode_timeout; |
| 5206 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5207 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5208 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5209 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5210 | spin_lock(&mddev->lock); |
| 5211 | mddev->pers = pers; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5212 | mddev->ready = 1; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5213 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5214 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5215 | if (rdev->raid_disk >= 0) |
| 5216 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5217 | /* failure here is OK */; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5219 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5220 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5221 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5222 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5223 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5224 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5225 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5226 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5227 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | return 0; |
| 5229 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5230 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5232 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5233 | { |
| 5234 | int err; |
| 5235 | |
| 5236 | err = md_run(mddev); |
| 5237 | if (err) |
| 5238 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5239 | err = bitmap_load(mddev); |
| 5240 | if (err) { |
| 5241 | bitmap_destroy(mddev); |
| 5242 | goto out; |
| 5243 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5244 | |
| 5245 | md_wakeup_thread(mddev->thread); |
| 5246 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5247 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5248 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5249 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5250 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5251 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5252 | out: |
| 5253 | return err; |
| 5254 | } |
| 5255 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5256 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | { |
| 5258 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5260 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5262 | return -ENXIO; |
| 5263 | if (!mddev->pers) |
| 5264 | return -EINVAL; |
| 5265 | if (!mddev->ro) |
| 5266 | return -EBUSY; |
| 5267 | mddev->safemode = 0; |
| 5268 | mddev->ro = 0; |
| 5269 | set_disk_ro(disk, 0); |
| 5270 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5271 | mdname(mddev)); |
| 5272 | /* Kick recovery or resync if necessary */ |
| 5273 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5274 | md_wakeup_thread(mddev->thread); |
| 5275 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5276 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5277 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5278 | } |
| 5279 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5280 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5281 | { |
| 5282 | mddev->array_sectors = 0; |
| 5283 | mddev->external_size = 0; |
| 5284 | mddev->dev_sectors = 0; |
| 5285 | mddev->raid_disks = 0; |
| 5286 | mddev->recovery_cp = 0; |
| 5287 | mddev->resync_min = 0; |
| 5288 | mddev->resync_max = MaxSector; |
| 5289 | mddev->reshape_position = MaxSector; |
| 5290 | mddev->external = 0; |
| 5291 | mddev->persistent = 0; |
| 5292 | mddev->level = LEVEL_NONE; |
| 5293 | mddev->clevel[0] = 0; |
| 5294 | mddev->flags = 0; |
| 5295 | mddev->ro = 0; |
| 5296 | mddev->metadata_type[0] = 0; |
| 5297 | mddev->chunk_sectors = 0; |
| 5298 | mddev->ctime = mddev->utime = 0; |
| 5299 | mddev->layout = 0; |
| 5300 | mddev->max_disks = 0; |
| 5301 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5302 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5303 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5304 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5305 | mddev->new_level = LEVEL_NONE; |
| 5306 | mddev->new_layout = 0; |
| 5307 | mddev->new_chunk_sectors = 0; |
| 5308 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5309 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5310 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5311 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5312 | mddev->recovery = 0; |
| 5313 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5314 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5315 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5316 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5317 | mddev->private = NULL; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5318 | mddev->merge_check_needed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5319 | mddev->bitmap_info.offset = 0; |
| 5320 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5321 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5322 | mddev->bitmap_info.chunksize = 0; |
| 5323 | mddev->bitmap_info.daemon_sleep = 0; |
| 5324 | mddev->bitmap_info.max_write_behind = 0; |
| 5325 | } |
| 5326 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5327 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5328 | { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 5329 | if (mddev_is_clustered(mddev)) |
| 5330 | md_cluster_ops->metadata_update_start(mddev); |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 5331 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5332 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5333 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5334 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5335 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5336 | } |
| 5337 | |
| 5338 | del_timer_sync(&mddev->safemode_timer); |
| 5339 | |
| 5340 | bitmap_flush(mddev); |
| 5341 | md_super_wait(mddev); |
| 5342 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5343 | if (mddev->ro == 0 && |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5344 | (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5345 | /* mark array as shutdown cleanly */ |
| 5346 | mddev->in_sync = 1; |
| 5347 | md_update_sb(mddev, 1); |
| 5348 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 5349 | if (mddev_is_clustered(mddev)) |
| 5350 | md_cluster_ops->metadata_update_finish(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5351 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5352 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5353 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5354 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 5355 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5356 | __md_stop_writes(mddev); |
| 5357 | mddev_unlock(mddev); |
| 5358 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5359 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5360 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5361 | static void mddev_detach(struct mddev *mddev) |
| 5362 | { |
| 5363 | struct bitmap *bitmap = mddev->bitmap; |
| 5364 | /* wait for behind writes to complete */ |
| 5365 | if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { |
| 5366 | printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n", |
| 5367 | mdname(mddev)); |
| 5368 | /* need to kick something here to make sure I/O goes? */ |
| 5369 | wait_event(bitmap->behind_wait, |
| 5370 | atomic_read(&bitmap->behind_writes) == 0); |
| 5371 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5372 | if (mddev->pers && mddev->pers->quiesce) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5373 | mddev->pers->quiesce(mddev, 1); |
| 5374 | mddev->pers->quiesce(mddev, 0); |
| 5375 | } |
| 5376 | md_unregister_thread(&mddev->thread); |
| 5377 | if (mddev->queue) |
| 5378 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 5379 | } |
| 5380 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5381 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5382 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5383 | struct md_personality *pers = mddev->pers; |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5384 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 5385 | /* Ensure ->event_work is done */ |
| 5386 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5387 | spin_lock(&mddev->lock); |
| 5388 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5389 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5390 | spin_unlock(&mddev->lock); |
| 5391 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5392 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5393 | if (pers->sync_request && mddev->to_remove == NULL) |
| 5394 | mddev->to_remove = &md_redundancy_group; |
| 5395 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5396 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5397 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5398 | |
| 5399 | void md_stop(struct mddev *mddev) |
| 5400 | { |
| 5401 | /* stop the array and free an attached data structures. |
| 5402 | * This is called from dm-raid |
| 5403 | */ |
| 5404 | __md_stop(mddev); |
| 5405 | bitmap_destroy(mddev); |
| 5406 | if (mddev->bio_set) |
| 5407 | bioset_free(mddev->bio_set); |
| 5408 | } |
| 5409 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5410 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5411 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5412 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5413 | { |
| 5414 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5415 | int did_freeze = 0; |
| 5416 | |
| 5417 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5418 | did_freeze = 1; |
| 5419 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5420 | md_wakeup_thread(mddev->thread); |
| 5421 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5422 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5423 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5424 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5425 | /* Thread might be blocked waiting for metadata update |
| 5426 | * which will now never happen */ |
| 5427 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5428 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5429 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5430 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 5431 | &mddev->recovery)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5432 | mddev_lock_nointr(mddev); |
| 5433 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5434 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5435 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5436 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5437 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5438 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5439 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5440 | if (did_freeze) { |
| 5441 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5442 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5443 | md_wakeup_thread(mddev->thread); |
| 5444 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5445 | err = -EBUSY; |
| 5446 | goto out; |
| 5447 | } |
| 5448 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5449 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5450 | |
| 5451 | err = -ENXIO; |
| 5452 | if (mddev->ro==1) |
| 5453 | goto out; |
| 5454 | mddev->ro = 1; |
| 5455 | set_disk_ro(mddev->gendisk, 1); |
| 5456 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5457 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5458 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5459 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5460 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5461 | } |
| 5462 | out: |
| 5463 | mutex_unlock(&mddev->open_mutex); |
| 5464 | return err; |
| 5465 | } |
| 5466 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5467 | /* mode: |
| 5468 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5469 | * 2 - stop but do not disassemble array |
| 5470 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5471 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5472 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5475 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5476 | int did_freeze = 0; |
| 5477 | |
| 5478 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5479 | did_freeze = 1; |
| 5480 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5481 | md_wakeup_thread(mddev->thread); |
| 5482 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5483 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5484 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5485 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5486 | /* Thread might be blocked waiting for metadata update |
| 5487 | * which will now never happen */ |
| 5488 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5489 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5490 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5491 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 5492 | !test_bit(MD_RECOVERY_RUNNING, |
| 5493 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5494 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5495 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5496 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5497 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5498 | mddev->sysfs_active || |
| 5499 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5500 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5501 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5502 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5503 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5504 | if (did_freeze) { |
| 5505 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5506 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5507 | md_wakeup_thread(mddev->thread); |
| 5508 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 5509 | return -EBUSY; |
| 5510 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5511 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5512 | if (mddev->ro) |
| 5513 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5514 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5515 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5516 | __md_stop(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5517 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5518 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5519 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5520 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5521 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5522 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5523 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5524 | if (rdev->raid_disk >= 0) |
| 5525 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5526 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5527 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5528 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5529 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5530 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5531 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5532 | if (mddev->ro) |
| 5533 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5534 | } else |
| 5535 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | /* |
| 5537 | * Free resources if final stop |
| 5538 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5539 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5540 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5541 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5542 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5543 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5544 | struct file *f = mddev->bitmap_info.file; |
| 5545 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5546 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5547 | spin_unlock(&mddev->lock); |
| 5548 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5549 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5550 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5552 | export_array(mddev); |
| 5553 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5554 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5555 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5556 | if (mddev->hold_active == UNTIL_STOP) |
| 5557 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5558 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5559 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5560 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5561 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5562 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5563 | } |
| 5564 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5565 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5566 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5567 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5568 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | int err; |
| 5570 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5571 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5572 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5573 | |
| 5574 | printk(KERN_INFO "md: running: "); |
| 5575 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5576 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5577 | char b[BDEVNAME_SIZE]; |
| 5578 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5579 | } |
| 5580 | printk("\n"); |
| 5581 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5582 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | if (err) { |
| 5584 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5585 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5586 | } |
| 5587 | } |
| 5588 | |
| 5589 | /* |
| 5590 | * lets try to run arrays based on all disks that have arrived |
| 5591 | * until now. (those are in pending_raid_disks) |
| 5592 | * |
| 5593 | * the method: pick the first pending disk, collect all disks with |
| 5594 | * the same UUID, remove all from the pending list and put them into |
| 5595 | * the 'same_array' list. Then order this list based on superblock |
| 5596 | * update time (freshest comes first), kick out 'old' disks and |
| 5597 | * compare superblocks. If everything's fine then run it. |
| 5598 | * |
| 5599 | * If "unit" is allocated, then bump its reference count |
| 5600 | */ |
| 5601 | static void autorun_devices(int part) |
| 5602 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5603 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5604 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | char b[BDEVNAME_SIZE]; |
| 5606 | |
| 5607 | printk(KERN_INFO "md: autorun ...\n"); |
| 5608 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5609 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5611 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5612 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5613 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5614 | |
| 5615 | printk(KERN_INFO "md: considering %s ...\n", |
| 5616 | bdevname(rdev0->bdev,b)); |
| 5617 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5618 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5619 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5620 | printk(KERN_INFO "md: adding %s ...\n", |
| 5621 | bdevname(rdev->bdev,b)); |
| 5622 | list_move(&rdev->same_set, &candidates); |
| 5623 | } |
| 5624 | /* |
| 5625 | * now we have a set of devices, with all of them having |
| 5626 | * mostly sane superblocks. It's time to allocate the |
| 5627 | * mddev. |
| 5628 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5629 | if (part) { |
| 5630 | dev = MKDEV(mdp_major, |
| 5631 | rdev0->preferred_minor << MdpMinorShift); |
| 5632 | unit = MINOR(dev) >> MdpMinorShift; |
| 5633 | } else { |
| 5634 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5635 | unit = MINOR(dev); |
| 5636 | } |
| 5637 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5639 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5640 | break; |
| 5641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | |
| 5643 | md_probe(dev, NULL, NULL); |
| 5644 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5645 | if (!mddev || !mddev->gendisk) { |
| 5646 | if (mddev) |
| 5647 | mddev_put(mddev); |
| 5648 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5649 | "md: cannot allocate memory for md drive.\n"); |
| 5650 | break; |
| 5651 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5652 | if (mddev_lock(mddev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5653 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5654 | mdname(mddev)); |
| 5655 | else if (mddev->raid_disks || mddev->major_version |
| 5656 | || !list_empty(&mddev->disks)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5657 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | "md: %s already running, cannot run %s\n", |
| 5659 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5660 | mddev_unlock(mddev); |
| 5661 | } else { |
| 5662 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5663 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5664 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5665 | list_del_init(&rdev->same_set); |
| 5666 | if (bind_rdev_to_array(rdev, mddev)) |
| 5667 | export_rdev(rdev); |
| 5668 | } |
| 5669 | autorun_array(mddev); |
| 5670 | mddev_unlock(mddev); |
| 5671 | } |
| 5672 | /* on success, candidates will be empty, on error |
| 5673 | * it won't... |
| 5674 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5675 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5676 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5677 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5678 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | mddev_put(mddev); |
| 5680 | } |
| 5681 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5682 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5683 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5684 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5685 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | { |
| 5687 | mdu_version_t ver; |
| 5688 | |
| 5689 | ver.major = MD_MAJOR_VERSION; |
| 5690 | ver.minor = MD_MINOR_VERSION; |
| 5691 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5692 | |
| 5693 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5694 | return -EFAULT; |
| 5695 | |
| 5696 | return 0; |
| 5697 | } |
| 5698 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5699 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5700 | { |
| 5701 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5702 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5703 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5704 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5705 | nr = working = insync = failed = spare = 0; |
| 5706 | rcu_read_lock(); |
| 5707 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5709 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | failed++; |
| 5711 | else { |
| 5712 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5713 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5714 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | else |
| 5716 | spare++; |
| 5717 | } |
| 5718 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5719 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5720 | |
| 5721 | info.major_version = mddev->major_version; |
| 5722 | info.minor_version = mddev->minor_version; |
| 5723 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5724 | info.ctime = mddev->ctime; |
| 5725 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5726 | info.size = mddev->dev_sectors / 2; |
| 5727 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5728 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | info.nr_disks = nr; |
| 5730 | info.raid_disks = mddev->raid_disks; |
| 5731 | info.md_minor = mddev->md_minor; |
| 5732 | info.not_persistent= !mddev->persistent; |
| 5733 | |
| 5734 | info.utime = mddev->utime; |
| 5735 | info.state = 0; |
| 5736 | if (mddev->in_sync) |
| 5737 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5738 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 5739 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 5740 | if (mddev_is_clustered(mddev)) |
| 5741 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5742 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | info.working_disks = working; |
| 5744 | info.failed_disks = failed; |
| 5745 | info.spare_disks = spare; |
| 5746 | |
| 5747 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5748 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5749 | |
| 5750 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5751 | return -EFAULT; |
| 5752 | |
| 5753 | return 0; |
| 5754 | } |
| 5755 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5756 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5757 | { |
| 5758 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5759 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5760 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5761 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5762 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5763 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5764 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5765 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5766 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5767 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame^] | 5768 | /* bitmap enabled */ |
| 5769 | if (mddev->bitmap_info.file) { |
| 5770 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 5771 | sizeof(file->pathname)); |
| 5772 | if (IS_ERR(ptr)) |
| 5773 | err = PTR_ERR(ptr); |
| 5774 | else |
| 5775 | memmove(file->pathname, ptr, |
| 5776 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 5777 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5778 | spin_unlock(&mddev->lock); |
| 5779 | |
| 5780 | if (err == 0 && |
| 5781 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5782 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5783 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5784 | kfree(file); |
| 5785 | return err; |
| 5786 | } |
| 5787 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5788 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5789 | { |
| 5790 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5791 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | |
| 5793 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5794 | return -EFAULT; |
| 5795 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5796 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 5797 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5798 | if (rdev) { |
| 5799 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5800 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5801 | info.raid_disk = rdev->raid_disk; |
| 5802 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5803 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5804 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5805 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5806 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5807 | info.state |= (1<<MD_DISK_SYNC); |
| 5808 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5809 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5810 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5811 | } else { |
| 5812 | info.major = info.minor = 0; |
| 5813 | info.raid_disk = -1; |
| 5814 | info.state = (1<<MD_DISK_REMOVED); |
| 5815 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5816 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5817 | |
| 5818 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5819 | return -EFAULT; |
| 5820 | |
| 5821 | return 0; |
| 5822 | } |
| 5823 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5824 | 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] | 5825 | { |
| 5826 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5827 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | dev_t dev = MKDEV(info->major,info->minor); |
| 5829 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5830 | if (mddev_is_clustered(mddev) && |
| 5831 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 5832 | pr_err("%s: Cannot add to clustered mddev.\n", |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5833 | mdname(mddev)); |
| 5834 | return -EINVAL; |
| 5835 | } |
| 5836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5837 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5838 | return -EOVERFLOW; |
| 5839 | |
| 5840 | if (!mddev->raid_disks) { |
| 5841 | int err; |
| 5842 | /* expecting a device which has a superblock */ |
| 5843 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5844 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5845 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5846 | "md: md_import_device returned %ld\n", |
| 5847 | PTR_ERR(rdev)); |
| 5848 | return PTR_ERR(rdev); |
| 5849 | } |
| 5850 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5851 | struct md_rdev *rdev0 |
| 5852 | = list_entry(mddev->disks.next, |
| 5853 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5854 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5855 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5856 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5857 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | "md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5859 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5860 | bdevname(rdev0->bdev,b2)); |
| 5861 | export_rdev(rdev); |
| 5862 | return -EINVAL; |
| 5863 | } |
| 5864 | } |
| 5865 | err = bind_rdev_to_array(rdev, mddev); |
| 5866 | if (err) |
| 5867 | export_rdev(rdev); |
| 5868 | return err; |
| 5869 | } |
| 5870 | |
| 5871 | /* |
| 5872 | * add_new_disk can be used once the array is assembled |
| 5873 | * to add "hot spares". They must already have a superblock |
| 5874 | * written |
| 5875 | */ |
| 5876 | if (mddev->pers) { |
| 5877 | int err; |
| 5878 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5879 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5880 | "%s: personality does not support diskops!\n", |
| 5881 | mdname(mddev)); |
| 5882 | return -EINVAL; |
| 5883 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5884 | if (mddev->persistent) |
| 5885 | rdev = md_import_device(dev, mddev->major_version, |
| 5886 | mddev->minor_version); |
| 5887 | else |
| 5888 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5889 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5890 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5891 | "md: md_import_device returned %ld\n", |
| 5892 | PTR_ERR(rdev)); |
| 5893 | return PTR_ERR(rdev); |
| 5894 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5895 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5896 | if (!mddev->persistent) { |
| 5897 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5898 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5899 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5900 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 5901 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5902 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5903 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 5904 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5905 | } else |
| 5906 | super_types[mddev->major_version]. |
| 5907 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5908 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 5909 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5910 | /* This was a hot-add request, but events doesn't |
| 5911 | * match, so reject it. |
| 5912 | */ |
| 5913 | export_rdev(rdev); |
| 5914 | return -EINVAL; |
| 5915 | } |
| 5916 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5917 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5918 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5919 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5920 | else |
| 5921 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5922 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5923 | /* |
| 5924 | * check whether the device shows up in other nodes |
| 5925 | */ |
| 5926 | if (mddev_is_clustered(mddev)) { |
| 5927 | if (info->state & (1 << MD_DISK_CANDIDATE)) { |
| 5928 | /* Through --cluster-confirm */ |
| 5929 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 5930 | err = md_cluster_ops->new_disk_ack(mddev, true); |
| 5931 | if (err) { |
| 5932 | export_rdev(rdev); |
| 5933 | return err; |
| 5934 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5935 | } else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
| 5936 | /* --add initiated by this node */ |
| 5937 | err = md_cluster_ops->add_new_disk_start(mddev, rdev); |
| 5938 | if (err) { |
| 5939 | md_cluster_ops->add_new_disk_finish(mddev); |
| 5940 | export_rdev(rdev); |
| 5941 | return err; |
| 5942 | } |
| 5943 | } |
| 5944 | } |
| 5945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | rdev->raid_disk = -1; |
| 5947 | err = bind_rdev_to_array(rdev, mddev); |
| 5948 | if (err) |
| 5949 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5950 | else |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 5951 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5952 | if (mddev_is_clustered(mddev) && |
| 5953 | (info->state & (1 << MD_DISK_CLUSTER_ADD))) |
| 5954 | md_cluster_ops->add_new_disk_finish(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | return err; |
| 5956 | } |
| 5957 | |
| 5958 | /* otherwise, add_new_disk is only allowed |
| 5959 | * for major_version==0 superblocks |
| 5960 | */ |
| 5961 | if (mddev->major_version != 0) { |
| 5962 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5963 | mdname(mddev)); |
| 5964 | return -EINVAL; |
| 5965 | } |
| 5966 | |
| 5967 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5968 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5969 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5970 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5971 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | "md: error, md_import_device() returned %ld\n", |
| 5973 | PTR_ERR(rdev)); |
| 5974 | return PTR_ERR(rdev); |
| 5975 | } |
| 5976 | rdev->desc_nr = info->number; |
| 5977 | if (info->raid_disk < mddev->raid_disks) |
| 5978 | rdev->raid_disk = info->raid_disk; |
| 5979 | else |
| 5980 | rdev->raid_disk = -1; |
| 5981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5982 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5983 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5984 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5985 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5986 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5987 | set_bit(WriteMostly, &rdev->flags); |
| 5988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5989 | if (!mddev->persistent) { |
| 5990 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5991 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5992 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5993 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5994 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5995 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5996 | err = bind_rdev_to_array(rdev, mddev); |
| 5997 | if (err) { |
| 5998 | export_rdev(rdev); |
| 5999 | return err; |
| 6000 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | } |
| 6002 | |
| 6003 | return 0; |
| 6004 | } |
| 6005 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6006 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6007 | { |
| 6008 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6009 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6011 | rdev = find_rdev(mddev, dev); |
| 6012 | if (!rdev) |
| 6013 | return -ENXIO; |
| 6014 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6015 | if (mddev_is_clustered(mddev)) |
| 6016 | md_cluster_ops->metadata_update_start(mddev); |
| 6017 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6018 | clear_bit(Blocked, &rdev->flags); |
| 6019 | remove_and_add_spares(mddev, rdev); |
| 6020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6021 | if (rdev->raid_disk >= 0) |
| 6022 | goto busy; |
| 6023 | |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6024 | if (mddev_is_clustered(mddev)) |
| 6025 | md_cluster_ops->remove_disk(mddev, rdev); |
| 6026 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6027 | md_kick_rdev_from_array(rdev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6028 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6029 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6030 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6031 | if (mddev_is_clustered(mddev)) |
| 6032 | md_cluster_ops->metadata_update_finish(mddev); |
| 6033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6034 | return 0; |
| 6035 | busy: |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6036 | if (mddev_is_clustered(mddev)) |
| 6037 | md_cluster_ops->metadata_update_cancel(mddev); |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 6038 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6039 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6040 | return -EBUSY; |
| 6041 | } |
| 6042 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6043 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6044 | { |
| 6045 | char b[BDEVNAME_SIZE]; |
| 6046 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6047 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6048 | |
| 6049 | if (!mddev->pers) |
| 6050 | return -ENODEV; |
| 6051 | |
| 6052 | if (mddev->major_version != 0) { |
| 6053 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 6054 | " version-0 superblocks.\n", |
| 6055 | mdname(mddev)); |
| 6056 | return -EINVAL; |
| 6057 | } |
| 6058 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6059 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6060 | "%s: personality does not support diskops!\n", |
| 6061 | mdname(mddev)); |
| 6062 | return -EINVAL; |
| 6063 | } |
| 6064 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6065 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6067 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | "md: error, md_import_device() returned %ld\n", |
| 6069 | PTR_ERR(rdev)); |
| 6070 | return -EINVAL; |
| 6071 | } |
| 6072 | |
| 6073 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6074 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6076 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6077 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6078 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6079 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6080 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6081 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | "md: can not hot-add faulty %s disk to %s!\n", |
| 6083 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6084 | err = -EINVAL; |
| 6085 | goto abort_export; |
| 6086 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6087 | |
| 6088 | if (mddev_is_clustered(mddev)) |
| 6089 | md_cluster_ops->metadata_update_start(mddev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6090 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6091 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 6092 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6093 | err = bind_rdev_to_array(rdev, mddev); |
| 6094 | if (err) |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6095 | goto abort_clustered; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6096 | |
| 6097 | /* |
| 6098 | * The rest should better be atomic, we can have disk failures |
| 6099 | * noticed in interrupt contexts ... |
| 6100 | */ |
| 6101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6102 | rdev->raid_disk = -1; |
| 6103 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6104 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6106 | if (mddev_is_clustered(mddev)) |
| 6107 | md_cluster_ops->metadata_update_finish(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | /* |
| 6109 | * Kick recovery, maybe this spare has to be added to the |
| 6110 | * array immediately. |
| 6111 | */ |
| 6112 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6113 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6114 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | return 0; |
| 6116 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6117 | abort_clustered: |
| 6118 | if (mddev_is_clustered(mddev)) |
| 6119 | md_cluster_ops->metadata_update_cancel(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | abort_export: |
| 6121 | export_rdev(rdev); |
| 6122 | return err; |
| 6123 | } |
| 6124 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6125 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6126 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6127 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6128 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6129 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 6130 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6131 | return -EBUSY; |
| 6132 | if (mddev->recovery || mddev->sync_thread) |
| 6133 | return -EBUSY; |
| 6134 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6135 | } |
| 6136 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6137 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6138 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6139 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6140 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6141 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 6142 | return -EEXIST; /* cannot add when bitmap is present */ |
| 6143 | f = fget(fd); |
| 6144 | |
| 6145 | if (f == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6146 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 6147 | mdname(mddev)); |
| 6148 | return -EBADF; |
| 6149 | } |
| 6150 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6151 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6152 | if (!S_ISREG(inode->i_mode)) { |
| 6153 | printk(KERN_ERR "%s: error: bitmap file must be a regular file\n", |
| 6154 | mdname(mddev)); |
| 6155 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6156 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6157 | printk(KERN_ERR "%s: error: bitmap file must open for write\n", |
| 6158 | mdname(mddev)); |
| 6159 | err = -EBADF; |
| 6160 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6161 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 6162 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6163 | err = -EBUSY; |
| 6164 | } |
| 6165 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6166 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6167 | return err; |
| 6168 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6169 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6170 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6171 | } else if (mddev->bitmap == NULL) |
| 6172 | return -ENOENT; /* cannot remove what isn't there */ |
| 6173 | err = 0; |
| 6174 | if (mddev->pers) { |
| 6175 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6176 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6177 | struct bitmap *bitmap; |
| 6178 | |
| 6179 | bitmap = bitmap_create(mddev, -1); |
| 6180 | if (!IS_ERR(bitmap)) { |
| 6181 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6182 | err = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6183 | } else |
| 6184 | err = PTR_ERR(bitmap); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6185 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6186 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6187 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6188 | fd = -1; /* make sure to put the file */ |
| 6189 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6190 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6191 | } |
| 6192 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6193 | struct file *f = mddev->bitmap_info.file; |
| 6194 | if (f) { |
| 6195 | spin_lock(&mddev->lock); |
| 6196 | mddev->bitmap_info.file = NULL; |
| 6197 | spin_unlock(&mddev->lock); |
| 6198 | fput(f); |
| 6199 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6200 | } |
| 6201 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6202 | return err; |
| 6203 | } |
| 6204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6205 | /* |
| 6206 | * set_array_info is used two different ways |
| 6207 | * The original usage is when creating a new array. |
| 6208 | * In this usage, raid_disks is > 0 and it together with |
| 6209 | * level, size, not_persistent,layout,chunksize determine the |
| 6210 | * shape of the array. |
| 6211 | * This will always create an array with a type-0.90.0 superblock. |
| 6212 | * The newer usage is when assembling an array. |
| 6213 | * In this case raid_disks will be 0, and the major_version field is |
| 6214 | * use to determine which style super-blocks are to be found on the devices. |
| 6215 | * The minor and patch _version numbers are also kept incase the |
| 6216 | * super_block handler wishes to interpret them. |
| 6217 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6218 | 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] | 6219 | { |
| 6220 | |
| 6221 | if (info->raid_disks == 0) { |
| 6222 | /* just setting version number for superblock loading */ |
| 6223 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6224 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6225 | super_types[info->major_version].name == NULL) { |
| 6226 | /* maybe try to auto-load a module? */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6227 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6228 | "md: superblock version %d not known\n", |
| 6229 | info->major_version); |
| 6230 | return -EINVAL; |
| 6231 | } |
| 6232 | mddev->major_version = info->major_version; |
| 6233 | mddev->minor_version = info->minor_version; |
| 6234 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6235 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6236 | /* ensure mddev_put doesn't delete this now that there |
| 6237 | * is some minimal configuration. |
| 6238 | */ |
| 6239 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6240 | return 0; |
| 6241 | } |
| 6242 | mddev->major_version = MD_MAJOR_VERSION; |
| 6243 | mddev->minor_version = MD_MINOR_VERSION; |
| 6244 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 6245 | mddev->ctime = get_seconds(); |
| 6246 | |
| 6247 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6248 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6249 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6250 | mddev->raid_disks = info->raid_disks; |
| 6251 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6252 | * openned |
| 6253 | */ |
| 6254 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6255 | mddev->recovery_cp = MaxSector; |
| 6256 | else |
| 6257 | mddev->recovery_cp = 0; |
| 6258 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6259 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6260 | |
| 6261 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6262 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6263 | |
| 6264 | mddev->max_disks = MD_SB_DISKS; |
| 6265 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6266 | if (mddev->persistent) |
| 6267 | mddev->flags = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6268 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6269 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6270 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6271 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6272 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6273 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6274 | mddev->reshape_position = MaxSector; |
| 6275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6276 | /* |
| 6277 | * Generate a 128 bit UUID |
| 6278 | */ |
| 6279 | get_random_bytes(mddev->uuid, 16); |
| 6280 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6281 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6282 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6283 | mddev->new_layout = mddev->layout; |
| 6284 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6285 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6287 | return 0; |
| 6288 | } |
| 6289 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6290 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6291 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6292 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6293 | |
| 6294 | if (mddev->external_size) |
| 6295 | return; |
| 6296 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6297 | mddev->array_sectors = array_sectors; |
| 6298 | } |
| 6299 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6300 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6301 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6302 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6303 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6304 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6305 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6306 | |
| 6307 | if (mddev->pers->resize == NULL) |
| 6308 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6309 | /* The "num_sectors" is the number of sectors of each device that |
| 6310 | * is used. This can only make sense for arrays with redundancy. |
| 6311 | * linear and raid0 always use whatever space is available. We can only |
| 6312 | * consider changing this number if no resync or reconstruction is |
| 6313 | * 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] | 6314 | * 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] | 6315 | * of each device. If num_sectors is zero, we find the largest size |
| 6316 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6317 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6318 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6319 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6320 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6321 | if (mddev->ro) |
| 6322 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6323 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6324 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6325 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6326 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6327 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6328 | num_sectors = avail; |
| 6329 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6330 | return -ENOSPC; |
| 6331 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6332 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6333 | if (!rv) |
| 6334 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6335 | return rv; |
| 6336 | } |
| 6337 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6338 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6339 | { |
| 6340 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6341 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6342 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6343 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6344 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6345 | if (mddev->ro) |
| 6346 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6347 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6348 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6349 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6350 | if (mddev->sync_thread || |
| 6351 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6352 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6353 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6354 | |
| 6355 | rdev_for_each(rdev, mddev) { |
| 6356 | if (mddev->raid_disks < raid_disks && |
| 6357 | rdev->data_offset < rdev->new_data_offset) |
| 6358 | return -EINVAL; |
| 6359 | if (mddev->raid_disks > raid_disks && |
| 6360 | rdev->data_offset > rdev->new_data_offset) |
| 6361 | return -EINVAL; |
| 6362 | } |
| 6363 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6364 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6365 | if (mddev->delta_disks < 0) |
| 6366 | mddev->reshape_backwards = 1; |
| 6367 | else if (mddev->delta_disks > 0) |
| 6368 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6369 | |
| 6370 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6371 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6372 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6373 | mddev->reshape_backwards = 0; |
| 6374 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6375 | return rv; |
| 6376 | } |
| 6377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6378 | /* |
| 6379 | * update_array_info is used to change the configuration of an |
| 6380 | * on-line array. |
| 6381 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6382 | * fields in the info are checked against the array. |
| 6383 | * Any differences that cannot be handled will cause an error. |
| 6384 | * Normally, only one change can be managed at a time. |
| 6385 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6386 | 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] | 6387 | { |
| 6388 | int rv = 0; |
| 6389 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6390 | int state = 0; |
| 6391 | |
| 6392 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6393 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6394 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6395 | |
| 6396 | if (mddev->major_version != info->major_version || |
| 6397 | mddev->minor_version != info->minor_version || |
| 6398 | /* mddev->patch_version != info->patch_version || */ |
| 6399 | mddev->ctime != info->ctime || |
| 6400 | mddev->level != info->level || |
| 6401 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 6402 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6403 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6404 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6405 | ((state^info->state) & 0xfffffe00) |
| 6406 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6407 | return -EINVAL; |
| 6408 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6409 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6410 | cnt++; |
| 6411 | if (mddev->raid_disks != info->raid_disks) |
| 6412 | cnt++; |
| 6413 | if (mddev->layout != info->layout) |
| 6414 | cnt++; |
| 6415 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6416 | cnt++; |
| 6417 | if (cnt == 0) |
| 6418 | return 0; |
| 6419 | if (cnt > 1) |
| 6420 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6421 | |
| 6422 | if (mddev->layout != info->layout) { |
| 6423 | /* Change layout |
| 6424 | * we don't need to do anything at the md level, the |
| 6425 | * personality will take care of it all. |
| 6426 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6427 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6428 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6429 | else { |
| 6430 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6431 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6432 | if (rv) |
| 6433 | mddev->new_layout = mddev->layout; |
| 6434 | return rv; |
| 6435 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6436 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6437 | if (mddev_is_clustered(mddev)) |
| 6438 | md_cluster_ops->metadata_update_start(mddev); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6439 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6440 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6441 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6442 | if (mddev->raid_disks != info->raid_disks) |
| 6443 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6444 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6445 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6446 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 6447 | rv = -EINVAL; |
| 6448 | goto err; |
| 6449 | } |
| 6450 | if (mddev->recovery || mddev->sync_thread) { |
| 6451 | rv = -EBUSY; |
| 6452 | goto err; |
| 6453 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6454 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6455 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6456 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6457 | if (mddev->bitmap) { |
| 6458 | rv = -EEXIST; |
| 6459 | goto err; |
| 6460 | } |
| 6461 | if (mddev->bitmap_info.default_offset == 0) { |
| 6462 | rv = -EINVAL; |
| 6463 | goto err; |
| 6464 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6465 | mddev->bitmap_info.offset = |
| 6466 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6467 | mddev->bitmap_info.space = |
| 6468 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6469 | mddev->pers->quiesce(mddev, 1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6470 | bitmap = bitmap_create(mddev, -1); |
| 6471 | if (!IS_ERR(bitmap)) { |
| 6472 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6473 | rv = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6474 | } else |
| 6475 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6476 | if (rv) |
| 6477 | bitmap_destroy(mddev); |
| 6478 | mddev->pers->quiesce(mddev, 0); |
| 6479 | } else { |
| 6480 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6481 | if (!mddev->bitmap) { |
| 6482 | rv = -ENOENT; |
| 6483 | goto err; |
| 6484 | } |
| 6485 | if (mddev->bitmap->storage.file) { |
| 6486 | rv = -EINVAL; |
| 6487 | goto err; |
| 6488 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6489 | mddev->pers->quiesce(mddev, 1); |
| 6490 | bitmap_destroy(mddev); |
| 6491 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6492 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6493 | } |
| 6494 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6495 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6496 | if (mddev_is_clustered(mddev)) |
| 6497 | md_cluster_ops->metadata_update_finish(mddev); |
| 6498 | return rv; |
| 6499 | err: |
| 6500 | if (mddev_is_clustered(mddev)) |
| 6501 | md_cluster_ops->metadata_update_cancel(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6502 | return rv; |
| 6503 | } |
| 6504 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6505 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6506 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6507 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6508 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6509 | |
| 6510 | if (mddev->pers == NULL) |
| 6511 | return -ENODEV; |
| 6512 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6513 | rcu_read_lock(); |
| 6514 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6515 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6516 | err = -ENODEV; |
| 6517 | else { |
| 6518 | md_error(mddev, rdev); |
| 6519 | if (!test_bit(Faulty, &rdev->flags)) |
| 6520 | err = -EBUSY; |
| 6521 | } |
| 6522 | rcu_read_unlock(); |
| 6523 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6524 | } |
| 6525 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6526 | /* |
| 6527 | * We have a problem here : there is no easy way to give a CHS |
| 6528 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6529 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6530 | * dosfs just mad... ;-) |
| 6531 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6532 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6533 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6534 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6535 | |
| 6536 | geo->heads = 2; |
| 6537 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6538 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6539 | return 0; |
| 6540 | } |
| 6541 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6542 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 6543 | { |
| 6544 | switch (cmd) { |
| 6545 | case ADD_NEW_DISK: |
| 6546 | case BLKROSET: |
| 6547 | case GET_ARRAY_INFO: |
| 6548 | case GET_BITMAP_FILE: |
| 6549 | case GET_DISK_INFO: |
| 6550 | case HOT_ADD_DISK: |
| 6551 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6552 | case RAID_AUTORUN: |
| 6553 | case RAID_VERSION: |
| 6554 | case RESTART_ARRAY_RW: |
| 6555 | case RUN_ARRAY: |
| 6556 | case SET_ARRAY_INFO: |
| 6557 | case SET_BITMAP_FILE: |
| 6558 | case SET_DISK_FAULTY: |
| 6559 | case STOP_ARRAY: |
| 6560 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6561 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6562 | return true; |
| 6563 | default: |
| 6564 | return false; |
| 6565 | } |
| 6566 | } |
| 6567 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6568 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6569 | unsigned int cmd, unsigned long arg) |
| 6570 | { |
| 6571 | int err = 0; |
| 6572 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6573 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6574 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6575 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6576 | if (!md_ioctl_valid(cmd)) |
| 6577 | return -ENOTTY; |
| 6578 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6579 | switch (cmd) { |
| 6580 | case RAID_VERSION: |
| 6581 | case GET_ARRAY_INFO: |
| 6582 | case GET_DISK_INFO: |
| 6583 | break; |
| 6584 | default: |
| 6585 | if (!capable(CAP_SYS_ADMIN)) |
| 6586 | return -EACCES; |
| 6587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6588 | |
| 6589 | /* |
| 6590 | * Commands dealing with the RAID driver but not any |
| 6591 | * particular array: |
| 6592 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6593 | switch (cmd) { |
| 6594 | case RAID_VERSION: |
| 6595 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6596 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6598 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6599 | case RAID_AUTORUN: |
| 6600 | err = 0; |
| 6601 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6602 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6603 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6604 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6605 | } |
| 6606 | |
| 6607 | /* |
| 6608 | * Commands creating/starting a new array: |
| 6609 | */ |
| 6610 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6611 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6612 | |
| 6613 | if (!mddev) { |
| 6614 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6615 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6616 | } |
| 6617 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6618 | /* Some actions do not requires the mutex */ |
| 6619 | switch (cmd) { |
| 6620 | case GET_ARRAY_INFO: |
| 6621 | if (!mddev->raid_disks && !mddev->external) |
| 6622 | err = -ENODEV; |
| 6623 | else |
| 6624 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6625 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6626 | |
| 6627 | case GET_DISK_INFO: |
| 6628 | if (!mddev->raid_disks && !mddev->external) |
| 6629 | err = -ENODEV; |
| 6630 | else |
| 6631 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6632 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6633 | |
| 6634 | case SET_DISK_FAULTY: |
| 6635 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6636 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6637 | |
| 6638 | case GET_BITMAP_FILE: |
| 6639 | err = get_bitmap_file(mddev, argp); |
| 6640 | goto out; |
| 6641 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6642 | } |
| 6643 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 6644 | if (cmd == ADD_NEW_DISK) |
| 6645 | /* need to ensure md_delayed_delete() has completed */ |
| 6646 | flush_workqueue(md_misc_wq); |
| 6647 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 6648 | if (cmd == HOT_REMOVE_DISK) |
| 6649 | /* need to ensure recovery thread has run */ |
| 6650 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 6651 | !test_bit(MD_RECOVERY_NEEDED, |
| 6652 | &mddev->flags), |
| 6653 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6654 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 6655 | /* Need to flush page cache, and ensure no-one else opens |
| 6656 | * and writes |
| 6657 | */ |
| 6658 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6659 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6660 | mutex_unlock(&mddev->open_mutex); |
| 6661 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6662 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6663 | } |
| 6664 | set_bit(MD_STILL_CLOSED, &mddev->flags); |
| 6665 | mutex_unlock(&mddev->open_mutex); |
| 6666 | sync_blockdev(bdev); |
| 6667 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6668 | err = mddev_lock(mddev); |
| 6669 | if (err) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6670 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6671 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6672 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6673 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6674 | } |
| 6675 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6676 | if (cmd == SET_ARRAY_INFO) { |
| 6677 | mdu_array_info_t info; |
| 6678 | if (!arg) |
| 6679 | memset(&info, 0, sizeof(info)); |
| 6680 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6681 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6682 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6683 | } |
| 6684 | if (mddev->pers) { |
| 6685 | err = update_array_info(mddev, &info); |
| 6686 | if (err) { |
| 6687 | printk(KERN_WARNING "md: couldn't update" |
| 6688 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6689 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6690 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6691 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6692 | } |
| 6693 | if (!list_empty(&mddev->disks)) { |
| 6694 | printk(KERN_WARNING |
| 6695 | "md: array %s already has disks!\n", |
| 6696 | mdname(mddev)); |
| 6697 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6698 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6699 | } |
| 6700 | if (mddev->raid_disks) { |
| 6701 | printk(KERN_WARNING |
| 6702 | "md: array %s already initialised!\n", |
| 6703 | mdname(mddev)); |
| 6704 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6705 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6706 | } |
| 6707 | err = set_array_info(mddev, &info); |
| 6708 | if (err) { |
| 6709 | printk(KERN_WARNING "md: couldn't set" |
| 6710 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6711 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6712 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6713 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6714 | } |
| 6715 | |
| 6716 | /* |
| 6717 | * Commands querying/configuring an existing array: |
| 6718 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6719 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6720 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6721 | if ((!mddev->raid_disks && !mddev->external) |
| 6722 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6723 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6724 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6725 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6726 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6727 | } |
| 6728 | |
| 6729 | /* |
| 6730 | * Commands even a read-only array can execute: |
| 6731 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6732 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6733 | case RESTART_ARRAY_RW: |
| 6734 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6735 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6736 | |
| 6737 | case STOP_ARRAY: |
| 6738 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6739 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6740 | |
| 6741 | case STOP_ARRAY_RO: |
| 6742 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6743 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6744 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6745 | case HOT_REMOVE_DISK: |
| 6746 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6747 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6748 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6749 | case ADD_NEW_DISK: |
| 6750 | /* We can support ADD_NEW_DISK on read-only arrays |
| 6751 | * on if we are re-adding a preexisting device. |
| 6752 | * So require mddev->pers and MD_DISK_SYNC. |
| 6753 | */ |
| 6754 | if (mddev->pers) { |
| 6755 | mdu_disk_info_t info; |
| 6756 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6757 | err = -EFAULT; |
| 6758 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 6759 | /* Need to clear read-only for this */ |
| 6760 | break; |
| 6761 | else |
| 6762 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6763 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6764 | } |
| 6765 | break; |
| 6766 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6767 | case BLKROSET: |
| 6768 | if (get_user(ro, (int __user *)(arg))) { |
| 6769 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6770 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6771 | } |
| 6772 | err = -EINVAL; |
| 6773 | |
| 6774 | /* if the bdev is going readonly the value of mddev->ro |
| 6775 | * does not matter, no writes are coming |
| 6776 | */ |
| 6777 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6778 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6779 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6780 | /* are we are already prepared for writes? */ |
| 6781 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6782 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6783 | |
| 6784 | /* transitioning to readauto need only happen for |
| 6785 | * arrays that call md_write_start |
| 6786 | */ |
| 6787 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6788 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6789 | if (err == 0) { |
| 6790 | mddev->ro = 2; |
| 6791 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6792 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6793 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6794 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6795 | } |
| 6796 | |
| 6797 | /* |
| 6798 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6799 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6800 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 6801 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6802 | if (mddev->ro == 2) { |
| 6803 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6804 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6805 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6806 | /* mddev_unlock will wake thread */ |
| 6807 | /* If a device failed while we were read-only, we |
| 6808 | * need to make sure the metadata is updated now. |
| 6809 | */ |
| 6810 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
| 6811 | mddev_unlock(mddev); |
| 6812 | wait_event(mddev->sb_wait, |
| 6813 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && |
| 6814 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6815 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6816 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6817 | } else { |
| 6818 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6819 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6820 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6821 | } |
| 6822 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6823 | switch (cmd) { |
| 6824 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6825 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6826 | mdu_disk_info_t info; |
| 6827 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6828 | err = -EFAULT; |
| 6829 | else |
| 6830 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6831 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6832 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6833 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6834 | case CLUSTERED_DISK_NACK: |
| 6835 | if (mddev_is_clustered(mddev)) |
| 6836 | md_cluster_ops->new_disk_ack(mddev, false); |
| 6837 | else |
| 6838 | err = -EINVAL; |
| 6839 | goto unlock; |
| 6840 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6841 | case HOT_ADD_DISK: |
| 6842 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6843 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6844 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6845 | case RUN_ARRAY: |
| 6846 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6847 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6848 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6849 | case SET_BITMAP_FILE: |
| 6850 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6851 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6852 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6853 | default: |
| 6854 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6855 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6856 | } |
| 6857 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6858 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6859 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6860 | err != -EINVAL) |
| 6861 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6862 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6863 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6864 | return err; |
| 6865 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6866 | #ifdef CONFIG_COMPAT |
| 6867 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6868 | unsigned int cmd, unsigned long arg) |
| 6869 | { |
| 6870 | switch (cmd) { |
| 6871 | case HOT_REMOVE_DISK: |
| 6872 | case HOT_ADD_DISK: |
| 6873 | case SET_DISK_FAULTY: |
| 6874 | case SET_BITMAP_FILE: |
| 6875 | /* These take in integer arg, do not convert */ |
| 6876 | break; |
| 6877 | default: |
| 6878 | arg = (unsigned long)compat_ptr(arg); |
| 6879 | break; |
| 6880 | } |
| 6881 | |
| 6882 | return md_ioctl(bdev, mode, cmd, arg); |
| 6883 | } |
| 6884 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6885 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6886 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6887 | { |
| 6888 | /* |
| 6889 | * Succeed if we can lock the mddev, which confirms that |
| 6890 | * it isn't being stopped right now. |
| 6891 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6892 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6893 | int err; |
| 6894 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 6895 | if (!mddev) |
| 6896 | return -ENODEV; |
| 6897 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6898 | if (mddev->gendisk != bdev->bd_disk) { |
| 6899 | /* we are racing with mddev_put which is discarding this |
| 6900 | * bd_disk. |
| 6901 | */ |
| 6902 | mddev_put(mddev); |
| 6903 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6904 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6905 | /* Then retry the open from the top */ |
| 6906 | return -ERESTARTSYS; |
| 6907 | } |
| 6908 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6909 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6910 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6911 | goto out; |
| 6912 | |
| 6913 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6914 | atomic_inc(&mddev->openers); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6915 | clear_bit(MD_STILL_CLOSED, &mddev->flags); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6916 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6917 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6918 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6919 | out: |
| 6920 | return err; |
| 6921 | } |
| 6922 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 6923 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6924 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6925 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6926 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6927 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6928 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6929 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6930 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6931 | |
| 6932 | static int md_media_changed(struct gendisk *disk) |
| 6933 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6934 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6935 | |
| 6936 | return mddev->changed; |
| 6937 | } |
| 6938 | |
| 6939 | static int md_revalidate(struct gendisk *disk) |
| 6940 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6941 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6942 | |
| 6943 | mddev->changed = 0; |
| 6944 | return 0; |
| 6945 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6946 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6947 | { |
| 6948 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6949 | .open = md_open, |
| 6950 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6951 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6952 | #ifdef CONFIG_COMPAT |
| 6953 | .compat_ioctl = md_compat_ioctl, |
| 6954 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6955 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6956 | .media_changed = md_media_changed, |
| 6957 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6958 | }; |
| 6959 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6960 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6961 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6962 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6964 | /* |
| 6965 | * md_thread is a 'system-thread', it's priority should be very |
| 6966 | * high. We avoid resource deadlocks individually in each |
| 6967 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6968 | * the very same RT priority as kswapd, thus we will never get |
| 6969 | * into a priority inversion deadlock. |
| 6970 | * |
| 6971 | * we definitely have to have equal or higher priority than |
| 6972 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6973 | * many dirty RAID5 blocks. |
| 6974 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6975 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6976 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6977 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6979 | /* We need to wait INTERRUPTIBLE so that |
| 6980 | * we don't add to the load-average. |
| 6981 | * That means we need to be sure no signals are |
| 6982 | * pending |
| 6983 | */ |
| 6984 | if (signal_pending(current)) |
| 6985 | flush_signals(current); |
| 6986 | |
| 6987 | wait_event_interruptible_timeout |
| 6988 | (thread->wqueue, |
| 6989 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6990 | || kthread_should_stop(), |
| 6991 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6992 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6993 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6994 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6995 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6996 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6998 | return 0; |
| 6999 | } |
| 7000 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7001 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7002 | { |
| 7003 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7004 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7005 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 7006 | wake_up(&thread->wqueue); |
| 7007 | } |
| 7008 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7009 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7010 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7011 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7012 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7013 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7014 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7015 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7016 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7017 | if (!thread) |
| 7018 | return NULL; |
| 7019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7020 | init_waitqueue_head(&thread->wqueue); |
| 7021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7022 | thread->run = run; |
| 7023 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7024 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7025 | thread->tsk = kthread_run(md_thread, thread, |
| 7026 | "%s_%s", |
| 7027 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7028 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7029 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7030 | kfree(thread); |
| 7031 | return NULL; |
| 7032 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7033 | return thread; |
| 7034 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7035 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7036 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7037 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7038 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7039 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7040 | if (!thread) |
| 7041 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7042 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7043 | /* Locking ensures that mddev_unlock does not wake_up a |
| 7044 | * non-existent thread |
| 7045 | */ |
| 7046 | spin_lock(&pers_lock); |
| 7047 | *threadp = NULL; |
| 7048 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7049 | |
| 7050 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7051 | kfree(thread); |
| 7052 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7053 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7054 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7055 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7056 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7057 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7058 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7059 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7060 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7061 | return; |
| 7062 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7063 | if (mddev->degraded) |
| 7064 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7065 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7066 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7067 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7068 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 7069 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7070 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 7071 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7072 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7073 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7074 | |
| 7075 | /* seq_file implementation /proc/mdstat */ |
| 7076 | |
| 7077 | static void status_unused(struct seq_file *seq) |
| 7078 | { |
| 7079 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7080 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7081 | |
| 7082 | seq_printf(seq, "unused devices: "); |
| 7083 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7084 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7085 | char b[BDEVNAME_SIZE]; |
| 7086 | i++; |
| 7087 | seq_printf(seq, "%s ", |
| 7088 | bdevname(rdev->bdev,b)); |
| 7089 | } |
| 7090 | if (!i) |
| 7091 | seq_printf(seq, "<none>"); |
| 7092 | |
| 7093 | seq_printf(seq, "\n"); |
| 7094 | } |
| 7095 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7096 | static void status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7097 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7098 | sector_t max_sectors, resync, res; |
| 7099 | unsigned long dt, db; |
| 7100 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7101 | int scale; |
| 7102 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7103 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7104 | if (mddev->curr_resync <= 3) |
| 7105 | resync = 0; |
| 7106 | else |
| 7107 | resync = mddev->curr_resync |
| 7108 | - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7109 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7110 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 7111 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7112 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7113 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7114 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7115 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 7116 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7117 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7118 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7119 | * u32, as those are the requirements for sector_div. |
| 7120 | * Thus 'scale' must be at least 10 |
| 7121 | */ |
| 7122 | scale = 10; |
| 7123 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7124 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7125 | scale++; |
| 7126 | } |
| 7127 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7128 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7129 | |
| 7130 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7131 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7132 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7133 | seq_printf(seq, "["); |
| 7134 | for (i = 0; i < x; i++) |
| 7135 | seq_printf(seq, "="); |
| 7136 | seq_printf(seq, ">"); |
| 7137 | for (i = 0; i < y; i++) |
| 7138 | seq_printf(seq, "."); |
| 7139 | seq_printf(seq, "] "); |
| 7140 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7141 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7142 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 7143 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7144 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 7145 | "check" : |
| 7146 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 7147 | "resync" : "recovery"))), |
| 7148 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7149 | (unsigned long long) resync/2, |
| 7150 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7151 | |
| 7152 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7153 | * dt: time from mark until now |
| 7154 | * db: blocks written from mark until now |
| 7155 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7156 | * |
| 7157 | * rt is a sector_t, so could be 32bit or 64bit. |
| 7158 | * So we divide before multiply in case it is 32bit and close |
| 7159 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 7160 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7161 | * near the end of resync when the number of remaining sectors |
| 7162 | * is close to 'db'. |
| 7163 | * We then divide rt by 32 after multiplying by db to compensate. |
| 7164 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7165 | */ |
| 7166 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 7167 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7168 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 7169 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7170 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7171 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 7172 | sector_div(rt, db/32+1); |
| 7173 | rt *= dt; |
| 7174 | rt >>= 5; |
| 7175 | |
| 7176 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 7177 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7178 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7179 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7180 | } |
| 7181 | |
| 7182 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 7183 | { |
| 7184 | struct list_head *tmp; |
| 7185 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7186 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7187 | |
| 7188 | if (l >= 0x10000) |
| 7189 | return NULL; |
| 7190 | if (!l--) |
| 7191 | /* header */ |
| 7192 | return (void*)1; |
| 7193 | |
| 7194 | spin_lock(&all_mddevs_lock); |
| 7195 | list_for_each(tmp,&all_mddevs) |
| 7196 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7197 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7198 | mddev_get(mddev); |
| 7199 | spin_unlock(&all_mddevs_lock); |
| 7200 | return mddev; |
| 7201 | } |
| 7202 | spin_unlock(&all_mddevs_lock); |
| 7203 | if (!l--) |
| 7204 | return (void*)2;/* tail */ |
| 7205 | return NULL; |
| 7206 | } |
| 7207 | |
| 7208 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 7209 | { |
| 7210 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7211 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7213 | ++*pos; |
| 7214 | if (v == (void*)2) |
| 7215 | return NULL; |
| 7216 | |
| 7217 | spin_lock(&all_mddevs_lock); |
| 7218 | if (v == (void*)1) |
| 7219 | tmp = all_mddevs.next; |
| 7220 | else |
| 7221 | tmp = mddev->all_mddevs.next; |
| 7222 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7223 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7224 | else { |
| 7225 | next_mddev = (void*)2; |
| 7226 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7228 | spin_unlock(&all_mddevs_lock); |
| 7229 | |
| 7230 | if (v != (void*)1) |
| 7231 | mddev_put(mddev); |
| 7232 | return next_mddev; |
| 7233 | |
| 7234 | } |
| 7235 | |
| 7236 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 7237 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7238 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7239 | |
| 7240 | if (mddev && v != (void*)1 && v != (void*)2) |
| 7241 | mddev_put(mddev); |
| 7242 | } |
| 7243 | |
| 7244 | static int md_seq_show(struct seq_file *seq, void *v) |
| 7245 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7246 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7247 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7248 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7249 | |
| 7250 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7251 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7252 | seq_printf(seq, "Personalities : "); |
| 7253 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7254 | list_for_each_entry(pers, &pers_list, list) |
| 7255 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7256 | |
| 7257 | spin_unlock(&pers_lock); |
| 7258 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7259 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7260 | return 0; |
| 7261 | } |
| 7262 | if (v == (void*)2) { |
| 7263 | status_unused(seq); |
| 7264 | return 0; |
| 7265 | } |
| 7266 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7267 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7268 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7269 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7270 | mddev->pers ? "" : "in"); |
| 7271 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7272 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7273 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7274 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7275 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7276 | seq_printf(seq, " %s", mddev->pers->name); |
| 7277 | } |
| 7278 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7279 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7280 | rcu_read_lock(); |
| 7281 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7282 | char b[BDEVNAME_SIZE]; |
| 7283 | seq_printf(seq, " %s[%d]", |
| 7284 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7285 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7286 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7287 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7288 | seq_printf(seq, "(F)"); |
| 7289 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7290 | } |
| 7291 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7292 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7293 | if (test_bit(Replacement, &rdev->flags)) |
| 7294 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7295 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7296 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7297 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7298 | |
| 7299 | if (!list_empty(&mddev->disks)) { |
| 7300 | if (mddev->pers) |
| 7301 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7302 | (unsigned long long) |
| 7303 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7304 | else |
| 7305 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7306 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7307 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7308 | if (mddev->persistent) { |
| 7309 | if (mddev->major_version != 0 || |
| 7310 | mddev->minor_version != 90) { |
| 7311 | seq_printf(seq," super %d.%d", |
| 7312 | mddev->major_version, |
| 7313 | mddev->minor_version); |
| 7314 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7315 | } else if (mddev->external) |
| 7316 | seq_printf(seq, " super external:%s", |
| 7317 | mddev->metadata_type); |
| 7318 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7319 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7320 | |
| 7321 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7322 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7323 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7324 | if (mddev->pers->sync_request) { |
| 7325 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7326 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7327 | seq_printf(seq, "\n "); |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7328 | } else if (mddev->curr_resync >= 1) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7329 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 7330 | else if (mddev->recovery_cp < MaxSector) |
| 7331 | seq_printf(seq, "\tresync=PENDING\n "); |
| 7332 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7333 | } else |
| 7334 | seq_printf(seq, "\n "); |
| 7335 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7336 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7337 | |
| 7338 | seq_printf(seq, "\n"); |
| 7339 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7340 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7342 | return 0; |
| 7343 | } |
| 7344 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7345 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7346 | .start = md_seq_start, |
| 7347 | .next = md_seq_next, |
| 7348 | .stop = md_seq_stop, |
| 7349 | .show = md_seq_show, |
| 7350 | }; |
| 7351 | |
| 7352 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7353 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7354 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7355 | int error; |
| 7356 | |
| 7357 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7358 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7359 | return error; |
| 7360 | |
| 7361 | seq = file->private_data; |
| 7362 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7363 | return error; |
| 7364 | } |
| 7365 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7366 | static int md_unloading; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7367 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7368 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7369 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7370 | int mask; |
| 7371 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7372 | if (md_unloading) |
kbuild test robot | 7d7e64f | 2014-12-03 16:07:59 +1100 | [diff] [blame] | 7373 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7374 | poll_wait(filp, &md_event_waiters, wait); |
| 7375 | |
| 7376 | /* always allow read */ |
| 7377 | mask = POLLIN | POLLRDNORM; |
| 7378 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7379 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7380 | mask |= POLLERR | POLLPRI; |
| 7381 | return mask; |
| 7382 | } |
| 7383 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7384 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7385 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7386 | .open = md_seq_open, |
| 7387 | .read = seq_read, |
| 7388 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7389 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7390 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7391 | }; |
| 7392 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7393 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7394 | { |
Chao Yu | 50bd377 | 2014-09-25 15:28:34 +0800 | [diff] [blame] | 7395 | printk(KERN_INFO "md: %s personality registered for level %d\n", |
| 7396 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7397 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7398 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7399 | spin_unlock(&pers_lock); |
| 7400 | return 0; |
| 7401 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7402 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7403 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7404 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7405 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7406 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7407 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7408 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7409 | spin_unlock(&pers_lock); |
| 7410 | return 0; |
| 7411 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7412 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7413 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7414 | int register_md_cluster_operations(struct md_cluster_operations *ops, struct module *module) |
| 7415 | { |
| 7416 | if (md_cluster_ops != NULL) |
| 7417 | return -EALREADY; |
| 7418 | spin_lock(&pers_lock); |
| 7419 | md_cluster_ops = ops; |
| 7420 | md_cluster_mod = module; |
| 7421 | spin_unlock(&pers_lock); |
| 7422 | return 0; |
| 7423 | } |
| 7424 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 7425 | |
| 7426 | int unregister_md_cluster_operations(void) |
| 7427 | { |
| 7428 | spin_lock(&pers_lock); |
| 7429 | md_cluster_ops = NULL; |
| 7430 | spin_unlock(&pers_lock); |
| 7431 | return 0; |
| 7432 | } |
| 7433 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 7434 | |
| 7435 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 7436 | { |
| 7437 | int err; |
| 7438 | |
| 7439 | err = request_module("md-cluster"); |
| 7440 | if (err) { |
| 7441 | pr_err("md-cluster module not found.\n"); |
Goldwyn Rodrigues | b0c26a7 | 2015-07-22 12:09:15 -0500 | [diff] [blame] | 7442 | return -ENOENT; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7443 | } |
| 7444 | |
| 7445 | spin_lock(&pers_lock); |
| 7446 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
| 7447 | spin_unlock(&pers_lock); |
| 7448 | return -ENOENT; |
| 7449 | } |
| 7450 | spin_unlock(&pers_lock); |
| 7451 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 7452 | return md_cluster_ops->join(mddev, nodes); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7453 | } |
| 7454 | |
| 7455 | void md_cluster_stop(struct mddev *mddev) |
| 7456 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 7457 | if (!md_cluster_ops) |
| 7458 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7459 | md_cluster_ops->leave(mddev); |
| 7460 | module_put(md_cluster_mod); |
| 7461 | } |
| 7462 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7463 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7464 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7465 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7466 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7467 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7468 | |
| 7469 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7470 | rcu_read_lock(); |
| 7471 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7472 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7473 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7474 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7475 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7476 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7477 | * as sync_io is counted when a request starts, and |
| 7478 | * disk_stats is counted when it completes. |
| 7479 | * So resync activity will cause curr_events to be smaller than |
| 7480 | * when there was no such activity. |
| 7481 | * non-sync IO will cause disk_stat to increase without |
| 7482 | * increasing sync_io so curr_events will (eventually) |
| 7483 | * be larger than it was before. Once it becomes |
| 7484 | * substantially larger, the test below will cause |
| 7485 | * the array to appear non-idle, and resync will slow |
| 7486 | * down. |
| 7487 | * If there is a lot of outstanding resync activity when |
| 7488 | * we set last_event to curr_events, then all that activity |
| 7489 | * completing might cause the array to appear non-idle |
| 7490 | * and resync will be slowed down even though there might |
| 7491 | * not have been non-resync activity. This will only |
| 7492 | * happen once though. 'last_events' will soon reflect |
| 7493 | * the state where there is little or no outstanding |
| 7494 | * resync requests, and further resync activity will |
| 7495 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7496 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7497 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7498 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7499 | rdev->last_events = curr_events; |
| 7500 | idle = 0; |
| 7501 | } |
| 7502 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7503 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7504 | return idle; |
| 7505 | } |
| 7506 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7507 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7508 | { |
| 7509 | /* another "blocks" (512byte) blocks have been synced */ |
| 7510 | atomic_sub(blocks, &mddev->recovery_active); |
| 7511 | wake_up(&mddev->recovery_wait); |
| 7512 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7513 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7514 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7515 | md_wakeup_thread(mddev->thread); |
| 7516 | // stop recovery, signal do_sync .... |
| 7517 | } |
| 7518 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7519 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7520 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7521 | /* md_write_start(mddev, bi) |
| 7522 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7523 | * in superblock) before writing, schedule a superblock update |
| 7524 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7525 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7526 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7527 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7528 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7529 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7530 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7531 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7532 | BUG_ON(mddev->ro == 1); |
| 7533 | if (mddev->ro == 2) { |
| 7534 | /* need to switch to read/write */ |
| 7535 | mddev->ro = 0; |
| 7536 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7537 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7538 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7539 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7540 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7541 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7542 | if (mddev->safemode == 1) |
| 7543 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7544 | if (mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7545 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7546 | if (mddev->in_sync) { |
| 7547 | mddev->in_sync = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7548 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7549 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7550 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7551 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7552 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7553 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7554 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7555 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7556 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7557 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7558 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7559 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7560 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7561 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7562 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7563 | { |
| 7564 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7565 | if (mddev->safemode == 2) |
| 7566 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7567 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7568 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7569 | } |
| 7570 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7571 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7572 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7573 | /* md_allow_write(mddev) |
| 7574 | * Calling this ensures that the array is marked 'active' so that writes |
| 7575 | * may proceed without blocking. It is important to call this before |
| 7576 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7577 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7578 | * |
| 7579 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7580 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7581 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7582 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7583 | { |
| 7584 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7585 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7586 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7587 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7588 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7589 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7590 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7591 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7592 | if (mddev->in_sync) { |
| 7593 | mddev->in_sync = 0; |
| 7594 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7595 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7596 | if (mddev->safemode_delay && |
| 7597 | mddev->safemode == 0) |
| 7598 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7599 | spin_unlock(&mddev->lock); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7600 | if (mddev_is_clustered(mddev)) |
| 7601 | md_cluster_ops->metadata_update_start(mddev); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7602 | md_update_sb(mddev, 0); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7603 | if (mddev_is_clustered(mddev)) |
| 7604 | md_cluster_ops->metadata_update_finish(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7605 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7606 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7607 | spin_unlock(&mddev->lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7608 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7609 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7610 | return -EAGAIN; |
| 7611 | else |
| 7612 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7613 | } |
| 7614 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7616 | #define SYNC_MARKS 10 |
| 7617 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7618 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7619 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7620 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7621 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7622 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7623 | unsigned int currspeed = 0, |
| 7624 | window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7625 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7626 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7627 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7628 | sector_t mark_cnt[SYNC_MARKS]; |
| 7629 | int last_mark,m; |
| 7630 | struct list_head *tmp; |
| 7631 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7632 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7633 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7634 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7635 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7636 | |
| 7637 | /* just incase thread restarts... */ |
| 7638 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7639 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7640 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 7641 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7642 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7643 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7644 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7645 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7646 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7647 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7648 | action = "check"; |
| 7649 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7650 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7651 | action = "repair"; |
| 7652 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7653 | desc = "resync"; |
| 7654 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7655 | desc = "reshape"; |
| 7656 | else |
| 7657 | desc = "recovery"; |
| 7658 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7659 | mddev->last_sync_action = action ?: desc; |
| 7660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7661 | /* we overload curr_resync somewhat here. |
| 7662 | * 0 == not engaged in resync at all |
| 7663 | * 2 == checking that there is no conflict with another sync |
| 7664 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7665 | * commense |
| 7666 | * other == active in resync - this many blocks |
| 7667 | * |
| 7668 | * Before starting a resync we must have set curr_resync to |
| 7669 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7670 | * less than ours. When we find one that is the same or higher |
| 7671 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7672 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7673 | * This will mean we have to start checking from the beginning again. |
| 7674 | * |
| 7675 | */ |
| 7676 | |
| 7677 | do { |
| 7678 | mddev->curr_resync = 2; |
| 7679 | |
| 7680 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7681 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7682 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7683 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7684 | if (mddev2 == mddev) |
| 7685 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7686 | if (!mddev->parallel_resync |
| 7687 | && mddev2->curr_resync |
| 7688 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7689 | DEFINE_WAIT(wq); |
| 7690 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7691 | /* arbitrarily yield */ |
| 7692 | mddev->curr_resync = 1; |
| 7693 | wake_up(&resync_wait); |
| 7694 | } |
| 7695 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7696 | /* no need to wait here, we can wait the next |
| 7697 | * time 'round when curr_resync == 2 |
| 7698 | */ |
| 7699 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7700 | /* We need to wait 'interruptible' so as not to |
| 7701 | * contribute to the load average, and not to |
| 7702 | * be caught by 'softlockup' |
| 7703 | */ |
| 7704 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7705 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7706 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7707 | printk(KERN_INFO "md: delaying %s of %s" |
| 7708 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7709 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7710 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7711 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7712 | if (signal_pending(current)) |
| 7713 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7714 | schedule(); |
| 7715 | finish_wait(&resync_wait, &wq); |
| 7716 | goto try_again; |
| 7717 | } |
| 7718 | finish_wait(&resync_wait, &wq); |
| 7719 | } |
| 7720 | } |
| 7721 | } while (mddev->curr_resync < 2); |
| 7722 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7723 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7724 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7725 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7726 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7727 | */ |
| 7728 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 7729 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7730 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7731 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7732 | j = mddev->resync_min; |
| 7733 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7734 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7735 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7736 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7737 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7738 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7739 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7740 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7741 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7742 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7743 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7744 | if (rdev->raid_disk >= 0 && |
| 7745 | !test_bit(Faulty, &rdev->flags) && |
| 7746 | !test_bit(In_sync, &rdev->flags) && |
| 7747 | rdev->recovery_offset < j) |
| 7748 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7749 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 7750 | |
| 7751 | /* If there is a bitmap, we need to make sure all |
| 7752 | * writes that started before we added a spare |
| 7753 | * complete before we start doing a recovery. |
| 7754 | * Otherwise the write might complete and (via |
| 7755 | * bitmap_endwrite) set a bit in the bitmap after the |
| 7756 | * recovery has checked that bit and skipped that |
| 7757 | * region. |
| 7758 | */ |
| 7759 | if (mddev->bitmap) { |
| 7760 | mddev->pers->quiesce(mddev, 1); |
| 7761 | mddev->pers->quiesce(mddev, 0); |
| 7762 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7763 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7764 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7765 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7766 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7767 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7768 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7769 | "(but not more than %d KB/sec) for %s.\n", |
| 7770 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7771 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7772 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7773 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7774 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7775 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7776 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7777 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7778 | } |
| 7779 | last_mark = 0; |
| 7780 | mddev->resync_mark = mark[last_mark]; |
| 7781 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7782 | |
| 7783 | /* |
| 7784 | * Tune reconstruction: |
| 7785 | */ |
| 7786 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450c | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7787 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7788 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7789 | |
| 7790 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7791 | last_check = 0; |
| 7792 | |
| 7793 | if (j>2) { |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7794 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7795 | "md: resuming %s of %s from checkpoint.\n", |
| 7796 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7797 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7798 | } else |
| 7799 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7800 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7801 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 7802 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7803 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7804 | |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 7805 | if (mddev_is_clustered(mddev)) |
Goldwyn Rodrigues | 965400e | 2014-06-07 02:16:58 -0500 | [diff] [blame] | 7806 | md_cluster_ops->resync_start(mddev, j, max_sectors); |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 7807 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7808 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7809 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7810 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7811 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7812 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7813 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7814 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7815 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7816 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7817 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7818 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7819 | (j - mddev->curr_resync_completed)*2 |
| 7820 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7821 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7822 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7823 | wait_event(mddev->recovery_wait, |
| 7824 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7825 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7826 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 7827 | j > mddev->recovery_cp) |
| 7828 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7829 | update_time = jiffies; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7830 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7831 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7832 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7833 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7834 | while (j >= mddev->resync_max && |
| 7835 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7836 | /* As this condition is controlled by user-space, |
| 7837 | * we can block indefinitely, so use '_interruptible' |
| 7838 | * to avoid triggering warnings. |
| 7839 | */ |
| 7840 | flush_signals(current); /* just in case */ |
| 7841 | wait_event_interruptible(mddev->recovery_wait, |
| 7842 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7843 | || test_bit(MD_RECOVERY_INTR, |
| 7844 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7845 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7846 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7847 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7848 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7849 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 7850 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7851 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7852 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7853 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7854 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7855 | |
| 7856 | if (!skipped) { /* actual IO requested */ |
| 7857 | io_sectors += sectors; |
| 7858 | atomic_add(sectors, &mddev->recovery_active); |
| 7859 | } |
| 7860 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7861 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7862 | break; |
| 7863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7864 | j += sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7865 | if (j > 2) |
| 7866 | mddev->curr_resync = j; |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 7867 | if (mddev_is_clustered(mddev)) |
| 7868 | md_cluster_ops->resync_info_update(mddev, j, max_sectors); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7869 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7870 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7871 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7872 | * visible in /proc/mdstat |
| 7873 | */ |
| 7874 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7875 | |
| 7876 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7877 | continue; |
| 7878 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7879 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7880 | repeat: |
| 7881 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7882 | /* step marks */ |
| 7883 | int next = (last_mark+1) % SYNC_MARKS; |
| 7884 | |
| 7885 | mddev->resync_mark = mark[next]; |
| 7886 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7887 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7888 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7889 | last_mark = next; |
| 7890 | } |
| 7891 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7892 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7893 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7894 | |
| 7895 | /* |
| 7896 | * this loop exits only if either when we are slower than |
| 7897 | * the 'hard' speed limit, or the system was IO-idle for |
| 7898 | * a jiffy. |
| 7899 | * the system might be non-idle CPU-wise, but we only care |
| 7900 | * about not overloading the IO subsystem. (things like an |
| 7901 | * e2fsck being done on the RAID array should execute fast) |
| 7902 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7903 | cond_resched(); |
| 7904 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7905 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 7906 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7907 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7908 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7909 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 7910 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7911 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7912 | goto repeat; |
| 7913 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 7914 | if (!is_mddev_idle(mddev, 0)) { |
| 7915 | /* |
| 7916 | * Give other IO more of a chance. |
| 7917 | * The faster the devices, the less we wait. |
| 7918 | */ |
| 7919 | wait_event(mddev->recovery_wait, |
| 7920 | !atomic_read(&mddev->recovery_active)); |
| 7921 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7922 | } |
| 7923 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7924 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
| 7925 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 7926 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7927 | /* |
| 7928 | * this also signals 'finished resyncing' to md_stop |
| 7929 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7930 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7931 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7932 | |
| 7933 | /* tell personality that we are finished */ |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 7934 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7935 | |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 7936 | if (mddev_is_clustered(mddev)) |
Goldwyn Rodrigues | 965400e | 2014-06-07 02:16:58 -0500 | [diff] [blame] | 7937 | md_cluster_ops->resync_finish(mddev); |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 7938 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7939 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7940 | mddev->curr_resync > 2) { |
| 7941 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7942 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7943 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7944 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7945 | "md: checkpointing %s of %s.\n", |
| 7946 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7947 | if (test_bit(MD_RECOVERY_ERROR, |
| 7948 | &mddev->recovery)) |
| 7949 | mddev->recovery_cp = |
| 7950 | mddev->curr_resync_completed; |
| 7951 | else |
| 7952 | mddev->recovery_cp = |
| 7953 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7954 | } |
| 7955 | } else |
| 7956 | mddev->recovery_cp = MaxSector; |
| 7957 | } else { |
| 7958 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7959 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7960 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7961 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7962 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7963 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7964 | !test_bit(Faulty, &rdev->flags) && |
| 7965 | !test_bit(In_sync, &rdev->flags) && |
| 7966 | rdev->recovery_offset < mddev->curr_resync) |
| 7967 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7968 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7969 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7970 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7971 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7972 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7973 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 7974 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7975 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7976 | /* We completed so min/max setting can be forgotten if used. */ |
| 7977 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7978 | mddev->resync_min = 0; |
| 7979 | mddev->resync_max = MaxSector; |
| 7980 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7981 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7982 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 7983 | spin_unlock(&mddev->lock); |
| 7984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7985 | wake_up(&resync_wait); |
| 7986 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7987 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7988 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7989 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7990 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7991 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7992 | static int remove_and_add_spares(struct mddev *mddev, |
| 7993 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7994 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7995 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7996 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7997 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7998 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7999 | rdev_for_each(rdev, mddev) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8000 | if ((this == NULL || rdev == this) && |
| 8001 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8002 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8003 | (test_bit(Faulty, &rdev->flags) || |
| 8004 | ! test_bit(In_sync, &rdev->flags)) && |
| 8005 | atomic_read(&rdev->nr_pending)==0) { |
| 8006 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8007 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 8008 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8009 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8010 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8011 | } |
| 8012 | } |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 8013 | if (removed && mddev->kobj.sd) |
| 8014 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8015 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8016 | if (this) |
| 8017 | goto no_add; |
| 8018 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8019 | rdev_for_each(rdev, mddev) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8020 | if (rdev->raid_disk >= 0 && |
| 8021 | !test_bit(In_sync, &rdev->flags) && |
| 8022 | !test_bit(Faulty, &rdev->flags)) |
| 8023 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8024 | if (rdev->raid_disk >= 0) |
| 8025 | continue; |
| 8026 | if (test_bit(Faulty, &rdev->flags)) |
| 8027 | continue; |
| 8028 | if (mddev->ro && |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8029 | ! (rdev->saved_raid_disk >= 0 && |
| 8030 | !test_bit(Bitmap_sync, &rdev->flags))) |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8031 | continue; |
| 8032 | |
NeilBrown | 7eb4188 | 2014-01-14 15:55:14 +1100 | [diff] [blame] | 8033 | if (rdev->saved_raid_disk < 0) |
| 8034 | rdev->recovery_offset = 0; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8035 | if (mddev->pers-> |
| 8036 | hot_add_disk(mddev, rdev) == 0) { |
| 8037 | if (sysfs_link_rdev(mddev, rdev)) |
| 8038 | /* failure here is OK */; |
| 8039 | spares++; |
| 8040 | md_new_event(mddev); |
| 8041 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8042 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8043 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8044 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 8045 | if (removed) |
| 8046 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8047 | return spares; |
| 8048 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 8049 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8050 | static void md_start_sync(struct work_struct *ws) |
| 8051 | { |
| 8052 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
| 8053 | |
| 8054 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 8055 | mddev, |
| 8056 | "resync"); |
| 8057 | if (!mddev->sync_thread) { |
| 8058 | printk(KERN_ERR "%s: could not start resync" |
| 8059 | " thread...\n", |
| 8060 | mdname(mddev)); |
| 8061 | /* leave the spares where they are, it shouldn't hurt */ |
| 8062 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8063 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8064 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8065 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 8066 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8067 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8068 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8069 | &mddev->recovery)) |
| 8070 | if (mddev->sysfs_action) |
| 8071 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8072 | } else |
| 8073 | md_wakeup_thread(mddev->sync_thread); |
| 8074 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8075 | md_new_event(mddev); |
| 8076 | } |
| 8077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8078 | /* |
| 8079 | * This routine is regularly called by all per-raid-array threads to |
| 8080 | * deal with generic issues like resync and super-block update. |
| 8081 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 8082 | * need this as they never do any recovery or update the superblock. |
| 8083 | * |
| 8084 | * It does not do any resync itself, but rather "forks" off other threads |
| 8085 | * to do that as needed. |
| 8086 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 8087 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8088 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8089 | * and wakeups up this thread which will reap the thread and finish up. |
| 8090 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 8091 | * |
| 8092 | * The overall approach is: |
| 8093 | * 1/ if the superblock needs updating, update it. |
| 8094 | * 2/ If a recovery thread is running, don't do anything else. |
| 8095 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 8096 | * 4/ If there are any faulty devices, remove them. |
| 8097 | * 5/ If array is degraded, try to add spares devices |
| 8098 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 8099 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8100 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8101 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 8102 | if (mddev->suspended) |
| 8103 | return; |
| 8104 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 8105 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 8106 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8107 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8108 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8109 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8110 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 8111 | mdname(mddev)); |
| 8112 | mddev->safemode = 2; |
| 8113 | } |
| 8114 | flush_signals(current); |
| 8115 | } |
| 8116 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8117 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 8118 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8119 | if ( ! ( |
NeilBrown | 142d44c | 2013-11-28 10:34:18 +1100 | [diff] [blame] | 8120 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8121 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8122 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8123 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8124 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 8125 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8126 | )) |
| 8127 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8128 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 8129 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8130 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8131 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8132 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 8133 | struct md_rdev *rdev; |
| 8134 | if (!mddev->external && mddev->in_sync) |
| 8135 | /* 'Blocked' flag not needed as failed devices |
| 8136 | * will be recorded if array switched to read/write. |
| 8137 | * Leaving it set will prevent the device |
| 8138 | * from being removed. |
| 8139 | */ |
| 8140 | rdev_for_each(rdev, mddev) |
| 8141 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8142 | /* On a read-only array we can: |
| 8143 | * - remove failed devices |
| 8144 | * - add already-in_sync devices if the array itself |
| 8145 | * is in-sync. |
| 8146 | * As we only add devices that are already in-sync, |
| 8147 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8148 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8149 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8150 | /* There is no thread, but we need to call |
| 8151 | * ->spare_active and clear saved_raid_disk |
| 8152 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 8153 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8154 | md_reap_sync_thread(mddev); |
| 8155 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8156 | goto unlock; |
| 8157 | } |
| 8158 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8159 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8160 | int did_change = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8161 | spin_lock(&mddev->lock); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8162 | if (mddev->safemode && |
| 8163 | !atomic_read(&mddev->writes_pending) && |
| 8164 | !mddev->in_sync && |
| 8165 | mddev->recovery_cp == MaxSector) { |
| 8166 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8167 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 8168 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8169 | } |
| 8170 | if (mddev->safemode == 1) |
| 8171 | mddev->safemode = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8172 | spin_unlock(&mddev->lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8173 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8174 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8175 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8176 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 8177 | if (mddev->flags & MD_UPDATE_SB_FLAGS) { |
| 8178 | if (mddev_is_clustered(mddev)) |
| 8179 | md_cluster_ops->metadata_update_start(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 8180 | md_update_sb(mddev, 0); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 8181 | if (mddev_is_clustered(mddev)) |
| 8182 | md_cluster_ops->metadata_update_finish(mddev); |
| 8183 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8185 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 8186 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 8187 | /* resync/recovery still happening */ |
| 8188 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8189 | goto unlock; |
| 8190 | } |
| 8191 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8192 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8193 | goto unlock; |
| 8194 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8195 | /* Set RUNNING before clearing NEEDED to avoid |
| 8196 | * any transients in the value of "sync_action". |
| 8197 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8198 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8199 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8200 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8201 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8202 | /* Clear some bits that don't mean anything, but |
| 8203 | * might be left set |
| 8204 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8205 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 8206 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8207 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 8208 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 8209 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8210 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8211 | /* no recovery is running. |
| 8212 | * remove any failed drives, then |
| 8213 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8214 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8215 | * the personality to fail the re-add. |
| 8216 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8217 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8218 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8219 | if (mddev->pers->check_reshape == NULL || |
| 8220 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8221 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8222 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8223 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8224 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8225 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8226 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8227 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 8228 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8229 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8230 | } else if (mddev->recovery_cp < MaxSector) { |
| 8231 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8232 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8233 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 8234 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8235 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8237 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 8238 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 8239 | /* We are adding a device or devices to an array |
| 8240 | * which has the bitmap stored on all devices. |
| 8241 | * So make sure all bitmap pages get written |
| 8242 | */ |
| 8243 | bitmap_write_all(mddev->bitmap); |
| 8244 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8245 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 8246 | queue_work(md_misc_wq, &mddev->del_work); |
| 8247 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8248 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8249 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8250 | if (!mddev->sync_thread) { |
| 8251 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8252 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8253 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8254 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 8255 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8256 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8257 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8258 | unlock: |
| 8259 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8260 | mddev_unlock(mddev); |
| 8261 | } |
| 8262 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8263 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8264 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8265 | void md_reap_sync_thread(struct mddev *mddev) |
| 8266 | { |
| 8267 | struct md_rdev *rdev; |
| 8268 | |
| 8269 | /* resync has finished, collect result */ |
| 8270 | md_unregister_thread(&mddev->sync_thread); |
| 8271 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8272 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 8273 | /* success...*/ |
| 8274 | /* activate any spares */ |
| 8275 | if (mddev->pers->spare_active(mddev)) { |
| 8276 | sysfs_notify(&mddev->kobj, NULL, |
| 8277 | "degraded"); |
| 8278 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 8279 | } |
| 8280 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 8281 | if (mddev_is_clustered(mddev)) |
| 8282 | md_cluster_ops->metadata_update_start(mddev); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8283 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8284 | mddev->pers->finish_reshape) |
| 8285 | mddev->pers->finish_reshape(mddev); |
| 8286 | |
| 8287 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8288 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8289 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8290 | if (!mddev->degraded) |
| 8291 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8292 | rdev->saved_raid_disk = -1; |
| 8293 | |
| 8294 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 8295 | if (mddev_is_clustered(mddev)) |
| 8296 | md_cluster_ops->metadata_update_finish(mddev); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8297 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 8298 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8299 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8300 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8301 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8302 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8303 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8304 | /* flag recovery needed just to double check */ |
| 8305 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8306 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8307 | md_new_event(mddev); |
| 8308 | if (mddev->event_work.func) |
| 8309 | queue_work(md_misc_wq, &mddev->event_work); |
| 8310 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8311 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8312 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8313 | 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] | 8314 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8315 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8316 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8317 | !test_bit(Blocked, &rdev->flags) && |
| 8318 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8319 | msecs_to_jiffies(5000)); |
| 8320 | rdev_dec_pending(rdev, mddev); |
| 8321 | } |
| 8322 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 8323 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8324 | void md_finish_reshape(struct mddev *mddev) |
| 8325 | { |
| 8326 | /* called be personality module when reshape completes. */ |
| 8327 | struct md_rdev *rdev; |
| 8328 | |
| 8329 | rdev_for_each(rdev, mddev) { |
| 8330 | if (rdev->data_offset > rdev->new_data_offset) |
| 8331 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 8332 | else |
| 8333 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 8334 | rdev->data_offset = rdev->new_data_offset; |
| 8335 | } |
| 8336 | } |
| 8337 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8338 | |
| 8339 | /* Bad block management. |
| 8340 | * We can record which blocks on each device are 'bad' and so just |
| 8341 | * fail those blocks, or that stripe, rather than the whole device. |
| 8342 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 8343 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 8344 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 8345 | * A 'shift' can be set so that larger blocks are tracked and |
| 8346 | * consequently larger devices can be covered. |
| 8347 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 8348 | * |
| 8349 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 8350 | * might need to retry if it is very unlucky. |
| 8351 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 8352 | * so we use the write_seqlock_irq variant. |
| 8353 | * |
| 8354 | * When looking for a bad block we specify a range and want to |
| 8355 | * know if any block in the range is bad. So we binary-search |
| 8356 | * to the last range that starts at-or-before the given endpoint, |
| 8357 | * (or "before the sector after the target range") |
| 8358 | * then see if it ends after the given start. |
| 8359 | * We return |
| 8360 | * 0 if there are no known bad blocks in the range |
| 8361 | * 1 if there are known bad block which are all acknowledged |
| 8362 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 8363 | * plus the start/length of the first bad section we overlap. |
| 8364 | */ |
| 8365 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 8366 | sector_t *first_bad, int *bad_sectors) |
| 8367 | { |
| 8368 | int hi; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8369 | int lo; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8370 | u64 *p = bb->page; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8371 | int rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8372 | sector_t target = s + sectors; |
| 8373 | unsigned seq; |
| 8374 | |
| 8375 | if (bb->shift > 0) { |
| 8376 | /* round the start down, and the end up */ |
| 8377 | s >>= bb->shift; |
| 8378 | target += (1<<bb->shift) - 1; |
| 8379 | target >>= bb->shift; |
| 8380 | sectors = target - s; |
| 8381 | } |
| 8382 | /* 'target' is now the first block after the bad range */ |
| 8383 | |
| 8384 | retry: |
| 8385 | seq = read_seqbegin(&bb->lock); |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8386 | lo = 0; |
| 8387 | rv = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8388 | hi = bb->count; |
| 8389 | |
| 8390 | /* Binary search between lo and hi for 'target' |
| 8391 | * i.e. for the last range that starts before 'target' |
| 8392 | */ |
| 8393 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 8394 | * are known not to be the last range before target. |
| 8395 | * VARIANT: hi-lo is the number of possible |
| 8396 | * ranges, and decreases until it reaches 1 |
| 8397 | */ |
| 8398 | while (hi - lo > 1) { |
| 8399 | int mid = (lo + hi) / 2; |
| 8400 | sector_t a = BB_OFFSET(p[mid]); |
| 8401 | if (a < target) |
| 8402 | /* This could still be the one, earlier ranges |
| 8403 | * could not. */ |
| 8404 | lo = mid; |
| 8405 | else |
| 8406 | /* This and later ranges are definitely out. */ |
| 8407 | hi = mid; |
| 8408 | } |
| 8409 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 8410 | if (hi > lo) { |
| 8411 | /* need to check all range that end after 's' to see if |
| 8412 | * any are unacknowledged. |
| 8413 | */ |
| 8414 | while (lo >= 0 && |
| 8415 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8416 | if (BB_OFFSET(p[lo]) < target) { |
| 8417 | /* starts before the end, and finishes after |
| 8418 | * the start, so they must overlap |
| 8419 | */ |
| 8420 | if (rv != -1 && BB_ACK(p[lo])) |
| 8421 | rv = 1; |
| 8422 | else |
| 8423 | rv = -1; |
| 8424 | *first_bad = BB_OFFSET(p[lo]); |
| 8425 | *bad_sectors = BB_LEN(p[lo]); |
| 8426 | } |
| 8427 | lo--; |
| 8428 | } |
| 8429 | } |
| 8430 | |
| 8431 | if (read_seqretry(&bb->lock, seq)) |
| 8432 | goto retry; |
| 8433 | |
| 8434 | return rv; |
| 8435 | } |
| 8436 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 8437 | |
| 8438 | /* |
| 8439 | * Add a range of bad blocks to the table. |
| 8440 | * This might extend the table, or might contract it |
| 8441 | * if two adjacent ranges can be merged. |
| 8442 | * We binary-search to find the 'insertion' point, then |
| 8443 | * decide how best to handle it. |
| 8444 | */ |
| 8445 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 8446 | int acknowledged) |
| 8447 | { |
| 8448 | u64 *p; |
| 8449 | int lo, hi; |
| 8450 | int rv = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8451 | unsigned long flags; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8452 | |
| 8453 | if (bb->shift < 0) |
| 8454 | /* badblocks are disabled */ |
| 8455 | return 0; |
| 8456 | |
| 8457 | if (bb->shift) { |
| 8458 | /* round the start down, and the end up */ |
| 8459 | sector_t next = s + sectors; |
| 8460 | s >>= bb->shift; |
| 8461 | next += (1<<bb->shift) - 1; |
| 8462 | next >>= bb->shift; |
| 8463 | sectors = next - s; |
| 8464 | } |
| 8465 | |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8466 | write_seqlock_irqsave(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8467 | |
| 8468 | p = bb->page; |
| 8469 | lo = 0; |
| 8470 | hi = bb->count; |
| 8471 | /* Find the last range that starts at-or-before 's' */ |
| 8472 | while (hi - lo > 1) { |
| 8473 | int mid = (lo + hi) / 2; |
| 8474 | sector_t a = BB_OFFSET(p[mid]); |
| 8475 | if (a <= s) |
| 8476 | lo = mid; |
| 8477 | else |
| 8478 | hi = mid; |
| 8479 | } |
| 8480 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 8481 | hi = lo; |
| 8482 | |
| 8483 | if (hi > lo) { |
| 8484 | /* we found a range that might merge with the start |
| 8485 | * of our new range |
| 8486 | */ |
| 8487 | sector_t a = BB_OFFSET(p[lo]); |
| 8488 | sector_t e = a + BB_LEN(p[lo]); |
| 8489 | int ack = BB_ACK(p[lo]); |
| 8490 | if (e >= s) { |
| 8491 | /* Yes, we can merge with a previous range */ |
| 8492 | if (s == a && s + sectors >= e) |
| 8493 | /* new range covers old */ |
| 8494 | ack = acknowledged; |
| 8495 | else |
| 8496 | ack = ack && acknowledged; |
| 8497 | |
| 8498 | if (e < s + sectors) |
| 8499 | e = s + sectors; |
| 8500 | if (e - a <= BB_MAX_LEN) { |
| 8501 | p[lo] = BB_MAKE(a, e-a, ack); |
| 8502 | s = e; |
| 8503 | } else { |
| 8504 | /* does not all fit in one range, |
| 8505 | * make p[lo] maximal |
| 8506 | */ |
| 8507 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8508 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8509 | s = a + BB_MAX_LEN; |
| 8510 | } |
| 8511 | sectors = e - s; |
| 8512 | } |
| 8513 | } |
| 8514 | if (sectors && hi < bb->count) { |
| 8515 | /* 'hi' points to the first range that starts after 's'. |
| 8516 | * Maybe we can merge with the start of that range */ |
| 8517 | sector_t a = BB_OFFSET(p[hi]); |
| 8518 | sector_t e = a + BB_LEN(p[hi]); |
| 8519 | int ack = BB_ACK(p[hi]); |
| 8520 | if (a <= s + sectors) { |
| 8521 | /* merging is possible */ |
| 8522 | if (e <= s + sectors) { |
| 8523 | /* full overlap */ |
| 8524 | e = s + sectors; |
| 8525 | ack = acknowledged; |
| 8526 | } else |
| 8527 | ack = ack && acknowledged; |
| 8528 | |
| 8529 | a = s; |
| 8530 | if (e - a <= BB_MAX_LEN) { |
| 8531 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8532 | s = e; |
| 8533 | } else { |
| 8534 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8535 | s = a + BB_MAX_LEN; |
| 8536 | } |
| 8537 | sectors = e - s; |
| 8538 | lo = hi; |
| 8539 | hi++; |
| 8540 | } |
| 8541 | } |
| 8542 | if (sectors == 0 && hi < bb->count) { |
| 8543 | /* we might be able to combine lo and hi */ |
| 8544 | /* Note: 's' is at the end of 'lo' */ |
| 8545 | sector_t a = BB_OFFSET(p[hi]); |
| 8546 | int lolen = BB_LEN(p[lo]); |
| 8547 | int hilen = BB_LEN(p[hi]); |
| 8548 | int newlen = lolen + hilen - (s - a); |
| 8549 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8550 | /* yes, we can combine them */ |
| 8551 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8552 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8553 | memmove(p + hi, p + hi + 1, |
| 8554 | (bb->count - hi - 1) * 8); |
| 8555 | bb->count--; |
| 8556 | } |
| 8557 | } |
| 8558 | while (sectors) { |
| 8559 | /* didn't merge (it all). |
| 8560 | * Need to add a range just before 'hi' */ |
| 8561 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8562 | /* No room for more */ |
| 8563 | rv = 0; |
| 8564 | break; |
| 8565 | } else { |
| 8566 | int this_sectors = sectors; |
| 8567 | memmove(p + hi + 1, p + hi, |
| 8568 | (bb->count - hi) * 8); |
| 8569 | bb->count++; |
| 8570 | |
| 8571 | if (this_sectors > BB_MAX_LEN) |
| 8572 | this_sectors = BB_MAX_LEN; |
| 8573 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8574 | sectors -= this_sectors; |
| 8575 | s += this_sectors; |
| 8576 | } |
| 8577 | } |
| 8578 | |
| 8579 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8580 | if (!acknowledged) |
| 8581 | bb->unacked_exist = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8582 | write_sequnlock_irqrestore(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8583 | |
| 8584 | return rv; |
| 8585 | } |
| 8586 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8587 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8588 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8589 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8590 | int rv; |
| 8591 | if (is_new) |
| 8592 | s += rdev->new_data_offset; |
| 8593 | else |
| 8594 | s += rdev->data_offset; |
| 8595 | rv = md_set_badblocks(&rdev->badblocks, |
| 8596 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8597 | if (rv) { |
| 8598 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8599 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8600 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 8601 | md_wakeup_thread(rdev->mddev->thread); |
| 8602 | } |
| 8603 | return rv; |
| 8604 | } |
| 8605 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8606 | |
| 8607 | /* |
| 8608 | * Remove a range of bad blocks from the table. |
| 8609 | * This may involve extending the table if we spilt a region, |
| 8610 | * but it must not fail. So if the table becomes full, we just |
| 8611 | * drop the remove request. |
| 8612 | */ |
| 8613 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8614 | { |
| 8615 | u64 *p; |
| 8616 | int lo, hi; |
| 8617 | sector_t target = s + sectors; |
| 8618 | int rv = 0; |
| 8619 | |
| 8620 | if (bb->shift > 0) { |
| 8621 | /* When clearing we round the start up and the end down. |
| 8622 | * This should not matter as the shift should align with |
| 8623 | * the block size and no rounding should ever be needed. |
| 8624 | * However it is better the think a block is bad when it |
| 8625 | * isn't than to think a block is not bad when it is. |
| 8626 | */ |
| 8627 | s += (1<<bb->shift) - 1; |
| 8628 | s >>= bb->shift; |
| 8629 | target >>= bb->shift; |
| 8630 | sectors = target - s; |
| 8631 | } |
| 8632 | |
| 8633 | write_seqlock_irq(&bb->lock); |
| 8634 | |
| 8635 | p = bb->page; |
| 8636 | lo = 0; |
| 8637 | hi = bb->count; |
| 8638 | /* Find the last range that starts before 'target' */ |
| 8639 | while (hi - lo > 1) { |
| 8640 | int mid = (lo + hi) / 2; |
| 8641 | sector_t a = BB_OFFSET(p[mid]); |
| 8642 | if (a < target) |
| 8643 | lo = mid; |
| 8644 | else |
| 8645 | hi = mid; |
| 8646 | } |
| 8647 | if (hi > lo) { |
| 8648 | /* p[lo] is the last range that could overlap the |
| 8649 | * current range. Earlier ranges could also overlap, |
| 8650 | * but only this one can overlap the end of the range. |
| 8651 | */ |
| 8652 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8653 | /* Partial overlap, leave the tail of this range */ |
| 8654 | int ack = BB_ACK(p[lo]); |
| 8655 | sector_t a = BB_OFFSET(p[lo]); |
| 8656 | sector_t end = a + BB_LEN(p[lo]); |
| 8657 | |
| 8658 | if (a < s) { |
| 8659 | /* we need to split this range */ |
| 8660 | if (bb->count >= MD_MAX_BADBLOCKS) { |
NeilBrown | 8b32bf5 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 8661 | rv = -ENOSPC; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8662 | goto out; |
| 8663 | } |
| 8664 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8665 | bb->count++; |
| 8666 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8667 | lo++; |
| 8668 | } |
| 8669 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8670 | /* there is no longer an overlap */ |
| 8671 | hi = lo; |
| 8672 | lo--; |
| 8673 | } |
| 8674 | while (lo >= 0 && |
| 8675 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8676 | /* This range does overlap */ |
| 8677 | if (BB_OFFSET(p[lo]) < s) { |
| 8678 | /* Keep the early parts of this range. */ |
| 8679 | int ack = BB_ACK(p[lo]); |
| 8680 | sector_t start = BB_OFFSET(p[lo]); |
| 8681 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8682 | /* now low doesn't overlap, so.. */ |
| 8683 | break; |
| 8684 | } |
| 8685 | lo--; |
| 8686 | } |
| 8687 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8688 | * anything between needs to be discarded |
| 8689 | */ |
| 8690 | if (hi - lo > 1) { |
| 8691 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8692 | bb->count -= (hi - lo - 1); |
| 8693 | } |
| 8694 | } |
| 8695 | |
| 8696 | bb->changed = 1; |
| 8697 | out: |
| 8698 | write_sequnlock_irq(&bb->lock); |
| 8699 | return rv; |
| 8700 | } |
| 8701 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8702 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8703 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8704 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8705 | if (is_new) |
| 8706 | s += rdev->new_data_offset; |
| 8707 | else |
| 8708 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8709 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8710 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8711 | } |
| 8712 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8713 | |
| 8714 | /* |
| 8715 | * Acknowledge all bad blocks in a list. |
| 8716 | * This only succeeds if ->changed is clear. It is used by |
| 8717 | * in-kernel metadata updates |
| 8718 | */ |
| 8719 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8720 | { |
| 8721 | if (bb->page == NULL || bb->changed) |
| 8722 | /* no point even trying */ |
| 8723 | return; |
| 8724 | write_seqlock_irq(&bb->lock); |
| 8725 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8726 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8727 | u64 *p = bb->page; |
| 8728 | int i; |
| 8729 | for (i = 0; i < bb->count ; i++) { |
| 8730 | if (!BB_ACK(p[i])) { |
| 8731 | sector_t start = BB_OFFSET(p[i]); |
| 8732 | int len = BB_LEN(p[i]); |
| 8733 | p[i] = BB_MAKE(start, len, 1); |
| 8734 | } |
| 8735 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8736 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8737 | } |
| 8738 | write_sequnlock_irq(&bb->lock); |
| 8739 | } |
| 8740 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8741 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8742 | /* sysfs access to bad-blocks list. |
| 8743 | * We present two files. |
| 8744 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8745 | * are recorded as bad. The list is truncated to fit within |
| 8746 | * the one-page limit of sysfs. |
| 8747 | * Writing "sector length" to this file adds an acknowledged |
| 8748 | * bad block list. |
| 8749 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8750 | * been acknowledged. Writing to this file adds bad blocks |
| 8751 | * without acknowledging them. This is largely for testing. |
| 8752 | */ |
| 8753 | |
| 8754 | static ssize_t |
| 8755 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8756 | { |
| 8757 | size_t len; |
| 8758 | int i; |
| 8759 | u64 *p = bb->page; |
| 8760 | unsigned seq; |
| 8761 | |
| 8762 | if (bb->shift < 0) |
| 8763 | return 0; |
| 8764 | |
| 8765 | retry: |
| 8766 | seq = read_seqbegin(&bb->lock); |
| 8767 | |
| 8768 | len = 0; |
| 8769 | i = 0; |
| 8770 | |
| 8771 | while (len < PAGE_SIZE && i < bb->count) { |
| 8772 | sector_t s = BB_OFFSET(p[i]); |
| 8773 | unsigned int length = BB_LEN(p[i]); |
| 8774 | int ack = BB_ACK(p[i]); |
| 8775 | i++; |
| 8776 | |
| 8777 | if (unack && ack) |
| 8778 | continue; |
| 8779 | |
| 8780 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8781 | (unsigned long long)s << bb->shift, |
| 8782 | length << bb->shift); |
| 8783 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8784 | if (unack && len == 0) |
| 8785 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8786 | |
| 8787 | if (read_seqretry(&bb->lock, seq)) |
| 8788 | goto retry; |
| 8789 | |
| 8790 | return len; |
| 8791 | } |
| 8792 | |
| 8793 | #define DO_DEBUG 1 |
| 8794 | |
| 8795 | static ssize_t |
| 8796 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8797 | { |
| 8798 | unsigned long long sector; |
| 8799 | int length; |
| 8800 | char newline; |
| 8801 | #ifdef DO_DEBUG |
| 8802 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8803 | * Normally only a successful write may clear a badblock |
| 8804 | */ |
| 8805 | int clear = 0; |
| 8806 | if (page[0] == '-') { |
| 8807 | clear = 1; |
| 8808 | page++; |
| 8809 | } |
| 8810 | #endif /* DO_DEBUG */ |
| 8811 | |
| 8812 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8813 | case 3: |
| 8814 | if (newline != '\n') |
| 8815 | return -EINVAL; |
| 8816 | case 2: |
| 8817 | if (length <= 0) |
| 8818 | return -EINVAL; |
| 8819 | break; |
| 8820 | default: |
| 8821 | return -EINVAL; |
| 8822 | } |
| 8823 | |
| 8824 | #ifdef DO_DEBUG |
| 8825 | if (clear) { |
| 8826 | md_clear_badblocks(bb, sector, length); |
| 8827 | return len; |
| 8828 | } |
| 8829 | #endif /* DO_DEBUG */ |
| 8830 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8831 | return len; |
| 8832 | else |
| 8833 | return -ENOSPC; |
| 8834 | } |
| 8835 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8836 | static int md_notify_reboot(struct notifier_block *this, |
| 8837 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8838 | { |
| 8839 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8840 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8841 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8842 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8843 | for_each_mddev(mddev, tmp) { |
| 8844 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8845 | if (mddev->pers) |
| 8846 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 8847 | if (mddev->persistent) |
| 8848 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8849 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8850 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8851 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8852 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8853 | /* |
| 8854 | * certain more exotic SCSI devices are known to be |
| 8855 | * volatile wrt too early system reboots. While the |
| 8856 | * right place to handle this issue is the given |
| 8857 | * driver, we do want to have a safe RAID driver ... |
| 8858 | */ |
| 8859 | if (need_delay) |
| 8860 | mdelay(1000*1); |
| 8861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8862 | return NOTIFY_DONE; |
| 8863 | } |
| 8864 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8865 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8866 | .notifier_call = md_notify_reboot, |
| 8867 | .next = NULL, |
| 8868 | .priority = INT_MAX, /* before any real devices */ |
| 8869 | }; |
| 8870 | |
| 8871 | static void md_geninit(void) |
| 8872 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8873 | 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] | 8874 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8875 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8876 | } |
| 8877 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8878 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8879 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8880 | int ret = -ENOMEM; |
| 8881 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8882 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8883 | if (!md_wq) |
| 8884 | goto err_wq; |
| 8885 | |
| 8886 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8887 | if (!md_misc_wq) |
| 8888 | goto err_misc_wq; |
| 8889 | |
| 8890 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8891 | goto err_md; |
| 8892 | |
| 8893 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8894 | goto err_mdp; |
| 8895 | mdp_major = ret; |
| 8896 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8897 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8898 | md_probe, NULL, NULL); |
| 8899 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8900 | md_probe, NULL, NULL); |
| 8901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8902 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8903 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8904 | |
| 8905 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8906 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8907 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8908 | err_mdp: |
| 8909 | unregister_blkdev(MD_MAJOR, "md"); |
| 8910 | err_md: |
| 8911 | destroy_workqueue(md_misc_wq); |
| 8912 | err_misc_wq: |
| 8913 | destroy_workqueue(md_wq); |
| 8914 | err_wq: |
| 8915 | return ret; |
| 8916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8917 | |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8918 | void md_reload_sb(struct mddev *mddev) |
| 8919 | { |
| 8920 | struct md_rdev *rdev, *tmp; |
| 8921 | |
| 8922 | rdev_for_each_safe(rdev, tmp, mddev) { |
| 8923 | rdev->sb_loaded = 0; |
| 8924 | ClearPageUptodate(rdev->sb_page); |
| 8925 | } |
| 8926 | mddev->raid_disks = 0; |
| 8927 | analyze_sbs(mddev); |
| 8928 | rdev_for_each_safe(rdev, tmp, mddev) { |
| 8929 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 8930 | /* since we don't write to faulty devices, we figure out if the |
| 8931 | * disk is faulty by comparing events |
| 8932 | */ |
| 8933 | if (mddev->events > sb->events) |
| 8934 | set_bit(Faulty, &rdev->flags); |
| 8935 | } |
| 8936 | |
| 8937 | } |
| 8938 | EXPORT_SYMBOL(md_reload_sb); |
| 8939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8940 | #ifndef MODULE |
| 8941 | |
| 8942 | /* |
| 8943 | * Searches all registered partitions for autorun RAID arrays |
| 8944 | * at boot time. |
| 8945 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8946 | |
| 8947 | static LIST_HEAD(all_detected_devices); |
| 8948 | struct detected_devices_node { |
| 8949 | struct list_head list; |
| 8950 | dev_t dev; |
| 8951 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8952 | |
| 8953 | void md_autodetect_dev(dev_t dev) |
| 8954 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8955 | struct detected_devices_node *node_detected_dev; |
| 8956 | |
| 8957 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8958 | if (node_detected_dev) { |
| 8959 | node_detected_dev->dev = dev; |
| 8960 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8961 | } else { |
| 8962 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8963 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8964 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8965 | } |
| 8966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8967 | static void autostart_arrays(int part) |
| 8968 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8969 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8970 | struct detected_devices_node *node_detected_dev; |
| 8971 | dev_t dev; |
| 8972 | int i_scanned, i_passed; |
| 8973 | |
| 8974 | i_scanned = 0; |
| 8975 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8976 | |
| 8977 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8978 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8979 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8980 | i_scanned++; |
| 8981 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8982 | struct detected_devices_node, list); |
| 8983 | list_del(&node_detected_dev->list); |
| 8984 | dev = node_detected_dev->dev; |
| 8985 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8986 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8987 | if (IS_ERR(rdev)) |
| 8988 | continue; |
| 8989 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8990 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8991 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8992 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8993 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8994 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8995 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8996 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8997 | |
| 8998 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8999 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9000 | |
| 9001 | autorun_devices(part); |
| 9002 | } |
| 9003 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 9004 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9005 | |
| 9006 | static __exit void md_exit(void) |
| 9007 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9008 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9009 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9010 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 9011 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 9012 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 9013 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9014 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 9015 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9016 | unregister_blkdev(mdp_major, "mdp"); |
| 9017 | unregister_reboot_notifier(&md_notifier); |
| 9018 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9019 | |
| 9020 | /* We cannot unload the modules while some process is |
| 9021 | * waiting for us in select() or poll() - wake them up |
| 9022 | */ |
| 9023 | md_unloading = 1; |
| 9024 | while (waitqueue_active(&md_event_waiters)) { |
| 9025 | /* not safe to leave yet */ |
| 9026 | wake_up(&md_event_waiters); |
| 9027 | msleep(delay); |
| 9028 | delay += delay; |
| 9029 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9030 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9031 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 9032 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9033 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9034 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9035 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9036 | destroy_workqueue(md_misc_wq); |
| 9037 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9038 | } |
| 9039 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 9040 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9041 | module_exit(md_exit) |
| 9042 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9043 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 9044 | { |
| 9045 | return sprintf(buffer, "%d", start_readonly); |
| 9046 | } |
| 9047 | static int set_ro(const char *val, struct kernel_param *kp) |
| 9048 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 9049 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9050 | } |
| 9051 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 9052 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 9053 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9054 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9056 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 9057 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 9058 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 9059 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |