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> |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 37 | #include <linux/badblocks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 39 | #include <linux/seq_file.h> |
Al Viro | ff01bb48 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 40 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 41 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 42 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 43 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 44 | #include <linux/hdreg.h> |
| 45 | #include <linux/proc_fs.h> |
| 46 | #include <linux/random.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 47 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 48 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 49 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 50 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 51 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 52 | #include <linux/raid/md_p.h> |
| 53 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 54 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 55 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 56 | #include "bitmap.h" |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 57 | #include "md-cluster.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 60 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #endif |
| 62 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 63 | /* pers_list is a list of registered personalities protected |
| 64 | * by pers_lock. |
| 65 | * pers_lock does extra service to protect accesses to |
| 66 | * mddev->thread when the mutex cannot be held. |
| 67 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 68 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | static DEFINE_SPINLOCK(pers_lock); |
| 70 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 71 | struct md_cluster_operations *md_cluster_ops; |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame] | 72 | EXPORT_SYMBOL(md_cluster_ops); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 73 | struct module *md_cluster_mod; |
| 74 | EXPORT_SYMBOL(md_cluster_mod); |
| 75 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 76 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 77 | static struct workqueue_struct *md_wq; |
| 78 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 79 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 80 | static int remove_and_add_spares(struct mddev *mddev, |
| 81 | struct md_rdev *this); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 82 | static void mddev_detach(struct mddev *mddev); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 85 | * Default number of read corrections we'll attempt on an rdev |
| 86 | * before ejecting it from the array. We divide the read error |
| 87 | * count by 2 for every hour elapsed between read errors. |
| 88 | */ |
| 89 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 90 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 92 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 93 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 94 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 96 | * speed limit - in case reconstruction slows down your system despite |
| 97 | * idle IO detection. |
| 98 | * |
| 99 | * 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] | 100 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | */ |
| 102 | |
| 103 | static int sysctl_speed_limit_min = 1000; |
| 104 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 105 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 106 | { |
| 107 | return mddev->sync_speed_min ? |
| 108 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 109 | } |
| 110 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 111 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 112 | { |
| 113 | return mddev->sync_speed_max ? |
| 114 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | static struct ctl_table_header *raid_table_header; |
| 118 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 119 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | .procname = "speed_limit_min", |
| 122 | .data = &sysctl_speed_limit_min, |
| 123 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 124 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 125 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | }, |
| 127 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | .procname = "speed_limit_max", |
| 129 | .data = &sysctl_speed_limit_max, |
| 130 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 131 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 132 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 134 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 137 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | .procname = "raid", |
| 140 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 141 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | .child = raid_table, |
| 143 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 144 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 147 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | .procname = "dev", |
| 150 | .maxlen = 0, |
| 151 | .mode = 0555, |
| 152 | .child = raid_dir_table, |
| 153 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 154 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 157 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 159 | static int start_readonly; |
| 160 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 161 | /* bio_clone_mddev |
| 162 | * like bio_clone, but with a local bio set |
| 163 | */ |
| 164 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 165 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 166 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 167 | { |
| 168 | struct bio *b; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 169 | |
| 170 | if (!mddev || !mddev->bio_set) |
| 171 | return bio_alloc(gfp_mask, nr_iovecs); |
| 172 | |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 173 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 174 | if (!b) |
| 175 | return NULL; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 176 | return b; |
| 177 | } |
| 178 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 179 | |
| 180 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 181 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 182 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 183 | if (!mddev || !mddev->bio_set) |
| 184 | return bio_clone(bio, gfp_mask); |
| 185 | |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 186 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 187 | } |
| 188 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 191 | * We have a system wide 'event count' that is incremented |
| 192 | * on any 'interesting' event, and readers of /proc/mdstat |
| 193 | * can use 'poll' or 'select' to find out when the event |
| 194 | * count increases. |
| 195 | * |
| 196 | * Events are: |
| 197 | * start array, stop array, error, add device, remove device, |
| 198 | * start build, activate spare |
| 199 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 200 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 201 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 202 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 203 | { |
| 204 | atomic_inc(&md_event_count); |
| 205 | wake_up(&md_event_waiters); |
| 206 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 207 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 208 | |
| 209 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | * Enables to iterate over all existing md arrays |
| 211 | * all_mddevs_lock protects this list. |
| 212 | */ |
| 213 | static LIST_HEAD(all_mddevs); |
| 214 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | /* |
| 217 | * iterates through all used mddevs in the system. |
| 218 | * We take care to grab the all_mddevs_lock whenever navigating |
| 219 | * the list, and to always hold a refcount when unlocked. |
| 220 | * Any code which breaks out of this loop while own |
| 221 | * a reference to the current mddev and must mddev_put it. |
| 222 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 223 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | \ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 225 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 226 | _tmp = all_mddevs.next; \ |
| 227 | _mddev = NULL;}); \ |
| 228 | ({ if (_tmp != &all_mddevs) \ |
| 229 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 231 | if (_mddev) mddev_put(_mddev); \ |
| 232 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 233 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 235 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | ) |
| 237 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 238 | /* Rather than calling directly into the personality make_request function, |
| 239 | * IO requests come here first so that we can check if the device is |
| 240 | * being suspended pending a reconfiguration. |
| 241 | * We hold a refcount over the call to ->make_request. By the time that |
| 242 | * call has finished, the bio has been linked into some internal structure |
| 243 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 244 | */ |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 245 | static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 247 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 248 | struct mddev *mddev = q->queuedata; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 249 | unsigned int sectors; |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 250 | int cpu; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 251 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 252 | blk_queue_split(q, &bio, q->bio_split); |
| 253 | |
NeilBrown | 274d8cb | 2016-01-04 16:16:58 +1100 | [diff] [blame] | 254 | if (mddev == NULL || mddev->pers == NULL) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 255 | bio_io_error(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 256 | return BLK_QC_T_NONE; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 257 | } |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 258 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 259 | if (bio_sectors(bio) != 0) |
| 260 | bio->bi_error = -EROFS; |
| 261 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 262 | return BLK_QC_T_NONE; |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 263 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 264 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 265 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 266 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 267 | DEFINE_WAIT(__wait); |
| 268 | for (;;) { |
| 269 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 270 | TASK_UNINTERRUPTIBLE); |
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 | break; |
| 273 | rcu_read_unlock(); |
| 274 | schedule(); |
| 275 | rcu_read_lock(); |
| 276 | } |
| 277 | finish_wait(&mddev->sb_wait, &__wait); |
| 278 | } |
| 279 | atomic_inc(&mddev->active_io); |
| 280 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 281 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 282 | /* |
| 283 | * save the sectors now since our bio can |
| 284 | * go away inside make_request |
| 285 | */ |
| 286 | sectors = bio_sectors(bio); |
Shaohua Li | 9c573de | 2016-04-25 16:52:38 -0700 | [diff] [blame] | 287 | /* bio could be mergeable after passing to underlayer */ |
| 288 | bio->bi_rw &= ~REQ_NOMERGE; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 289 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 290 | |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 291 | cpu = part_stat_lock(); |
| 292 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 293 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
| 294 | part_stat_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 295 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 296 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 297 | wake_up(&mddev->sb_wait); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 298 | |
| 299 | return BLK_QC_T_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 302 | /* mddev_suspend makes sure no new requests are submitted |
| 303 | * to the device, and that any requests that have been submitted |
| 304 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 305 | * Once mddev_detach() is called and completes, the module will be |
| 306 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 307 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 308 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 309 | { |
Heinz Mauelshagen | 092398d | 2016-05-03 19:43:57 +0200 | [diff] [blame] | 310 | WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk); |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 311 | if (mddev->suspended++) |
| 312 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 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 | { |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 323 | if (--mddev->suspended) |
| 324 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 325 | wake_up(&mddev->sb_wait); |
| 326 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 327 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 328 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 329 | md_wakeup_thread(mddev->thread); |
| 330 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 331 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 332 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 333 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 334 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 335 | { |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 336 | struct md_personality *pers = mddev->pers; |
| 337 | int ret = 0; |
| 338 | |
| 339 | rcu_read_lock(); |
| 340 | if (mddev->suspended) |
| 341 | ret = 1; |
| 342 | else if (pers && pers->congested) |
| 343 | ret = pers->congested(mddev, bits); |
| 344 | rcu_read_unlock(); |
| 345 | return ret; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 346 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 347 | EXPORT_SYMBOL_GPL(mddev_congested); |
| 348 | static int md_congested(void *data, int bits) |
| 349 | { |
| 350 | struct mddev *mddev = data; |
| 351 | return mddev_congested(mddev, bits); |
| 352 | } |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 353 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 354 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 355 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 356 | */ |
| 357 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 358 | static void md_end_flush(struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 359 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 360 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 361 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 362 | |
| 363 | rdev_dec_pending(rdev, mddev); |
| 364 | |
| 365 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 366 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 367 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 368 | } |
| 369 | bio_put(bio); |
| 370 | } |
| 371 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 372 | static void md_submit_flush_data(struct work_struct *ws); |
| 373 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 374 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 375 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 376 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 377 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 378 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 379 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 380 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 381 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 382 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 383 | if (rdev->raid_disk >= 0 && |
| 384 | !test_bit(Faulty, &rdev->flags)) { |
| 385 | /* Take two references, one is dropped |
| 386 | * when request finishes, one after |
| 387 | * we reclaim rcu_read_lock |
| 388 | */ |
| 389 | struct bio *bi; |
| 390 | atomic_inc(&rdev->nr_pending); |
| 391 | atomic_inc(&rdev->nr_pending); |
| 392 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 393 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 394 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 395 | bi->bi_private = rdev; |
| 396 | bi->bi_bdev = rdev->bdev; |
| 397 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 398 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 399 | rcu_read_lock(); |
| 400 | rdev_dec_pending(rdev, mddev); |
| 401 | } |
| 402 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 403 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 404 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 405 | } |
| 406 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 407 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 408 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 409 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 410 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 411 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 412 | if (bio->bi_iter.bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 413 | /* an empty barrier - all done */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 414 | bio_endio(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 415 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 416 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 417 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 418 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 419 | |
| 420 | mddev->flush_bio = NULL; |
| 421 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 422 | } |
| 423 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 424 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 425 | { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 426 | spin_lock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 427 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 428 | !mddev->flush_bio, |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 429 | mddev->lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 430 | mddev->flush_bio = bio; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 431 | spin_unlock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 432 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 433 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 434 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 435 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 436 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 437 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 438 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 439 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 440 | struct mddev *mddev = cb->data; |
| 441 | md_wakeup_thread(mddev->thread); |
| 442 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 443 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 444 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 445 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 446 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | { |
| 448 | atomic_inc(&mddev->active); |
| 449 | return mddev; |
| 450 | } |
| 451 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 452 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 453 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 454 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 456 | struct bio_set *bs = NULL; |
| 457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 459 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 460 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 461 | mddev->ctime == 0 && !mddev->hold_active) { |
| 462 | /* Array is not configured at all, and not held active, |
| 463 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 464 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 465 | bs = mddev->bio_set; |
| 466 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 467 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 468 | /* We did a probe so need to clean up. Call |
| 469 | * queue_work inside the spinlock so that |
| 470 | * flush_workqueue() after mddev_find will |
| 471 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 472 | */ |
| 473 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 474 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 475 | } else |
| 476 | kfree(mddev); |
| 477 | } |
| 478 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 479 | if (bs) |
| 480 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 483 | static void md_safemode_timeout(unsigned long data); |
| 484 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 485 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 486 | { |
| 487 | mutex_init(&mddev->open_mutex); |
| 488 | mutex_init(&mddev->reconfig_mutex); |
| 489 | mutex_init(&mddev->bitmap_info.mutex); |
| 490 | INIT_LIST_HEAD(&mddev->disks); |
| 491 | INIT_LIST_HEAD(&mddev->all_mddevs); |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 492 | setup_timer(&mddev->safemode_timer, md_safemode_timeout, |
| 493 | (unsigned long) mddev); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 494 | atomic_set(&mddev->active, 1); |
| 495 | atomic_set(&mddev->openers, 0); |
| 496 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 497 | spin_lock_init(&mddev->lock); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 498 | atomic_set(&mddev->flush_pending, 0); |
| 499 | init_waitqueue_head(&mddev->sb_wait); |
| 500 | init_waitqueue_head(&mddev->recovery_wait); |
| 501 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 502 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 503 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 504 | mddev->resync_min = 0; |
| 505 | mddev->resync_max = MaxSector; |
| 506 | mddev->level = LEVEL_NONE; |
| 507 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 508 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 509 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 510 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 512 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 514 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 515 | unit &= ~((1<<MdpMinorShift)-1); |
| 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | retry: |
| 518 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 520 | if (unit) { |
| 521 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 522 | if (mddev->unit == unit) { |
| 523 | mddev_get(mddev); |
| 524 | spin_unlock(&all_mddevs_lock); |
| 525 | kfree(new); |
| 526 | return mddev; |
| 527 | } |
| 528 | |
| 529 | if (new) { |
| 530 | list_add(&new->all_mddevs, &all_mddevs); |
| 531 | spin_unlock(&all_mddevs_lock); |
| 532 | new->hold_active = UNTIL_IOCTL; |
| 533 | return new; |
| 534 | } |
| 535 | } else if (new) { |
| 536 | /* find an unused unit number */ |
| 537 | static int next_minor = 512; |
| 538 | int start = next_minor; |
| 539 | int is_free = 0; |
| 540 | int dev = 0; |
| 541 | while (!is_free) { |
| 542 | dev = MKDEV(MD_MAJOR, next_minor); |
| 543 | next_minor++; |
| 544 | if (next_minor > MINORMASK) |
| 545 | next_minor = 0; |
| 546 | if (next_minor == start) { |
| 547 | /* Oh dear, all in use. */ |
| 548 | spin_unlock(&all_mddevs_lock); |
| 549 | kfree(new); |
| 550 | return NULL; |
| 551 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 552 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 553 | is_free = 1; |
| 554 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 555 | if (mddev->unit == dev) { |
| 556 | is_free = 0; |
| 557 | break; |
| 558 | } |
| 559 | } |
| 560 | new->unit = dev; |
| 561 | new->md_minor = MINOR(dev); |
| 562 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | list_add(&new->all_mddevs, &all_mddevs); |
| 564 | spin_unlock(&all_mddevs_lock); |
| 565 | return new; |
| 566 | } |
| 567 | spin_unlock(&all_mddevs_lock); |
| 568 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 569 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | if (!new) |
| 571 | return NULL; |
| 572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | new->unit = unit; |
| 574 | if (MAJOR(unit) == MD_MAJOR) |
| 575 | new->md_minor = MINOR(unit); |
| 576 | else |
| 577 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 578 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 579 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | goto retry; |
| 582 | } |
| 583 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 584 | static struct attribute_group md_redundancy_group; |
| 585 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 586 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 588 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 589 | /* These cannot be removed under reconfig_mutex as |
| 590 | * an access to the files will try to take reconfig_mutex |
| 591 | * while holding the file unremovable, which leads to |
| 592 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 593 | * So hold set sysfs_active while the remove in happeing, |
| 594 | * and anything else which might set ->to_remove or my |
| 595 | * otherwise change the sysfs namespace will fail with |
| 596 | * -EBUSY if sysfs_active is still set. |
| 597 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 598 | * test it under the same mutex to ensure its correct value |
| 599 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 600 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 601 | struct attribute_group *to_remove = mddev->to_remove; |
| 602 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 603 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 604 | mutex_unlock(&mddev->reconfig_mutex); |
| 605 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 606 | if (mddev->kobj.sd) { |
| 607 | if (to_remove != &md_redundancy_group) |
| 608 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 609 | if (mddev->pers == NULL || |
| 610 | mddev->pers->sync_request == NULL) { |
| 611 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 612 | if (mddev->sysfs_action) |
| 613 | sysfs_put(mddev->sysfs_action); |
| 614 | mddev->sysfs_action = NULL; |
| 615 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 616 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 617 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 618 | } else |
| 619 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 621 | /* As we've dropped the mutex we need a spinlock to |
| 622 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 623 | */ |
| 624 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 625 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 626 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 628 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 630 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 631 | { |
| 632 | struct md_rdev *rdev; |
| 633 | |
| 634 | rdev_for_each_rcu(rdev, mddev) |
| 635 | if (rdev->desc_nr == nr) |
| 636 | return rdev; |
| 637 | |
| 638 | return NULL; |
| 639 | } |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 640 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 641 | |
| 642 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 644 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 646 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | if (rdev->bdev->bd_dev == dev) |
| 648 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | return NULL; |
| 651 | } |
| 652 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 653 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 654 | { |
| 655 | struct md_rdev *rdev; |
| 656 | |
| 657 | rdev_for_each_rcu(rdev, mddev) |
| 658 | if (rdev->bdev->bd_dev == dev) |
| 659 | return rdev; |
| 660 | |
| 661 | return NULL; |
| 662 | } |
| 663 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 664 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 665 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 666 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 667 | list_for_each_entry(pers, &pers_list, list) { |
| 668 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 669 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 670 | if (strcmp(pers->name, clevel)==0) |
| 671 | return pers; |
| 672 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 673 | return NULL; |
| 674 | } |
| 675 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 676 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 677 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 679 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 680 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 683 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 686 | if (!rdev->sb_page) { |
| 687 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 688 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 694 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | { |
| 696 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 697 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | rdev->sb_loaded = 0; |
| 699 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 700 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 701 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 703 | if (rdev->bb_page) { |
| 704 | put_page(rdev->bb_page); |
| 705 | rdev->bb_page = NULL; |
| 706 | } |
Dan Williams | d3b407f | 2016-01-06 12:19:22 -0800 | [diff] [blame] | 707 | badblocks_exit(&rdev->badblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 709 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 711 | static void super_written(struct bio *bio) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 712 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 713 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 714 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 715 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 716 | if (bio->bi_error) { |
| 717 | printk("md: super_written gets error=%d\n", bio->bi_error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 718 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 719 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 720 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 721 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 722 | wake_up(&mddev->sb_wait); |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 723 | rdev_dec_pending(rdev, mddev); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 724 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 725 | } |
| 726 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 727 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 728 | sector_t sector, int size, struct page *page) |
| 729 | { |
| 730 | /* write first size bytes of page to sector of rdev |
| 731 | * Increment mddev->pending_writes before returning |
| 732 | * and decrement it on completion, waking up sb_wait |
| 733 | * if zero is reached. |
| 734 | * If an error occurred, call md_error |
| 735 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 736 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 737 | |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 738 | atomic_inc(&rdev->nr_pending); |
| 739 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 740 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 741 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 742 | bio_add_page(bio, page, size, 0); |
| 743 | bio->bi_private = rdev; |
| 744 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 745 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 746 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 747 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 748 | } |
| 749 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 750 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 751 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 752 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 753 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 754 | } |
| 755 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 756 | 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] | 757 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 759 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | int ret; |
| 761 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 762 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 763 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 764 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 765 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 766 | else if (rdev->mddev->reshape_position != MaxSector && |
| 767 | (rdev->mddev->reshape_backwards == |
| 768 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 769 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 770 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 771 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | bio_add_page(bio, page, size, 0); |
Kent Overstreet | c170bbb | 2013-11-24 16:32:22 -0700 | [diff] [blame] | 773 | submit_bio_wait(rw, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 775 | ret = !bio->bi_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | bio_put(bio); |
| 777 | return ret; |
| 778 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 779 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 781 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | { |
| 783 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | if (rdev->sb_loaded) |
| 786 | return 0; |
| 787 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 788 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | goto fail; |
| 790 | rdev->sb_loaded = 1; |
| 791 | return 0; |
| 792 | |
| 793 | fail: |
| 794 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 795 | bdevname(rdev->bdev,b)); |
| 796 | return -EINVAL; |
| 797 | } |
| 798 | |
| 799 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 800 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 801 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 802 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 803 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 804 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 808 | { |
| 809 | int ret; |
| 810 | mdp_super_t *tmp1, *tmp2; |
| 811 | |
| 812 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 813 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 814 | |
| 815 | if (!tmp1 || !tmp2) { |
| 816 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 817 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | goto abort; |
| 819 | } |
| 820 | |
| 821 | *tmp1 = *sb1; |
| 822 | *tmp2 = *sb2; |
| 823 | |
| 824 | /* |
| 825 | * nr_disks is not constant |
| 826 | */ |
| 827 | tmp1->nr_disks = 0; |
| 828 | tmp2->nr_disks = 0; |
| 829 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 830 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 832 | kfree(tmp1); |
| 833 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | return ret; |
| 835 | } |
| 836 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 837 | static u32 md_csum_fold(u32 csum) |
| 838 | { |
| 839 | csum = (csum & 0xffff) + (csum >> 16); |
| 840 | return (csum & 0xffff) + (csum >> 16); |
| 841 | } |
| 842 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 843 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 845 | u64 newcsum = 0; |
| 846 | u32 *sb32 = (u32*)sb; |
| 847 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | unsigned int disk_csum, csum; |
| 849 | |
| 850 | disk_csum = sb->sb_csum; |
| 851 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 852 | |
| 853 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 854 | newcsum += sb32[i]; |
| 855 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 856 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 857 | #ifdef CONFIG_ALPHA |
| 858 | /* This used to use csum_partial, which was wrong for several |
| 859 | * reasons including that different results are returned on |
| 860 | * different architectures. It isn't critical that we get exactly |
| 861 | * the same return value as before (we always csum_fold before |
| 862 | * testing, and that removes any differences). However as we |
| 863 | * know that csum_partial always returned a 16bit value on |
| 864 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 865 | */ |
| 866 | sb->sb_csum = md_csum_fold(disk_csum); |
| 867 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 869 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | return csum; |
| 871 | } |
| 872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | /* |
| 874 | * Handle superblock details. |
| 875 | * We want to be able to handle multiple superblock formats |
| 876 | * so we have a common interface to them all, and an array of |
| 877 | * different handlers. |
| 878 | * We rely on user-space to write the initial superblock, and support |
| 879 | * reading and updating of superblocks. |
| 880 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 881 | * 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] | 882 | * loads and validates a superblock on dev. |
| 883 | * if refdev != NULL, compare superblocks on both devices |
| 884 | * Return: |
| 885 | * 0 - dev has a superblock that is compatible with refdev |
| 886 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 887 | * so dev should be used as the refdev in future |
| 888 | * -EINVAL superblock incompatible or invalid |
| 889 | * -othererror e.g. -EIO |
| 890 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 891 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | * Verify that dev is acceptable into mddev. |
| 893 | * The first time, mddev->raid_disks will be 0, and data from |
| 894 | * dev should be merged in. Subsequent calls check that dev |
| 895 | * is new enough. Return 0 or -EINVAL |
| 896 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 897 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | * Update the superblock for rdev with data in mddev |
| 899 | * This does not write to disc. |
| 900 | * |
| 901 | */ |
| 902 | |
| 903 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 904 | char *name; |
| 905 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 906 | int (*load_super)(struct md_rdev *rdev, |
| 907 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 908 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 909 | int (*validate_super)(struct mddev *mddev, |
| 910 | struct md_rdev *rdev); |
| 911 | void (*sync_super)(struct mddev *mddev, |
| 912 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 913 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 914 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 915 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 916 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | }; |
| 918 | |
| 919 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 920 | * Check that the given mddev has no bitmap. |
| 921 | * |
| 922 | * This function is called from the run method of all personalities that do not |
| 923 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 924 | * has a bitmap. Otherwise, it returns 0. |
| 925 | * |
| 926 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 927 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 928 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 929 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 930 | return 0; |
| 931 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 932 | mdname(mddev), mddev->pers->name); |
| 933 | return 1; |
| 934 | } |
| 935 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 936 | |
| 937 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 938 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 940 | 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] | 941 | { |
| 942 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 943 | mdp_super_t *sb; |
| 944 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
| 946 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 947 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | * it's at the end of the disk. |
| 949 | * |
| 950 | * It also happens to be a multiple of 4Kb. |
| 951 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 952 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 954 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | if (ret) return ret; |
| 956 | |
| 957 | ret = -EINVAL; |
| 958 | |
| 959 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 960 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
| 962 | if (sb->md_magic != MD_SB_MAGIC) { |
| 963 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 964 | b); |
| 965 | goto abort; |
| 966 | } |
| 967 | |
| 968 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 969 | sb->minor_version < 90 || |
| 970 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 972 | sb->major_version, sb->minor_version, |
| 973 | b); |
| 974 | goto abort; |
| 975 | } |
| 976 | |
| 977 | if (sb->raid_disks <= 0) |
| 978 | goto abort; |
| 979 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 980 | 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] | 981 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 982 | b); |
| 983 | goto abort; |
| 984 | } |
| 985 | |
| 986 | rdev->preferred_minor = sb->md_minor; |
| 987 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 988 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 989 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 990 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
| 992 | if (sb->level == LEVEL_MULTIPATH) |
| 993 | rdev->desc_nr = -1; |
| 994 | else |
| 995 | rdev->desc_nr = sb->this_disk.number; |
| 996 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 997 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 999 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1001 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | if (!uuid_equal(refsb, sb)) { |
| 1003 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1004 | b, bdevname(refdev->bdev,b2)); |
| 1005 | goto abort; |
| 1006 | } |
| 1007 | if (!sb_equal(refsb, sb)) { |
| 1008 | printk(KERN_WARNING "md: %s has same UUID" |
| 1009 | " but different superblock to %s\n", |
| 1010 | b, bdevname(refdev->bdev, b2)); |
| 1011 | goto abort; |
| 1012 | } |
| 1013 | ev1 = md_event(sb); |
| 1014 | ev2 = md_event(refsb); |
| 1015 | if (ev1 > ev2) |
| 1016 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1017 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | ret = 0; |
| 1019 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1020 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1021 | /* Limit to 4TB as metadata cannot record more than that. |
| 1022 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1023 | * record this size) |
| 1024 | */ |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1025 | if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) && |
| 1026 | sb->level >= 1) |
| 1027 | rdev->sectors = (sector_t)(2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1029 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1030 | /* "this cannot possibly happen" ... */ |
| 1031 | ret = -EINVAL; |
| 1032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | abort: |
| 1034 | return ret; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * validate_super for 0.90.0 |
| 1039 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1040 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | { |
| 1042 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1043 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1044 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1046 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1047 | clear_bit(Faulty, &rdev->flags); |
| 1048 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1049 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1050 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | if (mddev->raid_disks == 0) { |
| 1053 | mddev->major_version = 0; |
| 1054 | mddev->minor_version = sb->minor_version; |
| 1055 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1056 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1057 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | mddev->ctime = sb->ctime; |
| 1059 | mddev->utime = sb->utime; |
| 1060 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1061 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | mddev->layout = sb->layout; |
| 1063 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1064 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1065 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1066 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1067 | mddev->bitmap_info.space = 0; |
| 1068 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1069 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1070 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1071 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1073 | if (mddev->minor_version >= 91) { |
| 1074 | mddev->reshape_position = sb->reshape_position; |
| 1075 | mddev->delta_disks = sb->delta_disks; |
| 1076 | mddev->new_level = sb->new_level; |
| 1077 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1078 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1079 | if (mddev->delta_disks < 0) |
| 1080 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1081 | } else { |
| 1082 | mddev->reshape_position = MaxSector; |
| 1083 | mddev->delta_disks = 0; |
| 1084 | mddev->new_level = mddev->level; |
| 1085 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1086 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1087 | } |
| 1088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1090 | mddev->recovery_cp = MaxSector; |
| 1091 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1092 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | sb->events_lo == sb->cp_events_lo) { |
| 1094 | mddev->recovery_cp = sb->recovery_cp; |
| 1095 | } else |
| 1096 | mddev->recovery_cp = 0; |
| 1097 | } |
| 1098 | |
| 1099 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1100 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1101 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1102 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1103 | |
| 1104 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1105 | |
| 1106 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1107 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1108 | mddev->bitmap_info.offset = |
| 1109 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1110 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1111 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1112 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1113 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1114 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1115 | /* Insist on good event counter while assembling, except |
| 1116 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1118 | if (sb->disks[rdev->desc_nr].state & ( |
| 1119 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1120 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1121 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1122 | } else if (mddev->bitmap) { |
| 1123 | /* if adding to array with a bitmap, then we can accept an |
| 1124 | * older device ... but not too old. |
| 1125 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1126 | if (ev1 < mddev->bitmap->events_cleared) |
| 1127 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1128 | if (ev1 < mddev->events) |
| 1129 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1130 | } else { |
| 1131 | if (ev1 < mddev->events) |
| 1132 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1133 | return 0; |
| 1134 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | desc = sb->disks + rdev->desc_nr; |
| 1138 | |
| 1139 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1140 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1141 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1142 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1143 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1145 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1146 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1147 | /* active but not in sync implies recovery up to |
| 1148 | * reshape position. We don't know exactly where |
| 1149 | * that is, so set to zero for now */ |
| 1150 | if (mddev->minor_version >= 91) { |
| 1151 | rdev->recovery_offset = 0; |
| 1152 | rdev->raid_disk = desc->raid_disk; |
| 1153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1155 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1156 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1157 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1158 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * sync_super for 0.90.0 |
| 1164 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1165 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | { |
| 1167 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1168 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | /* make rdev->sb match mddev data.. |
| 1172 | * |
| 1173 | * 1/ zero out disks |
| 1174 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1175 | * 3/ any empty disks < next_spare become removed |
| 1176 | * |
| 1177 | * disks[0] gets initialised to REMOVED because |
| 1178 | * we cannot be sure from other fields if it has |
| 1179 | * been initialised or not. |
| 1180 | */ |
| 1181 | int i; |
| 1182 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1183 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1184 | rdev->sb_size = MD_SB_BYTES; |
| 1185 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1186 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
| 1188 | memset(sb, 0, sizeof(*sb)); |
| 1189 | |
| 1190 | sb->md_magic = MD_SB_MAGIC; |
| 1191 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | sb->patch_version = mddev->patch_version; |
| 1193 | sb->gvalid_words = 0; /* ignored */ |
| 1194 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1195 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1196 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1197 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1198 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1199 | sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1201 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | sb->raid_disks = mddev->raid_disks; |
| 1203 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1204 | sb->not_persistent = 0; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1205 | sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | sb->state = 0; |
| 1207 | sb->events_hi = (mddev->events>>32); |
| 1208 | sb->events_lo = (u32)mddev->events; |
| 1209 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1210 | if (mddev->reshape_position == MaxSector) |
| 1211 | sb->minor_version = 90; |
| 1212 | else { |
| 1213 | sb->minor_version = 91; |
| 1214 | sb->reshape_position = mddev->reshape_position; |
| 1215 | sb->new_level = mddev->new_level; |
| 1216 | sb->delta_disks = mddev->delta_disks; |
| 1217 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1218 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1219 | } |
| 1220 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | if (mddev->in_sync) |
| 1222 | { |
| 1223 | sb->recovery_cp = mddev->recovery_cp; |
| 1224 | sb->cp_events_hi = (mddev->events>>32); |
| 1225 | sb->cp_events_lo = (u32)mddev->events; |
| 1226 | if (mddev->recovery_cp == MaxSector) |
| 1227 | sb->state = (1<< MD_SB_CLEAN); |
| 1228 | } else |
| 1229 | sb->recovery_cp = 0; |
| 1230 | |
| 1231 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1232 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1234 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1235 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1238 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1240 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1241 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1242 | |
| 1243 | if (rdev2->raid_disk >= 0 && |
| 1244 | sb->minor_version >= 91) |
| 1245 | /* we have nowhere to store the recovery_offset, |
| 1246 | * but if it is not below the reshape_position, |
| 1247 | * we can piggy-back on that. |
| 1248 | */ |
| 1249 | is_active = 1; |
| 1250 | if (rdev2->raid_disk < 0 || |
| 1251 | test_bit(Faulty, &rdev2->flags)) |
| 1252 | is_active = 0; |
| 1253 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1254 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1256 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1257 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | d = &sb->disks[rdev2->desc_nr]; |
| 1259 | nr_disks++; |
| 1260 | d->number = rdev2->desc_nr; |
| 1261 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1262 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1263 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | d->raid_disk = rdev2->raid_disk; |
| 1265 | else |
| 1266 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1267 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1269 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1271 | if (test_bit(In_sync, &rdev2->flags)) |
| 1272 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | active++; |
| 1274 | working++; |
| 1275 | } else { |
| 1276 | d->state = 0; |
| 1277 | spare++; |
| 1278 | working++; |
| 1279 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1280 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1281 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1284 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1285 | mdp_disk_t *d = &sb->disks[i]; |
| 1286 | if (d->state == 0 && d->number == 0) { |
| 1287 | d->number = i; |
| 1288 | d->raid_disk = i; |
| 1289 | d->state = (1<<MD_DISK_REMOVED); |
| 1290 | d->state |= (1<<MD_DISK_FAULTY); |
| 1291 | failed++; |
| 1292 | } |
| 1293 | } |
| 1294 | sb->nr_disks = nr_disks; |
| 1295 | sb->active_disks = active; |
| 1296 | sb->working_disks = working; |
| 1297 | sb->failed_disks = failed; |
| 1298 | sb->spare_disks = spare; |
| 1299 | |
| 1300 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1301 | sb->sb_csum = calc_sb_csum(sb); |
| 1302 | } |
| 1303 | |
| 1304 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1305 | * rdev_size_change for 0.90.0 |
| 1306 | */ |
| 1307 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1308 | super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1309 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1310 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1311 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1312 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1313 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1314 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1315 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1316 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1317 | /* Limit to 4TB as metadata cannot record more than that. |
| 1318 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1319 | */ |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1320 | if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) && |
| 1321 | rdev->mddev->level >= 1) |
| 1322 | num_sectors = (sector_t)(2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1323 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1324 | rdev->sb_page); |
| 1325 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1326 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1327 | } |
| 1328 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1329 | static int |
| 1330 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1331 | { |
| 1332 | /* non-zero offset changes not possible with v0.90 */ |
| 1333 | return new_offset == 0; |
| 1334 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1335 | |
| 1336 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | * version 1 superblock |
| 1338 | */ |
| 1339 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1340 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1342 | __le32 disk_csum; |
| 1343 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | unsigned long long newcsum; |
| 1345 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1346 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
| 1348 | disk_csum = sb->sb_csum; |
| 1349 | sb->sb_csum = 0; |
| 1350 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1351 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | newcsum += le32_to_cpu(*isuper++); |
| 1353 | |
| 1354 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1355 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | |
| 1357 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1358 | sb->sb_csum = disk_csum; |
| 1359 | return cpu_to_le32(csum); |
| 1360 | } |
| 1361 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1362 | 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] | 1363 | { |
| 1364 | struct mdp_superblock_1 *sb; |
| 1365 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1366 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1367 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1369 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | |
| 1371 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1372 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | * It is always aligned to a 4K boundary and |
| 1374 | * depeding on minor_version, it can be: |
| 1375 | * 0: At least 8K, but less than 12K, from end of device |
| 1376 | * 1: At start of device |
| 1377 | * 2: 4K from start of device. |
| 1378 | */ |
| 1379 | switch(minor_version) { |
| 1380 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1381 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1382 | sb_start -= 8*2; |
| 1383 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | break; |
| 1385 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1386 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | break; |
| 1388 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1389 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | break; |
| 1391 | default: |
| 1392 | return -EINVAL; |
| 1393 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1394 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1396 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1397 | * and it is safe to read 4k, so we do that |
| 1398 | */ |
| 1399 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | if (ret) return ret; |
| 1401 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1402 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | |
| 1404 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1405 | sb->major_version != cpu_to_le32(1) || |
| 1406 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1407 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1408 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | return -EINVAL; |
| 1410 | |
| 1411 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1412 | printk("md: invalid superblock checksum on %s\n", |
| 1413 | bdevname(rdev->bdev,b)); |
| 1414 | return -EINVAL; |
| 1415 | } |
| 1416 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1417 | printk("md: data_size too small on %s\n", |
| 1418 | bdevname(rdev->bdev,b)); |
| 1419 | return -EINVAL; |
| 1420 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1421 | if (sb->pad0 || |
| 1422 | sb->pad3[0] || |
| 1423 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1424 | /* Some padding is non-zero, might be a new feature */ |
| 1425 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | rdev->preferred_minor = 0xffff; |
| 1428 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1429 | rdev->new_data_offset = rdev->data_offset; |
| 1430 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1431 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1432 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1433 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1435 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1436 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1437 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1438 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1439 | |
| 1440 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1441 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1442 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1443 | if (minor_version |
| 1444 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1445 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1446 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1447 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1448 | rdev->desc_nr = -1; |
| 1449 | else |
| 1450 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1451 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1452 | if (!rdev->bb_page) { |
| 1453 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1454 | if (!rdev->bb_page) |
| 1455 | return -ENOMEM; |
| 1456 | } |
| 1457 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1458 | rdev->badblocks.count == 0) { |
| 1459 | /* need to load the bad block list. |
| 1460 | * Currently we limit it to one page. |
| 1461 | */ |
| 1462 | s32 offset; |
| 1463 | sector_t bb_sector; |
| 1464 | u64 *bbp; |
| 1465 | int i; |
| 1466 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1467 | if (sectors > (PAGE_SIZE / 512)) |
| 1468 | return -EINVAL; |
| 1469 | offset = le32_to_cpu(sb->bblog_offset); |
| 1470 | if (offset == 0) |
| 1471 | return -EINVAL; |
| 1472 | bb_sector = (long long)offset; |
| 1473 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1474 | rdev->bb_page, READ, true)) |
| 1475 | return -EIO; |
| 1476 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1477 | rdev->badblocks.shift = sb->bblog_shift; |
| 1478 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1479 | u64 bb = le64_to_cpu(*bbp); |
| 1480 | int count = bb & (0x3ff); |
| 1481 | u64 sector = bb >> 10; |
| 1482 | sector <<= sb->bblog_shift; |
| 1483 | count <<= sb->bblog_shift; |
| 1484 | if (bb + 1 == 0) |
| 1485 | break; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 1486 | if (badblocks_set(&rdev->badblocks, sector, count, 1)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1487 | return -EINVAL; |
| 1488 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1489 | } else if (sb->bblog_offset != 0) |
| 1490 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1491 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1492 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1493 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1494 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1496 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | |
| 1498 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1499 | sb->level != refsb->level || |
| 1500 | sb->layout != refsb->layout || |
| 1501 | sb->chunksize != refsb->chunksize) { |
| 1502 | printk(KERN_WARNING "md: %s has strangely different" |
| 1503 | " superblock to %s\n", |
| 1504 | bdevname(rdev->bdev,b), |
| 1505 | bdevname(refdev->bdev,b2)); |
| 1506 | return -EINVAL; |
| 1507 | } |
| 1508 | ev1 = le64_to_cpu(sb->events); |
| 1509 | ev2 = le64_to_cpu(refsb->events); |
| 1510 | |
| 1511 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1512 | ret = 1; |
| 1513 | else |
| 1514 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1516 | if (minor_version) { |
| 1517 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1518 | sectors -= rdev->data_offset; |
| 1519 | } else |
| 1520 | sectors = rdev->sb_start; |
| 1521 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1523 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1524 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | } |
| 1526 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1527 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1529 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1530 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1532 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1533 | clear_bit(Faulty, &rdev->flags); |
| 1534 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1535 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1536 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | if (mddev->raid_disks == 0) { |
| 1539 | mddev->major_version = 1; |
| 1540 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1541 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1542 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1543 | mddev->ctime = le64_to_cpu(sb->ctime); |
| 1544 | mddev->utime = le64_to_cpu(sb->utime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1546 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | mddev->layout = le32_to_cpu(sb->layout); |
| 1548 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1549 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1550 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1551 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1552 | mddev->bitmap_info.space = 0; |
| 1553 | /* Default location for bitmap is 1K after superblock |
| 1554 | * using 3K - total of 4K |
| 1555 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1556 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1557 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1558 | mddev->reshape_backwards = 0; |
| 1559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1561 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1562 | |
| 1563 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1564 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1565 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1566 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1567 | mddev->bitmap_info.offset = |
| 1568 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1569 | /* Metadata doesn't record how much space is available. |
| 1570 | * For 1.0, we assume we can use up to the superblock |
| 1571 | * if before, else to 4K beyond superblock. |
| 1572 | * For others, assume no change is possible. |
| 1573 | */ |
| 1574 | if (mddev->minor_version > 0) |
| 1575 | mddev->bitmap_info.space = 0; |
| 1576 | else if (mddev->bitmap_info.offset > 0) |
| 1577 | mddev->bitmap_info.space = |
| 1578 | 8 - mddev->bitmap_info.offset; |
| 1579 | else |
| 1580 | mddev->bitmap_info.space = |
| 1581 | -mddev->bitmap_info.offset; |
| 1582 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1583 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1584 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1585 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1586 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1587 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1588 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1589 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1590 | if (mddev->delta_disks < 0 || |
| 1591 | (mddev->delta_disks == 0 && |
| 1592 | (le32_to_cpu(sb->feature_map) |
| 1593 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1594 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1595 | } else { |
| 1596 | mddev->reshape_position = MaxSector; |
| 1597 | mddev->delta_disks = 0; |
| 1598 | mddev->new_level = mddev->level; |
| 1599 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1600 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1601 | } |
| 1602 | |
Shaohua Li | a62ab49 | 2016-01-06 14:37:13 -0800 | [diff] [blame] | 1603 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) { |
| 1604 | set_bit(MD_HAS_JOURNAL, &mddev->flags); |
| 1605 | if (mddev->recovery_cp == MaxSector) |
| 1606 | set_bit(MD_JOURNAL_CLEAN, &mddev->flags); |
| 1607 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1608 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1609 | /* Insist of good event counter while assembling, except for |
| 1610 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1612 | if (rdev->desc_nr >= 0 && |
| 1613 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
Song Liu | a3dfbda | 2015-10-08 21:54:11 -0700 | [diff] [blame] | 1614 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1615 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1616 | if (ev1 < mddev->events) |
| 1617 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1618 | } else if (mddev->bitmap) { |
| 1619 | /* If adding to array with a bitmap, then we can accept an |
| 1620 | * older device, but not too old. |
| 1621 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1622 | if (ev1 < mddev->bitmap->events_cleared) |
| 1623 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1624 | if (ev1 < mddev->events) |
| 1625 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1626 | } else { |
| 1627 | if (ev1 < mddev->events) |
| 1628 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1629 | return 0; |
| 1630 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1632 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1633 | if (rdev->desc_nr < 0 || |
| 1634 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1635 | role = MD_DISK_ROLE_SPARE; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1636 | rdev->desc_nr = -1; |
| 1637 | } else |
| 1638 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | switch(role) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1640 | case MD_DISK_ROLE_SPARE: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | break; |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1642 | case MD_DISK_ROLE_FAULTY: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1643 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | break; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1645 | case MD_DISK_ROLE_JOURNAL: /* journal device */ |
| 1646 | if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) { |
| 1647 | /* journal device without journal feature */ |
| 1648 | printk(KERN_WARNING |
| 1649 | "md: journal device provided without journal feature, ignoring the device\n"); |
| 1650 | return -EINVAL; |
| 1651 | } |
| 1652 | set_bit(Journal, &rdev->flags); |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1653 | rdev->journal_tail = le64_to_cpu(sb->journal_tail); |
Shaohua Li | 9b15603 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 1654 | rdev->raid_disk = 0; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1655 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1657 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1658 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1659 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1660 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1661 | if (!(le32_to_cpu(sb->feature_map) & |
| 1662 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1663 | rdev->saved_raid_disk = -1; |
| 1664 | } else |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1665 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | rdev->raid_disk = role; |
| 1667 | break; |
| 1668 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1669 | if (sb->devflags & WriteMostly1) |
| 1670 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1671 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1672 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1673 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1674 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1675 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | return 0; |
| 1677 | } |
| 1678 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1679 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | { |
| 1681 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1682 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | int max_dev, i; |
| 1684 | /* make rdev->sb match mddev and rdev data. */ |
| 1685 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1686 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | |
| 1688 | sb->feature_map = 0; |
| 1689 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1690 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1692 | |
| 1693 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1694 | sb->events = cpu_to_le64(mddev->events); |
| 1695 | if (mddev->in_sync) |
| 1696 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
Shaohua Li | bd18f64 | 2015-09-02 13:49:50 -0700 | [diff] [blame] | 1697 | else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags)) |
| 1698 | sb->resync_offset = cpu_to_le64(MaxSector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | else |
| 1700 | sb->resync_offset = cpu_to_le64(0); |
| 1701 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1702 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1703 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1704 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1705 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1706 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1707 | sb->level = cpu_to_le32(mddev->level); |
| 1708 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1709 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1710 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1711 | sb->devflags |= WriteMostly1; |
| 1712 | else |
| 1713 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1714 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1715 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1716 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1717 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1718 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1719 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1720 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1721 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 1722 | if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1723 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1724 | sb->feature_map |= |
| 1725 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1726 | sb->recovery_offset = |
| 1727 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1728 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 1729 | sb->feature_map |= |
| 1730 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1731 | } |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1732 | /* Note: recovery_offset and journal_tail share space */ |
| 1733 | if (test_bit(Journal, &rdev->flags)) |
| 1734 | sb->journal_tail = cpu_to_le64(rdev->journal_tail); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1735 | if (test_bit(Replacement, &rdev->flags)) |
| 1736 | sb->feature_map |= |
| 1737 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1738 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1739 | if (mddev->reshape_position != MaxSector) { |
| 1740 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1741 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1742 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1743 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1744 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1745 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1746 | if (mddev->delta_disks == 0 && |
| 1747 | mddev->reshape_backwards) |
| 1748 | sb->feature_map |
| 1749 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1750 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1751 | sb->feature_map |
| 1752 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1753 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1754 | - rdev->data_offset)); |
| 1755 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1756 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1757 | |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 1758 | if (mddev_is_clustered(mddev)) |
| 1759 | sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED); |
| 1760 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1761 | if (rdev->badblocks.count == 0) |
| 1762 | /* Nothing to do for bad blocks*/ ; |
| 1763 | else if (sb->bblog_offset == 0) |
| 1764 | /* Cannot record bad blocks on this device */ |
| 1765 | md_error(mddev, rdev); |
| 1766 | else { |
| 1767 | struct badblocks *bb = &rdev->badblocks; |
| 1768 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1769 | u64 *p = bb->page; |
| 1770 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1771 | if (bb->changed) { |
| 1772 | unsigned seq; |
| 1773 | |
| 1774 | retry: |
| 1775 | seq = read_seqbegin(&bb->lock); |
| 1776 | |
| 1777 | memset(bbp, 0xff, PAGE_SIZE); |
| 1778 | |
| 1779 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1780 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1781 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1782 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1783 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1784 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1785 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1786 | if (read_seqretry(&bb->lock, seq)) |
| 1787 | goto retry; |
| 1788 | |
| 1789 | bb->sector = (rdev->sb_start + |
| 1790 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1791 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1792 | } |
| 1793 | } |
| 1794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1796 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | if (rdev2->desc_nr+1 > max_dev) |
| 1798 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1799 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1800 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1801 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1802 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1803 | rdev->sb_size = max_dev * 2 + 256; |
| 1804 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1805 | if (rdev->sb_size & bmask) |
| 1806 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1807 | } else |
| 1808 | max_dev = le32_to_cpu(sb->max_dev); |
| 1809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | for (i=0; i<max_dev;i++) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1811 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1812 | |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1813 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) |
| 1814 | sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1816 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1818 | if (test_bit(Faulty, &rdev2->flags)) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1819 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1820 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1822 | else if (test_bit(Journal, &rdev2->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1823 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1824 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1825 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | else |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1827 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1831 | } |
| 1832 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1833 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1834 | 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] | 1835 | { |
| 1836 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1837 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1838 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1839 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1840 | if (rdev->data_offset != rdev->new_data_offset) |
| 1841 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1842 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1843 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1844 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1845 | max_sectors -= rdev->data_offset; |
| 1846 | if (!num_sectors || num_sectors > max_sectors) |
| 1847 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1848 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1849 | /* minor version 0 with bitmap we can't move */ |
| 1850 | return 0; |
| 1851 | } else { |
| 1852 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1853 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1854 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1855 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1856 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1857 | if (!num_sectors || num_sectors > max_sectors) |
| 1858 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1859 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1860 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1861 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1862 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1863 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1864 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1865 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1866 | rdev->sb_page); |
| 1867 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1868 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1869 | |
| 1870 | } |
| 1871 | |
| 1872 | static int |
| 1873 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1874 | unsigned long long new_offset) |
| 1875 | { |
| 1876 | /* All necessary checks on new >= old have been done */ |
| 1877 | struct bitmap *bitmap; |
| 1878 | if (new_offset >= rdev->data_offset) |
| 1879 | return 1; |
| 1880 | |
| 1881 | /* with 1.0 metadata, there is no metadata to tread on |
| 1882 | * so we can always move back */ |
| 1883 | if (rdev->mddev->minor_version == 0) |
| 1884 | return 1; |
| 1885 | |
| 1886 | /* otherwise we must be sure not to step on |
| 1887 | * any metadata, so stay: |
| 1888 | * 36K beyond start of superblock |
| 1889 | * beyond end of badblocks |
| 1890 | * beyond write-intent bitmap |
| 1891 | */ |
| 1892 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1893 | return 0; |
| 1894 | bitmap = rdev->mddev->bitmap; |
| 1895 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1896 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1897 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1898 | return 0; |
| 1899 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1900 | return 0; |
| 1901 | |
| 1902 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1903 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1905 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | [0] = { |
| 1907 | .name = "0.90.0", |
| 1908 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1909 | .load_super = super_90_load, |
| 1910 | .validate_super = super_90_validate, |
| 1911 | .sync_super = super_90_sync, |
| 1912 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1913 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | }, |
| 1915 | [1] = { |
| 1916 | .name = "md-1", |
| 1917 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1918 | .load_super = super_1_load, |
| 1919 | .validate_super = super_1_validate, |
| 1920 | .sync_super = super_1_sync, |
| 1921 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1922 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | }, |
| 1924 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1926 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1927 | { |
| 1928 | if (mddev->sync_super) { |
| 1929 | mddev->sync_super(mddev, rdev); |
| 1930 | return; |
| 1931 | } |
| 1932 | |
| 1933 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1934 | |
| 1935 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1936 | } |
| 1937 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1938 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1940 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1942 | rcu_read_lock(); |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 1943 | rdev_for_each_rcu(rdev, mddev1) { |
| 1944 | if (test_bit(Faulty, &rdev->flags) || |
| 1945 | test_bit(Journal, &rdev->flags) || |
| 1946 | rdev->raid_disk == -1) |
| 1947 | continue; |
| 1948 | rdev_for_each_rcu(rdev2, mddev2) { |
| 1949 | if (test_bit(Faulty, &rdev2->flags) || |
| 1950 | test_bit(Journal, &rdev2->flags) || |
| 1951 | rdev2->raid_disk == -1) |
| 1952 | continue; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1953 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1954 | rdev2->bdev->bd_contains) { |
| 1955 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1956 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1957 | } |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 1958 | } |
| 1959 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1960 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | return 0; |
| 1962 | } |
| 1963 | |
| 1964 | static LIST_HEAD(pending_raid_disks); |
| 1965 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1966 | /* |
| 1967 | * Try to register data integrity profile for an mddev |
| 1968 | * |
| 1969 | * This is called when an array is started and after a disk has been kicked |
| 1970 | * from the array. It only succeeds if all working and active component devices |
| 1971 | * are integrity capable with matching profiles. |
| 1972 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1973 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1974 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1975 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1976 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1977 | if (list_empty(&mddev->disks)) |
| 1978 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1979 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1980 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1981 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1982 | /* skip spares and non-functional disks */ |
| 1983 | if (test_bit(Faulty, &rdev->flags)) |
| 1984 | continue; |
| 1985 | if (rdev->raid_disk < 0) |
| 1986 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1987 | if (!reference) { |
| 1988 | /* Use the first rdev as the reference */ |
| 1989 | reference = rdev; |
| 1990 | continue; |
| 1991 | } |
| 1992 | /* does this rdev's profile match the reference profile? */ |
| 1993 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1994 | rdev->bdev->bd_disk) < 0) |
| 1995 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1996 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1997 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1998 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1999 | /* |
| 2000 | * All component devices are integrity capable and have matching |
| 2001 | * profiles, register the common profile for the md device. |
| 2002 | */ |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 2003 | blk_integrity_register(mddev->gendisk, |
| 2004 | bdev_get_integrity(reference->bdev)); |
| 2005 | |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2006 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 2007 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 2008 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 2009 | mdname(mddev)); |
| 2010 | return -EINVAL; |
| 2011 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2012 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2013 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2014 | EXPORT_SYMBOL(md_integrity_register); |
| 2015 | |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2016 | /* |
| 2017 | * Attempt to add an rdev, but only if it is consistent with the current |
| 2018 | * integrity profile |
| 2019 | */ |
| 2020 | int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2021 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2022 | struct blk_integrity *bi_rdev; |
| 2023 | struct blk_integrity *bi_mddev; |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2024 | char name[BDEVNAME_SIZE]; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2025 | |
| 2026 | if (!mddev->gendisk) |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2027 | return 0; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2028 | |
| 2029 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2030 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2031 | |
| 2032 | if (!bi_mddev) /* nothing to do */ |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2033 | return 0; |
| 2034 | |
| 2035 | if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) { |
| 2036 | printk(KERN_NOTICE "%s: incompatible integrity profile for %s\n", |
| 2037 | mdname(mddev), bdevname(rdev->bdev, name)); |
| 2038 | return -ENXIO; |
| 2039 | } |
| 2040 | |
| 2041 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2042 | } |
| 2043 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2044 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2045 | static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2047 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2048 | struct kobject *ko; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2049 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2051 | /* prevent duplicates */ |
| 2052 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2053 | return -EEXIST; |
| 2054 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2055 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2056 | if (!test_bit(Journal, &rdev->flags) && |
| 2057 | rdev->sectors && |
| 2058 | (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2059 | if (mddev->pers) { |
| 2060 | /* Cannot change size, so fail |
| 2061 | * If mddev->level <= 0, then we don't care |
| 2062 | * about aligning sizes (e.g. linear) |
| 2063 | */ |
| 2064 | if (mddev->level > 0) |
| 2065 | return -ENOSPC; |
| 2066 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2067 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2068 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | |
| 2070 | /* Verify rdev->desc_nr is unique. |
| 2071 | * If it is -1, assign a free number, else |
| 2072 | * check number is not in use |
| 2073 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2074 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | if (rdev->desc_nr < 0) { |
| 2076 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2077 | if (mddev->pers) |
| 2078 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2079 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | choice++; |
| 2081 | rdev->desc_nr = choice; |
| 2082 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2083 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2084 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2086 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2088 | rcu_read_unlock(); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2089 | if (!test_bit(Journal, &rdev->flags) && |
| 2090 | mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2091 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2092 | mdname(mddev), mddev->max_disks); |
| 2093 | return -EBUSY; |
| 2094 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2095 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2096 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2099 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2100 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2101 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2102 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2103 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2104 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2105 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2106 | /* failure here is OK */; |
| 2107 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2108 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2109 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2110 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2111 | |
| 2112 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2113 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2116 | |
| 2117 | fail: |
| 2118 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2119 | b, mdname(mddev)); |
| 2120 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | } |
| 2122 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2123 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2124 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2125 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2126 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2127 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2128 | } |
| 2129 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2130 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | { |
| 2132 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2133 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2134 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2135 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2137 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2138 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2139 | sysfs_put(rdev->sysfs_state); |
| 2140 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2141 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2142 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2143 | * writing to 'remove' to "dev/state". We also need |
| 2144 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2145 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2146 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2147 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2148 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2149 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | /* |
| 2153 | * prevent the device from being mounted, repartitioned or |
| 2154 | * otherwise reused by a RAID array (or any other kernel |
| 2155 | * subsystem), by bd_claiming the device. |
| 2156 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2157 | 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] | 2158 | { |
| 2159 | int err = 0; |
| 2160 | struct block_device *bdev; |
| 2161 | char b[BDEVNAME_SIZE]; |
| 2162 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2163 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2164 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | if (IS_ERR(bdev)) { |
| 2166 | printk(KERN_ERR "md: could not open %s.\n", |
| 2167 | __bdevname(dev, b)); |
| 2168 | return PTR_ERR(bdev); |
| 2169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | rdev->bdev = bdev; |
| 2171 | return err; |
| 2172 | } |
| 2173 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2174 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | { |
| 2176 | struct block_device *bdev = rdev->bdev; |
| 2177 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2178 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | void md_autodetect_dev(dev_t dev); |
| 2182 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2183 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | { |
| 2185 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2188 | bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2189 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2191 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2192 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | #endif |
| 2194 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2195 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2198 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | { |
| 2200 | unbind_rdev_from_array(rdev); |
| 2201 | export_rdev(rdev); |
| 2202 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2203 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2205 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2207 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2209 | while (!list_empty(&mddev->disks)) { |
| 2210 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2211 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2212 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | mddev->raid_disks = 0; |
| 2215 | mddev->major_version = 0; |
| 2216 | } |
| 2217 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2218 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2220 | /* Update each superblock (in-memory image), but |
| 2221 | * if we are allowed to, skip spares which already |
| 2222 | * have the right event counter, or have one earlier |
| 2223 | * (which would mean they aren't being marked as dirty |
| 2224 | * with the rest of the array) |
| 2225 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2226 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2227 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2228 | if (rdev->sb_events == mddev->events || |
| 2229 | (nospares && |
| 2230 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2231 | rdev->sb_events+1 == mddev->events)) { |
| 2232 | /* Don't update this superblock */ |
| 2233 | rdev->sb_loaded = 2; |
| 2234 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2235 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2236 | rdev->sb_loaded = 1; |
| 2237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | } |
| 2239 | } |
| 2240 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2241 | static bool does_sb_need_changing(struct mddev *mddev) |
| 2242 | { |
| 2243 | struct md_rdev *rdev; |
| 2244 | struct mdp_superblock_1 *sb; |
| 2245 | int role; |
| 2246 | |
| 2247 | /* Find a good rdev */ |
| 2248 | rdev_for_each(rdev, mddev) |
| 2249 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) |
| 2250 | break; |
| 2251 | |
| 2252 | /* No good device found. */ |
| 2253 | if (!rdev) |
| 2254 | return false; |
| 2255 | |
| 2256 | sb = page_address(rdev->sb_page); |
| 2257 | /* Check if a device has become faulty or a spare become active */ |
| 2258 | rdev_for_each(rdev, mddev) { |
| 2259 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 2260 | /* Device activated? */ |
| 2261 | if (role == 0xffff && rdev->raid_disk >=0 && |
| 2262 | !test_bit(Faulty, &rdev->flags)) |
| 2263 | return true; |
| 2264 | /* Device turned faulty? */ |
| 2265 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) |
| 2266 | return true; |
| 2267 | } |
| 2268 | |
| 2269 | /* Check if any mddev parameters have changed */ |
| 2270 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || |
| 2271 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2272 | (mddev->layout != le64_to_cpu(sb->layout)) || |
| 2273 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || |
| 2274 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) |
| 2275 | return true; |
| 2276 | |
| 2277 | return false; |
| 2278 | } |
| 2279 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2280 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2282 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2283 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2284 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2285 | int any_badblocks_changed = 0; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2286 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2288 | if (mddev->ro) { |
| 2289 | if (force_change) |
| 2290 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2291 | return; |
| 2292 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2293 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2294 | repeat: |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2295 | if (mddev_is_clustered(mddev)) { |
| 2296 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2297 | force_change = 1; |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 2298 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2299 | nospares = 1; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2300 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2301 | /* Has someone else has updated the sb */ |
| 2302 | if (!does_sb_need_changing(mddev)) { |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2303 | if (ret == 0) |
| 2304 | md_cluster_ops->metadata_update_cancel(mddev); |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 2305 | bit_clear_unless(&mddev->flags, BIT(MD_CHANGE_PENDING), |
| 2306 | BIT(MD_CHANGE_DEVS) | |
| 2307 | BIT(MD_CHANGE_CLEAN)); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2308 | return; |
| 2309 | } |
| 2310 | } |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2311 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2312 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2313 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2314 | if (rdev->raid_disk >= 0 && |
| 2315 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2316 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2317 | !test_bit(In_sync, &rdev->flags) && |
| 2318 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2319 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2320 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2321 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2322 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2323 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2324 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2325 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2326 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2327 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2328 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2329 | rdev->badblocks.changed = 0; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2330 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2331 | md_error(mddev, rdev); |
| 2332 | } |
| 2333 | clear_bit(Blocked, &rdev->flags); |
| 2334 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2335 | wake_up(&rdev->blocked_wait); |
| 2336 | } |
| 2337 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2338 | wake_up(&mddev->sb_wait); |
| 2339 | return; |
| 2340 | } |
| 2341 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2342 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2343 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 2344 | mddev->utime = ktime_get_real_seconds(); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2345 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2346 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2347 | force_change = 1; |
| 2348 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2349 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2350 | * though if events isn't the right even/odd, we will have to do |
| 2351 | * spares after all |
| 2352 | */ |
| 2353 | nospares = 1; |
| 2354 | if (force_change) |
| 2355 | nospares = 0; |
| 2356 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2357 | /* If the array is degraded, then skipping spares is both |
| 2358 | * dangerous and fairly pointless. |
| 2359 | * Dangerous because a device that was removed from the array |
| 2360 | * might have a event_count that still looks up-to-date, |
| 2361 | * so it can be re-added without a resync. |
| 2362 | * Pointless because if there are any spares to skip, |
| 2363 | * then a recovery will happen and soon that array won't |
| 2364 | * be degraded any more and the spare can go back to sleep then. |
| 2365 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2366 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2367 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2368 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2369 | |
| 2370 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2371 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2372 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2373 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2374 | && mddev->can_decrease_events |
| 2375 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2376 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2377 | mddev->can_decrease_events = 0; |
| 2378 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2379 | /* otherwise we have to go forward and ... */ |
| 2380 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2381 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2382 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2384 | /* |
| 2385 | * This 64-bit counter should never wrap. |
| 2386 | * Either we are in around ~1 trillion A.C., assuming |
| 2387 | * 1 reboot per second, or we have a bug... |
| 2388 | */ |
| 2389 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2390 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2391 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2392 | if (rdev->badblocks.changed) |
| 2393 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2394 | if (test_bit(Faulty, &rdev->flags)) |
| 2395 | set_bit(FaultRecorded, &rdev->flags); |
| 2396 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2397 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2398 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2399 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2401 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2402 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2404 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2405 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2407 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2408 | if (rdev->sb_loaded != 1) |
| 2409 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2411 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2412 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2413 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2414 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2415 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2416 | bdevname(rdev->bdev, b), |
| 2417 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2418 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2419 | if (rdev->badblocks.size) { |
| 2420 | md_super_write(mddev, rdev, |
| 2421 | rdev->badblocks.sector, |
| 2422 | rdev->badblocks.size << 9, |
| 2423 | rdev->bb_page); |
| 2424 | rdev->badblocks.size = 0; |
| 2425 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2426 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2427 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2428 | pr_debug("md: %s (skipping faulty)\n", |
| 2429 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2430 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2431 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | /* only need to write one superblock... */ |
| 2433 | break; |
| 2434 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2435 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2436 | /* 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] | 2437 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2438 | if (mddev_is_clustered(mddev) && ret == 0) |
| 2439 | md_cluster_ops->metadata_update_finish(mddev); |
| 2440 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2441 | if (mddev->in_sync != sync_req || |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 2442 | !bit_clear_unless(&mddev->flags, BIT(MD_CHANGE_PENDING), |
| 2443 | BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_CLEAN))) |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2444 | /* have to write it out again */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2445 | goto repeat; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2446 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2447 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2448 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2449 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2450 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2451 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2452 | clear_bit(Blocked, &rdev->flags); |
| 2453 | |
| 2454 | if (any_badblocks_changed) |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2455 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2456 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2457 | wake_up(&rdev->blocked_wait); |
| 2458 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2460 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2462 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2463 | { |
| 2464 | struct mddev *mddev = rdev->mddev; |
| 2465 | int err = 0; |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2466 | bool add_journal = test_bit(Journal, &rdev->flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2467 | |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2468 | if (!mddev->pers->hot_remove_disk || add_journal) { |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2469 | /* If there is hot_add_disk but no hot_remove_disk |
| 2470 | * then added disks for geometry changes, |
| 2471 | * and should be added immediately. |
| 2472 | */ |
| 2473 | super_types[mddev->major_version]. |
| 2474 | validate_super(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2475 | if (add_journal) |
| 2476 | mddev_suspend(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2477 | err = mddev->pers->hot_add_disk(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2478 | if (add_journal) |
| 2479 | mddev_resume(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2480 | if (err) { |
| 2481 | unbind_rdev_from_array(rdev); |
| 2482 | export_rdev(rdev); |
| 2483 | return err; |
| 2484 | } |
| 2485 | } |
| 2486 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2487 | |
| 2488 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2489 | if (mddev->degraded) |
| 2490 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2491 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2492 | md_new_event(mddev); |
| 2493 | md_wakeup_thread(mddev->thread); |
| 2494 | return 0; |
| 2495 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2497 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2498 | * We want to accept with case. For this we use cmd_match. |
| 2499 | */ |
| 2500 | static int cmd_match(const char *cmd, const char *str) |
| 2501 | { |
| 2502 | /* See if cmd, written into a sysfs file, matches |
| 2503 | * str. They must either be the same, or cmd can |
| 2504 | * have a trailing newline |
| 2505 | */ |
| 2506 | while (*cmd && *str && *cmd == *str) { |
| 2507 | cmd++; |
| 2508 | str++; |
| 2509 | } |
| 2510 | if (*cmd == '\n') |
| 2511 | cmd++; |
| 2512 | if (*str || *cmd) |
| 2513 | return 0; |
| 2514 | return 1; |
| 2515 | } |
| 2516 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2517 | struct rdev_sysfs_entry { |
| 2518 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2519 | ssize_t (*show)(struct md_rdev *, char *); |
| 2520 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2521 | }; |
| 2522 | |
| 2523 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2524 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2525 | { |
| 2526 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2527 | size_t len = 0; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2528 | unsigned long flags = ACCESS_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2529 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2530 | if (test_bit(Faulty, &flags) || |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2531 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2532 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2533 | sep = ","; |
| 2534 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2535 | if (test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2536 | len += sprintf(page+len, "%sin_sync",sep); |
| 2537 | sep = ","; |
| 2538 | } |
Shaohua Li | ac6096e | 2015-10-04 09:20:11 -0700 | [diff] [blame] | 2539 | if (test_bit(Journal, &flags)) { |
| 2540 | len += sprintf(page+len, "%sjournal",sep); |
| 2541 | sep = ","; |
| 2542 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2543 | if (test_bit(WriteMostly, &flags)) { |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2544 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2545 | sep = ","; |
| 2546 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2547 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2548 | (rdev->badblocks.unacked_exist |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2549 | && !test_bit(Faulty, &flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2550 | len += sprintf(page+len, "%sblocked", sep); |
| 2551 | sep = ","; |
| 2552 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2553 | if (!test_bit(Faulty, &flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2554 | !test_bit(Journal, &flags) && |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2555 | !test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2556 | len += sprintf(page+len, "%sspare", sep); |
| 2557 | sep = ","; |
| 2558 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2559 | if (test_bit(WriteErrorSeen, &flags)) { |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2560 | len += sprintf(page+len, "%swrite_error", sep); |
| 2561 | sep = ","; |
| 2562 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2563 | if (test_bit(WantReplacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2564 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2565 | sep = ","; |
| 2566 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2567 | if (test_bit(Replacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2568 | len += sprintf(page+len, "%sreplacement", sep); |
| 2569 | sep = ","; |
| 2570 | } |
| 2571 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2572 | return len+sprintf(page+len, "\n"); |
| 2573 | } |
| 2574 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2575 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2576 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2577 | { |
| 2578 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2579 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2580 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2581 | * writemostly - sets write_mostly |
| 2582 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2583 | * blocked - sets the Blocked flags |
| 2584 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2585 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2586 | * -insync - clear Insync for a device with a slot assigned, |
| 2587 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2588 | * write_error - sets WriteErrorSeen |
| 2589 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2590 | */ |
| 2591 | int err = -EINVAL; |
| 2592 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2593 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2594 | if (test_bit(Faulty, &rdev->flags)) |
| 2595 | err = 0; |
| 2596 | else |
| 2597 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2598 | } else if (cmd_match(buf, "remove")) { |
| 2599 | if (rdev->raid_disk >= 0) |
| 2600 | err = -EBUSY; |
| 2601 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2602 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2603 | err = 0; |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 2604 | if (mddev_is_clustered(mddev)) |
| 2605 | err = md_cluster_ops->remove_disk(mddev, rdev); |
| 2606 | |
| 2607 | if (err == 0) { |
| 2608 | md_kick_rdev_from_array(rdev); |
| 2609 | if (mddev->pers) |
| 2610 | md_update_sb(mddev, 1); |
| 2611 | md_new_event(mddev); |
| 2612 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2613 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2614 | } else if (cmd_match(buf, "writemostly")) { |
| 2615 | set_bit(WriteMostly, &rdev->flags); |
| 2616 | err = 0; |
| 2617 | } else if (cmd_match(buf, "-writemostly")) { |
| 2618 | clear_bit(WriteMostly, &rdev->flags); |
| 2619 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2620 | } else if (cmd_match(buf, "blocked")) { |
| 2621 | set_bit(Blocked, &rdev->flags); |
| 2622 | err = 0; |
| 2623 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2624 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2625 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2626 | /* metadata handler doesn't understand badblocks, |
| 2627 | * so we need to fail the device |
| 2628 | */ |
| 2629 | md_error(rdev->mddev, rdev); |
| 2630 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2631 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2632 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2633 | wake_up(&rdev->blocked_wait); |
| 2634 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2635 | md_wakeup_thread(rdev->mddev->thread); |
| 2636 | |
| 2637 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2638 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2639 | set_bit(In_sync, &rdev->flags); |
| 2640 | err = 0; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2641 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 && |
| 2642 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 2643 | if (rdev->mddev->pers == NULL) { |
| 2644 | clear_bit(In_sync, &rdev->flags); |
| 2645 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2646 | rdev->raid_disk = -1; |
| 2647 | err = 0; |
| 2648 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2649 | } else if (cmd_match(buf, "write_error")) { |
| 2650 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2651 | err = 0; |
| 2652 | } else if (cmd_match(buf, "-write_error")) { |
| 2653 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2654 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2655 | } else if (cmd_match(buf, "want_replacement")) { |
| 2656 | /* Any non-spare device that is not a replacement can |
| 2657 | * become want_replacement at any time, but we then need to |
| 2658 | * check if recovery is needed. |
| 2659 | */ |
| 2660 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2661 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2662 | !test_bit(Replacement, &rdev->flags)) |
| 2663 | set_bit(WantReplacement, &rdev->flags); |
| 2664 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2665 | md_wakeup_thread(rdev->mddev->thread); |
| 2666 | err = 0; |
| 2667 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2668 | /* Clearing 'want_replacement' is always allowed. |
| 2669 | * Once replacements starts it is too late though. |
| 2670 | */ |
| 2671 | err = 0; |
| 2672 | clear_bit(WantReplacement, &rdev->flags); |
| 2673 | } else if (cmd_match(buf, "replacement")) { |
| 2674 | /* Can only set a device as a replacement when array has not |
| 2675 | * yet been started. Once running, replacement is automatic |
| 2676 | * from spares, or by assigning 'slot'. |
| 2677 | */ |
| 2678 | if (rdev->mddev->pers) |
| 2679 | err = -EBUSY; |
| 2680 | else { |
| 2681 | set_bit(Replacement, &rdev->flags); |
| 2682 | err = 0; |
| 2683 | } |
| 2684 | } else if (cmd_match(buf, "-replacement")) { |
| 2685 | /* Similarly, can only clear Replacement before start */ |
| 2686 | if (rdev->mddev->pers) |
| 2687 | err = -EBUSY; |
| 2688 | else { |
| 2689 | clear_bit(Replacement, &rdev->flags); |
| 2690 | err = 0; |
| 2691 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2692 | } else if (cmd_match(buf, "re-add")) { |
| 2693 | if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 2694 | /* clear_bit is performed _after_ all the devices |
| 2695 | * have their local Faulty bit cleared. If any writes |
| 2696 | * happen in the meantime in the local node, they |
| 2697 | * will land in the local bitmap, which will be synced |
| 2698 | * by this node eventually |
| 2699 | */ |
| 2700 | if (!mddev_is_clustered(rdev->mddev) || |
| 2701 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 2702 | clear_bit(Faulty, &rdev->flags); |
| 2703 | err = add_bound_rdev(rdev); |
| 2704 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2705 | } else |
| 2706 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2707 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2708 | if (!err) |
| 2709 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2710 | return err ? err : len; |
| 2711 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2712 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 2713 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2714 | |
| 2715 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2716 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2717 | { |
| 2718 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2719 | } |
| 2720 | |
| 2721 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2722 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2723 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2724 | unsigned int n; |
| 2725 | int rv; |
| 2726 | |
| 2727 | rv = kstrtouint(buf, 10, &n); |
| 2728 | if (rv < 0) |
| 2729 | return rv; |
| 2730 | atomic_set(&rdev->corrected_errors, n); |
| 2731 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2732 | } |
| 2733 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2734 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2735 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2736 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2737 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2738 | { |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2739 | if (test_bit(Journal, &rdev->flags)) |
| 2740 | return sprintf(page, "journal\n"); |
| 2741 | else if (rdev->raid_disk < 0) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2742 | return sprintf(page, "none\n"); |
| 2743 | else |
| 2744 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2745 | } |
| 2746 | |
| 2747 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2748 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2749 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2750 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2751 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2752 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2753 | if (test_bit(Journal, &rdev->flags)) |
| 2754 | return -EBUSY; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2755 | if (strncmp(buf, "none", 4)==0) |
| 2756 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2757 | else { |
| 2758 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 2759 | if (err < 0) |
| 2760 | return err; |
| 2761 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2762 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2763 | /* Setting 'slot' on an active array requires also |
| 2764 | * updating the 'rd%d' link, and communicating |
| 2765 | * with the personality with ->hot_*_disk. |
| 2766 | * For now we only support removing |
| 2767 | * failed/spare devices. This normally happens automatically, |
| 2768 | * but not when the metadata is externally managed. |
| 2769 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2770 | if (rdev->raid_disk == -1) |
| 2771 | return -EEXIST; |
| 2772 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2773 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2774 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2775 | clear_bit(Blocked, &rdev->flags); |
| 2776 | remove_and_add_spares(rdev->mddev, rdev); |
| 2777 | if (rdev->raid_disk >= 0) |
| 2778 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2779 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2780 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2781 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2782 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2783 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2784 | */ |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2785 | int err; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2786 | |
| 2787 | if (rdev->raid_disk != -1) |
| 2788 | return -EBUSY; |
| 2789 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2790 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2791 | return -EBUSY; |
| 2792 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2793 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2794 | return -EINVAL; |
| 2795 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2796 | if (slot >= rdev->mddev->raid_disks && |
| 2797 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2798 | return -ENOSPC; |
| 2799 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2800 | rdev->raid_disk = slot; |
| 2801 | if (test_bit(In_sync, &rdev->flags)) |
| 2802 | rdev->saved_raid_disk = slot; |
| 2803 | else |
| 2804 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2805 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 2806 | clear_bit(Bitmap_sync, &rdev->flags); |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2807 | err = rdev->mddev->pers-> |
| 2808 | hot_add_disk(rdev->mddev, rdev); |
| 2809 | if (err) { |
| 2810 | rdev->raid_disk = -1; |
| 2811 | return err; |
| 2812 | } else |
| 2813 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2814 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
| 2815 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2816 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2817 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2818 | if (slot >= rdev->mddev->raid_disks && |
| 2819 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2820 | return -ENOSPC; |
| 2821 | rdev->raid_disk = slot; |
| 2822 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2823 | clear_bit(Faulty, &rdev->flags); |
| 2824 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2825 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2826 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2827 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2828 | return len; |
| 2829 | } |
| 2830 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2831 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2832 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2833 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2834 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2835 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2836 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2837 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2841 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2842 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2843 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2844 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2845 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2846 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2847 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2848 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2849 | /* Must set offset before size, so overlap checks |
| 2850 | * can be sane */ |
| 2851 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2852 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2853 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2854 | return len; |
| 2855 | } |
| 2856 | |
| 2857 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2858 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2859 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2860 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2861 | { |
| 2862 | return sprintf(page, "%llu\n", |
| 2863 | (unsigned long long)rdev->new_data_offset); |
| 2864 | } |
| 2865 | |
| 2866 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2867 | const char *buf, size_t len) |
| 2868 | { |
| 2869 | unsigned long long new_offset; |
| 2870 | struct mddev *mddev = rdev->mddev; |
| 2871 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2872 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2873 | return -EINVAL; |
| 2874 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 2875 | if (mddev->sync_thread || |
| 2876 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2877 | return -EBUSY; |
| 2878 | if (new_offset == rdev->data_offset) |
| 2879 | /* reset is always permitted */ |
| 2880 | ; |
| 2881 | else if (new_offset > rdev->data_offset) { |
| 2882 | /* must not push array size beyond rdev_sectors */ |
| 2883 | if (new_offset - rdev->data_offset |
| 2884 | + mddev->dev_sectors > rdev->sectors) |
| 2885 | return -E2BIG; |
| 2886 | } |
| 2887 | /* Metadata worries about other space details. */ |
| 2888 | |
| 2889 | /* decreasing the offset is inconsistent with a backwards |
| 2890 | * reshape. |
| 2891 | */ |
| 2892 | if (new_offset < rdev->data_offset && |
| 2893 | mddev->reshape_backwards) |
| 2894 | return -EINVAL; |
| 2895 | /* Increasing offset is inconsistent with forwards |
| 2896 | * reshape. reshape_direction should be set to |
| 2897 | * 'backwards' first. |
| 2898 | */ |
| 2899 | if (new_offset > rdev->data_offset && |
| 2900 | !mddev->reshape_backwards) |
| 2901 | return -EINVAL; |
| 2902 | |
| 2903 | if (mddev->pers && mddev->persistent && |
| 2904 | !super_types[mddev->major_version] |
| 2905 | .allow_new_offset(rdev, new_offset)) |
| 2906 | return -E2BIG; |
| 2907 | rdev->new_data_offset = new_offset; |
| 2908 | if (new_offset > rdev->data_offset) |
| 2909 | mddev->reshape_backwards = 1; |
| 2910 | else if (new_offset < rdev->data_offset) |
| 2911 | mddev->reshape_backwards = 0; |
| 2912 | |
| 2913 | return len; |
| 2914 | } |
| 2915 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2916 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2917 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2918 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2919 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2920 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2921 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2922 | } |
| 2923 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2924 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2925 | { |
| 2926 | /* check if two start/length pairs overlap */ |
| 2927 | if (s1+l1 <= s2) |
| 2928 | return 0; |
| 2929 | if (s2+l2 <= s1) |
| 2930 | return 0; |
| 2931 | return 1; |
| 2932 | } |
| 2933 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2934 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2935 | { |
| 2936 | unsigned long long blocks; |
| 2937 | sector_t new; |
| 2938 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2939 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2940 | return -EINVAL; |
| 2941 | |
| 2942 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2943 | return -EINVAL; /* sector conversion overflow */ |
| 2944 | |
| 2945 | new = blocks * 2; |
| 2946 | if (new != blocks * 2) |
| 2947 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2948 | |
| 2949 | *sectors = new; |
| 2950 | return 0; |
| 2951 | } |
| 2952 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2953 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2954 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2955 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2956 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2957 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2958 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2959 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2960 | if (test_bit(Journal, &rdev->flags)) |
| 2961 | return -EBUSY; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2962 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2963 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2964 | if (rdev->data_offset != rdev->new_data_offset) |
| 2965 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2966 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2967 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2968 | sectors = super_types[my_mddev->major_version]. |
| 2969 | rdev_size_change(rdev, sectors); |
| 2970 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2971 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2972 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2973 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2974 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 2975 | if (!my_mddev->pers->resize) |
| 2976 | /* Cannot change size for RAID0 or Linear etc */ |
| 2977 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2978 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2979 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2980 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2981 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2982 | rdev->sectors = sectors; |
| 2983 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2984 | /* Need to check that all other rdevs with the same |
| 2985 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 2986 | * the rdev lists safely. |
| 2987 | * This check does not provide a hard guarantee, it |
| 2988 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2989 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2990 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2991 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2992 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2993 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2994 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2995 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2996 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2997 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2998 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2999 | if (rdev->bdev == rdev2->bdev && |
| 3000 | rdev != rdev2 && |
| 3001 | overlaps(rdev->data_offset, rdev->sectors, |
| 3002 | rdev2->data_offset, |
| 3003 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3004 | overlap = 1; |
| 3005 | break; |
| 3006 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3007 | if (overlap) { |
| 3008 | mddev_put(mddev); |
| 3009 | break; |
| 3010 | } |
| 3011 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3012 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3013 | if (overlap) { |
| 3014 | /* Someone else could have slipped in a size |
| 3015 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3016 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3017 | * safe, and trust userspace not to race with |
| 3018 | * itself |
| 3019 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3020 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3021 | return -EBUSY; |
| 3022 | } |
| 3023 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3024 | return len; |
| 3025 | } |
| 3026 | |
| 3027 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3028 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3029 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3030 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3031 | { |
| 3032 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3033 | |
| 3034 | if (test_bit(In_sync, &rdev->flags) || |
| 3035 | recovery_start == MaxSector) |
| 3036 | return sprintf(page, "none\n"); |
| 3037 | |
| 3038 | return sprintf(page, "%llu\n", recovery_start); |
| 3039 | } |
| 3040 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3041 | 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] | 3042 | { |
| 3043 | unsigned long long recovery_start; |
| 3044 | |
| 3045 | if (cmd_match(buf, "none")) |
| 3046 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3047 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3048 | return -EINVAL; |
| 3049 | |
| 3050 | if (rdev->mddev->pers && |
| 3051 | rdev->raid_disk >= 0) |
| 3052 | return -EBUSY; |
| 3053 | |
| 3054 | rdev->recovery_offset = recovery_start; |
| 3055 | if (recovery_start == MaxSector) |
| 3056 | set_bit(In_sync, &rdev->flags); |
| 3057 | else |
| 3058 | clear_bit(In_sync, &rdev->flags); |
| 3059 | return len; |
| 3060 | } |
| 3061 | |
| 3062 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3063 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3064 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3065 | /* sysfs access to bad-blocks list. |
| 3066 | * We present two files. |
| 3067 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 3068 | * are recorded as bad. The list is truncated to fit within |
| 3069 | * the one-page limit of sysfs. |
| 3070 | * Writing "sector length" to this file adds an acknowledged |
| 3071 | * bad block list. |
| 3072 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 3073 | * been acknowledged. Writing to this file adds bad blocks |
| 3074 | * without acknowledging them. This is largely for testing. |
| 3075 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3076 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3077 | { |
| 3078 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3079 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3080 | 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] | 3081 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3082 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3083 | /* Maybe that ack was all we needed */ |
| 3084 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3085 | wake_up(&rdev->blocked_wait); |
| 3086 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3087 | } |
| 3088 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3089 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3090 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3091 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3092 | { |
| 3093 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3094 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3095 | 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] | 3096 | { |
| 3097 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3098 | } |
| 3099 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3100 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3101 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3102 | static struct attribute *rdev_default_attrs[] = { |
| 3103 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3104 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3105 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3106 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3107 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3108 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3109 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3110 | &rdev_bad_blocks.attr, |
| 3111 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3112 | NULL, |
| 3113 | }; |
| 3114 | static ssize_t |
| 3115 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3116 | { |
| 3117 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3118 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3119 | |
| 3120 | if (!entry->show) |
| 3121 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3122 | if (!rdev->mddev) |
| 3123 | return -EBUSY; |
| 3124 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3125 | } |
| 3126 | |
| 3127 | static ssize_t |
| 3128 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3129 | const char *page, size_t length) |
| 3130 | { |
| 3131 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3132 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3133 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3134 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3135 | |
| 3136 | if (!entry->store) |
| 3137 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3138 | if (!capable(CAP_SYS_ADMIN)) |
| 3139 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3140 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3141 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3142 | if (rdev->mddev == NULL) |
| 3143 | rv = -EBUSY; |
| 3144 | else |
| 3145 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3146 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3147 | } |
| 3148 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3149 | } |
| 3150 | |
| 3151 | static void rdev_free(struct kobject *ko) |
| 3152 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3153 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3154 | kfree(rdev); |
| 3155 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3156 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3157 | .show = rdev_attr_show, |
| 3158 | .store = rdev_attr_store, |
| 3159 | }; |
| 3160 | static struct kobj_type rdev_ktype = { |
| 3161 | .release = rdev_free, |
| 3162 | .sysfs_ops = &rdev_sysfs_ops, |
| 3163 | .default_attrs = rdev_default_attrs, |
| 3164 | }; |
| 3165 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3166 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3167 | { |
| 3168 | rdev->desc_nr = -1; |
| 3169 | rdev->saved_raid_disk = -1; |
| 3170 | rdev->raid_disk = -1; |
| 3171 | rdev->flags = 0; |
| 3172 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3173 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3174 | rdev->sb_events = 0; |
| 3175 | rdev->last_read_error.tv_sec = 0; |
| 3176 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3177 | rdev->sb_loaded = 0; |
| 3178 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3179 | atomic_set(&rdev->nr_pending, 0); |
| 3180 | atomic_set(&rdev->read_errors, 0); |
| 3181 | atomic_set(&rdev->corrected_errors, 0); |
| 3182 | |
| 3183 | INIT_LIST_HEAD(&rdev->same_set); |
| 3184 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3185 | |
| 3186 | /* Add space to store bad block list. |
| 3187 | * This reserves the space even on arrays where it cannot |
| 3188 | * be used - I wonder if that matters |
| 3189 | */ |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3190 | return badblocks_init(&rdev->badblocks, 0); |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3191 | } |
| 3192 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | /* |
| 3194 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3195 | * |
| 3196 | * mark the device faulty if: |
| 3197 | * |
| 3198 | * - the device is nonexistent (zero size) |
| 3199 | * - the device has no valid superblock |
| 3200 | * |
| 3201 | * a faulty rdev _never_ has rdev->sb set. |
| 3202 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3203 | 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] | 3204 | { |
| 3205 | char b[BDEVNAME_SIZE]; |
| 3206 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3207 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | sector_t size; |
| 3209 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3210 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3211 | if (!rdev) { |
| 3212 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3213 | return ERR_PTR(-ENOMEM); |
| 3214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3215 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3216 | err = md_rdev_init(rdev); |
| 3217 | if (err) |
| 3218 | goto abort_free; |
| 3219 | err = alloc_disk_sb(rdev); |
| 3220 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | goto abort_free; |
| 3222 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3223 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3224 | if (err) |
| 3225 | goto abort_free; |
| 3226 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3227 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3228 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3229 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3230 | if (!size) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3231 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3233 | bdevname(rdev->bdev,b)); |
| 3234 | err = -EINVAL; |
| 3235 | goto abort_free; |
| 3236 | } |
| 3237 | |
| 3238 | if (super_format >= 0) { |
| 3239 | err = super_types[super_format]. |
| 3240 | load_super(rdev, NULL, super_minor); |
| 3241 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3242 | printk(KERN_WARNING |
| 3243 | "md: %s does not have a valid v%d.%d " |
| 3244 | "superblock, not importing!\n", |
| 3245 | bdevname(rdev->bdev,b), |
| 3246 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3247 | goto abort_free; |
| 3248 | } |
| 3249 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3250 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3251 | "md: could not read %s's sb, not importing!\n", |
| 3252 | bdevname(rdev->bdev,b)); |
| 3253 | goto abort_free; |
| 3254 | } |
| 3255 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | return rdev; |
| 3258 | |
| 3259 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3260 | if (rdev->bdev) |
| 3261 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3262 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3263 | kfree(rdev); |
| 3264 | return ERR_PTR(err); |
| 3265 | } |
| 3266 | |
| 3267 | /* |
| 3268 | * Check a full RAID array for plausibility |
| 3269 | */ |
| 3270 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3271 | static void analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | { |
| 3273 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3274 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | char b[BDEVNAME_SIZE]; |
| 3276 | |
| 3277 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3278 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | switch (super_types[mddev->major_version]. |
| 3280 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3281 | case 1: |
| 3282 | freshest = rdev; |
| 3283 | break; |
| 3284 | case 0: |
| 3285 | break; |
| 3286 | default: |
| 3287 | printk( KERN_ERR \ |
| 3288 | "md: fatal superblock inconsistency in %s" |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3289 | " -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3290 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3291 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3292 | } |
| 3293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | super_types[mddev->major_version]. |
| 3295 | validate_super(mddev, freshest); |
| 3296 | |
| 3297 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3298 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3299 | if (mddev->max_disks && |
| 3300 | (rdev->desc_nr >= mddev->max_disks || |
| 3301 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3302 | printk(KERN_WARNING |
| 3303 | "md: %s: %s: only %d devices permitted\n", |
| 3304 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3305 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3306 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3307 | continue; |
| 3308 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3309 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3310 | if (super_types[mddev->major_version]. |
| 3311 | validate_super(mddev, rdev)) { |
| 3312 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3313 | " from array!\n", |
| 3314 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3315 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | continue; |
| 3317 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3318 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3320 | rdev->desc_nr = i++; |
| 3321 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3322 | set_bit(In_sync, &rdev->flags); |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3323 | } else if (rdev->raid_disk >= |
| 3324 | (mddev->raid_disks - min(0, mddev->delta_disks)) && |
| 3325 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3326 | rdev->raid_disk = -1; |
| 3327 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3328 | } |
| 3329 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | } |
| 3331 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3332 | /* Read a fixed-point number. |
| 3333 | * Numbers in sysfs attributes should be in "standard" units where |
| 3334 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3335 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3336 | * milliseconds or jiffies. |
| 3337 | * This function takes a decimal number with a possible fractional |
| 3338 | * component, and produces an integer which is the result of |
| 3339 | * multiplying that number by 10^'scale'. |
| 3340 | * all without any floating-point arithmetic. |
| 3341 | */ |
| 3342 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3343 | { |
| 3344 | unsigned long result = 0; |
| 3345 | long decimals = -1; |
| 3346 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3347 | if (*cp == '.') |
| 3348 | decimals = 0; |
| 3349 | else if (decimals < scale) { |
| 3350 | unsigned int value; |
| 3351 | value = *cp - '0'; |
| 3352 | result = result * 10 + value; |
| 3353 | if (decimals >= 0) |
| 3354 | decimals++; |
| 3355 | } |
| 3356 | cp++; |
| 3357 | } |
| 3358 | if (*cp == '\n') |
| 3359 | cp++; |
| 3360 | if (*cp) |
| 3361 | return -EINVAL; |
| 3362 | if (decimals < 0) |
| 3363 | decimals = 0; |
| 3364 | while (decimals < scale) { |
| 3365 | result *= 10; |
| 3366 | decimals ++; |
| 3367 | } |
| 3368 | *res = result; |
| 3369 | return 0; |
| 3370 | } |
| 3371 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3372 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3373 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3374 | { |
| 3375 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3376 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3377 | } |
| 3378 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3379 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3380 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3381 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3382 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3383 | if (mddev_is_clustered(mddev)) { |
| 3384 | pr_info("md: Safemode is disabled for clustered mode\n"); |
| 3385 | return -EINVAL; |
| 3386 | } |
| 3387 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3388 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3389 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3390 | if (msec == 0) |
| 3391 | mddev->safemode_delay = 0; |
| 3392 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3393 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3394 | unsigned long new_delay = (msec*HZ)/1000; |
| 3395 | |
| 3396 | if (new_delay == 0) |
| 3397 | new_delay = 1; |
| 3398 | mddev->safemode_delay = new_delay; |
| 3399 | if (new_delay < old_delay || old_delay == 0) |
| 3400 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3401 | } |
| 3402 | return len; |
| 3403 | } |
| 3404 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3405 | __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] | 3406 | |
| 3407 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3408 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3409 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3410 | struct md_personality *p; |
| 3411 | int ret; |
| 3412 | spin_lock(&mddev->lock); |
| 3413 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3414 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3415 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3416 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3417 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3418 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3419 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3420 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3421 | ret = 0; |
| 3422 | spin_unlock(&mddev->lock); |
| 3423 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3424 | } |
| 3425 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3426 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3427 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3428 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3429 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3430 | ssize_t rv; |
| 3431 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3432 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3433 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3434 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3435 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3436 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3437 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3438 | return -EINVAL; |
| 3439 | |
| 3440 | rv = mddev_lock(mddev); |
| 3441 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3442 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3443 | |
| 3444 | if (mddev->pers == NULL) { |
| 3445 | strncpy(mddev->clevel, buf, slen); |
| 3446 | if (mddev->clevel[slen-1] == '\n') |
| 3447 | slen--; |
| 3448 | mddev->clevel[slen] = 0; |
| 3449 | mddev->level = LEVEL_NONE; |
| 3450 | rv = len; |
| 3451 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3452 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3453 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3454 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3455 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3456 | |
| 3457 | /* request to change the personality. Need to ensure: |
| 3458 | * - array is not engaged in resync/recovery/reshape |
| 3459 | * - old personality can be suspended |
| 3460 | * - new personality will access other array. |
| 3461 | */ |
| 3462 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3463 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3464 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3465 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3466 | mddev->reshape_position != MaxSector || |
| 3467 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3468 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3469 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3470 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3471 | if (!mddev->pers->quiesce) { |
| 3472 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3473 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3474 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3475 | } |
| 3476 | |
| 3477 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3478 | strncpy(clevel, buf, slen); |
| 3479 | if (clevel[slen-1] == '\n') |
| 3480 | slen--; |
| 3481 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3482 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3483 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3484 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3485 | if (request_module("md-%s", clevel) != 0) |
| 3486 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3487 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3488 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3489 | if (!pers || !try_module_get(pers->owner)) { |
| 3490 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3491 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3492 | rv = -EINVAL; |
| 3493 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3494 | } |
| 3495 | spin_unlock(&pers_lock); |
| 3496 | |
| 3497 | if (pers == mddev->pers) { |
| 3498 | /* Nothing to do! */ |
| 3499 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3500 | rv = len; |
| 3501 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3502 | } |
| 3503 | if (!pers->takeover) { |
| 3504 | module_put(pers->owner); |
| 3505 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3506 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3507 | rv = -EINVAL; |
| 3508 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3509 | } |
| 3510 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3511 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3512 | rdev->new_raid_disk = rdev->raid_disk; |
| 3513 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3514 | /* ->takeover must set new_* and/or delta_disks |
| 3515 | * if it succeeds, and may set them when it fails. |
| 3516 | */ |
| 3517 | priv = pers->takeover(mddev); |
| 3518 | if (IS_ERR(priv)) { |
| 3519 | mddev->new_level = mddev->level; |
| 3520 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3521 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3522 | mddev->raid_disks -= mddev->delta_disks; |
| 3523 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3524 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3525 | module_put(pers->owner); |
| 3526 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3527 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3528 | rv = PTR_ERR(priv); |
| 3529 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | /* Looks like we have a winner */ |
| 3533 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 3534 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3535 | |
| 3536 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3537 | oldpers = mddev->pers; |
| 3538 | oldpriv = mddev->private; |
| 3539 | mddev->pers = pers; |
| 3540 | mddev->private = priv; |
| 3541 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3542 | mddev->level = mddev->new_level; |
| 3543 | mddev->layout = mddev->new_layout; |
| 3544 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 3545 | mddev->delta_disks = 0; |
| 3546 | mddev->reshape_backwards = 0; |
| 3547 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3548 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3549 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3550 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3551 | mddev->external) { |
| 3552 | /* We are converting from a no-redundancy array |
| 3553 | * to a redundancy array and metadata is managed |
| 3554 | * externally so we need to be sure that writes |
| 3555 | * won't block due to a need to transition |
| 3556 | * clean->dirty |
| 3557 | * until external management is started. |
| 3558 | */ |
| 3559 | mddev->in_sync = 0; |
| 3560 | mddev->safemode_delay = 0; |
| 3561 | mddev->safemode = 0; |
| 3562 | } |
| 3563 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3564 | oldpers->free(mddev, oldpriv); |
| 3565 | |
| 3566 | if (oldpers->sync_request == NULL && |
| 3567 | pers->sync_request != NULL) { |
| 3568 | /* need to add the md_redundancy_group */ |
| 3569 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3570 | printk(KERN_WARNING |
| 3571 | "md: cannot register extra attributes for %s\n", |
| 3572 | mdname(mddev)); |
| 3573 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3574 | } |
| 3575 | if (oldpers->sync_request != NULL && |
| 3576 | pers->sync_request == NULL) { |
| 3577 | /* need to remove the md_redundancy_group */ |
| 3578 | if (mddev->to_remove == NULL) |
| 3579 | mddev->to_remove = &md_redundancy_group; |
| 3580 | } |
| 3581 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3582 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3583 | if (rdev->raid_disk < 0) |
| 3584 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3585 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3586 | rdev->new_raid_disk = -1; |
| 3587 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3588 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3589 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3590 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3591 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3592 | if (rdev->raid_disk < 0) |
| 3593 | continue; |
| 3594 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3595 | continue; |
| 3596 | rdev->raid_disk = rdev->new_raid_disk; |
| 3597 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3598 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3599 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3600 | if (sysfs_link_rdev(mddev, rdev)) |
| 3601 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3602 | " for %s after level change\n", |
| 3603 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3604 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3605 | } |
| 3606 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3607 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3608 | /* this is now an array without redundancy, so |
| 3609 | * it must always be in_sync |
| 3610 | */ |
| 3611 | mddev->in_sync = 1; |
| 3612 | del_timer_sync(&mddev->safemode_timer); |
| 3613 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 3614 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3615 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3616 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3617 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 3618 | if (!mddev->thread) |
| 3619 | md_update_sb(mddev, 1); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3620 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3621 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3622 | rv = len; |
| 3623 | out_unlock: |
| 3624 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3625 | return rv; |
| 3626 | } |
| 3627 | |
| 3628 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3629 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3630 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3631 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3632 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3633 | { |
| 3634 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3635 | if (mddev->reshape_position != MaxSector && |
| 3636 | mddev->layout != mddev->new_layout) |
| 3637 | return sprintf(page, "%d (%d)\n", |
| 3638 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3639 | return sprintf(page, "%d\n", mddev->layout); |
| 3640 | } |
| 3641 | |
| 3642 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3643 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3644 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3645 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3646 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3647 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3648 | err = kstrtouint(buf, 10, &n); |
| 3649 | if (err < 0) |
| 3650 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3651 | err = mddev_lock(mddev); |
| 3652 | if (err) |
| 3653 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3654 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3655 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3656 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3657 | err = -EBUSY; |
| 3658 | else if (mddev->ro) |
| 3659 | err = -EROFS; |
| 3660 | else { |
| 3661 | mddev->new_layout = n; |
| 3662 | err = mddev->pers->check_reshape(mddev); |
| 3663 | if (err) |
| 3664 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3665 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3666 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3667 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3668 | if (mddev->reshape_position == MaxSector) |
| 3669 | mddev->layout = n; |
| 3670 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3671 | mddev_unlock(mddev); |
| 3672 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3673 | } |
| 3674 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3675 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3676 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3677 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3678 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3679 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3680 | if (mddev->raid_disks == 0) |
| 3681 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3682 | if (mddev->reshape_position != MaxSector && |
| 3683 | mddev->delta_disks != 0) |
| 3684 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3685 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3686 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3687 | } |
| 3688 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3689 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3690 | |
| 3691 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3692 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3693 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3694 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3695 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3696 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3697 | err = kstrtouint(buf, 10, &n); |
| 3698 | if (err < 0) |
| 3699 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3700 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3701 | err = mddev_lock(mddev); |
| 3702 | if (err) |
| 3703 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3704 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3705 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3706 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3707 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3708 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3709 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3710 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3711 | rdev_for_each(rdev, mddev) { |
| 3712 | if (olddisks < n && |
| 3713 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3714 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3715 | if (olddisks > n && |
| 3716 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3717 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3718 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3719 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3720 | mddev->delta_disks = n - olddisks; |
| 3721 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3722 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3723 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3724 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3725 | out_unlock: |
| 3726 | mddev_unlock(mddev); |
| 3727 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3728 | } |
| 3729 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3730 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3731 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3732 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3733 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3734 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3735 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3736 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3737 | return sprintf(page, "%d (%d)\n", |
| 3738 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3739 | mddev->chunk_sectors << 9); |
| 3740 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3744 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3745 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3746 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3747 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3748 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3749 | err = kstrtoul(buf, 10, &n); |
| 3750 | if (err < 0) |
| 3751 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3752 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3753 | err = mddev_lock(mddev); |
| 3754 | if (err) |
| 3755 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3756 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3757 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3758 | err = -EBUSY; |
| 3759 | else if (mddev->ro) |
| 3760 | err = -EROFS; |
| 3761 | else { |
| 3762 | mddev->new_chunk_sectors = n >> 9; |
| 3763 | err = mddev->pers->check_reshape(mddev); |
| 3764 | if (err) |
| 3765 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3766 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3767 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3768 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3769 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3770 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3771 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3772 | mddev_unlock(mddev); |
| 3773 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3774 | } |
| 3775 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3776 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3777 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3778 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3779 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3780 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3781 | if (mddev->recovery_cp == MaxSector) |
| 3782 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3783 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3784 | } |
| 3785 | |
| 3786 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3787 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3788 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3789 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3790 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3791 | |
| 3792 | if (cmd_match(buf, "none")) |
| 3793 | n = MaxSector; |
| 3794 | else { |
| 3795 | err = kstrtoull(buf, 10, &n); |
| 3796 | if (err < 0) |
| 3797 | return err; |
| 3798 | if (n != (sector_t)n) |
| 3799 | return -EINVAL; |
| 3800 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3801 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3802 | err = mddev_lock(mddev); |
| 3803 | if (err) |
| 3804 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3805 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3806 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3807 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3808 | if (!err) { |
| 3809 | mddev->recovery_cp = n; |
| 3810 | if (mddev->pers) |
| 3811 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3812 | } |
| 3813 | mddev_unlock(mddev); |
| 3814 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3815 | } |
| 3816 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3817 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 3818 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3819 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3820 | /* |
| 3821 | * The array state can be: |
| 3822 | * |
| 3823 | * clear |
| 3824 | * No devices, no size, no level |
| 3825 | * Equivalent to STOP_ARRAY ioctl |
| 3826 | * inactive |
| 3827 | * May have some settings, but array is not active |
| 3828 | * all IO results in error |
| 3829 | * When written, doesn't tear down array, but just stops it |
| 3830 | * suspended (not supported yet) |
| 3831 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3832 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3833 | * readonly |
| 3834 | * no resync can happen. no superblocks get written. |
| 3835 | * write requests fail |
| 3836 | * read-auto |
| 3837 | * like readonly, but behaves like 'clean' on a write request. |
| 3838 | * |
| 3839 | * clean - no pending writes, but otherwise active. |
| 3840 | * When written to inactive array, starts without resync |
| 3841 | * If a write request arrives then |
| 3842 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3843 | * if not known, block and switch to write-pending |
| 3844 | * If written to an active array that has pending writes, then fails. |
| 3845 | * active |
| 3846 | * fully active: IO and resync can be happening. |
| 3847 | * When written to inactive array, starts with resync |
| 3848 | * |
| 3849 | * write-pending |
| 3850 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3851 | * |
| 3852 | * active-idle |
| 3853 | * like active, but no writes have been seen for a while (100msec). |
| 3854 | * |
| 3855 | */ |
| 3856 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3857 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3858 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3859 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3860 | "write-pending", "active-idle", NULL }; |
| 3861 | |
| 3862 | static int match_word(const char *word, char **list) |
| 3863 | { |
| 3864 | int n; |
| 3865 | for (n=0; list[n]; n++) |
| 3866 | if (cmd_match(word, list[n])) |
| 3867 | break; |
| 3868 | return n; |
| 3869 | } |
| 3870 | |
| 3871 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3872 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3873 | { |
| 3874 | enum array_state st = inactive; |
| 3875 | |
| 3876 | if (mddev->pers) |
| 3877 | switch(mddev->ro) { |
| 3878 | case 1: |
| 3879 | st = readonly; |
| 3880 | break; |
| 3881 | case 2: |
| 3882 | st = read_auto; |
| 3883 | break; |
| 3884 | case 0: |
| 3885 | if (mddev->in_sync) |
| 3886 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3887 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3888 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3889 | else if (mddev->safemode) |
| 3890 | st = active_idle; |
| 3891 | else |
| 3892 | st = active; |
| 3893 | } |
| 3894 | else { |
| 3895 | if (list_empty(&mddev->disks) && |
| 3896 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3897 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3898 | st = clear; |
| 3899 | else |
| 3900 | st = inactive; |
| 3901 | } |
| 3902 | return sprintf(page, "%s\n", array_states[st]); |
| 3903 | } |
| 3904 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3905 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 3906 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 3907 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3908 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3909 | |
| 3910 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3911 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3912 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3913 | int err; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3914 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3915 | |
| 3916 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 3917 | /* don't take reconfig_mutex when toggling between |
| 3918 | * clean and active |
| 3919 | */ |
| 3920 | spin_lock(&mddev->lock); |
| 3921 | if (st == active) { |
| 3922 | restart_array(mddev); |
| 3923 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 3924 | wake_up(&mddev->sb_wait); |
| 3925 | err = 0; |
| 3926 | } else /* st == clean */ { |
| 3927 | restart_array(mddev); |
| 3928 | if (atomic_read(&mddev->writes_pending) == 0) { |
| 3929 | if (mddev->in_sync == 0) { |
| 3930 | mddev->in_sync = 1; |
| 3931 | if (mddev->safemode == 1) |
| 3932 | mddev->safemode = 0; |
| 3933 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3934 | } |
| 3935 | err = 0; |
| 3936 | } else |
| 3937 | err = -EBUSY; |
| 3938 | } |
| 3939 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 3940 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3941 | } |
| 3942 | err = mddev_lock(mddev); |
| 3943 | if (err) |
| 3944 | return err; |
| 3945 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3946 | switch(st) { |
| 3947 | case bad_word: |
| 3948 | break; |
| 3949 | case clear: |
| 3950 | /* stopping an active array */ |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3951 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3952 | break; |
| 3953 | case inactive: |
| 3954 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3955 | if (mddev->pers) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3956 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3957 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3958 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3959 | break; |
| 3960 | case suspended: |
| 3961 | break; /* not supported yet */ |
| 3962 | case readonly: |
| 3963 | if (mddev->pers) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3964 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3965 | else { |
| 3966 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3967 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3968 | err = do_md_run(mddev); |
| 3969 | } |
| 3970 | break; |
| 3971 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3972 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3973 | if (mddev->ro == 0) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3974 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3975 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3976 | err = restart_array(mddev); |
| 3977 | if (err == 0) { |
| 3978 | mddev->ro = 2; |
| 3979 | set_disk_ro(mddev->gendisk, 0); |
| 3980 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3981 | } else { |
| 3982 | mddev->ro = 2; |
| 3983 | err = do_md_run(mddev); |
| 3984 | } |
| 3985 | break; |
| 3986 | case clean: |
| 3987 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 3988 | err = restart_array(mddev); |
| 3989 | if (err) |
| 3990 | break; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3991 | spin_lock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3992 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3993 | if (mddev->in_sync == 0) { |
| 3994 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3995 | if (mddev->safemode == 1) |
| 3996 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3997 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3998 | } |
| 3999 | err = 0; |
| 4000 | } else |
| 4001 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4002 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 4003 | } else |
| 4004 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4005 | break; |
| 4006 | case active: |
| 4007 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4008 | err = restart_array(mddev); |
| 4009 | if (err) |
| 4010 | break; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 4011 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4012 | wake_up(&mddev->sb_wait); |
| 4013 | err = 0; |
| 4014 | } else { |
| 4015 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4016 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4017 | err = do_md_run(mddev); |
| 4018 | } |
| 4019 | break; |
| 4020 | case write_pending: |
| 4021 | case active_idle: |
| 4022 | /* these cannot be set */ |
| 4023 | break; |
| 4024 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4025 | |
| 4026 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 4027 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4028 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4029 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4030 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4031 | mddev_unlock(mddev); |
| 4032 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4033 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4034 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4035 | __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] | 4036 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4037 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4038 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4039 | return sprintf(page, "%d\n", |
| 4040 | atomic_read(&mddev->max_corr_read_errors)); |
| 4041 | } |
| 4042 | |
| 4043 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4044 | 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] | 4045 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4046 | unsigned int n; |
| 4047 | int rv; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4048 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4049 | rv = kstrtouint(buf, 10, &n); |
| 4050 | if (rv < 0) |
| 4051 | return rv; |
| 4052 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4053 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | static struct md_sysfs_entry max_corr_read_errors = |
| 4057 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4058 | max_corrected_read_errors_store); |
| 4059 | |
| 4060 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4061 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4062 | { |
| 4063 | return -EINVAL; |
| 4064 | } |
| 4065 | |
| 4066 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4067 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4068 | { |
| 4069 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4070 | /* The new device is added to the array. |
| 4071 | * If the array has a persistent superblock, we read the |
| 4072 | * superblock to initialise info and check validity. |
| 4073 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4074 | * which mainly checks size. |
| 4075 | */ |
| 4076 | char *e; |
| 4077 | int major = simple_strtoul(buf, &e, 10); |
| 4078 | int minor; |
| 4079 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4080 | struct md_rdev *rdev; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4081 | int err; |
| 4082 | |
| 4083 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4084 | return -EINVAL; |
| 4085 | minor = simple_strtoul(e+1, &e, 10); |
| 4086 | if (*e && *e != '\n') |
| 4087 | return -EINVAL; |
| 4088 | dev = MKDEV(major, minor); |
| 4089 | if (major != MAJOR(dev) || |
| 4090 | minor != MINOR(dev)) |
| 4091 | return -EOVERFLOW; |
| 4092 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4093 | flush_workqueue(md_misc_wq); |
| 4094 | |
| 4095 | err = mddev_lock(mddev); |
| 4096 | if (err) |
| 4097 | return err; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4098 | if (mddev->persistent) { |
| 4099 | rdev = md_import_device(dev, mddev->major_version, |
| 4100 | mddev->minor_version); |
| 4101 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4102 | struct md_rdev *rdev0 |
| 4103 | = list_entry(mddev->disks.next, |
| 4104 | struct md_rdev, same_set); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4105 | err = super_types[mddev->major_version] |
| 4106 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4107 | if (err < 0) |
| 4108 | goto out; |
| 4109 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4110 | } else if (mddev->external) |
| 4111 | rdev = md_import_device(dev, -2, -1); |
| 4112 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4113 | rdev = md_import_device(dev, -1, -1); |
| 4114 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4115 | if (IS_ERR(rdev)) { |
| 4116 | mddev_unlock(mddev); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4117 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4118 | } |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4119 | err = bind_rdev_to_array(rdev, mddev); |
| 4120 | out: |
| 4121 | if (err) |
| 4122 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4123 | mddev_unlock(mddev); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4124 | return err ? err : len; |
| 4125 | } |
| 4126 | |
| 4127 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4128 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4129 | |
| 4130 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4131 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4132 | { |
| 4133 | char *end; |
| 4134 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4135 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4136 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4137 | err = mddev_lock(mddev); |
| 4138 | if (err) |
| 4139 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4140 | if (!mddev->bitmap) |
| 4141 | goto out; |
| 4142 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4143 | while (*buf) { |
| 4144 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4145 | if (buf == end) break; |
| 4146 | if (*end == '-') { /* range */ |
| 4147 | buf = end + 1; |
| 4148 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4149 | if (buf == end) break; |
| 4150 | } |
| 4151 | if (*end && !isspace(*end)) break; |
| 4152 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4153 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4154 | } |
| 4155 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4156 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4157 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4158 | return len; |
| 4159 | } |
| 4160 | |
| 4161 | static struct md_sysfs_entry md_bitmap = |
| 4162 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4163 | |
| 4164 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4165 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4166 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4167 | return sprintf(page, "%llu\n", |
| 4168 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4169 | } |
| 4170 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4171 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4172 | |
| 4173 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4174 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4175 | { |
| 4176 | /* If array is inactive, we can reduce the component size, but |
| 4177 | * not increase it (except from 0). |
| 4178 | * If array is active, we can try an on-line resize |
| 4179 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4180 | sector_t sectors; |
| 4181 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4182 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4183 | if (err < 0) |
| 4184 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4185 | err = mddev_lock(mddev); |
| 4186 | if (err) |
| 4187 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4188 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4189 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4190 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4191 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4192 | if (mddev->dev_sectors == 0 || |
| 4193 | mddev->dev_sectors > sectors) |
| 4194 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4195 | else |
| 4196 | err = -ENOSPC; |
| 4197 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4198 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4199 | return err ? err : len; |
| 4200 | } |
| 4201 | |
| 4202 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4203 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4204 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4205 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4206 | * This is one of |
| 4207 | * 'none' for arrays with no metadata (good luck...) |
| 4208 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4209 | * or N.M for internally known formats |
| 4210 | */ |
| 4211 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4212 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4213 | { |
| 4214 | if (mddev->persistent) |
| 4215 | return sprintf(page, "%d.%d\n", |
| 4216 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4217 | else if (mddev->external) |
| 4218 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4219 | else |
| 4220 | return sprintf(page, "none\n"); |
| 4221 | } |
| 4222 | |
| 4223 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4224 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4225 | { |
| 4226 | int major, minor; |
| 4227 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4228 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4229 | /* Changing the details of 'external' metadata is |
| 4230 | * always permitted. Otherwise there must be |
| 4231 | * no devices attached to the array. |
| 4232 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4233 | |
| 4234 | err = mddev_lock(mddev); |
| 4235 | if (err) |
| 4236 | return err; |
| 4237 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4238 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4239 | ; |
| 4240 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4241 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4242 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4243 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4244 | if (cmd_match(buf, "none")) { |
| 4245 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4246 | mddev->external = 0; |
| 4247 | mddev->major_version = 0; |
| 4248 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4249 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4250 | } |
| 4251 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4252 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4253 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4254 | namelen = sizeof(mddev->metadata_type)-1; |
| 4255 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4256 | mddev->metadata_type[namelen] = 0; |
| 4257 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4258 | mddev->metadata_type[--namelen] = 0; |
| 4259 | mddev->persistent = 0; |
| 4260 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4261 | mddev->major_version = 0; |
| 4262 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4263 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4264 | } |
| 4265 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4266 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4267 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4268 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4269 | buf = e+1; |
| 4270 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4271 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4272 | goto out_unlock; |
| 4273 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4274 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4275 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4276 | mddev->major_version = major; |
| 4277 | mddev->minor_version = minor; |
| 4278 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4279 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4280 | err = 0; |
| 4281 | out_unlock: |
| 4282 | mddev_unlock(mddev); |
| 4283 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4284 | } |
| 4285 | |
| 4286 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4287 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4288 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4289 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4290 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4291 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4292 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4293 | unsigned long recovery = mddev->recovery; |
| 4294 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4295 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4296 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4297 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4298 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4299 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4300 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4301 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4302 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4303 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4304 | type = "check"; |
| 4305 | else |
| 4306 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4307 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4308 | type = "recover"; |
NeilBrown | 985ca97 | 2015-07-06 12:26:57 +1000 | [diff] [blame] | 4309 | else if (mddev->reshape_position != MaxSector) |
| 4310 | type = "reshape"; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4311 | } |
| 4312 | return sprintf(page, "%s\n", type); |
| 4313 | } |
| 4314 | |
| 4315 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4316 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4317 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4318 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4319 | return -EINVAL; |
| 4320 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4321 | |
| 4322 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4323 | if (cmd_match(page, "frozen")) |
| 4324 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4325 | else |
| 4326 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4327 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4328 | mddev_lock(mddev) == 0) { |
| 4329 | flush_workqueue(md_misc_wq); |
| 4330 | if (mddev->sync_thread) { |
| 4331 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4332 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4333 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4334 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4335 | } |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4336 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4337 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4338 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4339 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4340 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4341 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4342 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4343 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4344 | int err; |
| 4345 | if (mddev->pers->start_reshape == NULL) |
| 4346 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4347 | err = mddev_lock(mddev); |
| 4348 | if (!err) { |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4349 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4350 | err = -EBUSY; |
| 4351 | else { |
| 4352 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4353 | err = mddev->pers->start_reshape(mddev); |
| 4354 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4355 | mddev_unlock(mddev); |
| 4356 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4357 | if (err) |
| 4358 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4359 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4360 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4361 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4362 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4363 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4364 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4365 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4366 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4367 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4368 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4369 | if (mddev->ro == 2) { |
| 4370 | /* A write to sync_action is enough to justify |
| 4371 | * canceling read-auto mode |
| 4372 | */ |
| 4373 | mddev->ro = 0; |
| 4374 | md_wakeup_thread(mddev->sync_thread); |
| 4375 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4376 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4377 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4378 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4379 | return len; |
| 4380 | } |
| 4381 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4382 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4383 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4384 | |
| 4385 | static ssize_t |
| 4386 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4387 | { |
| 4388 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4389 | } |
| 4390 | |
| 4391 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4392 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4393 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4394 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4395 | { |
| 4396 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4397 | (unsigned long long) |
| 4398 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4399 | } |
| 4400 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4401 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4402 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4403 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4404 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4405 | { |
| 4406 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4407 | mddev->sync_speed_min ? "local": "system"); |
| 4408 | } |
| 4409 | |
| 4410 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4411 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4412 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4413 | unsigned int min; |
| 4414 | int rv; |
| 4415 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4416 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4417 | min = 0; |
| 4418 | } else { |
| 4419 | rv = kstrtouint(buf, 10, &min); |
| 4420 | if (rv < 0) |
| 4421 | return rv; |
| 4422 | if (min == 0) |
| 4423 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4424 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4425 | mddev->sync_speed_min = min; |
| 4426 | return len; |
| 4427 | } |
| 4428 | |
| 4429 | static struct md_sysfs_entry md_sync_min = |
| 4430 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4431 | |
| 4432 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4433 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4434 | { |
| 4435 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4436 | mddev->sync_speed_max ? "local": "system"); |
| 4437 | } |
| 4438 | |
| 4439 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4440 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4441 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4442 | unsigned int max; |
| 4443 | int rv; |
| 4444 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4445 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4446 | max = 0; |
| 4447 | } else { |
| 4448 | rv = kstrtouint(buf, 10, &max); |
| 4449 | if (rv < 0) |
| 4450 | return rv; |
| 4451 | if (max == 0) |
| 4452 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4453 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4454 | mddev->sync_speed_max = max; |
| 4455 | return len; |
| 4456 | } |
| 4457 | |
| 4458 | static struct md_sysfs_entry md_sync_max = |
| 4459 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4460 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4461 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4462 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4463 | { |
| 4464 | return sprintf(page, "%d\n", mddev->degraded); |
| 4465 | } |
| 4466 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4467 | |
| 4468 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4469 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4470 | { |
| 4471 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4472 | } |
| 4473 | |
| 4474 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4475 | 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] | 4476 | { |
| 4477 | long n; |
| 4478 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4479 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4480 | return -EINVAL; |
| 4481 | |
| 4482 | if (n != 0 && n != 1) |
| 4483 | return -EINVAL; |
| 4484 | |
| 4485 | mddev->parallel_resync = n; |
| 4486 | |
| 4487 | if (mddev->sync_thread) |
| 4488 | wake_up(&resync_wait); |
| 4489 | |
| 4490 | return len; |
| 4491 | } |
| 4492 | |
| 4493 | /* force parallel resync, even with shared block devices */ |
| 4494 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4495 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4496 | sync_force_parallel_show, sync_force_parallel_store); |
| 4497 | |
| 4498 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4499 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4500 | { |
| 4501 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4502 | if (mddev->curr_resync == 0) |
| 4503 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4504 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4505 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4506 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4507 | db = resync - mddev->resync_mark_cnt; |
| 4508 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4509 | } |
| 4510 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4511 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4512 | |
| 4513 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4514 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4515 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4516 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4517 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4518 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4519 | return sprintf(page, "none\n"); |
| 4520 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4521 | if (mddev->curr_resync == 1 || |
| 4522 | mddev->curr_resync == 2) |
| 4523 | return sprintf(page, "delayed\n"); |
| 4524 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4525 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4526 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4527 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4528 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4529 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4530 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4531 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4532 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4533 | } |
| 4534 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4535 | static struct md_sysfs_entry md_sync_completed = |
| 4536 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4537 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4538 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4539 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4540 | { |
| 4541 | return sprintf(page, "%llu\n", |
| 4542 | (unsigned long long)mddev->resync_min); |
| 4543 | } |
| 4544 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4545 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4546 | { |
| 4547 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4548 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4549 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4550 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4551 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4552 | |
| 4553 | spin_lock(&mddev->lock); |
| 4554 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4555 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4556 | goto out_unlock; |
| 4557 | |
| 4558 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4559 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4560 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4561 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 4562 | /* Round down to multiple of 4K for safety */ |
| 4563 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4564 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4565 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4566 | out_unlock: |
| 4567 | spin_unlock(&mddev->lock); |
| 4568 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4569 | } |
| 4570 | |
| 4571 | static struct md_sysfs_entry md_min_sync = |
| 4572 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4573 | |
| 4574 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4575 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4576 | { |
| 4577 | if (mddev->resync_max == MaxSector) |
| 4578 | return sprintf(page, "max\n"); |
| 4579 | else |
| 4580 | return sprintf(page, "%llu\n", |
| 4581 | (unsigned long long)mddev->resync_max); |
| 4582 | } |
| 4583 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4584 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4585 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4586 | int err; |
| 4587 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4588 | if (strncmp(buf, "max", 3) == 0) |
| 4589 | mddev->resync_max = MaxSector; |
| 4590 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4591 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4592 | int chunk; |
| 4593 | |
| 4594 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4595 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4596 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4597 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4598 | goto out_unlock; |
| 4599 | |
| 4600 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4601 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4602 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4603 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4604 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4605 | |
| 4606 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4607 | chunk = mddev->chunk_sectors; |
| 4608 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4609 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4610 | |
| 4611 | err = -EINVAL; |
| 4612 | if (sector_div(temp, chunk)) |
| 4613 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4614 | } |
| 4615 | mddev->resync_max = max; |
| 4616 | } |
| 4617 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4618 | err = 0; |
| 4619 | out_unlock: |
| 4620 | spin_unlock(&mddev->lock); |
| 4621 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | static struct md_sysfs_entry md_max_sync = |
| 4625 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4626 | |
| 4627 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4628 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4629 | { |
| 4630 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4631 | } |
| 4632 | |
| 4633 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4634 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4635 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4636 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4637 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4638 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4639 | err = kstrtoull(buf, 10, &new); |
| 4640 | if (err < 0) |
| 4641 | return err; |
| 4642 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4643 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4644 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4645 | err = mddev_lock(mddev); |
| 4646 | if (err) |
| 4647 | return err; |
| 4648 | err = -EINVAL; |
| 4649 | if (mddev->pers == NULL || |
| 4650 | mddev->pers->quiesce == NULL) |
| 4651 | goto unlock; |
| 4652 | old = mddev->suspend_lo; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4653 | mddev->suspend_lo = new; |
| 4654 | if (new >= old) |
| 4655 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4656 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4657 | else { |
| 4658 | /* Expanding suspended region - need to wait */ |
| 4659 | mddev->pers->quiesce(mddev, 1); |
| 4660 | mddev->pers->quiesce(mddev, 0); |
| 4661 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4662 | err = 0; |
| 4663 | unlock: |
| 4664 | mddev_unlock(mddev); |
| 4665 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4666 | } |
| 4667 | static struct md_sysfs_entry md_suspend_lo = |
| 4668 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4669 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4670 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4671 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4672 | { |
| 4673 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4674 | } |
| 4675 | |
| 4676 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4677 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4678 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4679 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4680 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4681 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4682 | err = kstrtoull(buf, 10, &new); |
| 4683 | if (err < 0) |
| 4684 | return err; |
| 4685 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4686 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4687 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4688 | err = mddev_lock(mddev); |
| 4689 | if (err) |
| 4690 | return err; |
| 4691 | err = -EINVAL; |
| 4692 | if (mddev->pers == NULL || |
| 4693 | mddev->pers->quiesce == NULL) |
| 4694 | goto unlock; |
| 4695 | old = mddev->suspend_hi; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4696 | mddev->suspend_hi = new; |
| 4697 | if (new <= old) |
| 4698 | /* Shrinking suspended region */ |
| 4699 | mddev->pers->quiesce(mddev, 2); |
| 4700 | else { |
| 4701 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4702 | mddev->pers->quiesce(mddev, 1); |
| 4703 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4704 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4705 | err = 0; |
| 4706 | unlock: |
| 4707 | mddev_unlock(mddev); |
| 4708 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4709 | } |
| 4710 | static struct md_sysfs_entry md_suspend_hi = |
| 4711 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4712 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4713 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4714 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4715 | { |
| 4716 | if (mddev->reshape_position != MaxSector) |
| 4717 | return sprintf(page, "%llu\n", |
| 4718 | (unsigned long long)mddev->reshape_position); |
| 4719 | strcpy(page, "none\n"); |
| 4720 | return 5; |
| 4721 | } |
| 4722 | |
| 4723 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4724 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4725 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4726 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4727 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4728 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4729 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4730 | err = kstrtoull(buf, 10, &new); |
| 4731 | if (err < 0) |
| 4732 | return err; |
| 4733 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4734 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4735 | err = mddev_lock(mddev); |
| 4736 | if (err) |
| 4737 | return err; |
| 4738 | err = -EBUSY; |
| 4739 | if (mddev->pers) |
| 4740 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4741 | mddev->reshape_position = new; |
| 4742 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4743 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4744 | mddev->new_level = mddev->level; |
| 4745 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4746 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4747 | rdev_for_each(rdev, mddev) |
| 4748 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4749 | err = 0; |
| 4750 | unlock: |
| 4751 | mddev_unlock(mddev); |
| 4752 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4753 | } |
| 4754 | |
| 4755 | static struct md_sysfs_entry md_reshape_position = |
| 4756 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4757 | reshape_position_store); |
| 4758 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4759 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4760 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4761 | { |
| 4762 | return sprintf(page, "%s\n", |
| 4763 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4764 | } |
| 4765 | |
| 4766 | static ssize_t |
| 4767 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4768 | { |
| 4769 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4770 | int err; |
| 4771 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4772 | if (cmd_match(buf, "forwards")) |
| 4773 | backwards = 0; |
| 4774 | else if (cmd_match(buf, "backwards")) |
| 4775 | backwards = 1; |
| 4776 | else |
| 4777 | return -EINVAL; |
| 4778 | if (mddev->reshape_backwards == backwards) |
| 4779 | return len; |
| 4780 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4781 | err = mddev_lock(mddev); |
| 4782 | if (err) |
| 4783 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4784 | /* check if we are allowed to change */ |
| 4785 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4786 | err = -EBUSY; |
| 4787 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4788 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4789 | err = -EINVAL; |
| 4790 | else |
| 4791 | mddev->reshape_backwards = backwards; |
| 4792 | mddev_unlock(mddev); |
| 4793 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4794 | } |
| 4795 | |
| 4796 | static struct md_sysfs_entry md_reshape_direction = |
| 4797 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4798 | reshape_direction_store); |
| 4799 | |
| 4800 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4801 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4802 | { |
| 4803 | if (mddev->external_size) |
| 4804 | return sprintf(page, "%llu\n", |
| 4805 | (unsigned long long)mddev->array_sectors/2); |
| 4806 | else |
| 4807 | return sprintf(page, "default\n"); |
| 4808 | } |
| 4809 | |
| 4810 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4811 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4812 | { |
| 4813 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4814 | int err; |
| 4815 | |
| 4816 | err = mddev_lock(mddev); |
| 4817 | if (err) |
| 4818 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4819 | |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 4820 | /* cluster raid doesn't support change array_sectors */ |
| 4821 | if (mddev_is_clustered(mddev)) |
| 4822 | return -EINVAL; |
| 4823 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4824 | if (strncmp(buf, "default", 7) == 0) { |
| 4825 | if (mddev->pers) |
| 4826 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4827 | else |
| 4828 | sectors = mddev->array_sectors; |
| 4829 | |
| 4830 | mddev->external_size = 0; |
| 4831 | } else { |
| 4832 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4833 | err = -EINVAL; |
| 4834 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 4835 | err = -E2BIG; |
| 4836 | else |
| 4837 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4838 | } |
| 4839 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4840 | if (!err) { |
| 4841 | mddev->array_sectors = sectors; |
| 4842 | if (mddev->pers) { |
| 4843 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4844 | revalidate_disk(mddev->gendisk); |
| 4845 | } |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4846 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4847 | mddev_unlock(mddev); |
| 4848 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4849 | } |
| 4850 | |
| 4851 | static struct md_sysfs_entry md_array_size = |
| 4852 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4853 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4854 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4855 | static struct attribute *md_default_attrs[] = { |
| 4856 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4857 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4858 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4859 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4860 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4861 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4862 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4863 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4864 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4865 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4866 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4867 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4868 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4869 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4870 | NULL, |
| 4871 | }; |
| 4872 | |
| 4873 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4874 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4875 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4876 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4877 | &md_sync_min.attr, |
| 4878 | &md_sync_max.attr, |
| 4879 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4880 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4881 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4882 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4883 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4884 | &md_suspend_lo.attr, |
| 4885 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4886 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4887 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4888 | NULL, |
| 4889 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4890 | static struct attribute_group md_redundancy_group = { |
| 4891 | .name = NULL, |
| 4892 | .attrs = md_redundancy_attrs, |
| 4893 | }; |
| 4894 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4895 | static ssize_t |
| 4896 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4897 | { |
| 4898 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4899 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4900 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4901 | |
| 4902 | if (!entry->show) |
| 4903 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4904 | spin_lock(&all_mddevs_lock); |
| 4905 | if (list_empty(&mddev->all_mddevs)) { |
| 4906 | spin_unlock(&all_mddevs_lock); |
| 4907 | return -EBUSY; |
| 4908 | } |
| 4909 | mddev_get(mddev); |
| 4910 | spin_unlock(&all_mddevs_lock); |
| 4911 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4912 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4913 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4914 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | static ssize_t |
| 4918 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4919 | const char *page, size_t length) |
| 4920 | { |
| 4921 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4922 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4923 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4924 | |
| 4925 | if (!entry->store) |
| 4926 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4927 | if (!capable(CAP_SYS_ADMIN)) |
| 4928 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4929 | spin_lock(&all_mddevs_lock); |
| 4930 | if (list_empty(&mddev->all_mddevs)) { |
| 4931 | spin_unlock(&all_mddevs_lock); |
| 4932 | return -EBUSY; |
| 4933 | } |
| 4934 | mddev_get(mddev); |
| 4935 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4936 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4937 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4938 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4939 | } |
| 4940 | |
| 4941 | static void md_free(struct kobject *ko) |
| 4942 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4943 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4944 | |
| 4945 | if (mddev->sysfs_state) |
| 4946 | sysfs_put(mddev->sysfs_state); |
| 4947 | |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 4948 | if (mddev->queue) |
| 4949 | blk_cleanup_queue(mddev->queue); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4950 | if (mddev->gendisk) { |
| 4951 | del_gendisk(mddev->gendisk); |
| 4952 | put_disk(mddev->gendisk); |
| 4953 | } |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4954 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4955 | kfree(mddev); |
| 4956 | } |
| 4957 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4958 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4959 | .show = md_attr_show, |
| 4960 | .store = md_attr_store, |
| 4961 | }; |
| 4962 | static struct kobj_type md_ktype = { |
| 4963 | .release = md_free, |
| 4964 | .sysfs_ops = &md_sysfs_ops, |
| 4965 | .default_attrs = md_default_attrs, |
| 4966 | }; |
| 4967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4968 | int mdp_major = 0; |
| 4969 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4970 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4971 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4972 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4973 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4974 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4975 | kobject_del(&mddev->kobj); |
| 4976 | kobject_put(&mddev->kobj); |
| 4977 | } |
| 4978 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4979 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4981 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4982 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4983 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4984 | int partitioned; |
| 4985 | int shift; |
| 4986 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4987 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4988 | |
| 4989 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4990 | return -ENODEV; |
| 4991 | |
| 4992 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4993 | shift = partitioned ? MdpMinorShift : 0; |
| 4994 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4996 | /* wait for any previous instance of this device to be |
| 4997 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4998 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4999 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5000 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5001 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5002 | error = -EEXIST; |
| 5003 | if (mddev->gendisk) |
| 5004 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5005 | |
| 5006 | if (name) { |
| 5007 | /* Need to ensure that 'name' is not a duplicate. |
| 5008 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5009 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5010 | spin_lock(&all_mddevs_lock); |
| 5011 | |
| 5012 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 5013 | if (mddev2->gendisk && |
| 5014 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 5015 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5016 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5017 | } |
| 5018 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5020 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5021 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5022 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5023 | if (!mddev->queue) |
| 5024 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5025 | mddev->queue->queuedata = mddev; |
| 5026 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5027 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 5028 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5029 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | disk = alloc_disk(1 << shift); |
| 5031 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5032 | blk_cleanup_queue(mddev->queue); |
| 5033 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5034 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5035 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5036 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5038 | if (name) |
| 5039 | strcpy(disk->disk_name, name); |
| 5040 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5042 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5043 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5044 | disk->fops = &md_fops; |
| 5045 | disk->private_data = mddev; |
| 5046 | disk->queue = mddev->queue; |
Jens Axboe | 56883a7 | 2016-03-30 10:16:53 -0600 | [diff] [blame] | 5047 | blk_queue_write_cache(mddev->queue, true, true); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5048 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5049 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5050 | * remove it now. |
| 5051 | */ |
| 5052 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5053 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5054 | /* As soon as we call add_disk(), another thread could get |
| 5055 | * through to md_open, so make sure it doesn't get too far |
| 5056 | */ |
| 5057 | mutex_lock(&mddev->open_mutex); |
| 5058 | add_disk(disk); |
| 5059 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 5060 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 5061 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5062 | if (error) { |
| 5063 | /* This isn't possible, but as kobject_init_and_add is marked |
| 5064 | * __must_check, we must do something with the result |
| 5065 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5066 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 5067 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5068 | error = 0; |
| 5069 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5070 | if (mddev->kobj.sd && |
| 5071 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5072 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5073 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5074 | abort: |
| 5075 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5076 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5077 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5078 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5079 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5080 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5081 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5082 | } |
| 5083 | |
| 5084 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 5085 | { |
| 5086 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5087 | return NULL; |
| 5088 | } |
| 5089 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5090 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 5091 | { |
| 5092 | /* val must be "md_*" where * is not all digits. |
| 5093 | * We allocate an array with a large free minor number, and |
| 5094 | * set the name to val. val must not already be an active name. |
| 5095 | */ |
| 5096 | int len = strlen(val); |
| 5097 | char buf[DISK_NAME_LEN]; |
| 5098 | |
| 5099 | while (len && val[len-1] == '\n') |
| 5100 | len--; |
| 5101 | if (len >= DISK_NAME_LEN) |
| 5102 | return -E2BIG; |
| 5103 | strlcpy(buf, val, len+1); |
| 5104 | if (strncmp(buf, "md_", 3) != 0) |
| 5105 | return -EINVAL; |
| 5106 | return md_alloc(0, buf); |
| 5107 | } |
| 5108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5109 | static void md_safemode_timeout(unsigned long data) |
| 5110 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5111 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5112 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5113 | if (!atomic_read(&mddev->writes_pending)) { |
| 5114 | mddev->safemode = 1; |
| 5115 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5116 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5117 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5118 | md_wakeup_thread(mddev->thread); |
| 5119 | } |
| 5120 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5121 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5122 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5123 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5124 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5125 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5126 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5127 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5129 | if (list_empty(&mddev->disks)) |
| 5130 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | |
| 5133 | if (mddev->pers) |
| 5134 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5135 | /* Cannot run until previous stop completes properly */ |
| 5136 | if (mddev->sysfs_active) |
| 5137 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5139 | /* |
| 5140 | * Analyze all RAID superblock(s) |
| 5141 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5142 | if (!mddev->raid_disks) { |
| 5143 | if (!mddev->persistent) |
| 5144 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5145 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5148 | if (mddev->level != LEVEL_NONE) |
| 5149 | request_module("md-level-%d", mddev->level); |
| 5150 | else if (mddev->clevel[0]) |
| 5151 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | |
| 5153 | /* |
| 5154 | * Drop all container device buffers, from now on |
| 5155 | * the only valid external interface is through the md |
| 5156 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5158 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5159 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5160 | continue; |
| 5161 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5162 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5163 | |
| 5164 | /* perform some consistency tests on the device. |
| 5165 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5166 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5167 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5168 | if (rdev->meta_bdev) { |
| 5169 | /* Nothing to check */; |
| 5170 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5171 | if (mddev->dev_sectors && |
| 5172 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5173 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5174 | printk("md: %s: data overlaps metadata\n", |
| 5175 | mdname(mddev)); |
| 5176 | return -EINVAL; |
| 5177 | } |
| 5178 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5179 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5180 | > rdev->data_offset) { |
| 5181 | printk("md: %s: metadata overlaps data\n", |
| 5182 | mdname(mddev)); |
| 5183 | return -EINVAL; |
| 5184 | } |
| 5185 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5186 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | } |
| 5188 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5189 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 5190 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5193 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5194 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5196 | if (mddev->level != LEVEL_NONE) |
| 5197 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5198 | mddev->level); |
| 5199 | else |
| 5200 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5201 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5202 | return -EINVAL; |
| 5203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5205 | if (mddev->level != pers->level) { |
| 5206 | mddev->level = pers->level; |
| 5207 | mddev->new_level = pers->level; |
| 5208 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5209 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5210 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5211 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5212 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5213 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5214 | module_put(pers->owner); |
| 5215 | return -EINVAL; |
| 5216 | } |
| 5217 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5218 | if (pers->sync_request) { |
| 5219 | /* Warn if this is a potentially silly |
| 5220 | * configuration. |
| 5221 | */ |
| 5222 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5223 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5224 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5225 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5226 | rdev_for_each(rdev, mddev) |
| 5227 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5228 | if (rdev < rdev2 && |
| 5229 | rdev->bdev->bd_contains == |
| 5230 | rdev2->bdev->bd_contains) { |
| 5231 | printk(KERN_WARNING |
| 5232 | "%s: WARNING: %s appears to be" |
| 5233 | " on the same physical disk as" |
| 5234 | " %s.\n", |
| 5235 | mdname(mddev), |
| 5236 | bdevname(rdev->bdev,b), |
| 5237 | bdevname(rdev2->bdev,b2)); |
| 5238 | warned = 1; |
| 5239 | } |
| 5240 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5241 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5242 | if (warned) |
| 5243 | printk(KERN_WARNING |
| 5244 | "True protection against single-disk" |
| 5245 | " failure might be compromised.\n"); |
| 5246 | } |
| 5247 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5248 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5249 | /* may be over-ridden by personality */ |
| 5250 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5251 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5252 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5254 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5255 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5256 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5257 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5258 | if (err) |
| 5259 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5260 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5261 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5262 | " but 'external_size' not in effect?\n", __func__); |
| 5263 | printk(KERN_ERR |
| 5264 | "md: invalid array_size %llu > default size %llu\n", |
| 5265 | (unsigned long long)mddev->array_sectors / 2, |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5266 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5267 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5268 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5269 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5270 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5271 | struct bitmap *bitmap; |
| 5272 | |
| 5273 | bitmap = bitmap_create(mddev, -1); |
| 5274 | if (IS_ERR(bitmap)) { |
| 5275 | err = PTR_ERR(bitmap); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5276 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5277 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5278 | } else |
| 5279 | mddev->bitmap = bitmap; |
| 5280 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | if (err) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5283 | mddev_detach(mddev); |
NeilBrown | 0c35bd4 | 2015-03-13 11:51:18 +1100 | [diff] [blame] | 5284 | if (mddev->private) |
| 5285 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5286 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5287 | module_put(pers->owner); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5288 | bitmap_destroy(mddev); |
| 5289 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5291 | if (mddev->queue) { |
| 5292 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5293 | mddev->queue->backing_dev_info.congested_fn = md_congested; |
| 5294 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5295 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5296 | if (mddev->kobj.sd && |
| 5297 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5298 | printk(KERN_WARNING |
| 5299 | "md: cannot register extra attributes for %s\n", |
| 5300 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5301 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5302 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5303 | mddev->ro = 0; |
| 5304 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5305 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5306 | atomic_set(&mddev->max_corr_read_errors, |
| 5307 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | mddev->safemode = 0; |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5309 | if (mddev_is_clustered(mddev)) |
| 5310 | mddev->safemode_delay = 0; |
| 5311 | else |
| 5312 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5314 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5315 | spin_lock(&mddev->lock); |
| 5316 | mddev->pers = pers; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5317 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5318 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5319 | if (rdev->raid_disk >= 0) |
| 5320 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5321 | /* failure here is OK */; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5322 | |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 5323 | if (mddev->degraded && !mddev->ro) |
| 5324 | /* This ensures that recovering status is reported immediately |
| 5325 | * via sysfs - until a lack of spares is confirmed. |
| 5326 | */ |
| 5327 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5329 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5330 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5331 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5332 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5333 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5334 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5335 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5336 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | return 0; |
| 5338 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5339 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5340 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5341 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5342 | { |
| 5343 | int err; |
| 5344 | |
| 5345 | err = md_run(mddev); |
| 5346 | if (err) |
| 5347 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5348 | err = bitmap_load(mddev); |
| 5349 | if (err) { |
| 5350 | bitmap_destroy(mddev); |
| 5351 | goto out; |
| 5352 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5353 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5354 | if (mddev_is_clustered(mddev)) |
| 5355 | md_allow_write(mddev); |
| 5356 | |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5357 | md_wakeup_thread(mddev->thread); |
| 5358 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5359 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5360 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5361 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5362 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5363 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5364 | out: |
| 5365 | return err; |
| 5366 | } |
| 5367 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5368 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | { |
| 5370 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5372 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5374 | return -ENXIO; |
| 5375 | if (!mddev->pers) |
| 5376 | return -EINVAL; |
| 5377 | if (!mddev->ro) |
| 5378 | return -EBUSY; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 5379 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 5380 | struct md_rdev *rdev; |
| 5381 | bool has_journal = false; |
| 5382 | |
| 5383 | rcu_read_lock(); |
| 5384 | rdev_for_each_rcu(rdev, mddev) { |
| 5385 | if (test_bit(Journal, &rdev->flags) && |
| 5386 | !test_bit(Faulty, &rdev->flags)) { |
| 5387 | has_journal = true; |
| 5388 | break; |
| 5389 | } |
| 5390 | } |
| 5391 | rcu_read_unlock(); |
| 5392 | |
| 5393 | /* Don't restart rw with journal missing/faulty */ |
| 5394 | if (!has_journal) |
| 5395 | return -EINVAL; |
| 5396 | } |
| 5397 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5398 | mddev->safemode = 0; |
| 5399 | mddev->ro = 0; |
| 5400 | set_disk_ro(disk, 0); |
| 5401 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5402 | mdname(mddev)); |
| 5403 | /* Kick recovery or resync if necessary */ |
| 5404 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5405 | md_wakeup_thread(mddev->thread); |
| 5406 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5407 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5408 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | } |
| 5410 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5411 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5412 | { |
| 5413 | mddev->array_sectors = 0; |
| 5414 | mddev->external_size = 0; |
| 5415 | mddev->dev_sectors = 0; |
| 5416 | mddev->raid_disks = 0; |
| 5417 | mddev->recovery_cp = 0; |
| 5418 | mddev->resync_min = 0; |
| 5419 | mddev->resync_max = MaxSector; |
| 5420 | mddev->reshape_position = MaxSector; |
| 5421 | mddev->external = 0; |
| 5422 | mddev->persistent = 0; |
| 5423 | mddev->level = LEVEL_NONE; |
| 5424 | mddev->clevel[0] = 0; |
| 5425 | mddev->flags = 0; |
| 5426 | mddev->ro = 0; |
| 5427 | mddev->metadata_type[0] = 0; |
| 5428 | mddev->chunk_sectors = 0; |
| 5429 | mddev->ctime = mddev->utime = 0; |
| 5430 | mddev->layout = 0; |
| 5431 | mddev->max_disks = 0; |
| 5432 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5433 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5434 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5435 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5436 | mddev->new_level = LEVEL_NONE; |
| 5437 | mddev->new_layout = 0; |
| 5438 | mddev->new_chunk_sectors = 0; |
| 5439 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5440 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5441 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5442 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5443 | mddev->recovery = 0; |
| 5444 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5445 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5446 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5447 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5448 | mddev->private = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5449 | mddev->bitmap_info.offset = 0; |
| 5450 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5451 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5452 | mddev->bitmap_info.chunksize = 0; |
| 5453 | mddev->bitmap_info.daemon_sleep = 0; |
| 5454 | mddev->bitmap_info.max_write_behind = 0; |
| 5455 | } |
| 5456 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5457 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5458 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 5459 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5460 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5461 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5462 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5463 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5464 | } |
| 5465 | |
| 5466 | del_timer_sync(&mddev->safemode_timer); |
| 5467 | |
| 5468 | bitmap_flush(mddev); |
| 5469 | md_super_wait(mddev); |
| 5470 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5471 | if (mddev->ro == 0 && |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5472 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
| 5473 | (mddev->flags & MD_UPDATE_SB_FLAGS))) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5474 | /* mark array as shutdown cleanly */ |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5475 | if (!mddev_is_clustered(mddev)) |
| 5476 | mddev->in_sync = 1; |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5477 | md_update_sb(mddev, 1); |
| 5478 | } |
| 5479 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5480 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5481 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5482 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 5483 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5484 | __md_stop_writes(mddev); |
| 5485 | mddev_unlock(mddev); |
| 5486 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5487 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5488 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5489 | static void mddev_detach(struct mddev *mddev) |
| 5490 | { |
| 5491 | struct bitmap *bitmap = mddev->bitmap; |
| 5492 | /* wait for behind writes to complete */ |
| 5493 | if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { |
| 5494 | printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n", |
| 5495 | mdname(mddev)); |
| 5496 | /* need to kick something here to make sure I/O goes? */ |
| 5497 | wait_event(bitmap->behind_wait, |
| 5498 | atomic_read(&bitmap->behind_writes) == 0); |
| 5499 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5500 | if (mddev->pers && mddev->pers->quiesce) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5501 | mddev->pers->quiesce(mddev, 1); |
| 5502 | mddev->pers->quiesce(mddev, 0); |
| 5503 | } |
| 5504 | md_unregister_thread(&mddev->thread); |
| 5505 | if (mddev->queue) |
| 5506 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 5507 | } |
| 5508 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5509 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5510 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5511 | struct md_personality *pers = mddev->pers; |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5512 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 5513 | /* Ensure ->event_work is done */ |
| 5514 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5515 | spin_lock(&mddev->lock); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5516 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5517 | spin_unlock(&mddev->lock); |
| 5518 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5519 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5520 | if (pers->sync_request && mddev->to_remove == NULL) |
| 5521 | mddev->to_remove = &md_redundancy_group; |
| 5522 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5523 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5524 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5525 | |
| 5526 | void md_stop(struct mddev *mddev) |
| 5527 | { |
| 5528 | /* stop the array and free an attached data structures. |
| 5529 | * This is called from dm-raid |
| 5530 | */ |
| 5531 | __md_stop(mddev); |
| 5532 | bitmap_destroy(mddev); |
| 5533 | if (mddev->bio_set) |
| 5534 | bioset_free(mddev->bio_set); |
| 5535 | } |
| 5536 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5537 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5538 | |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5539 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5540 | { |
| 5541 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5542 | int did_freeze = 0; |
| 5543 | |
| 5544 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5545 | did_freeze = 1; |
| 5546 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5547 | md_wakeup_thread(mddev->thread); |
| 5548 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5549 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5550 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5551 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5552 | /* Thread might be blocked waiting for metadata update |
| 5553 | * which will now never happen */ |
| 5554 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5555 | |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5556 | if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
| 5557 | return -EBUSY; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5558 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5559 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 5560 | &mddev->recovery)); |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5561 | wait_event(mddev->sb_wait, |
| 5562 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5563 | mddev_lock_nointr(mddev); |
| 5564 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5565 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5566 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5567 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5568 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5569 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5570 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5571 | if (did_freeze) { |
| 5572 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5573 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5574 | md_wakeup_thread(mddev->thread); |
| 5575 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5576 | err = -EBUSY; |
| 5577 | goto out; |
| 5578 | } |
| 5579 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5580 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5581 | |
| 5582 | err = -ENXIO; |
| 5583 | if (mddev->ro==1) |
| 5584 | goto out; |
| 5585 | mddev->ro = 1; |
| 5586 | set_disk_ro(mddev->gendisk, 1); |
| 5587 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5588 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5589 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5590 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5591 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5592 | } |
| 5593 | out: |
| 5594 | mutex_unlock(&mddev->open_mutex); |
| 5595 | return err; |
| 5596 | } |
| 5597 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5598 | /* mode: |
| 5599 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5600 | * 2 - stop but do not disassemble array |
| 5601 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5602 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5603 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5606 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5607 | int did_freeze = 0; |
| 5608 | |
| 5609 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5610 | did_freeze = 1; |
| 5611 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5612 | md_wakeup_thread(mddev->thread); |
| 5613 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5614 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5615 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5616 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5617 | /* Thread might be blocked waiting for metadata update |
| 5618 | * which will now never happen */ |
| 5619 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5620 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5621 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5622 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 5623 | !test_bit(MD_RECOVERY_RUNNING, |
| 5624 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5625 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5626 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5627 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5628 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5629 | mddev->sysfs_active || |
| 5630 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5631 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5632 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5633 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5634 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5635 | if (did_freeze) { |
| 5636 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5637 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5638 | md_wakeup_thread(mddev->thread); |
| 5639 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 5640 | return -EBUSY; |
| 5641 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5642 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5643 | if (mddev->ro) |
| 5644 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5645 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5646 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5647 | __md_stop(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5648 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5649 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5650 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5651 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5652 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5653 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5654 | if (rdev->raid_disk >= 0) |
| 5655 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5656 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5657 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5658 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5659 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5660 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5661 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5662 | if (mddev->ro) |
| 5663 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5664 | } else |
| 5665 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | /* |
| 5667 | * Free resources if final stop |
| 5668 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5669 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5671 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5672 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5673 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5674 | struct file *f = mddev->bitmap_info.file; |
| 5675 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5676 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5677 | spin_unlock(&mddev->lock); |
| 5678 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5679 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5680 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | export_array(mddev); |
| 5683 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5684 | md_clean(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5685 | if (mddev->hold_active == UNTIL_STOP) |
| 5686 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5687 | } |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5688 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5689 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5690 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | } |
| 5692 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5693 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5694 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5696 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | int err; |
| 5698 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5699 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5700 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | |
| 5702 | printk(KERN_INFO "md: running: "); |
| 5703 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5704 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | char b[BDEVNAME_SIZE]; |
| 5706 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5707 | } |
| 5708 | printk("\n"); |
| 5709 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5710 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5711 | if (err) { |
| 5712 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5713 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5714 | } |
| 5715 | } |
| 5716 | |
| 5717 | /* |
| 5718 | * lets try to run arrays based on all disks that have arrived |
| 5719 | * until now. (those are in pending_raid_disks) |
| 5720 | * |
| 5721 | * the method: pick the first pending disk, collect all disks with |
| 5722 | * the same UUID, remove all from the pending list and put them into |
| 5723 | * the 'same_array' list. Then order this list based on superblock |
| 5724 | * update time (freshest comes first), kick out 'old' disks and |
| 5725 | * compare superblocks. If everything's fine then run it. |
| 5726 | * |
| 5727 | * If "unit" is allocated, then bump its reference count |
| 5728 | */ |
| 5729 | static void autorun_devices(int part) |
| 5730 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5731 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5732 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5733 | char b[BDEVNAME_SIZE]; |
| 5734 | |
| 5735 | printk(KERN_INFO "md: autorun ...\n"); |
| 5736 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5737 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5739 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5741 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5742 | |
| 5743 | printk(KERN_INFO "md: considering %s ...\n", |
| 5744 | bdevname(rdev0->bdev,b)); |
| 5745 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5746 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5748 | printk(KERN_INFO "md: adding %s ...\n", |
| 5749 | bdevname(rdev->bdev,b)); |
| 5750 | list_move(&rdev->same_set, &candidates); |
| 5751 | } |
| 5752 | /* |
| 5753 | * now we have a set of devices, with all of them having |
| 5754 | * mostly sane superblocks. It's time to allocate the |
| 5755 | * mddev. |
| 5756 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5757 | if (part) { |
| 5758 | dev = MKDEV(mdp_major, |
| 5759 | rdev0->preferred_minor << MdpMinorShift); |
| 5760 | unit = MINOR(dev) >> MdpMinorShift; |
| 5761 | } else { |
| 5762 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5763 | unit = MINOR(dev); |
| 5764 | } |
| 5765 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5767 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5768 | break; |
| 5769 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5770 | |
| 5771 | md_probe(dev, NULL, NULL); |
| 5772 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5773 | if (!mddev || !mddev->gendisk) { |
| 5774 | if (mddev) |
| 5775 | mddev_put(mddev); |
| 5776 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5777 | "md: cannot allocate memory for md drive.\n"); |
| 5778 | break; |
| 5779 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5780 | if (mddev_lock(mddev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5782 | mdname(mddev)); |
| 5783 | else if (mddev->raid_disks || mddev->major_version |
| 5784 | || !list_empty(&mddev->disks)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5785 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5786 | "md: %s already running, cannot run %s\n", |
| 5787 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5788 | mddev_unlock(mddev); |
| 5789 | } else { |
| 5790 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5791 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5792 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5793 | list_del_init(&rdev->same_set); |
| 5794 | if (bind_rdev_to_array(rdev, mddev)) |
| 5795 | export_rdev(rdev); |
| 5796 | } |
| 5797 | autorun_array(mddev); |
| 5798 | mddev_unlock(mddev); |
| 5799 | } |
| 5800 | /* on success, candidates will be empty, on error |
| 5801 | * it won't... |
| 5802 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5803 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5804 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5806 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | mddev_put(mddev); |
| 5808 | } |
| 5809 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5810 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5811 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5812 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5813 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5814 | { |
| 5815 | mdu_version_t ver; |
| 5816 | |
| 5817 | ver.major = MD_MAJOR_VERSION; |
| 5818 | ver.minor = MD_MINOR_VERSION; |
| 5819 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5820 | |
| 5821 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5822 | return -EFAULT; |
| 5823 | |
| 5824 | return 0; |
| 5825 | } |
| 5826 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5827 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | { |
| 5829 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5830 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5831 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5832 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5833 | nr = working = insync = failed = spare = 0; |
| 5834 | rcu_read_lock(); |
| 5835 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5836 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5837 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5838 | failed++; |
| 5839 | else { |
| 5840 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5841 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5842 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5843 | else |
| 5844 | spare++; |
| 5845 | } |
| 5846 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5847 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5848 | |
| 5849 | info.major_version = mddev->major_version; |
| 5850 | info.minor_version = mddev->minor_version; |
| 5851 | info.patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 5852 | info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5853 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5854 | info.size = mddev->dev_sectors / 2; |
| 5855 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5856 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | info.nr_disks = nr; |
| 5858 | info.raid_disks = mddev->raid_disks; |
| 5859 | info.md_minor = mddev->md_minor; |
| 5860 | info.not_persistent= !mddev->persistent; |
| 5861 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 5862 | info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | info.state = 0; |
| 5864 | if (mddev->in_sync) |
| 5865 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5866 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 5867 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 5868 | if (mddev_is_clustered(mddev)) |
| 5869 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5870 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5871 | info.working_disks = working; |
| 5872 | info.failed_disks = failed; |
| 5873 | info.spare_disks = spare; |
| 5874 | |
| 5875 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5876 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | |
| 5878 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5879 | return -EFAULT; |
| 5880 | |
| 5881 | return 0; |
| 5882 | } |
| 5883 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5884 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5885 | { |
| 5886 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5887 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5888 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5889 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5890 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5891 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5892 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5893 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5894 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5895 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5896 | /* bitmap enabled */ |
| 5897 | if (mddev->bitmap_info.file) { |
| 5898 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 5899 | sizeof(file->pathname)); |
| 5900 | if (IS_ERR(ptr)) |
| 5901 | err = PTR_ERR(ptr); |
| 5902 | else |
| 5903 | memmove(file->pathname, ptr, |
| 5904 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 5905 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5906 | spin_unlock(&mddev->lock); |
| 5907 | |
| 5908 | if (err == 0 && |
| 5909 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5910 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5911 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5912 | kfree(file); |
| 5913 | return err; |
| 5914 | } |
| 5915 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5916 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | { |
| 5918 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5919 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5920 | |
| 5921 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5922 | return -EFAULT; |
| 5923 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5924 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 5925 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5926 | if (rdev) { |
| 5927 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5928 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5929 | info.raid_disk = rdev->raid_disk; |
| 5930 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5931 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5933 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5935 | info.state |= (1<<MD_DISK_SYNC); |
| 5936 | } |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 5937 | if (test_bit(Journal, &rdev->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 5938 | info.state |= (1<<MD_DISK_JOURNAL); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5939 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5940 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5941 | } else { |
| 5942 | info.major = info.minor = 0; |
| 5943 | info.raid_disk = -1; |
| 5944 | info.state = (1<<MD_DISK_REMOVED); |
| 5945 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5946 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5947 | |
| 5948 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5949 | return -EFAULT; |
| 5950 | |
| 5951 | return 0; |
| 5952 | } |
| 5953 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5954 | 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] | 5955 | { |
| 5956 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5957 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5958 | dev_t dev = MKDEV(info->major,info->minor); |
| 5959 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5960 | if (mddev_is_clustered(mddev) && |
| 5961 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 5962 | pr_err("%s: Cannot add to clustered mddev.\n", |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5963 | mdname(mddev)); |
| 5964 | return -EINVAL; |
| 5965 | } |
| 5966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5968 | return -EOVERFLOW; |
| 5969 | |
| 5970 | if (!mddev->raid_disks) { |
| 5971 | int err; |
| 5972 | /* expecting a device which has a superblock */ |
| 5973 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5974 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5975 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5976 | "md: md_import_device returned %ld\n", |
| 5977 | PTR_ERR(rdev)); |
| 5978 | return PTR_ERR(rdev); |
| 5979 | } |
| 5980 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5981 | struct md_rdev *rdev0 |
| 5982 | = list_entry(mddev->disks.next, |
| 5983 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5984 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5985 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5986 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5987 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | "md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5989 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5990 | bdevname(rdev0->bdev,b2)); |
| 5991 | export_rdev(rdev); |
| 5992 | return -EINVAL; |
| 5993 | } |
| 5994 | } |
| 5995 | err = bind_rdev_to_array(rdev, mddev); |
| 5996 | if (err) |
| 5997 | export_rdev(rdev); |
| 5998 | return err; |
| 5999 | } |
| 6000 | |
| 6001 | /* |
| 6002 | * add_new_disk can be used once the array is assembled |
| 6003 | * to add "hot spares". They must already have a superblock |
| 6004 | * written |
| 6005 | */ |
| 6006 | if (mddev->pers) { |
| 6007 | int err; |
| 6008 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6009 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | "%s: personality does not support diskops!\n", |
| 6011 | mdname(mddev)); |
| 6012 | return -EINVAL; |
| 6013 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 6014 | if (mddev->persistent) |
| 6015 | rdev = md_import_device(dev, mddev->major_version, |
| 6016 | mddev->minor_version); |
| 6017 | else |
| 6018 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6019 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6020 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6021 | "md: md_import_device returned %ld\n", |
| 6022 | PTR_ERR(rdev)); |
| 6023 | return PTR_ERR(rdev); |
| 6024 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 6025 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6026 | if (!mddev->persistent) { |
| 6027 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6028 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6029 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6030 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 6031 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6032 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6033 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 6034 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6035 | } else |
| 6036 | super_types[mddev->major_version]. |
| 6037 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6038 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 6039 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6040 | /* This was a hot-add request, but events doesn't |
| 6041 | * match, so reject it. |
| 6042 | */ |
| 6043 | export_rdev(rdev); |
| 6044 | return -EINVAL; |
| 6045 | } |
| 6046 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6047 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6048 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6049 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6050 | else |
| 6051 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6052 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6053 | if (info->state & (1<<MD_DISK_JOURNAL)) { |
| 6054 | struct md_rdev *rdev2; |
| 6055 | bool has_journal = false; |
| 6056 | |
| 6057 | /* make sure no existing journal disk */ |
| 6058 | rdev_for_each(rdev2, mddev) { |
| 6059 | if (test_bit(Journal, &rdev2->flags)) { |
| 6060 | has_journal = true; |
| 6061 | break; |
| 6062 | } |
| 6063 | } |
| 6064 | if (has_journal) { |
| 6065 | export_rdev(rdev); |
| 6066 | return -EBUSY; |
| 6067 | } |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6068 | set_bit(Journal, &rdev->flags); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6069 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6070 | /* |
| 6071 | * check whether the device shows up in other nodes |
| 6072 | */ |
| 6073 | if (mddev_is_clustered(mddev)) { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6074 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6075 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6076 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6077 | /* --add initiated by this node */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6078 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6079 | if (err) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6080 | export_rdev(rdev); |
| 6081 | return err; |
| 6082 | } |
| 6083 | } |
| 6084 | } |
| 6085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | rdev->raid_disk = -1; |
| 6087 | err = bind_rdev_to_array(rdev, mddev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6089 | if (err) |
| 6090 | export_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6091 | |
| 6092 | if (mddev_is_clustered(mddev)) { |
| 6093 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
| 6094 | md_cluster_ops->new_disk_ack(mddev, (err == 0)); |
| 6095 | else { |
| 6096 | if (err) |
| 6097 | md_cluster_ops->add_new_disk_cancel(mddev); |
| 6098 | else |
| 6099 | err = add_bound_rdev(rdev); |
| 6100 | } |
| 6101 | |
| 6102 | } else if (!err) |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 6103 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | return err; |
| 6106 | } |
| 6107 | |
| 6108 | /* otherwise, add_new_disk is only allowed |
| 6109 | * for major_version==0 superblocks |
| 6110 | */ |
| 6111 | if (mddev->major_version != 0) { |
| 6112 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 6113 | mdname(mddev)); |
| 6114 | return -EINVAL; |
| 6115 | } |
| 6116 | |
| 6117 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 6118 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6119 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6121 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6122 | "md: error, md_import_device() returned %ld\n", |
| 6123 | PTR_ERR(rdev)); |
| 6124 | return PTR_ERR(rdev); |
| 6125 | } |
| 6126 | rdev->desc_nr = info->number; |
| 6127 | if (info->raid_disk < mddev->raid_disks) |
| 6128 | rdev->raid_disk = info->raid_disk; |
| 6129 | else |
| 6130 | rdev->raid_disk = -1; |
| 6131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6132 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6133 | if (info->state & (1<<MD_DISK_SYNC)) |
| 6134 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6136 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6137 | set_bit(WriteMostly, &rdev->flags); |
| 6138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | if (!mddev->persistent) { |
| 6140 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6141 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 6142 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6143 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6144 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6145 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6146 | err = bind_rdev_to_array(rdev, mddev); |
| 6147 | if (err) { |
| 6148 | export_rdev(rdev); |
| 6149 | return err; |
| 6150 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6151 | } |
| 6152 | |
| 6153 | return 0; |
| 6154 | } |
| 6155 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6156 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6157 | { |
| 6158 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6159 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6161 | rdev = find_rdev(mddev, dev); |
| 6162 | if (!rdev) |
| 6163 | return -ENXIO; |
| 6164 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6165 | if (rdev->raid_disk < 0) |
| 6166 | goto kick_rdev; |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6167 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6168 | clear_bit(Blocked, &rdev->flags); |
| 6169 | remove_and_add_spares(mddev, rdev); |
| 6170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6171 | if (rdev->raid_disk >= 0) |
| 6172 | goto busy; |
| 6173 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6174 | kick_rdev: |
Goldwyn Rodrigues | 54a8839 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6175 | if (mddev_is_clustered(mddev)) |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6176 | md_cluster_ops->remove_disk(mddev, rdev); |
| 6177 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6178 | md_kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6179 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6180 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6181 | |
| 6182 | return 0; |
| 6183 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 6184 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6185 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6186 | return -EBUSY; |
| 6187 | } |
| 6188 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6189 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6190 | { |
| 6191 | char b[BDEVNAME_SIZE]; |
| 6192 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6193 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6194 | |
| 6195 | if (!mddev->pers) |
| 6196 | return -ENODEV; |
| 6197 | |
| 6198 | if (mddev->major_version != 0) { |
| 6199 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 6200 | " version-0 superblocks.\n", |
| 6201 | mdname(mddev)); |
| 6202 | return -EINVAL; |
| 6203 | } |
| 6204 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6205 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6206 | "%s: personality does not support diskops!\n", |
| 6207 | mdname(mddev)); |
| 6208 | return -EINVAL; |
| 6209 | } |
| 6210 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6211 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6212 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6213 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6214 | "md: error, md_import_device() returned %ld\n", |
| 6215 | PTR_ERR(rdev)); |
| 6216 | return -EINVAL; |
| 6217 | } |
| 6218 | |
| 6219 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6220 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6221 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6222 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6223 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6224 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6225 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6226 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6227 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6228 | "md: can not hot-add faulty %s disk to %s!\n", |
| 6229 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6230 | err = -EINVAL; |
| 6231 | goto abort_export; |
| 6232 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6233 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6234 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6235 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 6236 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6237 | err = bind_rdev_to_array(rdev, mddev); |
| 6238 | if (err) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 6239 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6240 | |
| 6241 | /* |
| 6242 | * The rest should better be atomic, we can have disk failures |
| 6243 | * noticed in interrupt contexts ... |
| 6244 | */ |
| 6245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6246 | rdev->raid_disk = -1; |
| 6247 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6248 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | /* |
| 6250 | * Kick recovery, maybe this spare has to be added to the |
| 6251 | * array immediately. |
| 6252 | */ |
| 6253 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6254 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6255 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6256 | return 0; |
| 6257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6258 | abort_export: |
| 6259 | export_rdev(rdev); |
| 6260 | return err; |
| 6261 | } |
| 6262 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6263 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6264 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6265 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6266 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6267 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 6268 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6269 | return -EBUSY; |
| 6270 | if (mddev->recovery || mddev->sync_thread) |
| 6271 | return -EBUSY; |
| 6272 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6273 | } |
| 6274 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6275 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6276 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6277 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6278 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6279 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 6280 | return -EEXIST; /* cannot add when bitmap is present */ |
| 6281 | f = fget(fd); |
| 6282 | |
| 6283 | if (f == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6284 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 6285 | mdname(mddev)); |
| 6286 | return -EBADF; |
| 6287 | } |
| 6288 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6289 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6290 | if (!S_ISREG(inode->i_mode)) { |
| 6291 | printk(KERN_ERR "%s: error: bitmap file must be a regular file\n", |
| 6292 | mdname(mddev)); |
| 6293 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6294 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6295 | printk(KERN_ERR "%s: error: bitmap file must open for write\n", |
| 6296 | mdname(mddev)); |
| 6297 | err = -EBADF; |
| 6298 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6299 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 6300 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6301 | err = -EBUSY; |
| 6302 | } |
| 6303 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6304 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6305 | return err; |
| 6306 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6307 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6308 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6309 | } else if (mddev->bitmap == NULL) |
| 6310 | return -ENOENT; /* cannot remove what isn't there */ |
| 6311 | err = 0; |
| 6312 | if (mddev->pers) { |
| 6313 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6314 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6315 | struct bitmap *bitmap; |
| 6316 | |
| 6317 | bitmap = bitmap_create(mddev, -1); |
| 6318 | if (!IS_ERR(bitmap)) { |
| 6319 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6320 | err = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6321 | } else |
| 6322 | err = PTR_ERR(bitmap); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6323 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6324 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6325 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6326 | fd = -1; /* make sure to put the file */ |
| 6327 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6328 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6329 | } |
| 6330 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6331 | struct file *f = mddev->bitmap_info.file; |
| 6332 | if (f) { |
| 6333 | spin_lock(&mddev->lock); |
| 6334 | mddev->bitmap_info.file = NULL; |
| 6335 | spin_unlock(&mddev->lock); |
| 6336 | fput(f); |
| 6337 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6338 | } |
| 6339 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6340 | return err; |
| 6341 | } |
| 6342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6343 | /* |
| 6344 | * set_array_info is used two different ways |
| 6345 | * The original usage is when creating a new array. |
| 6346 | * In this usage, raid_disks is > 0 and it together with |
| 6347 | * level, size, not_persistent,layout,chunksize determine the |
| 6348 | * shape of the array. |
| 6349 | * This will always create an array with a type-0.90.0 superblock. |
| 6350 | * The newer usage is when assembling an array. |
| 6351 | * In this case raid_disks will be 0, and the major_version field is |
| 6352 | * use to determine which style super-blocks are to be found on the devices. |
| 6353 | * The minor and patch _version numbers are also kept incase the |
| 6354 | * super_block handler wishes to interpret them. |
| 6355 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6356 | 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] | 6357 | { |
| 6358 | |
| 6359 | if (info->raid_disks == 0) { |
| 6360 | /* just setting version number for superblock loading */ |
| 6361 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6362 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6363 | super_types[info->major_version].name == NULL) { |
| 6364 | /* maybe try to auto-load a module? */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6365 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6366 | "md: superblock version %d not known\n", |
| 6367 | info->major_version); |
| 6368 | return -EINVAL; |
| 6369 | } |
| 6370 | mddev->major_version = info->major_version; |
| 6371 | mddev->minor_version = info->minor_version; |
| 6372 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6373 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6374 | /* ensure mddev_put doesn't delete this now that there |
| 6375 | * is some minimal configuration. |
| 6376 | */ |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6377 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6378 | return 0; |
| 6379 | } |
| 6380 | mddev->major_version = MD_MAJOR_VERSION; |
| 6381 | mddev->minor_version = MD_MINOR_VERSION; |
| 6382 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6383 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6384 | |
| 6385 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6386 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6387 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6388 | mddev->raid_disks = info->raid_disks; |
| 6389 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6390 | * openned |
| 6391 | */ |
| 6392 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6393 | mddev->recovery_cp = MaxSector; |
| 6394 | else |
| 6395 | mddev->recovery_cp = 0; |
| 6396 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6397 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | |
| 6399 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6400 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6401 | |
| 6402 | mddev->max_disks = MD_SB_DISKS; |
| 6403 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6404 | if (mddev->persistent) |
| 6405 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6406 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6407 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6408 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6409 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6410 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6411 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6412 | mddev->reshape_position = MaxSector; |
| 6413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6414 | /* |
| 6415 | * Generate a 128 bit UUID |
| 6416 | */ |
| 6417 | get_random_bytes(mddev->uuid, 16); |
| 6418 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6419 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6420 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6421 | mddev->new_layout = mddev->layout; |
| 6422 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6423 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6425 | return 0; |
| 6426 | } |
| 6427 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6428 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6429 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6430 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6431 | |
| 6432 | if (mddev->external_size) |
| 6433 | return; |
| 6434 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6435 | mddev->array_sectors = array_sectors; |
| 6436 | } |
| 6437 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6438 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6439 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6440 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6441 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6442 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6443 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6444 | |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 6445 | /* cluster raid doesn't support update size */ |
| 6446 | if (mddev_is_clustered(mddev)) |
| 6447 | return -EINVAL; |
| 6448 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6449 | if (mddev->pers->resize == NULL) |
| 6450 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6451 | /* The "num_sectors" is the number of sectors of each device that |
| 6452 | * is used. This can only make sense for arrays with redundancy. |
| 6453 | * linear and raid0 always use whatever space is available. We can only |
| 6454 | * consider changing this number if no resync or reconstruction is |
| 6455 | * 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] | 6456 | * 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] | 6457 | * of each device. If num_sectors is zero, we find the largest size |
| 6458 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6459 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6460 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6461 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6462 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6463 | if (mddev->ro) |
| 6464 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6465 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6466 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6467 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6468 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6469 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6470 | num_sectors = avail; |
| 6471 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6472 | return -ENOSPC; |
| 6473 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6474 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6475 | if (!rv) |
| 6476 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6477 | return rv; |
| 6478 | } |
| 6479 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6480 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6481 | { |
| 6482 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6483 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6484 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6485 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6486 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6487 | if (mddev->ro) |
| 6488 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6489 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6490 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6491 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6492 | if (mddev->sync_thread || |
| 6493 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6494 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6495 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6496 | |
| 6497 | rdev_for_each(rdev, mddev) { |
| 6498 | if (mddev->raid_disks < raid_disks && |
| 6499 | rdev->data_offset < rdev->new_data_offset) |
| 6500 | return -EINVAL; |
| 6501 | if (mddev->raid_disks > raid_disks && |
| 6502 | rdev->data_offset > rdev->new_data_offset) |
| 6503 | return -EINVAL; |
| 6504 | } |
| 6505 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6506 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6507 | if (mddev->delta_disks < 0) |
| 6508 | mddev->reshape_backwards = 1; |
| 6509 | else if (mddev->delta_disks > 0) |
| 6510 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6511 | |
| 6512 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6513 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6514 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6515 | mddev->reshape_backwards = 0; |
| 6516 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6517 | return rv; |
| 6518 | } |
| 6519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6520 | /* |
| 6521 | * update_array_info is used to change the configuration of an |
| 6522 | * on-line array. |
| 6523 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6524 | * fields in the info are checked against the array. |
| 6525 | * Any differences that cannot be handled will cause an error. |
| 6526 | * Normally, only one change can be managed at a time. |
| 6527 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6528 | 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] | 6529 | { |
| 6530 | int rv = 0; |
| 6531 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6532 | int state = 0; |
| 6533 | |
| 6534 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6535 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6536 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6537 | |
| 6538 | if (mddev->major_version != info->major_version || |
| 6539 | mddev->minor_version != info->minor_version || |
| 6540 | /* mddev->patch_version != info->patch_version || */ |
| 6541 | mddev->ctime != info->ctime || |
| 6542 | mddev->level != info->level || |
| 6543 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 6544 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6545 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6546 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6547 | ((state^info->state) & 0xfffffe00) |
| 6548 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6549 | return -EINVAL; |
| 6550 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6551 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6552 | cnt++; |
| 6553 | if (mddev->raid_disks != info->raid_disks) |
| 6554 | cnt++; |
| 6555 | if (mddev->layout != info->layout) |
| 6556 | cnt++; |
| 6557 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6558 | cnt++; |
| 6559 | if (cnt == 0) |
| 6560 | return 0; |
| 6561 | if (cnt > 1) |
| 6562 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6563 | |
| 6564 | if (mddev->layout != info->layout) { |
| 6565 | /* Change layout |
| 6566 | * we don't need to do anything at the md level, the |
| 6567 | * personality will take care of it all. |
| 6568 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6569 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6570 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6571 | else { |
| 6572 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6573 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6574 | if (rv) |
| 6575 | mddev->new_layout = mddev->layout; |
| 6576 | return rv; |
| 6577 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6578 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6579 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6580 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6581 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6582 | if (mddev->raid_disks != info->raid_disks) |
| 6583 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6584 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6585 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6586 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 6587 | rv = -EINVAL; |
| 6588 | goto err; |
| 6589 | } |
| 6590 | if (mddev->recovery || mddev->sync_thread) { |
| 6591 | rv = -EBUSY; |
| 6592 | goto err; |
| 6593 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6594 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6595 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6596 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6597 | if (mddev->bitmap) { |
| 6598 | rv = -EEXIST; |
| 6599 | goto err; |
| 6600 | } |
| 6601 | if (mddev->bitmap_info.default_offset == 0) { |
| 6602 | rv = -EINVAL; |
| 6603 | goto err; |
| 6604 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6605 | mddev->bitmap_info.offset = |
| 6606 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6607 | mddev->bitmap_info.space = |
| 6608 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6609 | mddev->pers->quiesce(mddev, 1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6610 | bitmap = bitmap_create(mddev, -1); |
| 6611 | if (!IS_ERR(bitmap)) { |
| 6612 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6613 | rv = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6614 | } else |
| 6615 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6616 | if (rv) |
| 6617 | bitmap_destroy(mddev); |
| 6618 | mddev->pers->quiesce(mddev, 0); |
| 6619 | } else { |
| 6620 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6621 | if (!mddev->bitmap) { |
| 6622 | rv = -ENOENT; |
| 6623 | goto err; |
| 6624 | } |
| 6625 | if (mddev->bitmap->storage.file) { |
| 6626 | rv = -EINVAL; |
| 6627 | goto err; |
| 6628 | } |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6629 | if (mddev->bitmap_info.nodes) { |
| 6630 | /* hold PW on all the bitmap lock */ |
| 6631 | if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) { |
| 6632 | printk("md: can't change bitmap to none since the" |
| 6633 | " array is in use by more than one node\n"); |
| 6634 | rv = -EPERM; |
| 6635 | md_cluster_ops->unlock_all_bitmaps(mddev); |
| 6636 | goto err; |
| 6637 | } |
| 6638 | |
| 6639 | mddev->bitmap_info.nodes = 0; |
| 6640 | md_cluster_ops->leave(mddev); |
| 6641 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6642 | mddev->pers->quiesce(mddev, 1); |
| 6643 | bitmap_destroy(mddev); |
| 6644 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6645 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6646 | } |
| 6647 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6648 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6649 | return rv; |
| 6650 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6651 | return rv; |
| 6652 | } |
| 6653 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6654 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6655 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6656 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6657 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6658 | |
| 6659 | if (mddev->pers == NULL) |
| 6660 | return -ENODEV; |
| 6661 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6662 | rcu_read_lock(); |
| 6663 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6664 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6665 | err = -ENODEV; |
| 6666 | else { |
| 6667 | md_error(mddev, rdev); |
| 6668 | if (!test_bit(Faulty, &rdev->flags)) |
| 6669 | err = -EBUSY; |
| 6670 | } |
| 6671 | rcu_read_unlock(); |
| 6672 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6673 | } |
| 6674 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6675 | /* |
| 6676 | * We have a problem here : there is no easy way to give a CHS |
| 6677 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6678 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6679 | * dosfs just mad... ;-) |
| 6680 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6681 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6682 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6683 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6684 | |
| 6685 | geo->heads = 2; |
| 6686 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6687 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6688 | return 0; |
| 6689 | } |
| 6690 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6691 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 6692 | { |
| 6693 | switch (cmd) { |
| 6694 | case ADD_NEW_DISK: |
| 6695 | case BLKROSET: |
| 6696 | case GET_ARRAY_INFO: |
| 6697 | case GET_BITMAP_FILE: |
| 6698 | case GET_DISK_INFO: |
| 6699 | case HOT_ADD_DISK: |
| 6700 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6701 | case RAID_AUTORUN: |
| 6702 | case RAID_VERSION: |
| 6703 | case RESTART_ARRAY_RW: |
| 6704 | case RUN_ARRAY: |
| 6705 | case SET_ARRAY_INFO: |
| 6706 | case SET_BITMAP_FILE: |
| 6707 | case SET_DISK_FAULTY: |
| 6708 | case STOP_ARRAY: |
| 6709 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6710 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6711 | return true; |
| 6712 | default: |
| 6713 | return false; |
| 6714 | } |
| 6715 | } |
| 6716 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6717 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6718 | unsigned int cmd, unsigned long arg) |
| 6719 | { |
| 6720 | int err = 0; |
| 6721 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6722 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6723 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6724 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6725 | if (!md_ioctl_valid(cmd)) |
| 6726 | return -ENOTTY; |
| 6727 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6728 | switch (cmd) { |
| 6729 | case RAID_VERSION: |
| 6730 | case GET_ARRAY_INFO: |
| 6731 | case GET_DISK_INFO: |
| 6732 | break; |
| 6733 | default: |
| 6734 | if (!capable(CAP_SYS_ADMIN)) |
| 6735 | return -EACCES; |
| 6736 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6737 | |
| 6738 | /* |
| 6739 | * Commands dealing with the RAID driver but not any |
| 6740 | * particular array: |
| 6741 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6742 | switch (cmd) { |
| 6743 | case RAID_VERSION: |
| 6744 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6745 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6747 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6748 | case RAID_AUTORUN: |
| 6749 | err = 0; |
| 6750 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6751 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6753 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6754 | } |
| 6755 | |
| 6756 | /* |
| 6757 | * Commands creating/starting a new array: |
| 6758 | */ |
| 6759 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6760 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6761 | |
| 6762 | if (!mddev) { |
| 6763 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6764 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6765 | } |
| 6766 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6767 | /* Some actions do not requires the mutex */ |
| 6768 | switch (cmd) { |
| 6769 | case GET_ARRAY_INFO: |
| 6770 | if (!mddev->raid_disks && !mddev->external) |
| 6771 | err = -ENODEV; |
| 6772 | else |
| 6773 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6774 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6775 | |
| 6776 | case GET_DISK_INFO: |
| 6777 | if (!mddev->raid_disks && !mddev->external) |
| 6778 | err = -ENODEV; |
| 6779 | else |
| 6780 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6781 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6782 | |
| 6783 | case SET_DISK_FAULTY: |
| 6784 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6785 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6786 | |
| 6787 | case GET_BITMAP_FILE: |
| 6788 | err = get_bitmap_file(mddev, argp); |
| 6789 | goto out; |
| 6790 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6791 | } |
| 6792 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 6793 | if (cmd == ADD_NEW_DISK) |
| 6794 | /* need to ensure md_delayed_delete() has completed */ |
| 6795 | flush_workqueue(md_misc_wq); |
| 6796 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 6797 | if (cmd == HOT_REMOVE_DISK) |
| 6798 | /* need to ensure recovery thread has run */ |
| 6799 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 6800 | !test_bit(MD_RECOVERY_NEEDED, |
| 6801 | &mddev->flags), |
| 6802 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6803 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 6804 | /* Need to flush page cache, and ensure no-one else opens |
| 6805 | * and writes |
| 6806 | */ |
| 6807 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6808 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6809 | mutex_unlock(&mddev->open_mutex); |
| 6810 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6811 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6812 | } |
| 6813 | set_bit(MD_STILL_CLOSED, &mddev->flags); |
| 6814 | mutex_unlock(&mddev->open_mutex); |
| 6815 | sync_blockdev(bdev); |
| 6816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6817 | err = mddev_lock(mddev); |
| 6818 | if (err) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6819 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6820 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6821 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6822 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6823 | } |
| 6824 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6825 | if (cmd == SET_ARRAY_INFO) { |
| 6826 | mdu_array_info_t info; |
| 6827 | if (!arg) |
| 6828 | memset(&info, 0, sizeof(info)); |
| 6829 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6830 | err = -EFAULT; |
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 | } |
| 6833 | if (mddev->pers) { |
| 6834 | err = update_array_info(mddev, &info); |
| 6835 | if (err) { |
| 6836 | printk(KERN_WARNING "md: couldn't update" |
| 6837 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6838 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6839 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6840 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6841 | } |
| 6842 | if (!list_empty(&mddev->disks)) { |
| 6843 | printk(KERN_WARNING |
| 6844 | "md: array %s already has disks!\n", |
| 6845 | mdname(mddev)); |
| 6846 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6847 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6848 | } |
| 6849 | if (mddev->raid_disks) { |
| 6850 | printk(KERN_WARNING |
| 6851 | "md: array %s already initialised!\n", |
| 6852 | mdname(mddev)); |
| 6853 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6854 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6855 | } |
| 6856 | err = set_array_info(mddev, &info); |
| 6857 | if (err) { |
| 6858 | printk(KERN_WARNING "md: couldn't set" |
| 6859 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6860 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6861 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6862 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6863 | } |
| 6864 | |
| 6865 | /* |
| 6866 | * Commands querying/configuring an existing array: |
| 6867 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6868 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6869 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6870 | if ((!mddev->raid_disks && !mddev->external) |
| 6871 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6872 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6873 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6874 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6875 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6876 | } |
| 6877 | |
| 6878 | /* |
| 6879 | * Commands even a read-only array can execute: |
| 6880 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6881 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6882 | case RESTART_ARRAY_RW: |
| 6883 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6884 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6885 | |
| 6886 | case STOP_ARRAY: |
| 6887 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6888 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6889 | |
| 6890 | case STOP_ARRAY_RO: |
| 6891 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6892 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6893 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6894 | case HOT_REMOVE_DISK: |
| 6895 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6896 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6897 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6898 | case ADD_NEW_DISK: |
| 6899 | /* We can support ADD_NEW_DISK on read-only arrays |
Wei Fang | 466ad29 | 2016-03-21 19:19:30 +0800 | [diff] [blame] | 6900 | * only if we are re-adding a preexisting device. |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6901 | * So require mddev->pers and MD_DISK_SYNC. |
| 6902 | */ |
| 6903 | if (mddev->pers) { |
| 6904 | mdu_disk_info_t info; |
| 6905 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6906 | err = -EFAULT; |
| 6907 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 6908 | /* Need to clear read-only for this */ |
| 6909 | break; |
| 6910 | else |
| 6911 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6912 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6913 | } |
| 6914 | break; |
| 6915 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6916 | case BLKROSET: |
| 6917 | if (get_user(ro, (int __user *)(arg))) { |
| 6918 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6919 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6920 | } |
| 6921 | err = -EINVAL; |
| 6922 | |
| 6923 | /* if the bdev is going readonly the value of mddev->ro |
| 6924 | * does not matter, no writes are coming |
| 6925 | */ |
| 6926 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6927 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6928 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6929 | /* are we are already prepared for writes? */ |
| 6930 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6931 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6932 | |
| 6933 | /* transitioning to readauto need only happen for |
| 6934 | * arrays that call md_write_start |
| 6935 | */ |
| 6936 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6937 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6938 | if (err == 0) { |
| 6939 | mddev->ro = 2; |
| 6940 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6941 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6942 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6943 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6944 | } |
| 6945 | |
| 6946 | /* |
| 6947 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6948 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6949 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 6950 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6951 | if (mddev->ro == 2) { |
| 6952 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6953 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6954 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6955 | /* mddev_unlock will wake thread */ |
| 6956 | /* If a device failed while we were read-only, we |
| 6957 | * need to make sure the metadata is updated now. |
| 6958 | */ |
| 6959 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
| 6960 | mddev_unlock(mddev); |
| 6961 | wait_event(mddev->sb_wait, |
| 6962 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && |
| 6963 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6964 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6965 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6966 | } else { |
| 6967 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6968 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6969 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | } |
| 6971 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6972 | switch (cmd) { |
| 6973 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6974 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6975 | mdu_disk_info_t info; |
| 6976 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6977 | err = -EFAULT; |
| 6978 | else |
| 6979 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6980 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6981 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6982 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6983 | case CLUSTERED_DISK_NACK: |
| 6984 | if (mddev_is_clustered(mddev)) |
| 6985 | md_cluster_ops->new_disk_ack(mddev, false); |
| 6986 | else |
| 6987 | err = -EINVAL; |
| 6988 | goto unlock; |
| 6989 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6990 | case HOT_ADD_DISK: |
| 6991 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6992 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6993 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6994 | case RUN_ARRAY: |
| 6995 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6996 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6997 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6998 | case SET_BITMAP_FILE: |
| 6999 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7000 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7001 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7002 | default: |
| 7003 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7004 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7005 | } |
| 7006 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7007 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7008 | if (mddev->hold_active == UNTIL_IOCTL && |
| 7009 | err != -EINVAL) |
| 7010 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7011 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7012 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7013 | return err; |
| 7014 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7015 | #ifdef CONFIG_COMPAT |
| 7016 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 7017 | unsigned int cmd, unsigned long arg) |
| 7018 | { |
| 7019 | switch (cmd) { |
| 7020 | case HOT_REMOVE_DISK: |
| 7021 | case HOT_ADD_DISK: |
| 7022 | case SET_DISK_FAULTY: |
| 7023 | case SET_BITMAP_FILE: |
| 7024 | /* These take in integer arg, do not convert */ |
| 7025 | break; |
| 7026 | default: |
| 7027 | arg = (unsigned long)compat_ptr(arg); |
| 7028 | break; |
| 7029 | } |
| 7030 | |
| 7031 | return md_ioctl(bdev, mode, cmd, arg); |
| 7032 | } |
| 7033 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7034 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7035 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7036 | { |
| 7037 | /* |
| 7038 | * Succeed if we can lock the mddev, which confirms that |
| 7039 | * it isn't being stopped right now. |
| 7040 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7041 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7042 | int err; |
| 7043 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 7044 | if (!mddev) |
| 7045 | return -ENODEV; |
| 7046 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7047 | if (mddev->gendisk != bdev->bd_disk) { |
| 7048 | /* we are racing with mddev_put which is discarding this |
| 7049 | * bd_disk. |
| 7050 | */ |
| 7051 | mddev_put(mddev); |
| 7052 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7053 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7054 | /* Then retry the open from the top */ |
| 7055 | return -ERESTARTSYS; |
| 7056 | } |
| 7057 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 7058 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7059 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7060 | goto out; |
| 7061 | |
| 7062 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7063 | atomic_inc(&mddev->openers); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7064 | clear_bit(MD_STILL_CLOSED, &mddev->flags); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7065 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7066 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7067 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7068 | out: |
| 7069 | return err; |
| 7070 | } |
| 7071 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 7072 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7073 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7074 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7075 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 7076 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7077 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7078 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7079 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7080 | |
| 7081 | static int md_media_changed(struct gendisk *disk) |
| 7082 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7083 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7084 | |
| 7085 | return mddev->changed; |
| 7086 | } |
| 7087 | |
| 7088 | static int md_revalidate(struct gendisk *disk) |
| 7089 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7090 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7091 | |
| 7092 | mddev->changed = 0; |
| 7093 | return 0; |
| 7094 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 7095 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7096 | { |
| 7097 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7098 | .open = md_open, |
| 7099 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 7100 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7101 | #ifdef CONFIG_COMPAT |
| 7102 | .compat_ioctl = md_compat_ioctl, |
| 7103 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7104 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7105 | .media_changed = md_media_changed, |
| 7106 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7107 | }; |
| 7108 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7109 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7110 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7111 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7113 | /* |
| 7114 | * md_thread is a 'system-thread', it's priority should be very |
| 7115 | * high. We avoid resource deadlocks individually in each |
| 7116 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 7117 | * the very same RT priority as kswapd, thus we will never get |
| 7118 | * into a priority inversion deadlock. |
| 7119 | * |
| 7120 | * we definitely have to have equal or higher priority than |
| 7121 | * bdflush, otherwise bdflush will deadlock if there are too |
| 7122 | * many dirty RAID5 blocks. |
| 7123 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7124 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7125 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7126 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7128 | /* We need to wait INTERRUPTIBLE so that |
| 7129 | * we don't add to the load-average. |
| 7130 | * That means we need to be sure no signals are |
| 7131 | * pending |
| 7132 | */ |
| 7133 | if (signal_pending(current)) |
| 7134 | flush_signals(current); |
| 7135 | |
| 7136 | wait_event_interruptible_timeout |
| 7137 | (thread->wqueue, |
| 7138 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 7139 | || kthread_should_stop(), |
| 7140 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7141 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7142 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 7143 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7144 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7145 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7147 | return 0; |
| 7148 | } |
| 7149 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7150 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7151 | { |
| 7152 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7153 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7154 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 7155 | wake_up(&thread->wqueue); |
| 7156 | } |
| 7157 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7158 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7159 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7160 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7161 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7162 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7163 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7164 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7165 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7166 | if (!thread) |
| 7167 | return NULL; |
| 7168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7169 | init_waitqueue_head(&thread->wqueue); |
| 7170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7171 | thread->run = run; |
| 7172 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7173 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7174 | thread->tsk = kthread_run(md_thread, thread, |
| 7175 | "%s_%s", |
| 7176 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7177 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7178 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7179 | kfree(thread); |
| 7180 | return NULL; |
| 7181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7182 | return thread; |
| 7183 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7184 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7185 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7186 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7187 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7188 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7189 | if (!thread) |
| 7190 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7191 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7192 | /* Locking ensures that mddev_unlock does not wake_up a |
| 7193 | * non-existent thread |
| 7194 | */ |
| 7195 | spin_lock(&pers_lock); |
| 7196 | *threadp = NULL; |
| 7197 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7198 | |
| 7199 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7200 | kfree(thread); |
| 7201 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7202 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7203 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7204 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7205 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7206 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7207 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7208 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7209 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7210 | return; |
| 7211 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7212 | if (mddev->degraded) |
| 7213 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7214 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7215 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7216 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7217 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 7218 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7219 | queue_work(md_misc_wq, &mddev->event_work); |
Guoqing Jiang | bb9ef71 | 2015-12-28 10:46:38 +0800 | [diff] [blame] | 7220 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7221 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7222 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7223 | |
| 7224 | /* seq_file implementation /proc/mdstat */ |
| 7225 | |
| 7226 | static void status_unused(struct seq_file *seq) |
| 7227 | { |
| 7228 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7229 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7230 | |
| 7231 | seq_printf(seq, "unused devices: "); |
| 7232 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7233 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7234 | char b[BDEVNAME_SIZE]; |
| 7235 | i++; |
| 7236 | seq_printf(seq, "%s ", |
| 7237 | bdevname(rdev->bdev,b)); |
| 7238 | } |
| 7239 | if (!i) |
| 7240 | seq_printf(seq, "<none>"); |
| 7241 | |
| 7242 | seq_printf(seq, "\n"); |
| 7243 | } |
| 7244 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7245 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7246 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7247 | sector_t max_sectors, resync, res; |
| 7248 | unsigned long dt, db; |
| 7249 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7250 | int scale; |
| 7251 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7252 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7253 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 7254 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7255 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7256 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7257 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7258 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7259 | resync = mddev->curr_resync; |
| 7260 | if (resync <= 3) { |
| 7261 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7262 | /* Still cleaning up */ |
| 7263 | resync = max_sectors; |
| 7264 | } else |
| 7265 | resync -= atomic_read(&mddev->recovery_active); |
| 7266 | |
| 7267 | if (resync == 0) { |
| 7268 | if (mddev->recovery_cp < MaxSector) { |
| 7269 | seq_printf(seq, "\tresync=PENDING"); |
| 7270 | return 1; |
| 7271 | } |
| 7272 | return 0; |
| 7273 | } |
| 7274 | if (resync < 3) { |
| 7275 | seq_printf(seq, "\tresync=DELAYED"); |
| 7276 | return 1; |
| 7277 | } |
| 7278 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 7279 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7280 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7281 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7282 | * u32, as those are the requirements for sector_div. |
| 7283 | * Thus 'scale' must be at least 10 |
| 7284 | */ |
| 7285 | scale = 10; |
| 7286 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7287 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7288 | scale++; |
| 7289 | } |
| 7290 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7291 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7292 | |
| 7293 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7294 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7295 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7296 | seq_printf(seq, "["); |
| 7297 | for (i = 0; i < x; i++) |
| 7298 | seq_printf(seq, "="); |
| 7299 | seq_printf(seq, ">"); |
| 7300 | for (i = 0; i < y; i++) |
| 7301 | seq_printf(seq, "."); |
| 7302 | seq_printf(seq, "] "); |
| 7303 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7304 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7305 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 7306 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7307 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 7308 | "check" : |
| 7309 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 7310 | "resync" : "recovery"))), |
| 7311 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7312 | (unsigned long long) resync/2, |
| 7313 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7314 | |
| 7315 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7316 | * dt: time from mark until now |
| 7317 | * db: blocks written from mark until now |
| 7318 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7319 | * |
| 7320 | * rt is a sector_t, so could be 32bit or 64bit. |
| 7321 | * So we divide before multiply in case it is 32bit and close |
| 7322 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 7323 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7324 | * near the end of resync when the number of remaining sectors |
| 7325 | * is close to 'db'. |
| 7326 | * We then divide rt by 32 after multiplying by db to compensate. |
| 7327 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7328 | */ |
| 7329 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 7330 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7331 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 7332 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7333 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7334 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 7335 | sector_div(rt, db/32+1); |
| 7336 | rt *= dt; |
| 7337 | rt >>= 5; |
| 7338 | |
| 7339 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 7340 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7341 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7342 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7343 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7344 | } |
| 7345 | |
| 7346 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 7347 | { |
| 7348 | struct list_head *tmp; |
| 7349 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7350 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7351 | |
| 7352 | if (l >= 0x10000) |
| 7353 | return NULL; |
| 7354 | if (!l--) |
| 7355 | /* header */ |
| 7356 | return (void*)1; |
| 7357 | |
| 7358 | spin_lock(&all_mddevs_lock); |
| 7359 | list_for_each(tmp,&all_mddevs) |
| 7360 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7361 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7362 | mddev_get(mddev); |
| 7363 | spin_unlock(&all_mddevs_lock); |
| 7364 | return mddev; |
| 7365 | } |
| 7366 | spin_unlock(&all_mddevs_lock); |
| 7367 | if (!l--) |
| 7368 | return (void*)2;/* tail */ |
| 7369 | return NULL; |
| 7370 | } |
| 7371 | |
| 7372 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 7373 | { |
| 7374 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7375 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7377 | ++*pos; |
| 7378 | if (v == (void*)2) |
| 7379 | return NULL; |
| 7380 | |
| 7381 | spin_lock(&all_mddevs_lock); |
| 7382 | if (v == (void*)1) |
| 7383 | tmp = all_mddevs.next; |
| 7384 | else |
| 7385 | tmp = mddev->all_mddevs.next; |
| 7386 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7387 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7388 | else { |
| 7389 | next_mddev = (void*)2; |
| 7390 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7391 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7392 | spin_unlock(&all_mddevs_lock); |
| 7393 | |
| 7394 | if (v != (void*)1) |
| 7395 | mddev_put(mddev); |
| 7396 | return next_mddev; |
| 7397 | |
| 7398 | } |
| 7399 | |
| 7400 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 7401 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7402 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7403 | |
| 7404 | if (mddev && v != (void*)1 && v != (void*)2) |
| 7405 | mddev_put(mddev); |
| 7406 | } |
| 7407 | |
| 7408 | static int md_seq_show(struct seq_file *seq, void *v) |
| 7409 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7410 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7411 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7412 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7413 | |
| 7414 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7415 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7416 | seq_printf(seq, "Personalities : "); |
| 7417 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7418 | list_for_each_entry(pers, &pers_list, list) |
| 7419 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7420 | |
| 7421 | spin_unlock(&pers_lock); |
| 7422 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7423 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7424 | return 0; |
| 7425 | } |
| 7426 | if (v == (void*)2) { |
| 7427 | status_unused(seq); |
| 7428 | return 0; |
| 7429 | } |
| 7430 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7431 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7432 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7433 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7434 | mddev->pers ? "" : "in"); |
| 7435 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7436 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7437 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7438 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7439 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7440 | seq_printf(seq, " %s", mddev->pers->name); |
| 7441 | } |
| 7442 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7443 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7444 | rcu_read_lock(); |
| 7445 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7446 | char b[BDEVNAME_SIZE]; |
| 7447 | seq_printf(seq, " %s[%d]", |
| 7448 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7449 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7450 | seq_printf(seq, "(W)"); |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 7451 | if (test_bit(Journal, &rdev->flags)) |
| 7452 | seq_printf(seq, "(J)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7453 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7454 | seq_printf(seq, "(F)"); |
| 7455 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7456 | } |
| 7457 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7458 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7459 | if (test_bit(Replacement, &rdev->flags)) |
| 7460 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7461 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7462 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7463 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7464 | |
| 7465 | if (!list_empty(&mddev->disks)) { |
| 7466 | if (mddev->pers) |
| 7467 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7468 | (unsigned long long) |
| 7469 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7470 | else |
| 7471 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7472 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7473 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7474 | if (mddev->persistent) { |
| 7475 | if (mddev->major_version != 0 || |
| 7476 | mddev->minor_version != 90) { |
| 7477 | seq_printf(seq," super %d.%d", |
| 7478 | mddev->major_version, |
| 7479 | mddev->minor_version); |
| 7480 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7481 | } else if (mddev->external) |
| 7482 | seq_printf(seq, " super external:%s", |
| 7483 | mddev->metadata_type); |
| 7484 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7485 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7486 | |
| 7487 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7488 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7489 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7490 | if (mddev->pers->sync_request) { |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7491 | if (status_resync(seq, mddev)) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7492 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7493 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7494 | } else |
| 7495 | seq_printf(seq, "\n "); |
| 7496 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7497 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7498 | |
| 7499 | seq_printf(seq, "\n"); |
| 7500 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7501 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7503 | return 0; |
| 7504 | } |
| 7505 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7506 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7507 | .start = md_seq_start, |
| 7508 | .next = md_seq_next, |
| 7509 | .stop = md_seq_stop, |
| 7510 | .show = md_seq_show, |
| 7511 | }; |
| 7512 | |
| 7513 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7514 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7515 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7516 | int error; |
| 7517 | |
| 7518 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7519 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7520 | return error; |
| 7521 | |
| 7522 | seq = file->private_data; |
| 7523 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7524 | return error; |
| 7525 | } |
| 7526 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7527 | static int md_unloading; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7528 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7529 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7530 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7531 | int mask; |
| 7532 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7533 | if (md_unloading) |
kbuild test robot | 7d7e64f | 2014-12-03 16:07:59 +1100 | [diff] [blame] | 7534 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7535 | poll_wait(filp, &md_event_waiters, wait); |
| 7536 | |
| 7537 | /* always allow read */ |
| 7538 | mask = POLLIN | POLLRDNORM; |
| 7539 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7540 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7541 | mask |= POLLERR | POLLPRI; |
| 7542 | return mask; |
| 7543 | } |
| 7544 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7545 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7546 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7547 | .open = md_seq_open, |
| 7548 | .read = seq_read, |
| 7549 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7550 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7551 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7552 | }; |
| 7553 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7554 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7555 | { |
Chao Yu | 50bd377 | 2014-09-25 15:28:34 +0800 | [diff] [blame] | 7556 | printk(KERN_INFO "md: %s personality registered for level %d\n", |
| 7557 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7558 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7559 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7560 | spin_unlock(&pers_lock); |
| 7561 | return 0; |
| 7562 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7563 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7564 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7565 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7566 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7567 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7568 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7569 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7570 | spin_unlock(&pers_lock); |
| 7571 | return 0; |
| 7572 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7573 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7574 | |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7575 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
| 7576 | struct module *module) |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7577 | { |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7578 | int ret = 0; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7579 | spin_lock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7580 | if (md_cluster_ops != NULL) |
| 7581 | ret = -EALREADY; |
| 7582 | else { |
| 7583 | md_cluster_ops = ops; |
| 7584 | md_cluster_mod = module; |
| 7585 | } |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7586 | spin_unlock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7587 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7588 | } |
| 7589 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 7590 | |
| 7591 | int unregister_md_cluster_operations(void) |
| 7592 | { |
| 7593 | spin_lock(&pers_lock); |
| 7594 | md_cluster_ops = NULL; |
| 7595 | spin_unlock(&pers_lock); |
| 7596 | return 0; |
| 7597 | } |
| 7598 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 7599 | |
| 7600 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 7601 | { |
| 7602 | int err; |
| 7603 | |
| 7604 | err = request_module("md-cluster"); |
| 7605 | if (err) { |
| 7606 | pr_err("md-cluster module not found.\n"); |
Goldwyn Rodrigues | b0c26a7 | 2015-07-22 12:09:15 -0500 | [diff] [blame] | 7607 | return -ENOENT; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7608 | } |
| 7609 | |
| 7610 | spin_lock(&pers_lock); |
| 7611 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
| 7612 | spin_unlock(&pers_lock); |
| 7613 | return -ENOENT; |
| 7614 | } |
| 7615 | spin_unlock(&pers_lock); |
| 7616 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 7617 | return md_cluster_ops->join(mddev, nodes); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7618 | } |
| 7619 | |
| 7620 | void md_cluster_stop(struct mddev *mddev) |
| 7621 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 7622 | if (!md_cluster_ops) |
| 7623 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7624 | md_cluster_ops->leave(mddev); |
| 7625 | module_put(md_cluster_mod); |
| 7626 | } |
| 7627 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7628 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7629 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7630 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7631 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7632 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7633 | |
| 7634 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7635 | rcu_read_lock(); |
| 7636 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7637 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7638 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7639 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7640 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7641 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7642 | * as sync_io is counted when a request starts, and |
| 7643 | * disk_stats is counted when it completes. |
| 7644 | * So resync activity will cause curr_events to be smaller than |
| 7645 | * when there was no such activity. |
| 7646 | * non-sync IO will cause disk_stat to increase without |
| 7647 | * increasing sync_io so curr_events will (eventually) |
| 7648 | * be larger than it was before. Once it becomes |
| 7649 | * substantially larger, the test below will cause |
| 7650 | * the array to appear non-idle, and resync will slow |
| 7651 | * down. |
| 7652 | * If there is a lot of outstanding resync activity when |
| 7653 | * we set last_event to curr_events, then all that activity |
| 7654 | * completing might cause the array to appear non-idle |
| 7655 | * and resync will be slowed down even though there might |
| 7656 | * not have been non-resync activity. This will only |
| 7657 | * happen once though. 'last_events' will soon reflect |
| 7658 | * the state where there is little or no outstanding |
| 7659 | * resync requests, and further resync activity will |
| 7660 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7661 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7662 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7663 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7664 | rdev->last_events = curr_events; |
| 7665 | idle = 0; |
| 7666 | } |
| 7667 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7668 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7669 | return idle; |
| 7670 | } |
| 7671 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7672 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7673 | { |
| 7674 | /* another "blocks" (512byte) blocks have been synced */ |
| 7675 | atomic_sub(blocks, &mddev->recovery_active); |
| 7676 | wake_up(&mddev->recovery_wait); |
| 7677 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7678 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7679 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7680 | md_wakeup_thread(mddev->thread); |
| 7681 | // stop recovery, signal do_sync .... |
| 7682 | } |
| 7683 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7684 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7685 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7686 | /* md_write_start(mddev, bi) |
| 7687 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7688 | * in superblock) before writing, schedule a superblock update |
| 7689 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7690 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7691 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7692 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7693 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7694 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7695 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7696 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7697 | BUG_ON(mddev->ro == 1); |
| 7698 | if (mddev->ro == 2) { |
| 7699 | /* need to switch to read/write */ |
| 7700 | mddev->ro = 0; |
| 7701 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7702 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7703 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7704 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7705 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7706 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7707 | if (mddev->safemode == 1) |
| 7708 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7709 | if (mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7710 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7711 | if (mddev->in_sync) { |
| 7712 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7713 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7714 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7715 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7716 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7717 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7718 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7719 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7720 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7721 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7722 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7723 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7724 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7725 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7726 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7727 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7728 | { |
| 7729 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7730 | if (mddev->safemode == 2) |
| 7731 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7732 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7733 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7734 | } |
| 7735 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7736 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7737 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7738 | /* md_allow_write(mddev) |
| 7739 | * Calling this ensures that the array is marked 'active' so that writes |
| 7740 | * may proceed without blocking. It is important to call this before |
| 7741 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7742 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7743 | * |
Guoqing Jiang | abf3508 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 7744 | * In the ->external case MD_CHANGE_PENDING can not be cleared until mddev->lock |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7745 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7746 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7747 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7748 | { |
| 7749 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7750 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7751 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7752 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7753 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7754 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7755 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7756 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7757 | if (mddev->in_sync) { |
| 7758 | mddev->in_sync = 0; |
| 7759 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7760 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7761 | if (mddev->safemode_delay && |
| 7762 | mddev->safemode == 0) |
| 7763 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7764 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7765 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7766 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7767 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7768 | spin_unlock(&mddev->lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7769 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7770 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7771 | return -EAGAIN; |
| 7772 | else |
| 7773 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7774 | } |
| 7775 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7777 | #define SYNC_MARKS 10 |
| 7778 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7779 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7780 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7781 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7782 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7783 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7784 | unsigned int currspeed = 0, |
| 7785 | window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7786 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7787 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7788 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7789 | sector_t mark_cnt[SYNC_MARKS]; |
| 7790 | int last_mark,m; |
| 7791 | struct list_head *tmp; |
| 7792 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7793 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7794 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7795 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7796 | struct blk_plug plug; |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 7797 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7798 | |
| 7799 | /* just incase thread restarts... */ |
| 7800 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7801 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7802 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 7803 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7804 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7805 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7806 | |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 7807 | if (mddev_is_clustered(mddev)) { |
| 7808 | ret = md_cluster_ops->resync_start(mddev); |
| 7809 | if (ret) |
| 7810 | goto skip; |
| 7811 | |
| 7812 | if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 7813 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) || |
| 7814 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
| 7815 | && ((unsigned long long)mddev->curr_resync_completed |
| 7816 | < (unsigned long long)mddev->resync_max_sectors)) |
| 7817 | goto skip; |
| 7818 | } |
| 7819 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7820 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7821 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7822 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7823 | action = "check"; |
| 7824 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7825 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7826 | action = "repair"; |
| 7827 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7828 | desc = "resync"; |
| 7829 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7830 | desc = "reshape"; |
| 7831 | else |
| 7832 | desc = "recovery"; |
| 7833 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7834 | mddev->last_sync_action = action ?: desc; |
| 7835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7836 | /* we overload curr_resync somewhat here. |
| 7837 | * 0 == not engaged in resync at all |
| 7838 | * 2 == checking that there is no conflict with another sync |
| 7839 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7840 | * commense |
| 7841 | * other == active in resync - this many blocks |
| 7842 | * |
| 7843 | * Before starting a resync we must have set curr_resync to |
| 7844 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7845 | * less than ours. When we find one that is the same or higher |
| 7846 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7847 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7848 | * This will mean we have to start checking from the beginning again. |
| 7849 | * |
| 7850 | */ |
| 7851 | |
| 7852 | do { |
| 7853 | mddev->curr_resync = 2; |
| 7854 | |
| 7855 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7856 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7857 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7858 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7859 | if (mddev2 == mddev) |
| 7860 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7861 | if (!mddev->parallel_resync |
| 7862 | && mddev2->curr_resync |
| 7863 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7864 | DEFINE_WAIT(wq); |
| 7865 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7866 | /* arbitrarily yield */ |
| 7867 | mddev->curr_resync = 1; |
| 7868 | wake_up(&resync_wait); |
| 7869 | } |
| 7870 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7871 | /* no need to wait here, we can wait the next |
| 7872 | * time 'round when curr_resync == 2 |
| 7873 | */ |
| 7874 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7875 | /* We need to wait 'interruptible' so as not to |
| 7876 | * contribute to the load average, and not to |
| 7877 | * be caught by 'softlockup' |
| 7878 | */ |
| 7879 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7880 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7881 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7882 | printk(KERN_INFO "md: delaying %s of %s" |
| 7883 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7884 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7885 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7886 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7887 | if (signal_pending(current)) |
| 7888 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7889 | schedule(); |
| 7890 | finish_wait(&resync_wait, &wq); |
| 7891 | goto try_again; |
| 7892 | } |
| 7893 | finish_wait(&resync_wait, &wq); |
| 7894 | } |
| 7895 | } |
| 7896 | } while (mddev->curr_resync < 2); |
| 7897 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7898 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7899 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7900 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7901 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7902 | */ |
| 7903 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 7904 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7905 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7906 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7907 | j = mddev->resync_min; |
| 7908 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7909 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7910 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7911 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7912 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7913 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7914 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7915 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7916 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7917 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7918 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7919 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 7920 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7921 | !test_bit(Faulty, &rdev->flags) && |
| 7922 | !test_bit(In_sync, &rdev->flags) && |
| 7923 | rdev->recovery_offset < j) |
| 7924 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7925 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 7926 | |
| 7927 | /* If there is a bitmap, we need to make sure all |
| 7928 | * writes that started before we added a spare |
| 7929 | * complete before we start doing a recovery. |
| 7930 | * Otherwise the write might complete and (via |
| 7931 | * bitmap_endwrite) set a bit in the bitmap after the |
| 7932 | * recovery has checked that bit and skipped that |
| 7933 | * region. |
| 7934 | */ |
| 7935 | if (mddev->bitmap) { |
| 7936 | mddev->pers->quiesce(mddev, 1); |
| 7937 | mddev->pers->quiesce(mddev, 0); |
| 7938 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7939 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7940 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7941 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7942 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7943 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7944 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7945 | "(but not more than %d KB/sec) for %s.\n", |
| 7946 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7947 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7948 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7949 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7950 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7951 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7952 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7953 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7954 | } |
| 7955 | last_mark = 0; |
| 7956 | mddev->resync_mark = mark[last_mark]; |
| 7957 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7958 | |
| 7959 | /* |
| 7960 | * Tune reconstruction: |
| 7961 | */ |
| 7962 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7963 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7964 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7965 | |
| 7966 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7967 | last_check = 0; |
| 7968 | |
| 7969 | if (j>2) { |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7970 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7971 | "md: resuming %s of %s from checkpoint.\n", |
| 7972 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7973 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7974 | } else |
| 7975 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7976 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7977 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 7978 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7979 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7980 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7981 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7982 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7983 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7984 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7985 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7986 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7987 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7988 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7989 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7990 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7991 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7992 | (j - mddev->curr_resync_completed)*2 |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 7993 | >= mddev->resync_max - mddev->curr_resync_completed || |
| 7994 | mddev->curr_resync_completed > mddev->resync_max |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7995 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7996 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7997 | wait_event(mddev->recovery_wait, |
| 7998 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7999 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8000 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 8001 | j > mddev->recovery_cp) |
| 8002 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8003 | update_time = jiffies; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 8004 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8005 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8006 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8007 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8008 | while (j >= mddev->resync_max && |
| 8009 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8010 | /* As this condition is controlled by user-space, |
| 8011 | * we can block indefinitely, so use '_interruptible' |
| 8012 | * to avoid triggering warnings. |
| 8013 | */ |
| 8014 | flush_signals(current); /* just in case */ |
| 8015 | wait_event_interruptible(mddev->recovery_wait, |
| 8016 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8017 | || test_bit(MD_RECOVERY_INTR, |
| 8018 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8019 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8020 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8021 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8022 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8023 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8024 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8025 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8026 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8027 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8028 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8029 | |
| 8030 | if (!skipped) { /* actual IO requested */ |
| 8031 | io_sectors += sectors; |
| 8032 | atomic_add(sectors, &mddev->recovery_active); |
| 8033 | } |
| 8034 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8035 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8036 | break; |
| 8037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8038 | j += sectors; |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8039 | if (j > max_sectors) |
| 8040 | /* when skipping, extra large numbers can be returned. */ |
| 8041 | j = max_sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8042 | if (j > 2) |
| 8043 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8044 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8045 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8046 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8047 | * visible in /proc/mdstat |
| 8048 | */ |
| 8049 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8050 | |
| 8051 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8052 | continue; |
| 8053 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8054 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8055 | repeat: |
| 8056 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 8057 | /* step marks */ |
| 8058 | int next = (last_mark+1) % SYNC_MARKS; |
| 8059 | |
| 8060 | mddev->resync_mark = mark[next]; |
| 8061 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 8062 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8063 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8064 | last_mark = next; |
| 8065 | } |
| 8066 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8067 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8068 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8069 | |
| 8070 | /* |
| 8071 | * this loop exits only if either when we are slower than |
| 8072 | * the 'hard' speed limit, or the system was IO-idle for |
| 8073 | * a jiffy. |
| 8074 | * the system might be non-idle CPU-wise, but we only care |
| 8075 | * about not overloading the IO subsystem. (things like an |
| 8076 | * e2fsck being done on the RAID array should execute fast) |
| 8077 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8078 | cond_resched(); |
| 8079 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8080 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 8081 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8082 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8083 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 8084 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8085 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8086 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8087 | goto repeat; |
| 8088 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8089 | if (!is_mddev_idle(mddev, 0)) { |
| 8090 | /* |
| 8091 | * Give other IO more of a chance. |
| 8092 | * The faster the devices, the less we wait. |
| 8093 | */ |
| 8094 | wait_event(mddev->recovery_wait, |
| 8095 | !atomic_read(&mddev->recovery_active)); |
| 8096 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8097 | } |
| 8098 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8099 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
| 8100 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 8101 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8102 | /* |
| 8103 | * this also signals 'finished resyncing' to md_stop |
| 8104 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8105 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8106 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 8107 | |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8108 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8109 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8110 | mddev->curr_resync > 2) { |
| 8111 | mddev->curr_resync_completed = mddev->curr_resync; |
| 8112 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 8113 | } |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8114 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8115 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8116 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8117 | mddev->curr_resync > 2) { |
| 8118 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 8119 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8120 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 8121 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8122 | "md: checkpointing %s of %s.\n", |
| 8123 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8124 | if (test_bit(MD_RECOVERY_ERROR, |
| 8125 | &mddev->recovery)) |
| 8126 | mddev->recovery_cp = |
| 8127 | mddev->curr_resync_completed; |
| 8128 | else |
| 8129 | mddev->recovery_cp = |
| 8130 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8131 | } |
| 8132 | } else |
| 8133 | mddev->recovery_cp = MaxSector; |
| 8134 | } else { |
| 8135 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8136 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8137 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8138 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8139 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 8140 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8141 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8142 | !test_bit(Faulty, &rdev->flags) && |
| 8143 | !test_bit(In_sync, &rdev->flags) && |
| 8144 | rdev->recovery_offset < mddev->curr_resync) |
| 8145 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8146 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8148 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 8149 | skip: |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8150 | if (mddev_is_clustered(mddev) && |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 8151 | ret == 0) { |
| 8152 | /* set CHANGE_PENDING here since maybe another |
| 8153 | * update is needed, so other nodes are informed */ |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 8154 | set_mask_bits(&mddev->flags, 0, |
| 8155 | BIT(MD_CHANGE_PENDING) | BIT(MD_CHANGE_DEVS)); |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 8156 | md_wakeup_thread(mddev->thread); |
| 8157 | wait_event(mddev->sb_wait, |
| 8158 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8159 | md_cluster_ops->resync_finish(mddev); |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 8160 | } else |
| 8161 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8162 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8163 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 8164 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8165 | /* We completed so min/max setting can be forgotten if used. */ |
| 8166 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8167 | mddev->resync_min = 0; |
| 8168 | mddev->resync_max = MaxSector; |
| 8169 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8170 | mddev->resync_min = mddev->curr_resync_completed; |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8171 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8172 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8173 | spin_unlock(&mddev->lock); |
| 8174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8175 | wake_up(&resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8176 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 8177 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8178 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8179 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8180 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8181 | static int remove_and_add_spares(struct mddev *mddev, |
| 8182 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8183 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8184 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8185 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8186 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8187 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8188 | rdev_for_each(rdev, mddev) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8189 | if ((this == NULL || rdev == this) && |
| 8190 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8191 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8192 | (test_bit(Faulty, &rdev->flags) || |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8193 | (!test_bit(In_sync, &rdev->flags) && |
| 8194 | !test_bit(Journal, &rdev->flags))) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8195 | atomic_read(&rdev->nr_pending)==0) { |
| 8196 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8197 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 8198 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8199 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8200 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8201 | } |
| 8202 | } |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 8203 | if (removed && mddev->kobj.sd) |
| 8204 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8205 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8206 | if (this && removed) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8207 | goto no_add; |
| 8208 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8209 | rdev_for_each(rdev, mddev) { |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8210 | if (this && this != rdev) |
| 8211 | continue; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 8212 | if (test_bit(Candidate, &rdev->flags)) |
| 8213 | continue; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8214 | if (rdev->raid_disk >= 0 && |
| 8215 | !test_bit(In_sync, &rdev->flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8216 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8217 | !test_bit(Faulty, &rdev->flags)) |
| 8218 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8219 | if (rdev->raid_disk >= 0) |
| 8220 | continue; |
| 8221 | if (test_bit(Faulty, &rdev->flags)) |
| 8222 | continue; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8223 | if (!test_bit(Journal, &rdev->flags)) { |
| 8224 | if (mddev->ro && |
| 8225 | ! (rdev->saved_raid_disk >= 0 && |
| 8226 | !test_bit(Bitmap_sync, &rdev->flags))) |
| 8227 | continue; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8228 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8229 | rdev->recovery_offset = 0; |
| 8230 | } |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8231 | if (mddev->pers-> |
| 8232 | hot_add_disk(mddev, rdev) == 0) { |
| 8233 | if (sysfs_link_rdev(mddev, rdev)) |
| 8234 | /* failure here is OK */; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8235 | if (!test_bit(Journal, &rdev->flags)) |
| 8236 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8237 | md_new_event(mddev); |
| 8238 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8239 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8240 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8241 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 8242 | if (removed) |
| 8243 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8244 | return spares; |
| 8245 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 8246 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8247 | static void md_start_sync(struct work_struct *ws) |
| 8248 | { |
| 8249 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8250 | int ret = 0; |
| 8251 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8252 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 8253 | mddev, |
| 8254 | "resync"); |
| 8255 | if (!mddev->sync_thread) { |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8256 | if (!(mddev_is_clustered(mddev) && ret == -EAGAIN)) |
| 8257 | printk(KERN_ERR "%s: could not start resync" |
| 8258 | " thread...\n", |
| 8259 | mdname(mddev)); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8260 | /* leave the spares where they are, it shouldn't hurt */ |
| 8261 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8262 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8263 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8264 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 8265 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8266 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8267 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8268 | &mddev->recovery)) |
| 8269 | if (mddev->sysfs_action) |
| 8270 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8271 | } else |
| 8272 | md_wakeup_thread(mddev->sync_thread); |
| 8273 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8274 | md_new_event(mddev); |
| 8275 | } |
| 8276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8277 | /* |
| 8278 | * This routine is regularly called by all per-raid-array threads to |
| 8279 | * deal with generic issues like resync and super-block update. |
| 8280 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 8281 | * need this as they never do any recovery or update the superblock. |
| 8282 | * |
| 8283 | * It does not do any resync itself, but rather "forks" off other threads |
| 8284 | * to do that as needed. |
| 8285 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 8286 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8287 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8288 | * and wakeups up this thread which will reap the thread and finish up. |
| 8289 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 8290 | * |
| 8291 | * The overall approach is: |
| 8292 | * 1/ if the superblock needs updating, update it. |
| 8293 | * 2/ If a recovery thread is running, don't do anything else. |
| 8294 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 8295 | * 4/ If there are any faulty devices, remove them. |
| 8296 | * 5/ If array is degraded, try to add spares devices |
| 8297 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 8298 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8299 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8300 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 8301 | if (mddev->suspended) |
| 8302 | return; |
| 8303 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 8304 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 8305 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8306 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8307 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8308 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8309 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 8310 | mdname(mddev)); |
| 8311 | mddev->safemode = 2; |
| 8312 | } |
| 8313 | flush_signals(current); |
| 8314 | } |
| 8315 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8316 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 8317 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8318 | if ( ! ( |
NeilBrown | 142d44c | 2013-11-28 10:34:18 +1100 | [diff] [blame] | 8319 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8320 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8321 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
Guoqing Jiang | 15858fa | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 8322 | test_bit(MD_RELOAD_SB, &mddev->flags) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8323 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8324 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 8325 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8326 | )) |
| 8327 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8328 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 8329 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8330 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8331 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8332 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 8333 | struct md_rdev *rdev; |
| 8334 | if (!mddev->external && mddev->in_sync) |
| 8335 | /* 'Blocked' flag not needed as failed devices |
| 8336 | * will be recorded if array switched to read/write. |
| 8337 | * Leaving it set will prevent the device |
| 8338 | * from being removed. |
| 8339 | */ |
| 8340 | rdev_for_each(rdev, mddev) |
| 8341 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8342 | /* On a read-only array we can: |
| 8343 | * - remove failed devices |
| 8344 | * - add already-in_sync devices if the array itself |
| 8345 | * is in-sync. |
| 8346 | * As we only add devices that are already in-sync, |
| 8347 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8348 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8349 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8350 | /* There is no thread, but we need to call |
| 8351 | * ->spare_active and clear saved_raid_disk |
| 8352 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 8353 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8354 | md_reap_sync_thread(mddev); |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 8355 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8356 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Shaohua Li | d4929ad | 2015-09-18 10:20:12 -0700 | [diff] [blame] | 8357 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8358 | goto unlock; |
| 8359 | } |
| 8360 | |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 8361 | if (mddev_is_clustered(mddev)) { |
| 8362 | struct md_rdev *rdev; |
| 8363 | /* kick the device if another node issued a |
| 8364 | * remove disk. |
| 8365 | */ |
| 8366 | rdev_for_each(rdev, mddev) { |
| 8367 | if (test_and_clear_bit(ClusterRemove, &rdev->flags) && |
| 8368 | rdev->raid_disk < 0) |
| 8369 | md_kick_rdev_from_array(rdev); |
| 8370 | } |
Guoqing Jiang | 15858fa | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 8371 | |
| 8372 | if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags)) |
| 8373 | md_reload_sb(mddev, mddev->good_device_nr); |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 8374 | } |
| 8375 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8376 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8377 | int did_change = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8378 | spin_lock(&mddev->lock); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8379 | if (mddev->safemode && |
| 8380 | !atomic_read(&mddev->writes_pending) && |
| 8381 | !mddev->in_sync && |
| 8382 | mddev->recovery_cp == MaxSector) { |
| 8383 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8384 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 8385 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8386 | } |
| 8387 | if (mddev->safemode == 1) |
| 8388 | mddev->safemode = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8389 | spin_unlock(&mddev->lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8390 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8391 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8392 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8393 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 8394 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 8395 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8397 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 8398 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 8399 | /* resync/recovery still happening */ |
| 8400 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8401 | goto unlock; |
| 8402 | } |
| 8403 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8404 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8405 | goto unlock; |
| 8406 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8407 | /* Set RUNNING before clearing NEEDED to avoid |
| 8408 | * any transients in the value of "sync_action". |
| 8409 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8410 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8411 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8412 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8413 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8414 | /* Clear some bits that don't mean anything, but |
| 8415 | * might be left set |
| 8416 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8417 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 8418 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8419 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 8420 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 8421 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8422 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8423 | /* no recovery is running. |
| 8424 | * remove any failed drives, then |
| 8425 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8426 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8427 | * the personality to fail the re-add. |
| 8428 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8429 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8430 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8431 | if (mddev->pers->check_reshape == NULL || |
| 8432 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8433 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8434 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8435 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8436 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8437 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8438 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8439 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 8440 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8441 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8442 | } else if (mddev->recovery_cp < MaxSector) { |
| 8443 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8444 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8445 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 8446 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8447 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8449 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 8450 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 8451 | /* We are adding a device or devices to an array |
| 8452 | * which has the bitmap stored on all devices. |
| 8453 | * So make sure all bitmap pages get written |
| 8454 | */ |
| 8455 | bitmap_write_all(mddev->bitmap); |
| 8456 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8457 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 8458 | queue_work(md_misc_wq, &mddev->del_work); |
| 8459 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8460 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8461 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8462 | if (!mddev->sync_thread) { |
| 8463 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8464 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8465 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8466 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 8467 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8468 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8469 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8470 | unlock: |
| 8471 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8472 | mddev_unlock(mddev); |
| 8473 | } |
| 8474 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8475 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8476 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8477 | void md_reap_sync_thread(struct mddev *mddev) |
| 8478 | { |
| 8479 | struct md_rdev *rdev; |
| 8480 | |
| 8481 | /* resync has finished, collect result */ |
| 8482 | md_unregister_thread(&mddev->sync_thread); |
| 8483 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8484 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 8485 | /* success...*/ |
| 8486 | /* activate any spares */ |
| 8487 | if (mddev->pers->spare_active(mddev)) { |
| 8488 | sysfs_notify(&mddev->kobj, NULL, |
| 8489 | "degraded"); |
| 8490 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 8491 | } |
| 8492 | } |
| 8493 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8494 | mddev->pers->finish_reshape) |
| 8495 | mddev->pers->finish_reshape(mddev); |
| 8496 | |
| 8497 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8498 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8499 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8500 | if (!mddev->degraded) |
| 8501 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8502 | rdev->saved_raid_disk = -1; |
| 8503 | |
| 8504 | md_update_sb(mddev, 1); |
| 8505 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 8506 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8507 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8508 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8509 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8510 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8511 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8512 | /* flag recovery needed just to double check */ |
| 8513 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8514 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8515 | md_new_event(mddev); |
| 8516 | if (mddev->event_work.func) |
| 8517 | queue_work(md_misc_wq, &mddev->event_work); |
| 8518 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8519 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8520 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8521 | 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] | 8522 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8523 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8524 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8525 | !test_bit(Blocked, &rdev->flags) && |
| 8526 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8527 | msecs_to_jiffies(5000)); |
| 8528 | rdev_dec_pending(rdev, mddev); |
| 8529 | } |
| 8530 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 8531 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8532 | void md_finish_reshape(struct mddev *mddev) |
| 8533 | { |
| 8534 | /* called be personality module when reshape completes. */ |
| 8535 | struct md_rdev *rdev; |
| 8536 | |
| 8537 | rdev_for_each(rdev, mddev) { |
| 8538 | if (rdev->data_offset > rdev->new_data_offset) |
| 8539 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 8540 | else |
| 8541 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 8542 | rdev->data_offset = rdev->new_data_offset; |
| 8543 | } |
| 8544 | } |
| 8545 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8546 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8547 | /* Bad block management */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8548 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8549 | /* Returns 1 on success, 0 on failure */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8550 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8551 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8552 | { |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 8553 | struct mddev *mddev = rdev->mddev; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8554 | int rv; |
| 8555 | if (is_new) |
| 8556 | s += rdev->new_data_offset; |
| 8557 | else |
| 8558 | s += rdev->data_offset; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8559 | rv = badblocks_set(&rdev->badblocks, s, sectors, 0); |
| 8560 | if (rv == 0) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8561 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8562 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 8563 | set_mask_bits(&mddev->flags, 0, |
| 8564 | BIT(MD_CHANGE_CLEAN) | BIT(MD_CHANGE_PENDING)); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8565 | md_wakeup_thread(rdev->mddev->thread); |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8566 | return 1; |
| 8567 | } else |
| 8568 | return 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8569 | } |
| 8570 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8571 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8572 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8573 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8574 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8575 | if (is_new) |
| 8576 | s += rdev->new_data_offset; |
| 8577 | else |
| 8578 | s += rdev->data_offset; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8579 | return badblocks_clear(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8580 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8581 | } |
| 8582 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8583 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8584 | static int md_notify_reboot(struct notifier_block *this, |
| 8585 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8586 | { |
| 8587 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8588 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8589 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8590 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8591 | for_each_mddev(mddev, tmp) { |
| 8592 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8593 | if (mddev->pers) |
| 8594 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 8595 | if (mddev->persistent) |
| 8596 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8597 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8598 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8599 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8600 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8601 | /* |
| 8602 | * certain more exotic SCSI devices are known to be |
| 8603 | * volatile wrt too early system reboots. While the |
| 8604 | * right place to handle this issue is the given |
| 8605 | * driver, we do want to have a safe RAID driver ... |
| 8606 | */ |
| 8607 | if (need_delay) |
| 8608 | mdelay(1000*1); |
| 8609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8610 | return NOTIFY_DONE; |
| 8611 | } |
| 8612 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8613 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8614 | .notifier_call = md_notify_reboot, |
| 8615 | .next = NULL, |
| 8616 | .priority = INT_MAX, /* before any real devices */ |
| 8617 | }; |
| 8618 | |
| 8619 | static void md_geninit(void) |
| 8620 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8621 | 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] | 8622 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8623 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8624 | } |
| 8625 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8626 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8627 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8628 | int ret = -ENOMEM; |
| 8629 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8630 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8631 | if (!md_wq) |
| 8632 | goto err_wq; |
| 8633 | |
| 8634 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8635 | if (!md_misc_wq) |
| 8636 | goto err_misc_wq; |
| 8637 | |
| 8638 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8639 | goto err_md; |
| 8640 | |
| 8641 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8642 | goto err_mdp; |
| 8643 | mdp_major = ret; |
| 8644 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8645 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8646 | md_probe, NULL, NULL); |
| 8647 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8648 | md_probe, NULL, NULL); |
| 8649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8650 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8651 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8652 | |
| 8653 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8654 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8655 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8656 | err_mdp: |
| 8657 | unregister_blkdev(MD_MAJOR, "md"); |
| 8658 | err_md: |
| 8659 | destroy_workqueue(md_misc_wq); |
| 8660 | err_misc_wq: |
| 8661 | destroy_workqueue(md_wq); |
| 8662 | err_wq: |
| 8663 | return ret; |
| 8664 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8665 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8666 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8667 | { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8668 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 8669 | struct md_rdev *rdev2; |
| 8670 | int role, ret; |
| 8671 | char b[BDEVNAME_SIZE]; |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8672 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8673 | /* Check for change of roles in the active devices */ |
| 8674 | rdev_for_each(rdev2, mddev) { |
| 8675 | if (test_bit(Faulty, &rdev2->flags)) |
| 8676 | continue; |
| 8677 | |
| 8678 | /* Check if the roles changed */ |
| 8679 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 8680 | |
| 8681 | if (test_bit(Candidate, &rdev2->flags)) { |
| 8682 | if (role == 0xfffe) { |
| 8683 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); |
| 8684 | md_kick_rdev_from_array(rdev2); |
| 8685 | continue; |
| 8686 | } |
| 8687 | else |
| 8688 | clear_bit(Candidate, &rdev2->flags); |
| 8689 | } |
| 8690 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8691 | if (role != rdev2->raid_disk) { |
| 8692 | /* got activated */ |
| 8693 | if (rdev2->raid_disk == -1 && role != 0xffff) { |
| 8694 | rdev2->saved_raid_disk = role; |
| 8695 | ret = remove_and_add_spares(mddev, rdev2); |
| 8696 | pr_info("Activated spare: %s\n", |
| 8697 | bdevname(rdev2->bdev,b)); |
Guoqing Jiang | a578183 | 2016-05-02 11:33:14 -0400 | [diff] [blame] | 8698 | /* wakeup mddev->thread here, so array could |
| 8699 | * perform resync with the new activated disk */ |
| 8700 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8701 | md_wakeup_thread(mddev->thread); |
| 8702 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8703 | } |
| 8704 | /* device faulty |
| 8705 | * We just want to do the minimum to mark the disk |
| 8706 | * as faulty. The recovery is performed by the |
| 8707 | * one who initiated the error. |
| 8708 | */ |
| 8709 | if ((role == 0xfffe) || (role == 0xfffd)) { |
| 8710 | md_error(mddev, rdev2); |
| 8711 | clear_bit(Blocked, &rdev2->flags); |
| 8712 | } |
| 8713 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8714 | } |
| 8715 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 8716 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) |
| 8717 | update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8718 | |
| 8719 | /* Finally set the event to be up to date */ |
| 8720 | mddev->events = le64_to_cpu(sb->events); |
| 8721 | } |
| 8722 | |
| 8723 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) |
| 8724 | { |
| 8725 | int err; |
| 8726 | struct page *swapout = rdev->sb_page; |
| 8727 | struct mdp_superblock_1 *sb; |
| 8728 | |
| 8729 | /* Store the sb page of the rdev in the swapout temporary |
| 8730 | * variable in case we err in the future |
| 8731 | */ |
| 8732 | rdev->sb_page = NULL; |
| 8733 | alloc_disk_sb(rdev); |
| 8734 | ClearPageUptodate(rdev->sb_page); |
| 8735 | rdev->sb_loaded = 0; |
| 8736 | err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version); |
| 8737 | |
| 8738 | if (err < 0) { |
| 8739 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", |
| 8740 | __func__, __LINE__, rdev->desc_nr, err); |
| 8741 | put_page(rdev->sb_page); |
| 8742 | rdev->sb_page = swapout; |
| 8743 | rdev->sb_loaded = 1; |
| 8744 | return err; |
| 8745 | } |
| 8746 | |
| 8747 | sb = page_address(rdev->sb_page); |
| 8748 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET |
| 8749 | * is not set |
| 8750 | */ |
| 8751 | |
| 8752 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) |
| 8753 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 8754 | |
| 8755 | /* The other node finished recovery, call spare_active to set |
| 8756 | * device In_sync and mddev->degraded |
| 8757 | */ |
| 8758 | if (rdev->recovery_offset == MaxSector && |
| 8759 | !test_bit(In_sync, &rdev->flags) && |
| 8760 | mddev->pers->spare_active(mddev)) |
| 8761 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
| 8762 | |
| 8763 | put_page(swapout); |
| 8764 | return 0; |
| 8765 | } |
| 8766 | |
| 8767 | void md_reload_sb(struct mddev *mddev, int nr) |
| 8768 | { |
| 8769 | struct md_rdev *rdev; |
| 8770 | int err; |
| 8771 | |
| 8772 | /* Find the rdev */ |
| 8773 | rdev_for_each_rcu(rdev, mddev) { |
| 8774 | if (rdev->desc_nr == nr) |
| 8775 | break; |
| 8776 | } |
| 8777 | |
| 8778 | if (!rdev || rdev->desc_nr != nr) { |
| 8779 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); |
| 8780 | return; |
| 8781 | } |
| 8782 | |
| 8783 | err = read_rdev(mddev, rdev); |
| 8784 | if (err < 0) |
| 8785 | return; |
| 8786 | |
| 8787 | check_sb_changes(mddev, rdev); |
| 8788 | |
| 8789 | /* Read all rdev's to update recovery_offset */ |
| 8790 | rdev_for_each_rcu(rdev, mddev) |
| 8791 | read_rdev(mddev, rdev); |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8792 | } |
| 8793 | EXPORT_SYMBOL(md_reload_sb); |
| 8794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8795 | #ifndef MODULE |
| 8796 | |
| 8797 | /* |
| 8798 | * Searches all registered partitions for autorun RAID arrays |
| 8799 | * at boot time. |
| 8800 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8801 | |
| 8802 | static LIST_HEAD(all_detected_devices); |
| 8803 | struct detected_devices_node { |
| 8804 | struct list_head list; |
| 8805 | dev_t dev; |
| 8806 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8807 | |
| 8808 | void md_autodetect_dev(dev_t dev) |
| 8809 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8810 | struct detected_devices_node *node_detected_dev; |
| 8811 | |
| 8812 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8813 | if (node_detected_dev) { |
| 8814 | node_detected_dev->dev = dev; |
| 8815 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8816 | } else { |
| 8817 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8818 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8819 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8820 | } |
| 8821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8822 | static void autostart_arrays(int part) |
| 8823 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8824 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8825 | struct detected_devices_node *node_detected_dev; |
| 8826 | dev_t dev; |
| 8827 | int i_scanned, i_passed; |
| 8828 | |
| 8829 | i_scanned = 0; |
| 8830 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8831 | |
| 8832 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8833 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8834 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8835 | i_scanned++; |
| 8836 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8837 | struct detected_devices_node, list); |
| 8838 | list_del(&node_detected_dev->list); |
| 8839 | dev = node_detected_dev->dev; |
| 8840 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8841 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8842 | if (IS_ERR(rdev)) |
| 8843 | continue; |
| 8844 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8845 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8846 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8847 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8848 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8849 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8850 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8851 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8852 | |
| 8853 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8854 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8855 | |
| 8856 | autorun_devices(part); |
| 8857 | } |
| 8858 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8859 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8860 | |
| 8861 | static __exit void md_exit(void) |
| 8862 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8863 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8864 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8865 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8866 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8867 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8868 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8869 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8870 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8871 | unregister_blkdev(mdp_major, "mdp"); |
| 8872 | unregister_reboot_notifier(&md_notifier); |
| 8873 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8874 | |
| 8875 | /* We cannot unload the modules while some process is |
| 8876 | * waiting for us in select() or poll() - wake them up |
| 8877 | */ |
| 8878 | md_unloading = 1; |
| 8879 | while (waitqueue_active(&md_event_waiters)) { |
| 8880 | /* not safe to leave yet */ |
| 8881 | wake_up(&md_event_waiters); |
| 8882 | msleep(delay); |
| 8883 | delay += delay; |
| 8884 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8885 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8886 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8887 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8888 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8889 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8890 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8891 | destroy_workqueue(md_misc_wq); |
| 8892 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8893 | } |
| 8894 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8895 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8896 | module_exit(md_exit) |
| 8897 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8898 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8899 | { |
| 8900 | return sprintf(buffer, "%d", start_readonly); |
| 8901 | } |
| 8902 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8903 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 8904 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8905 | } |
| 8906 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8907 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8908 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8909 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8911 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8912 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8913 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8914 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |