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); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 287 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 288 | |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 289 | cpu = part_stat_lock(); |
| 290 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 291 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
| 292 | part_stat_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 293 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 294 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 295 | wake_up(&mddev->sb_wait); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 296 | |
| 297 | return BLK_QC_T_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 300 | /* mddev_suspend makes sure no new requests are submitted |
| 301 | * to the device, and that any requests that have been submitted |
| 302 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 303 | * Once mddev_detach() is called and completes, the module will be |
| 304 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 305 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 306 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 307 | { |
Shaohua Li | 70d9798b | 2016-02-24 17:41:01 -0800 | [diff] [blame] | 308 | WARN_ON_ONCE(current == mddev->thread->tsk); |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 309 | if (mddev->suspended++) |
| 310 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 311 | synchronize_rcu(); |
| 312 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 313 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 314 | |
| 315 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 316 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 317 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 318 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 319 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 320 | { |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 321 | if (--mddev->suspended) |
| 322 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 323 | wake_up(&mddev->sb_wait); |
| 324 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 325 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 326 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 327 | md_wakeup_thread(mddev->thread); |
| 328 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 329 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 330 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 331 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 332 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 333 | { |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 334 | struct md_personality *pers = mddev->pers; |
| 335 | int ret = 0; |
| 336 | |
| 337 | rcu_read_lock(); |
| 338 | if (mddev->suspended) |
| 339 | ret = 1; |
| 340 | else if (pers && pers->congested) |
| 341 | ret = pers->congested(mddev, bits); |
| 342 | rcu_read_unlock(); |
| 343 | return ret; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 344 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 345 | EXPORT_SYMBOL_GPL(mddev_congested); |
| 346 | static int md_congested(void *data, int bits) |
| 347 | { |
| 348 | struct mddev *mddev = data; |
| 349 | return mddev_congested(mddev, bits); |
| 350 | } |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 351 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 352 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 353 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 354 | */ |
| 355 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 356 | static void md_end_flush(struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 357 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 358 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 359 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 360 | |
| 361 | rdev_dec_pending(rdev, mddev); |
| 362 | |
| 363 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 364 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 365 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 366 | } |
| 367 | bio_put(bio); |
| 368 | } |
| 369 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 370 | static void md_submit_flush_data(struct work_struct *ws); |
| 371 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 372 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 373 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 374 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 375 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 376 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 377 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 378 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 379 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 380 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 381 | if (rdev->raid_disk >= 0 && |
| 382 | !test_bit(Faulty, &rdev->flags)) { |
| 383 | /* Take two references, one is dropped |
| 384 | * when request finishes, one after |
| 385 | * we reclaim rcu_read_lock |
| 386 | */ |
| 387 | struct bio *bi; |
| 388 | atomic_inc(&rdev->nr_pending); |
| 389 | atomic_inc(&rdev->nr_pending); |
| 390 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 391 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 392 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 393 | bi->bi_private = rdev; |
| 394 | bi->bi_bdev = rdev->bdev; |
| 395 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 396 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 397 | rcu_read_lock(); |
| 398 | rdev_dec_pending(rdev, mddev); |
| 399 | } |
| 400 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 401 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 402 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 403 | } |
| 404 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 405 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 406 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 407 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 408 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 409 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 410 | if (bio->bi_iter.bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 411 | /* an empty barrier - all done */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 412 | bio_endio(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 413 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 414 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 415 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 416 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 417 | |
| 418 | mddev->flush_bio = NULL; |
| 419 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 420 | } |
| 421 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 422 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 423 | { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 424 | spin_lock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 425 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 426 | !mddev->flush_bio, |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 427 | mddev->lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 428 | mddev->flush_bio = bio; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 429 | spin_unlock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 430 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 431 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 432 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 433 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 434 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 435 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 436 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 437 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 438 | struct mddev *mddev = cb->data; |
| 439 | md_wakeup_thread(mddev->thread); |
| 440 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 441 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 442 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 443 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 444 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | { |
| 446 | atomic_inc(&mddev->active); |
| 447 | return mddev; |
| 448 | } |
| 449 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 450 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 451 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 452 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 454 | struct bio_set *bs = NULL; |
| 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 457 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 458 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 459 | mddev->ctime == 0 && !mddev->hold_active) { |
| 460 | /* Array is not configured at all, and not held active, |
| 461 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 462 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 463 | bs = mddev->bio_set; |
| 464 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 465 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 466 | /* We did a probe so need to clean up. Call |
| 467 | * queue_work inside the spinlock so that |
| 468 | * flush_workqueue() after mddev_find will |
| 469 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 470 | */ |
| 471 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 472 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 473 | } else |
| 474 | kfree(mddev); |
| 475 | } |
| 476 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 477 | if (bs) |
| 478 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 481 | static void md_safemode_timeout(unsigned long data); |
| 482 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 483 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 484 | { |
| 485 | mutex_init(&mddev->open_mutex); |
| 486 | mutex_init(&mddev->reconfig_mutex); |
| 487 | mutex_init(&mddev->bitmap_info.mutex); |
| 488 | INIT_LIST_HEAD(&mddev->disks); |
| 489 | INIT_LIST_HEAD(&mddev->all_mddevs); |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 490 | setup_timer(&mddev->safemode_timer, md_safemode_timeout, |
| 491 | (unsigned long) mddev); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 492 | atomic_set(&mddev->active, 1); |
| 493 | atomic_set(&mddev->openers, 0); |
| 494 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 495 | spin_lock_init(&mddev->lock); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 496 | atomic_set(&mddev->flush_pending, 0); |
| 497 | init_waitqueue_head(&mddev->sb_wait); |
| 498 | init_waitqueue_head(&mddev->recovery_wait); |
| 499 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 500 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 501 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 502 | mddev->resync_min = 0; |
| 503 | mddev->resync_max = MaxSector; |
| 504 | mddev->level = LEVEL_NONE; |
| 505 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 506 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 507 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 508 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 510 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 512 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 513 | unit &= ~((1<<MdpMinorShift)-1); |
| 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | retry: |
| 516 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 518 | if (unit) { |
| 519 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 520 | if (mddev->unit == unit) { |
| 521 | mddev_get(mddev); |
| 522 | spin_unlock(&all_mddevs_lock); |
| 523 | kfree(new); |
| 524 | return mddev; |
| 525 | } |
| 526 | |
| 527 | if (new) { |
| 528 | list_add(&new->all_mddevs, &all_mddevs); |
| 529 | spin_unlock(&all_mddevs_lock); |
| 530 | new->hold_active = UNTIL_IOCTL; |
| 531 | return new; |
| 532 | } |
| 533 | } else if (new) { |
| 534 | /* find an unused unit number */ |
| 535 | static int next_minor = 512; |
| 536 | int start = next_minor; |
| 537 | int is_free = 0; |
| 538 | int dev = 0; |
| 539 | while (!is_free) { |
| 540 | dev = MKDEV(MD_MAJOR, next_minor); |
| 541 | next_minor++; |
| 542 | if (next_minor > MINORMASK) |
| 543 | next_minor = 0; |
| 544 | if (next_minor == start) { |
| 545 | /* Oh dear, all in use. */ |
| 546 | spin_unlock(&all_mddevs_lock); |
| 547 | kfree(new); |
| 548 | return NULL; |
| 549 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 550 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 551 | is_free = 1; |
| 552 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 553 | if (mddev->unit == dev) { |
| 554 | is_free = 0; |
| 555 | break; |
| 556 | } |
| 557 | } |
| 558 | new->unit = dev; |
| 559 | new->md_minor = MINOR(dev); |
| 560 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | list_add(&new->all_mddevs, &all_mddevs); |
| 562 | spin_unlock(&all_mddevs_lock); |
| 563 | return new; |
| 564 | } |
| 565 | spin_unlock(&all_mddevs_lock); |
| 566 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 567 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | if (!new) |
| 569 | return NULL; |
| 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | new->unit = unit; |
| 572 | if (MAJOR(unit) == MD_MAJOR) |
| 573 | new->md_minor = MINOR(unit); |
| 574 | else |
| 575 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 576 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 577 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | goto retry; |
| 580 | } |
| 581 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 582 | static struct attribute_group md_redundancy_group; |
| 583 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 584 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 586 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 587 | /* These cannot be removed under reconfig_mutex as |
| 588 | * an access to the files will try to take reconfig_mutex |
| 589 | * while holding the file unremovable, which leads to |
| 590 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 591 | * So hold set sysfs_active while the remove in happeing, |
| 592 | * and anything else which might set ->to_remove or my |
| 593 | * otherwise change the sysfs namespace will fail with |
| 594 | * -EBUSY if sysfs_active is still set. |
| 595 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 596 | * test it under the same mutex to ensure its correct value |
| 597 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 598 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 599 | struct attribute_group *to_remove = mddev->to_remove; |
| 600 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 601 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 602 | mutex_unlock(&mddev->reconfig_mutex); |
| 603 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 604 | if (mddev->kobj.sd) { |
| 605 | if (to_remove != &md_redundancy_group) |
| 606 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 607 | if (mddev->pers == NULL || |
| 608 | mddev->pers->sync_request == NULL) { |
| 609 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 610 | if (mddev->sysfs_action) |
| 611 | sysfs_put(mddev->sysfs_action); |
| 612 | mddev->sysfs_action = NULL; |
| 613 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 614 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 615 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 616 | } else |
| 617 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 619 | /* As we've dropped the mutex we need a spinlock to |
| 620 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 621 | */ |
| 622 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 623 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 624 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 626 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 628 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 629 | { |
| 630 | struct md_rdev *rdev; |
| 631 | |
| 632 | rdev_for_each_rcu(rdev, mddev) |
| 633 | if (rdev->desc_nr == nr) |
| 634 | return rdev; |
| 635 | |
| 636 | return NULL; |
| 637 | } |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 638 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 639 | |
| 640 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 642 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 644 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | if (rdev->bdev->bd_dev == dev) |
| 646 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | return NULL; |
| 649 | } |
| 650 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 651 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 652 | { |
| 653 | struct md_rdev *rdev; |
| 654 | |
| 655 | rdev_for_each_rcu(rdev, mddev) |
| 656 | if (rdev->bdev->bd_dev == dev) |
| 657 | return rdev; |
| 658 | |
| 659 | return NULL; |
| 660 | } |
| 661 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 662 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 663 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 664 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 665 | list_for_each_entry(pers, &pers_list, list) { |
| 666 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 667 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 668 | if (strcmp(pers->name, clevel)==0) |
| 669 | return pers; |
| 670 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 671 | return NULL; |
| 672 | } |
| 673 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 674 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 675 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 677 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 678 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
| 680 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 681 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 684 | if (!rdev->sb_page) { |
| 685 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 686 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 692 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
| 694 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 695 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | rdev->sb_loaded = 0; |
| 697 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 698 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 699 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 701 | if (rdev->bb_page) { |
| 702 | put_page(rdev->bb_page); |
| 703 | rdev->bb_page = NULL; |
| 704 | } |
Dan Williams | d3b407f | 2016-01-06 12:19:22 -0800 | [diff] [blame] | 705 | badblocks_exit(&rdev->badblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 707 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 709 | static void super_written(struct bio *bio) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 710 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 711 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 712 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 713 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 714 | if (bio->bi_error) { |
| 715 | printk("md: super_written gets error=%d\n", bio->bi_error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 716 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 717 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 718 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 719 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 720 | wake_up(&mddev->sb_wait); |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 721 | rdev_dec_pending(rdev, mddev); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 722 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 723 | } |
| 724 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 725 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 726 | sector_t sector, int size, struct page *page) |
| 727 | { |
| 728 | /* write first size bytes of page to sector of rdev |
| 729 | * Increment mddev->pending_writes before returning |
| 730 | * and decrement it on completion, waking up sb_wait |
| 731 | * if zero is reached. |
| 732 | * If an error occurred, call md_error |
| 733 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 734 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 735 | |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 736 | atomic_inc(&rdev->nr_pending); |
| 737 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 738 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 739 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 740 | bio_add_page(bio, page, size, 0); |
| 741 | bio->bi_private = rdev; |
| 742 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 743 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 744 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 745 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 746 | } |
| 747 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 748 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 749 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 750 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 751 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 752 | } |
| 753 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 754 | 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] | 755 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 757 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | int ret; |
| 759 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 760 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 761 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 762 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 763 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 764 | else if (rdev->mddev->reshape_position != MaxSector && |
| 765 | (rdev->mddev->reshape_backwards == |
| 766 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 767 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 768 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 769 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | bio_add_page(bio, page, size, 0); |
Kent Overstreet | c170bbb | 2013-11-24 16:32:22 -0700 | [diff] [blame] | 771 | submit_bio_wait(rw, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 773 | ret = !bio->bi_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | bio_put(bio); |
| 775 | return ret; |
| 776 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 777 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 779 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | { |
| 781 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | if (rdev->sb_loaded) |
| 784 | return 0; |
| 785 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 786 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | goto fail; |
| 788 | rdev->sb_loaded = 1; |
| 789 | return 0; |
| 790 | |
| 791 | fail: |
| 792 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 793 | bdevname(rdev->bdev,b)); |
| 794 | return -EINVAL; |
| 795 | } |
| 796 | |
| 797 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 798 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 799 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 800 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 801 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 802 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 806 | { |
| 807 | int ret; |
| 808 | mdp_super_t *tmp1, *tmp2; |
| 809 | |
| 810 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 811 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 812 | |
| 813 | if (!tmp1 || !tmp2) { |
| 814 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 815 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | goto abort; |
| 817 | } |
| 818 | |
| 819 | *tmp1 = *sb1; |
| 820 | *tmp2 = *sb2; |
| 821 | |
| 822 | /* |
| 823 | * nr_disks is not constant |
| 824 | */ |
| 825 | tmp1->nr_disks = 0; |
| 826 | tmp2->nr_disks = 0; |
| 827 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 828 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 830 | kfree(tmp1); |
| 831 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | return ret; |
| 833 | } |
| 834 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 835 | static u32 md_csum_fold(u32 csum) |
| 836 | { |
| 837 | csum = (csum & 0xffff) + (csum >> 16); |
| 838 | return (csum & 0xffff) + (csum >> 16); |
| 839 | } |
| 840 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 841 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 843 | u64 newcsum = 0; |
| 844 | u32 *sb32 = (u32*)sb; |
| 845 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | unsigned int disk_csum, csum; |
| 847 | |
| 848 | disk_csum = sb->sb_csum; |
| 849 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 850 | |
| 851 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 852 | newcsum += sb32[i]; |
| 853 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 854 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 855 | #ifdef CONFIG_ALPHA |
| 856 | /* This used to use csum_partial, which was wrong for several |
| 857 | * reasons including that different results are returned on |
| 858 | * different architectures. It isn't critical that we get exactly |
| 859 | * the same return value as before (we always csum_fold before |
| 860 | * testing, and that removes any differences). However as we |
| 861 | * know that csum_partial always returned a 16bit value on |
| 862 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 863 | */ |
| 864 | sb->sb_csum = md_csum_fold(disk_csum); |
| 865 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 867 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | return csum; |
| 869 | } |
| 870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | /* |
| 872 | * Handle superblock details. |
| 873 | * We want to be able to handle multiple superblock formats |
| 874 | * so we have a common interface to them all, and an array of |
| 875 | * different handlers. |
| 876 | * We rely on user-space to write the initial superblock, and support |
| 877 | * reading and updating of superblocks. |
| 878 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 879 | * 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] | 880 | * loads and validates a superblock on dev. |
| 881 | * if refdev != NULL, compare superblocks on both devices |
| 882 | * Return: |
| 883 | * 0 - dev has a superblock that is compatible with refdev |
| 884 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 885 | * so dev should be used as the refdev in future |
| 886 | * -EINVAL superblock incompatible or invalid |
| 887 | * -othererror e.g. -EIO |
| 888 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 889 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | * Verify that dev is acceptable into mddev. |
| 891 | * The first time, mddev->raid_disks will be 0, and data from |
| 892 | * dev should be merged in. Subsequent calls check that dev |
| 893 | * is new enough. Return 0 or -EINVAL |
| 894 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 895 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | * Update the superblock for rdev with data in mddev |
| 897 | * This does not write to disc. |
| 898 | * |
| 899 | */ |
| 900 | |
| 901 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 902 | char *name; |
| 903 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 904 | int (*load_super)(struct md_rdev *rdev, |
| 905 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 906 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 907 | int (*validate_super)(struct mddev *mddev, |
| 908 | struct md_rdev *rdev); |
| 909 | void (*sync_super)(struct mddev *mddev, |
| 910 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 911 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 912 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 913 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 914 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | }; |
| 916 | |
| 917 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 918 | * Check that the given mddev has no bitmap. |
| 919 | * |
| 920 | * This function is called from the run method of all personalities that do not |
| 921 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 922 | * has a bitmap. Otherwise, it returns 0. |
| 923 | * |
| 924 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 925 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 926 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 927 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 928 | return 0; |
| 929 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 930 | mdname(mddev), mddev->pers->name); |
| 931 | return 1; |
| 932 | } |
| 933 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 934 | |
| 935 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 936 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 938 | 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] | 939 | { |
| 940 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 941 | mdp_super_t *sb; |
| 942 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | |
| 944 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 945 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | * it's at the end of the disk. |
| 947 | * |
| 948 | * It also happens to be a multiple of 4Kb. |
| 949 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 950 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 952 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | if (ret) return ret; |
| 954 | |
| 955 | ret = -EINVAL; |
| 956 | |
| 957 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 958 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | |
| 960 | if (sb->md_magic != MD_SB_MAGIC) { |
| 961 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 962 | b); |
| 963 | goto abort; |
| 964 | } |
| 965 | |
| 966 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 967 | sb->minor_version < 90 || |
| 968 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 970 | sb->major_version, sb->minor_version, |
| 971 | b); |
| 972 | goto abort; |
| 973 | } |
| 974 | |
| 975 | if (sb->raid_disks <= 0) |
| 976 | goto abort; |
| 977 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 978 | 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] | 979 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 980 | b); |
| 981 | goto abort; |
| 982 | } |
| 983 | |
| 984 | rdev->preferred_minor = sb->md_minor; |
| 985 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 986 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 987 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 988 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
| 990 | if (sb->level == LEVEL_MULTIPATH) |
| 991 | rdev->desc_nr = -1; |
| 992 | else |
| 993 | rdev->desc_nr = sb->this_disk.number; |
| 994 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 995 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 997 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 999 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | if (!uuid_equal(refsb, sb)) { |
| 1001 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1002 | b, bdevname(refdev->bdev,b2)); |
| 1003 | goto abort; |
| 1004 | } |
| 1005 | if (!sb_equal(refsb, sb)) { |
| 1006 | printk(KERN_WARNING "md: %s has same UUID" |
| 1007 | " but different superblock to %s\n", |
| 1008 | b, bdevname(refdev->bdev, b2)); |
| 1009 | goto abort; |
| 1010 | } |
| 1011 | ev1 = md_event(sb); |
| 1012 | ev2 = md_event(refsb); |
| 1013 | if (ev1 > ev2) |
| 1014 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1015 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | ret = 0; |
| 1017 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1018 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1019 | /* Limit to 4TB as metadata cannot record more than that. |
| 1020 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1021 | * record this size) |
| 1022 | */ |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1023 | if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) && |
| 1024 | sb->level >= 1) |
| 1025 | rdev->sectors = (sector_t)(2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1027 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1028 | /* "this cannot possibly happen" ... */ |
| 1029 | ret = -EINVAL; |
| 1030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | abort: |
| 1032 | return ret; |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * validate_super for 0.90.0 |
| 1037 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1038 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | { |
| 1040 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1041 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1042 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1044 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1045 | clear_bit(Faulty, &rdev->flags); |
| 1046 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1047 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1048 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | if (mddev->raid_disks == 0) { |
| 1051 | mddev->major_version = 0; |
| 1052 | mddev->minor_version = sb->minor_version; |
| 1053 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1054 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1055 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | mddev->ctime = sb->ctime; |
| 1057 | mddev->utime = sb->utime; |
| 1058 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1059 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | mddev->layout = sb->layout; |
| 1061 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1062 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1063 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1064 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1065 | mddev->bitmap_info.space = 0; |
| 1066 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1067 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1068 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1069 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1071 | if (mddev->minor_version >= 91) { |
| 1072 | mddev->reshape_position = sb->reshape_position; |
| 1073 | mddev->delta_disks = sb->delta_disks; |
| 1074 | mddev->new_level = sb->new_level; |
| 1075 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1076 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1077 | if (mddev->delta_disks < 0) |
| 1078 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1079 | } else { |
| 1080 | mddev->reshape_position = MaxSector; |
| 1081 | mddev->delta_disks = 0; |
| 1082 | mddev->new_level = mddev->level; |
| 1083 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1084 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1088 | mddev->recovery_cp = MaxSector; |
| 1089 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1090 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | sb->events_lo == sb->cp_events_lo) { |
| 1092 | mddev->recovery_cp = sb->recovery_cp; |
| 1093 | } else |
| 1094 | mddev->recovery_cp = 0; |
| 1095 | } |
| 1096 | |
| 1097 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1098 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1099 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1100 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1101 | |
| 1102 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1103 | |
| 1104 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1105 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1106 | mddev->bitmap_info.offset = |
| 1107 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1108 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1109 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1110 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1111 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1112 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1113 | /* Insist on good event counter while assembling, except |
| 1114 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1116 | if (sb->disks[rdev->desc_nr].state & ( |
| 1117 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1118 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1119 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1120 | } else if (mddev->bitmap) { |
| 1121 | /* if adding to array with a bitmap, then we can accept an |
| 1122 | * older device ... but not too old. |
| 1123 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1124 | if (ev1 < mddev->bitmap->events_cleared) |
| 1125 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1126 | if (ev1 < mddev->events) |
| 1127 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1128 | } else { |
| 1129 | if (ev1 < mddev->events) |
| 1130 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1131 | return 0; |
| 1132 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | desc = sb->disks + rdev->desc_nr; |
| 1136 | |
| 1137 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1138 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1139 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1140 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1141 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1143 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1144 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1145 | /* active but not in sync implies recovery up to |
| 1146 | * reshape position. We don't know exactly where |
| 1147 | * that is, so set to zero for now */ |
| 1148 | if (mddev->minor_version >= 91) { |
| 1149 | rdev->recovery_offset = 0; |
| 1150 | rdev->raid_disk = desc->raid_disk; |
| 1151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1153 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1154 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1155 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1156 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | return 0; |
| 1158 | } |
| 1159 | |
| 1160 | /* |
| 1161 | * sync_super for 0.90.0 |
| 1162 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1163 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | { |
| 1165 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1166 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | /* make rdev->sb match mddev data.. |
| 1170 | * |
| 1171 | * 1/ zero out disks |
| 1172 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1173 | * 3/ any empty disks < next_spare become removed |
| 1174 | * |
| 1175 | * disks[0] gets initialised to REMOVED because |
| 1176 | * we cannot be sure from other fields if it has |
| 1177 | * been initialised or not. |
| 1178 | */ |
| 1179 | int i; |
| 1180 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1181 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1182 | rdev->sb_size = MD_SB_BYTES; |
| 1183 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1184 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | |
| 1186 | memset(sb, 0, sizeof(*sb)); |
| 1187 | |
| 1188 | sb->md_magic = MD_SB_MAGIC; |
| 1189 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | sb->patch_version = mddev->patch_version; |
| 1191 | sb->gvalid_words = 0; /* ignored */ |
| 1192 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1193 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1194 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1195 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1196 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1197 | sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1199 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | sb->raid_disks = mddev->raid_disks; |
| 1201 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1202 | sb->not_persistent = 0; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1203 | sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | sb->state = 0; |
| 1205 | sb->events_hi = (mddev->events>>32); |
| 1206 | sb->events_lo = (u32)mddev->events; |
| 1207 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1208 | if (mddev->reshape_position == MaxSector) |
| 1209 | sb->minor_version = 90; |
| 1210 | else { |
| 1211 | sb->minor_version = 91; |
| 1212 | sb->reshape_position = mddev->reshape_position; |
| 1213 | sb->new_level = mddev->new_level; |
| 1214 | sb->delta_disks = mddev->delta_disks; |
| 1215 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1216 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1217 | } |
| 1218 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | if (mddev->in_sync) |
| 1220 | { |
| 1221 | sb->recovery_cp = mddev->recovery_cp; |
| 1222 | sb->cp_events_hi = (mddev->events>>32); |
| 1223 | sb->cp_events_lo = (u32)mddev->events; |
| 1224 | if (mddev->recovery_cp == MaxSector) |
| 1225 | sb->state = (1<< MD_SB_CLEAN); |
| 1226 | } else |
| 1227 | sb->recovery_cp = 0; |
| 1228 | |
| 1229 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1230 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1232 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1233 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1236 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1238 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1239 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1240 | |
| 1241 | if (rdev2->raid_disk >= 0 && |
| 1242 | sb->minor_version >= 91) |
| 1243 | /* we have nowhere to store the recovery_offset, |
| 1244 | * but if it is not below the reshape_position, |
| 1245 | * we can piggy-back on that. |
| 1246 | */ |
| 1247 | is_active = 1; |
| 1248 | if (rdev2->raid_disk < 0 || |
| 1249 | test_bit(Faulty, &rdev2->flags)) |
| 1250 | is_active = 0; |
| 1251 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1252 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1254 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1255 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | d = &sb->disks[rdev2->desc_nr]; |
| 1257 | nr_disks++; |
| 1258 | d->number = rdev2->desc_nr; |
| 1259 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1260 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1261 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | d->raid_disk = rdev2->raid_disk; |
| 1263 | else |
| 1264 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1265 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1267 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1269 | if (test_bit(In_sync, &rdev2->flags)) |
| 1270 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | active++; |
| 1272 | working++; |
| 1273 | } else { |
| 1274 | d->state = 0; |
| 1275 | spare++; |
| 1276 | working++; |
| 1277 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1278 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1279 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1282 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1283 | mdp_disk_t *d = &sb->disks[i]; |
| 1284 | if (d->state == 0 && d->number == 0) { |
| 1285 | d->number = i; |
| 1286 | d->raid_disk = i; |
| 1287 | d->state = (1<<MD_DISK_REMOVED); |
| 1288 | d->state |= (1<<MD_DISK_FAULTY); |
| 1289 | failed++; |
| 1290 | } |
| 1291 | } |
| 1292 | sb->nr_disks = nr_disks; |
| 1293 | sb->active_disks = active; |
| 1294 | sb->working_disks = working; |
| 1295 | sb->failed_disks = failed; |
| 1296 | sb->spare_disks = spare; |
| 1297 | |
| 1298 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1299 | sb->sb_csum = calc_sb_csum(sb); |
| 1300 | } |
| 1301 | |
| 1302 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1303 | * rdev_size_change for 0.90.0 |
| 1304 | */ |
| 1305 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1306 | 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] | 1307 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1308 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1309 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1310 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1311 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1312 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1313 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1314 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1315 | /* Limit to 4TB as metadata cannot record more than that. |
| 1316 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1317 | */ |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1318 | if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) && |
| 1319 | rdev->mddev->level >= 1) |
| 1320 | num_sectors = (sector_t)(2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1321 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1322 | rdev->sb_page); |
| 1323 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1324 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1325 | } |
| 1326 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1327 | static int |
| 1328 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1329 | { |
| 1330 | /* non-zero offset changes not possible with v0.90 */ |
| 1331 | return new_offset == 0; |
| 1332 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1333 | |
| 1334 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | * version 1 superblock |
| 1336 | */ |
| 1337 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1338 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1340 | __le32 disk_csum; |
| 1341 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | unsigned long long newcsum; |
| 1343 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1344 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | |
| 1346 | disk_csum = sb->sb_csum; |
| 1347 | sb->sb_csum = 0; |
| 1348 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1349 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | newcsum += le32_to_cpu(*isuper++); |
| 1351 | |
| 1352 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1353 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | |
| 1355 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1356 | sb->sb_csum = disk_csum; |
| 1357 | return cpu_to_le32(csum); |
| 1358 | } |
| 1359 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1360 | 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] | 1361 | { |
| 1362 | struct mdp_superblock_1 *sb; |
| 1363 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1364 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1365 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1367 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | |
| 1369 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1370 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | * It is always aligned to a 4K boundary and |
| 1372 | * depeding on minor_version, it can be: |
| 1373 | * 0: At least 8K, but less than 12K, from end of device |
| 1374 | * 1: At start of device |
| 1375 | * 2: 4K from start of device. |
| 1376 | */ |
| 1377 | switch(minor_version) { |
| 1378 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1379 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1380 | sb_start -= 8*2; |
| 1381 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | break; |
| 1383 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1384 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | break; |
| 1386 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1387 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | break; |
| 1389 | default: |
| 1390 | return -EINVAL; |
| 1391 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1392 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1394 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1395 | * and it is safe to read 4k, so we do that |
| 1396 | */ |
| 1397 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | if (ret) return ret; |
| 1399 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1400 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
| 1402 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1403 | sb->major_version != cpu_to_le32(1) || |
| 1404 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1405 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1406 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | return -EINVAL; |
| 1408 | |
| 1409 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1410 | printk("md: invalid superblock checksum on %s\n", |
| 1411 | bdevname(rdev->bdev,b)); |
| 1412 | return -EINVAL; |
| 1413 | } |
| 1414 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1415 | printk("md: data_size too small on %s\n", |
| 1416 | bdevname(rdev->bdev,b)); |
| 1417 | return -EINVAL; |
| 1418 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1419 | if (sb->pad0 || |
| 1420 | sb->pad3[0] || |
| 1421 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1422 | /* Some padding is non-zero, might be a new feature */ |
| 1423 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | rdev->preferred_minor = 0xffff; |
| 1426 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1427 | rdev->new_data_offset = rdev->data_offset; |
| 1428 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1429 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1430 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1431 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1433 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1434 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1435 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1436 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1437 | |
| 1438 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1439 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1440 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1441 | if (minor_version |
| 1442 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1443 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1444 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1445 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1446 | rdev->desc_nr = -1; |
| 1447 | else |
| 1448 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1449 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1450 | if (!rdev->bb_page) { |
| 1451 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1452 | if (!rdev->bb_page) |
| 1453 | return -ENOMEM; |
| 1454 | } |
| 1455 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1456 | rdev->badblocks.count == 0) { |
| 1457 | /* need to load the bad block list. |
| 1458 | * Currently we limit it to one page. |
| 1459 | */ |
| 1460 | s32 offset; |
| 1461 | sector_t bb_sector; |
| 1462 | u64 *bbp; |
| 1463 | int i; |
| 1464 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1465 | if (sectors > (PAGE_SIZE / 512)) |
| 1466 | return -EINVAL; |
| 1467 | offset = le32_to_cpu(sb->bblog_offset); |
| 1468 | if (offset == 0) |
| 1469 | return -EINVAL; |
| 1470 | bb_sector = (long long)offset; |
| 1471 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1472 | rdev->bb_page, READ, true)) |
| 1473 | return -EIO; |
| 1474 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1475 | rdev->badblocks.shift = sb->bblog_shift; |
| 1476 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1477 | u64 bb = le64_to_cpu(*bbp); |
| 1478 | int count = bb & (0x3ff); |
| 1479 | u64 sector = bb >> 10; |
| 1480 | sector <<= sb->bblog_shift; |
| 1481 | count <<= sb->bblog_shift; |
| 1482 | if (bb + 1 == 0) |
| 1483 | break; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 1484 | if (badblocks_set(&rdev->badblocks, sector, count, 1)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1485 | return -EINVAL; |
| 1486 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1487 | } else if (sb->bblog_offset != 0) |
| 1488 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1489 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1490 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1491 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1492 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1494 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | |
| 1496 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1497 | sb->level != refsb->level || |
| 1498 | sb->layout != refsb->layout || |
| 1499 | sb->chunksize != refsb->chunksize) { |
| 1500 | printk(KERN_WARNING "md: %s has strangely different" |
| 1501 | " superblock to %s\n", |
| 1502 | bdevname(rdev->bdev,b), |
| 1503 | bdevname(refdev->bdev,b2)); |
| 1504 | return -EINVAL; |
| 1505 | } |
| 1506 | ev1 = le64_to_cpu(sb->events); |
| 1507 | ev2 = le64_to_cpu(refsb->events); |
| 1508 | |
| 1509 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1510 | ret = 1; |
| 1511 | else |
| 1512 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1514 | if (minor_version) { |
| 1515 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1516 | sectors -= rdev->data_offset; |
| 1517 | } else |
| 1518 | sectors = rdev->sb_start; |
| 1519 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1521 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1522 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | } |
| 1524 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1525 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1527 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1528 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1530 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1531 | clear_bit(Faulty, &rdev->flags); |
| 1532 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1533 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1534 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | if (mddev->raid_disks == 0) { |
| 1537 | mddev->major_version = 1; |
| 1538 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1539 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1540 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1541 | mddev->ctime = le64_to_cpu(sb->ctime); |
| 1542 | mddev->utime = le64_to_cpu(sb->utime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1544 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | mddev->layout = le32_to_cpu(sb->layout); |
| 1546 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1547 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1548 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1549 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1550 | mddev->bitmap_info.space = 0; |
| 1551 | /* Default location for bitmap is 1K after superblock |
| 1552 | * using 3K - total of 4K |
| 1553 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1554 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1555 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1556 | mddev->reshape_backwards = 0; |
| 1557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1559 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1560 | |
| 1561 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1562 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1563 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1564 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1565 | mddev->bitmap_info.offset = |
| 1566 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1567 | /* Metadata doesn't record how much space is available. |
| 1568 | * For 1.0, we assume we can use up to the superblock |
| 1569 | * if before, else to 4K beyond superblock. |
| 1570 | * For others, assume no change is possible. |
| 1571 | */ |
| 1572 | if (mddev->minor_version > 0) |
| 1573 | mddev->bitmap_info.space = 0; |
| 1574 | else if (mddev->bitmap_info.offset > 0) |
| 1575 | mddev->bitmap_info.space = |
| 1576 | 8 - mddev->bitmap_info.offset; |
| 1577 | else |
| 1578 | mddev->bitmap_info.space = |
| 1579 | -mddev->bitmap_info.offset; |
| 1580 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1581 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1582 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1583 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1584 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1585 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1586 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1587 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1588 | if (mddev->delta_disks < 0 || |
| 1589 | (mddev->delta_disks == 0 && |
| 1590 | (le32_to_cpu(sb->feature_map) |
| 1591 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1592 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1593 | } else { |
| 1594 | mddev->reshape_position = MaxSector; |
| 1595 | mddev->delta_disks = 0; |
| 1596 | mddev->new_level = mddev->level; |
| 1597 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1598 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1599 | } |
| 1600 | |
Shaohua Li | a62ab49 | 2016-01-06 14:37:13 -0800 | [diff] [blame] | 1601 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) { |
| 1602 | set_bit(MD_HAS_JOURNAL, &mddev->flags); |
| 1603 | if (mddev->recovery_cp == MaxSector) |
| 1604 | set_bit(MD_JOURNAL_CLEAN, &mddev->flags); |
| 1605 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1606 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1607 | /* Insist of good event counter while assembling, except for |
| 1608 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1610 | if (rdev->desc_nr >= 0 && |
| 1611 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
Song Liu | a3dfbda | 2015-10-08 21:54:11 -0700 | [diff] [blame] | 1612 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1613 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1614 | if (ev1 < mddev->events) |
| 1615 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1616 | } else if (mddev->bitmap) { |
| 1617 | /* If adding to array with a bitmap, then we can accept an |
| 1618 | * older device, but not too old. |
| 1619 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1620 | if (ev1 < mddev->bitmap->events_cleared) |
| 1621 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1622 | if (ev1 < mddev->events) |
| 1623 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1624 | } else { |
| 1625 | if (ev1 < mddev->events) |
| 1626 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1627 | return 0; |
| 1628 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1630 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1631 | if (rdev->desc_nr < 0 || |
| 1632 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1633 | role = MD_DISK_ROLE_SPARE; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1634 | rdev->desc_nr = -1; |
| 1635 | } else |
| 1636 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | switch(role) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1638 | case MD_DISK_ROLE_SPARE: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | break; |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1640 | case MD_DISK_ROLE_FAULTY: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1641 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | break; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1643 | case MD_DISK_ROLE_JOURNAL: /* journal device */ |
| 1644 | if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) { |
| 1645 | /* journal device without journal feature */ |
| 1646 | printk(KERN_WARNING |
| 1647 | "md: journal device provided without journal feature, ignoring the device\n"); |
| 1648 | return -EINVAL; |
| 1649 | } |
| 1650 | set_bit(Journal, &rdev->flags); |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1651 | rdev->journal_tail = le64_to_cpu(sb->journal_tail); |
Shaohua Li | 9b15603 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 1652 | rdev->raid_disk = 0; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1653 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1655 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1656 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1657 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1658 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1659 | if (!(le32_to_cpu(sb->feature_map) & |
| 1660 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1661 | rdev->saved_raid_disk = -1; |
| 1662 | } else |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1663 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | rdev->raid_disk = role; |
| 1665 | break; |
| 1666 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1667 | if (sb->devflags & WriteMostly1) |
| 1668 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1669 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1670 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1671 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1672 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | return 0; |
| 1675 | } |
| 1676 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1677 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | { |
| 1679 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1680 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | int max_dev, i; |
| 1682 | /* make rdev->sb match mddev and rdev data. */ |
| 1683 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1684 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | |
| 1686 | sb->feature_map = 0; |
| 1687 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1688 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1690 | |
| 1691 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1692 | sb->events = cpu_to_le64(mddev->events); |
| 1693 | if (mddev->in_sync) |
| 1694 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
Shaohua Li | bd18f64 | 2015-09-02 13:49:50 -0700 | [diff] [blame] | 1695 | else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags)) |
| 1696 | sb->resync_offset = cpu_to_le64(MaxSector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | else |
| 1698 | sb->resync_offset = cpu_to_le64(0); |
| 1699 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1700 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1701 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1702 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1703 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1704 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1705 | sb->level = cpu_to_le32(mddev->level); |
| 1706 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1707 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1708 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1709 | sb->devflags |= WriteMostly1; |
| 1710 | else |
| 1711 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1712 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1713 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1714 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1715 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1716 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1717 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1718 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1719 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 1720 | if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1721 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1722 | sb->feature_map |= |
| 1723 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1724 | sb->recovery_offset = |
| 1725 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1726 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 1727 | sb->feature_map |= |
| 1728 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1729 | } |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1730 | /* Note: recovery_offset and journal_tail share space */ |
| 1731 | if (test_bit(Journal, &rdev->flags)) |
| 1732 | sb->journal_tail = cpu_to_le64(rdev->journal_tail); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1733 | if (test_bit(Replacement, &rdev->flags)) |
| 1734 | sb->feature_map |= |
| 1735 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1736 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1737 | if (mddev->reshape_position != MaxSector) { |
| 1738 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1739 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1740 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1741 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1742 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1743 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1744 | if (mddev->delta_disks == 0 && |
| 1745 | mddev->reshape_backwards) |
| 1746 | sb->feature_map |
| 1747 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1748 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1749 | sb->feature_map |
| 1750 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1751 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1752 | - rdev->data_offset)); |
| 1753 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1754 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1755 | |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 1756 | if (mddev_is_clustered(mddev)) |
| 1757 | sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED); |
| 1758 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1759 | if (rdev->badblocks.count == 0) |
| 1760 | /* Nothing to do for bad blocks*/ ; |
| 1761 | else if (sb->bblog_offset == 0) |
| 1762 | /* Cannot record bad blocks on this device */ |
| 1763 | md_error(mddev, rdev); |
| 1764 | else { |
| 1765 | struct badblocks *bb = &rdev->badblocks; |
| 1766 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1767 | u64 *p = bb->page; |
| 1768 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1769 | if (bb->changed) { |
| 1770 | unsigned seq; |
| 1771 | |
| 1772 | retry: |
| 1773 | seq = read_seqbegin(&bb->lock); |
| 1774 | |
| 1775 | memset(bbp, 0xff, PAGE_SIZE); |
| 1776 | |
| 1777 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1778 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1779 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1780 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1781 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1782 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1783 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1784 | if (read_seqretry(&bb->lock, seq)) |
| 1785 | goto retry; |
| 1786 | |
| 1787 | bb->sector = (rdev->sb_start + |
| 1788 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1789 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1790 | } |
| 1791 | } |
| 1792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1794 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | if (rdev2->desc_nr+1 > max_dev) |
| 1796 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1797 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1798 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1799 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1800 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1801 | rdev->sb_size = max_dev * 2 + 256; |
| 1802 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1803 | if (rdev->sb_size & bmask) |
| 1804 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1805 | } else |
| 1806 | max_dev = le32_to_cpu(sb->max_dev); |
| 1807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | for (i=0; i<max_dev;i++) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1809 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1810 | |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1811 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) |
| 1812 | sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1814 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1816 | if (test_bit(Faulty, &rdev2->flags)) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1817 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1818 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1820 | else if (test_bit(Journal, &rdev2->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1821 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1822 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1823 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | else |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1825 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1829 | } |
| 1830 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1831 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1832 | 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] | 1833 | { |
| 1834 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1835 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1836 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1837 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1838 | if (rdev->data_offset != rdev->new_data_offset) |
| 1839 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1840 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1841 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1842 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1843 | max_sectors -= rdev->data_offset; |
| 1844 | if (!num_sectors || num_sectors > max_sectors) |
| 1845 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1846 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1847 | /* minor version 0 with bitmap we can't move */ |
| 1848 | return 0; |
| 1849 | } else { |
| 1850 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1851 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1852 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1853 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1854 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1855 | if (!num_sectors || num_sectors > max_sectors) |
| 1856 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1857 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1858 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1859 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1860 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1861 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1862 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1863 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1864 | rdev->sb_page); |
| 1865 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1866 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1867 | |
| 1868 | } |
| 1869 | |
| 1870 | static int |
| 1871 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1872 | unsigned long long new_offset) |
| 1873 | { |
| 1874 | /* All necessary checks on new >= old have been done */ |
| 1875 | struct bitmap *bitmap; |
| 1876 | if (new_offset >= rdev->data_offset) |
| 1877 | return 1; |
| 1878 | |
| 1879 | /* with 1.0 metadata, there is no metadata to tread on |
| 1880 | * so we can always move back */ |
| 1881 | if (rdev->mddev->minor_version == 0) |
| 1882 | return 1; |
| 1883 | |
| 1884 | /* otherwise we must be sure not to step on |
| 1885 | * any metadata, so stay: |
| 1886 | * 36K beyond start of superblock |
| 1887 | * beyond end of badblocks |
| 1888 | * beyond write-intent bitmap |
| 1889 | */ |
| 1890 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1891 | return 0; |
| 1892 | bitmap = rdev->mddev->bitmap; |
| 1893 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1894 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1895 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1896 | return 0; |
| 1897 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1898 | return 0; |
| 1899 | |
| 1900 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1901 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1903 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | [0] = { |
| 1905 | .name = "0.90.0", |
| 1906 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1907 | .load_super = super_90_load, |
| 1908 | .validate_super = super_90_validate, |
| 1909 | .sync_super = super_90_sync, |
| 1910 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1911 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | }, |
| 1913 | [1] = { |
| 1914 | .name = "md-1", |
| 1915 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1916 | .load_super = super_1_load, |
| 1917 | .validate_super = super_1_validate, |
| 1918 | .sync_super = super_1_sync, |
| 1919 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1920 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | }, |
| 1922 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1924 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1925 | { |
| 1926 | if (mddev->sync_super) { |
| 1927 | mddev->sync_super(mddev, rdev); |
| 1928 | return; |
| 1929 | } |
| 1930 | |
| 1931 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1932 | |
| 1933 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1934 | } |
| 1935 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1936 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1938 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1940 | rcu_read_lock(); |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 1941 | rdev_for_each_rcu(rdev, mddev1) { |
| 1942 | if (test_bit(Faulty, &rdev->flags) || |
| 1943 | test_bit(Journal, &rdev->flags) || |
| 1944 | rdev->raid_disk == -1) |
| 1945 | continue; |
| 1946 | rdev_for_each_rcu(rdev2, mddev2) { |
| 1947 | if (test_bit(Faulty, &rdev2->flags) || |
| 1948 | test_bit(Journal, &rdev2->flags) || |
| 1949 | rdev2->raid_disk == -1) |
| 1950 | continue; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1951 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1952 | rdev2->bdev->bd_contains) { |
| 1953 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1954 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1955 | } |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 1956 | } |
| 1957 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1958 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | return 0; |
| 1960 | } |
| 1961 | |
| 1962 | static LIST_HEAD(pending_raid_disks); |
| 1963 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1964 | /* |
| 1965 | * Try to register data integrity profile for an mddev |
| 1966 | * |
| 1967 | * This is called when an array is started and after a disk has been kicked |
| 1968 | * from the array. It only succeeds if all working and active component devices |
| 1969 | * are integrity capable with matching profiles. |
| 1970 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1971 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1972 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1973 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1974 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1975 | if (list_empty(&mddev->disks)) |
| 1976 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1977 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1978 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1979 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1980 | /* skip spares and non-functional disks */ |
| 1981 | if (test_bit(Faulty, &rdev->flags)) |
| 1982 | continue; |
| 1983 | if (rdev->raid_disk < 0) |
| 1984 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1985 | if (!reference) { |
| 1986 | /* Use the first rdev as the reference */ |
| 1987 | reference = rdev; |
| 1988 | continue; |
| 1989 | } |
| 1990 | /* does this rdev's profile match the reference profile? */ |
| 1991 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1992 | rdev->bdev->bd_disk) < 0) |
| 1993 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1994 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1995 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1996 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1997 | /* |
| 1998 | * All component devices are integrity capable and have matching |
| 1999 | * profiles, register the common profile for the md device. |
| 2000 | */ |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 2001 | blk_integrity_register(mddev->gendisk, |
| 2002 | bdev_get_integrity(reference->bdev)); |
| 2003 | |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2004 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 2005 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 2006 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 2007 | mdname(mddev)); |
| 2008 | return -EINVAL; |
| 2009 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2010 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2011 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2012 | EXPORT_SYMBOL(md_integrity_register); |
| 2013 | |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2014 | /* |
| 2015 | * Attempt to add an rdev, but only if it is consistent with the current |
| 2016 | * integrity profile |
| 2017 | */ |
| 2018 | int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2019 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2020 | struct blk_integrity *bi_rdev; |
| 2021 | struct blk_integrity *bi_mddev; |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2022 | char name[BDEVNAME_SIZE]; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2023 | |
| 2024 | if (!mddev->gendisk) |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2025 | return 0; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2026 | |
| 2027 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2028 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2029 | |
| 2030 | if (!bi_mddev) /* nothing to do */ |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2031 | return 0; |
| 2032 | |
| 2033 | if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) { |
| 2034 | printk(KERN_NOTICE "%s: incompatible integrity profile for %s\n", |
| 2035 | mdname(mddev), bdevname(rdev->bdev, name)); |
| 2036 | return -ENXIO; |
| 2037 | } |
| 2038 | |
| 2039 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2040 | } |
| 2041 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2042 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2043 | 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] | 2044 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2045 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2046 | struct kobject *ko; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2047 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2049 | /* prevent duplicates */ |
| 2050 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2051 | return -EEXIST; |
| 2052 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2053 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2054 | if (!test_bit(Journal, &rdev->flags) && |
| 2055 | rdev->sectors && |
| 2056 | (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2057 | if (mddev->pers) { |
| 2058 | /* Cannot change size, so fail |
| 2059 | * If mddev->level <= 0, then we don't care |
| 2060 | * about aligning sizes (e.g. linear) |
| 2061 | */ |
| 2062 | if (mddev->level > 0) |
| 2063 | return -ENOSPC; |
| 2064 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2065 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2066 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | |
| 2068 | /* Verify rdev->desc_nr is unique. |
| 2069 | * If it is -1, assign a free number, else |
| 2070 | * check number is not in use |
| 2071 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2072 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | if (rdev->desc_nr < 0) { |
| 2074 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2075 | if (mddev->pers) |
| 2076 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2077 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | choice++; |
| 2079 | rdev->desc_nr = choice; |
| 2080 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2081 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2082 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2084 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2086 | rcu_read_unlock(); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2087 | if (!test_bit(Journal, &rdev->flags) && |
| 2088 | mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2089 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2090 | mdname(mddev), mddev->max_disks); |
| 2091 | return -EBUSY; |
| 2092 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2093 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2094 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2097 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2098 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2099 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2100 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2101 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2102 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2103 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2104 | /* failure here is OK */; |
| 2105 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2106 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2107 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2108 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2109 | |
| 2110 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2111 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2114 | |
| 2115 | fail: |
| 2116 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2117 | b, mdname(mddev)); |
| 2118 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | } |
| 2120 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2121 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2122 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2123 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2124 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2125 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2126 | } |
| 2127 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2128 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | { |
| 2130 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2131 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2132 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2133 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2135 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2136 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2137 | sysfs_put(rdev->sysfs_state); |
| 2138 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2139 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2140 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2141 | * writing to 'remove' to "dev/state". We also need |
| 2142 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2143 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2144 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2145 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2146 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2147 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | * prevent the device from being mounted, repartitioned or |
| 2152 | * otherwise reused by a RAID array (or any other kernel |
| 2153 | * subsystem), by bd_claiming the device. |
| 2154 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2155 | 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] | 2156 | { |
| 2157 | int err = 0; |
| 2158 | struct block_device *bdev; |
| 2159 | char b[BDEVNAME_SIZE]; |
| 2160 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2161 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2162 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | if (IS_ERR(bdev)) { |
| 2164 | printk(KERN_ERR "md: could not open %s.\n", |
| 2165 | __bdevname(dev, b)); |
| 2166 | return PTR_ERR(bdev); |
| 2167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | rdev->bdev = bdev; |
| 2169 | return err; |
| 2170 | } |
| 2171 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2172 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | { |
| 2174 | struct block_device *bdev = rdev->bdev; |
| 2175 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2176 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | void md_autodetect_dev(dev_t dev); |
| 2180 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2181 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | { |
| 2183 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2186 | bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2187 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2189 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2190 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | #endif |
| 2192 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2193 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | } |
| 2195 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2196 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | { |
| 2198 | unbind_rdev_from_array(rdev); |
| 2199 | export_rdev(rdev); |
| 2200 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2201 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2203 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2205 | struct md_rdev *rdev; |
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 | while (!list_empty(&mddev->disks)) { |
| 2208 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2209 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2210 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | mddev->raid_disks = 0; |
| 2213 | mddev->major_version = 0; |
| 2214 | } |
| 2215 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2216 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2218 | /* Update each superblock (in-memory image), but |
| 2219 | * if we are allowed to, skip spares which already |
| 2220 | * have the right event counter, or have one earlier |
| 2221 | * (which would mean they aren't being marked as dirty |
| 2222 | * with the rest of the array) |
| 2223 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2224 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2225 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2226 | if (rdev->sb_events == mddev->events || |
| 2227 | (nospares && |
| 2228 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2229 | rdev->sb_events+1 == mddev->events)) { |
| 2230 | /* Don't update this superblock */ |
| 2231 | rdev->sb_loaded = 2; |
| 2232 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2233 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2234 | rdev->sb_loaded = 1; |
| 2235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | } |
| 2237 | } |
| 2238 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2239 | static bool does_sb_need_changing(struct mddev *mddev) |
| 2240 | { |
| 2241 | struct md_rdev *rdev; |
| 2242 | struct mdp_superblock_1 *sb; |
| 2243 | int role; |
| 2244 | |
| 2245 | /* Find a good rdev */ |
| 2246 | rdev_for_each(rdev, mddev) |
| 2247 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) |
| 2248 | break; |
| 2249 | |
| 2250 | /* No good device found. */ |
| 2251 | if (!rdev) |
| 2252 | return false; |
| 2253 | |
| 2254 | sb = page_address(rdev->sb_page); |
| 2255 | /* Check if a device has become faulty or a spare become active */ |
| 2256 | rdev_for_each(rdev, mddev) { |
| 2257 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 2258 | /* Device activated? */ |
| 2259 | if (role == 0xffff && rdev->raid_disk >=0 && |
| 2260 | !test_bit(Faulty, &rdev->flags)) |
| 2261 | return true; |
| 2262 | /* Device turned faulty? */ |
| 2263 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) |
| 2264 | return true; |
| 2265 | } |
| 2266 | |
| 2267 | /* Check if any mddev parameters have changed */ |
| 2268 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || |
| 2269 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2270 | (mddev->layout != le64_to_cpu(sb->layout)) || |
| 2271 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || |
| 2272 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) |
| 2273 | return true; |
| 2274 | |
| 2275 | return false; |
| 2276 | } |
| 2277 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2278 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2280 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2281 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2282 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2283 | int any_badblocks_changed = 0; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2284 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2286 | if (mddev->ro) { |
| 2287 | if (force_change) |
| 2288 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2289 | return; |
| 2290 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2291 | |
| 2292 | if (mddev_is_clustered(mddev)) { |
| 2293 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2294 | force_change = 1; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2295 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2296 | /* Has someone else has updated the sb */ |
| 2297 | if (!does_sb_need_changing(mddev)) { |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2298 | if (ret == 0) |
| 2299 | md_cluster_ops->metadata_update_cancel(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2300 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2301 | return; |
| 2302 | } |
| 2303 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2305 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2306 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2307 | if (rdev->raid_disk >= 0 && |
| 2308 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2309 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2310 | !test_bit(In_sync, &rdev->flags) && |
| 2311 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2312 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2313 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2314 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2315 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2316 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2317 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2318 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2319 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2320 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2321 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2322 | rdev->badblocks.changed = 0; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2323 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2324 | md_error(mddev, rdev); |
| 2325 | } |
| 2326 | clear_bit(Blocked, &rdev->flags); |
| 2327 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2328 | wake_up(&rdev->blocked_wait); |
| 2329 | } |
| 2330 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2331 | wake_up(&mddev->sb_wait); |
| 2332 | return; |
| 2333 | } |
| 2334 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2335 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2336 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 2337 | mddev->utime = ktime_get_real_seconds(); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2338 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2339 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2340 | force_change = 1; |
| 2341 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2342 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2343 | * though if events isn't the right even/odd, we will have to do |
| 2344 | * spares after all |
| 2345 | */ |
| 2346 | nospares = 1; |
| 2347 | if (force_change) |
| 2348 | nospares = 0; |
| 2349 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2350 | /* If the array is degraded, then skipping spares is both |
| 2351 | * dangerous and fairly pointless. |
| 2352 | * Dangerous because a device that was removed from the array |
| 2353 | * might have a event_count that still looks up-to-date, |
| 2354 | * so it can be re-added without a resync. |
| 2355 | * Pointless because if there are any spares to skip, |
| 2356 | * then a recovery will happen and soon that array won't |
| 2357 | * be degraded any more and the spare can go back to sleep then. |
| 2358 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2359 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2360 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2361 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2362 | |
| 2363 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2364 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2365 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2366 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2367 | && mddev->can_decrease_events |
| 2368 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2369 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2370 | mddev->can_decrease_events = 0; |
| 2371 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2372 | /* otherwise we have to go forward and ... */ |
| 2373 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2374 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2375 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2377 | /* |
| 2378 | * This 64-bit counter should never wrap. |
| 2379 | * Either we are in around ~1 trillion A.C., assuming |
| 2380 | * 1 reboot per second, or we have a bug... |
| 2381 | */ |
| 2382 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2383 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2384 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2385 | if (rdev->badblocks.changed) |
| 2386 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2387 | if (test_bit(Faulty, &rdev->flags)) |
| 2388 | set_bit(FaultRecorded, &rdev->flags); |
| 2389 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2390 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2391 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2392 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2394 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2395 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2397 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2398 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2400 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2401 | if (rdev->sb_loaded != 1) |
| 2402 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2404 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2405 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2406 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2407 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2408 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2409 | bdevname(rdev->bdev, b), |
| 2410 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2411 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2412 | if (rdev->badblocks.size) { |
| 2413 | md_super_write(mddev, rdev, |
| 2414 | rdev->badblocks.sector, |
| 2415 | rdev->badblocks.size << 9, |
| 2416 | rdev->bb_page); |
| 2417 | rdev->badblocks.size = 0; |
| 2418 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2419 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2420 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2421 | pr_debug("md: %s (skipping faulty)\n", |
| 2422 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2423 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2424 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | /* only need to write one superblock... */ |
| 2426 | break; |
| 2427 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2428 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2429 | /* 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] | 2430 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2431 | spin_lock(&mddev->lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2432 | if (mddev->in_sync != sync_req || |
| 2433 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2434 | /* have to write it out again */ |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2435 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2436 | goto repeat; |
| 2437 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2438 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2439 | spin_unlock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2440 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2441 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2442 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2443 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2444 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2445 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2446 | clear_bit(Blocked, &rdev->flags); |
| 2447 | |
| 2448 | if (any_badblocks_changed) |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2449 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2450 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2451 | wake_up(&rdev->blocked_wait); |
| 2452 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2453 | |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2454 | if (mddev_is_clustered(mddev) && ret == 0) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2455 | md_cluster_ops->metadata_update_finish(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2457 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2459 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2460 | { |
| 2461 | struct mddev *mddev = rdev->mddev; |
| 2462 | int err = 0; |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2463 | bool add_journal = test_bit(Journal, &rdev->flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2464 | |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2465 | if (!mddev->pers->hot_remove_disk || add_journal) { |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2466 | /* If there is hot_add_disk but no hot_remove_disk |
| 2467 | * then added disks for geometry changes, |
| 2468 | * and should be added immediately. |
| 2469 | */ |
| 2470 | super_types[mddev->major_version]. |
| 2471 | validate_super(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2472 | if (add_journal) |
| 2473 | mddev_suspend(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2474 | err = mddev->pers->hot_add_disk(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2475 | if (add_journal) |
| 2476 | mddev_resume(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2477 | if (err) { |
| 2478 | unbind_rdev_from_array(rdev); |
| 2479 | export_rdev(rdev); |
| 2480 | return err; |
| 2481 | } |
| 2482 | } |
| 2483 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2484 | |
| 2485 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2486 | if (mddev->degraded) |
| 2487 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2488 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2489 | md_new_event(mddev); |
| 2490 | md_wakeup_thread(mddev->thread); |
| 2491 | return 0; |
| 2492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2494 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2495 | * We want to accept with case. For this we use cmd_match. |
| 2496 | */ |
| 2497 | static int cmd_match(const char *cmd, const char *str) |
| 2498 | { |
| 2499 | /* See if cmd, written into a sysfs file, matches |
| 2500 | * str. They must either be the same, or cmd can |
| 2501 | * have a trailing newline |
| 2502 | */ |
| 2503 | while (*cmd && *str && *cmd == *str) { |
| 2504 | cmd++; |
| 2505 | str++; |
| 2506 | } |
| 2507 | if (*cmd == '\n') |
| 2508 | cmd++; |
| 2509 | if (*str || *cmd) |
| 2510 | return 0; |
| 2511 | return 1; |
| 2512 | } |
| 2513 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2514 | struct rdev_sysfs_entry { |
| 2515 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2516 | ssize_t (*show)(struct md_rdev *, char *); |
| 2517 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2518 | }; |
| 2519 | |
| 2520 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2521 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2522 | { |
| 2523 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2524 | size_t len = 0; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2525 | unsigned long flags = ACCESS_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2526 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2527 | if (test_bit(Faulty, &flags) || |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2528 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2529 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2530 | sep = ","; |
| 2531 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2532 | if (test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2533 | len += sprintf(page+len, "%sin_sync",sep); |
| 2534 | sep = ","; |
| 2535 | } |
Shaohua Li | ac6096e | 2015-10-04 09:20:11 -0700 | [diff] [blame] | 2536 | if (test_bit(Journal, &flags)) { |
| 2537 | len += sprintf(page+len, "%sjournal",sep); |
| 2538 | sep = ","; |
| 2539 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2540 | if (test_bit(WriteMostly, &flags)) { |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2541 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2542 | sep = ","; |
| 2543 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2544 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2545 | (rdev->badblocks.unacked_exist |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2546 | && !test_bit(Faulty, &flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2547 | len += sprintf(page+len, "%sblocked", sep); |
| 2548 | sep = ","; |
| 2549 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2550 | if (!test_bit(Faulty, &flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2551 | !test_bit(Journal, &flags) && |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2552 | !test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2553 | len += sprintf(page+len, "%sspare", sep); |
| 2554 | sep = ","; |
| 2555 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2556 | if (test_bit(WriteErrorSeen, &flags)) { |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2557 | len += sprintf(page+len, "%swrite_error", sep); |
| 2558 | sep = ","; |
| 2559 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2560 | if (test_bit(WantReplacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2561 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2562 | sep = ","; |
| 2563 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2564 | if (test_bit(Replacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2565 | len += sprintf(page+len, "%sreplacement", sep); |
| 2566 | sep = ","; |
| 2567 | } |
| 2568 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2569 | return len+sprintf(page+len, "\n"); |
| 2570 | } |
| 2571 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2572 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2573 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2574 | { |
| 2575 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2576 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2577 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2578 | * writemostly - sets write_mostly |
| 2579 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2580 | * blocked - sets the Blocked flags |
| 2581 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2582 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2583 | * -insync - clear Insync for a device with a slot assigned, |
| 2584 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2585 | * write_error - sets WriteErrorSeen |
| 2586 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2587 | */ |
| 2588 | int err = -EINVAL; |
| 2589 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2590 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2591 | if (test_bit(Faulty, &rdev->flags)) |
| 2592 | err = 0; |
| 2593 | else |
| 2594 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2595 | } else if (cmd_match(buf, "remove")) { |
| 2596 | if (rdev->raid_disk >= 0) |
| 2597 | err = -EBUSY; |
| 2598 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2599 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2600 | err = 0; |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 2601 | if (mddev_is_clustered(mddev)) |
| 2602 | err = md_cluster_ops->remove_disk(mddev, rdev); |
| 2603 | |
| 2604 | if (err == 0) { |
| 2605 | md_kick_rdev_from_array(rdev); |
| 2606 | if (mddev->pers) |
| 2607 | md_update_sb(mddev, 1); |
| 2608 | md_new_event(mddev); |
| 2609 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2610 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2611 | } else if (cmd_match(buf, "writemostly")) { |
| 2612 | set_bit(WriteMostly, &rdev->flags); |
| 2613 | err = 0; |
| 2614 | } else if (cmd_match(buf, "-writemostly")) { |
| 2615 | clear_bit(WriteMostly, &rdev->flags); |
| 2616 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2617 | } else if (cmd_match(buf, "blocked")) { |
| 2618 | set_bit(Blocked, &rdev->flags); |
| 2619 | err = 0; |
| 2620 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2621 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2622 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2623 | /* metadata handler doesn't understand badblocks, |
| 2624 | * so we need to fail the device |
| 2625 | */ |
| 2626 | md_error(rdev->mddev, rdev); |
| 2627 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2628 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2629 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2630 | wake_up(&rdev->blocked_wait); |
| 2631 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2632 | md_wakeup_thread(rdev->mddev->thread); |
| 2633 | |
| 2634 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2635 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2636 | set_bit(In_sync, &rdev->flags); |
| 2637 | err = 0; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2638 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 && |
| 2639 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 2640 | if (rdev->mddev->pers == NULL) { |
| 2641 | clear_bit(In_sync, &rdev->flags); |
| 2642 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2643 | rdev->raid_disk = -1; |
| 2644 | err = 0; |
| 2645 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2646 | } else if (cmd_match(buf, "write_error")) { |
| 2647 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2648 | err = 0; |
| 2649 | } else if (cmd_match(buf, "-write_error")) { |
| 2650 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2651 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2652 | } else if (cmd_match(buf, "want_replacement")) { |
| 2653 | /* Any non-spare device that is not a replacement can |
| 2654 | * become want_replacement at any time, but we then need to |
| 2655 | * check if recovery is needed. |
| 2656 | */ |
| 2657 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2658 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2659 | !test_bit(Replacement, &rdev->flags)) |
| 2660 | set_bit(WantReplacement, &rdev->flags); |
| 2661 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2662 | md_wakeup_thread(rdev->mddev->thread); |
| 2663 | err = 0; |
| 2664 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2665 | /* Clearing 'want_replacement' is always allowed. |
| 2666 | * Once replacements starts it is too late though. |
| 2667 | */ |
| 2668 | err = 0; |
| 2669 | clear_bit(WantReplacement, &rdev->flags); |
| 2670 | } else if (cmd_match(buf, "replacement")) { |
| 2671 | /* Can only set a device as a replacement when array has not |
| 2672 | * yet been started. Once running, replacement is automatic |
| 2673 | * from spares, or by assigning 'slot'. |
| 2674 | */ |
| 2675 | if (rdev->mddev->pers) |
| 2676 | err = -EBUSY; |
| 2677 | else { |
| 2678 | set_bit(Replacement, &rdev->flags); |
| 2679 | err = 0; |
| 2680 | } |
| 2681 | } else if (cmd_match(buf, "-replacement")) { |
| 2682 | /* Similarly, can only clear Replacement before start */ |
| 2683 | if (rdev->mddev->pers) |
| 2684 | err = -EBUSY; |
| 2685 | else { |
| 2686 | clear_bit(Replacement, &rdev->flags); |
| 2687 | err = 0; |
| 2688 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2689 | } else if (cmd_match(buf, "re-add")) { |
| 2690 | if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 2691 | /* clear_bit is performed _after_ all the devices |
| 2692 | * have their local Faulty bit cleared. If any writes |
| 2693 | * happen in the meantime in the local node, they |
| 2694 | * will land in the local bitmap, which will be synced |
| 2695 | * by this node eventually |
| 2696 | */ |
| 2697 | if (!mddev_is_clustered(rdev->mddev) || |
| 2698 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 2699 | clear_bit(Faulty, &rdev->flags); |
| 2700 | err = add_bound_rdev(rdev); |
| 2701 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2702 | } else |
| 2703 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2704 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2705 | if (!err) |
| 2706 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2707 | return err ? err : len; |
| 2708 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2709 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 2710 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2711 | |
| 2712 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2713 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2714 | { |
| 2715 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2716 | } |
| 2717 | |
| 2718 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2719 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2720 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2721 | unsigned int n; |
| 2722 | int rv; |
| 2723 | |
| 2724 | rv = kstrtouint(buf, 10, &n); |
| 2725 | if (rv < 0) |
| 2726 | return rv; |
| 2727 | atomic_set(&rdev->corrected_errors, n); |
| 2728 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2729 | } |
| 2730 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2731 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2732 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2733 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2734 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2735 | { |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2736 | if (test_bit(Journal, &rdev->flags)) |
| 2737 | return sprintf(page, "journal\n"); |
| 2738 | else if (rdev->raid_disk < 0) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2739 | return sprintf(page, "none\n"); |
| 2740 | else |
| 2741 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2742 | } |
| 2743 | |
| 2744 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2745 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2746 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2747 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2748 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2749 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2750 | if (test_bit(Journal, &rdev->flags)) |
| 2751 | return -EBUSY; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2752 | if (strncmp(buf, "none", 4)==0) |
| 2753 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2754 | else { |
| 2755 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 2756 | if (err < 0) |
| 2757 | return err; |
| 2758 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2759 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2760 | /* Setting 'slot' on an active array requires also |
| 2761 | * updating the 'rd%d' link, and communicating |
| 2762 | * with the personality with ->hot_*_disk. |
| 2763 | * For now we only support removing |
| 2764 | * failed/spare devices. This normally happens automatically, |
| 2765 | * but not when the metadata is externally managed. |
| 2766 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2767 | if (rdev->raid_disk == -1) |
| 2768 | return -EEXIST; |
| 2769 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2770 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2771 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2772 | clear_bit(Blocked, &rdev->flags); |
| 2773 | remove_and_add_spares(rdev->mddev, rdev); |
| 2774 | if (rdev->raid_disk >= 0) |
| 2775 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2776 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2777 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2778 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2779 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2780 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2781 | */ |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2782 | int err; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2783 | |
| 2784 | if (rdev->raid_disk != -1) |
| 2785 | return -EBUSY; |
| 2786 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2787 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2788 | return -EBUSY; |
| 2789 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2790 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2791 | return -EINVAL; |
| 2792 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2793 | if (slot >= rdev->mddev->raid_disks && |
| 2794 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2795 | return -ENOSPC; |
| 2796 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2797 | rdev->raid_disk = slot; |
| 2798 | if (test_bit(In_sync, &rdev->flags)) |
| 2799 | rdev->saved_raid_disk = slot; |
| 2800 | else |
| 2801 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2802 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 2803 | clear_bit(Bitmap_sync, &rdev->flags); |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2804 | err = rdev->mddev->pers-> |
| 2805 | hot_add_disk(rdev->mddev, rdev); |
| 2806 | if (err) { |
| 2807 | rdev->raid_disk = -1; |
| 2808 | return err; |
| 2809 | } else |
| 2810 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2811 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
| 2812 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2813 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2814 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2815 | if (slot >= rdev->mddev->raid_disks && |
| 2816 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2817 | return -ENOSPC; |
| 2818 | rdev->raid_disk = slot; |
| 2819 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2820 | clear_bit(Faulty, &rdev->flags); |
| 2821 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2822 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2823 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2824 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2825 | return len; |
| 2826 | } |
| 2827 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2828 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2829 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2830 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2831 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2832 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2833 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2834 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2838 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2839 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2840 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2841 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2842 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2843 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2844 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2845 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2846 | /* Must set offset before size, so overlap checks |
| 2847 | * can be sane */ |
| 2848 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2849 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2850 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2851 | return len; |
| 2852 | } |
| 2853 | |
| 2854 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2855 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2856 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2857 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2858 | { |
| 2859 | return sprintf(page, "%llu\n", |
| 2860 | (unsigned long long)rdev->new_data_offset); |
| 2861 | } |
| 2862 | |
| 2863 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2864 | const char *buf, size_t len) |
| 2865 | { |
| 2866 | unsigned long long new_offset; |
| 2867 | struct mddev *mddev = rdev->mddev; |
| 2868 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2869 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2870 | return -EINVAL; |
| 2871 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 2872 | if (mddev->sync_thread || |
| 2873 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2874 | return -EBUSY; |
| 2875 | if (new_offset == rdev->data_offset) |
| 2876 | /* reset is always permitted */ |
| 2877 | ; |
| 2878 | else if (new_offset > rdev->data_offset) { |
| 2879 | /* must not push array size beyond rdev_sectors */ |
| 2880 | if (new_offset - rdev->data_offset |
| 2881 | + mddev->dev_sectors > rdev->sectors) |
| 2882 | return -E2BIG; |
| 2883 | } |
| 2884 | /* Metadata worries about other space details. */ |
| 2885 | |
| 2886 | /* decreasing the offset is inconsistent with a backwards |
| 2887 | * reshape. |
| 2888 | */ |
| 2889 | if (new_offset < rdev->data_offset && |
| 2890 | mddev->reshape_backwards) |
| 2891 | return -EINVAL; |
| 2892 | /* Increasing offset is inconsistent with forwards |
| 2893 | * reshape. reshape_direction should be set to |
| 2894 | * 'backwards' first. |
| 2895 | */ |
| 2896 | if (new_offset > rdev->data_offset && |
| 2897 | !mddev->reshape_backwards) |
| 2898 | return -EINVAL; |
| 2899 | |
| 2900 | if (mddev->pers && mddev->persistent && |
| 2901 | !super_types[mddev->major_version] |
| 2902 | .allow_new_offset(rdev, new_offset)) |
| 2903 | return -E2BIG; |
| 2904 | rdev->new_data_offset = new_offset; |
| 2905 | if (new_offset > rdev->data_offset) |
| 2906 | mddev->reshape_backwards = 1; |
| 2907 | else if (new_offset < rdev->data_offset) |
| 2908 | mddev->reshape_backwards = 0; |
| 2909 | |
| 2910 | return len; |
| 2911 | } |
| 2912 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2913 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2914 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2915 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2916 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2917 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2918 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2919 | } |
| 2920 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2921 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2922 | { |
| 2923 | /* check if two start/length pairs overlap */ |
| 2924 | if (s1+l1 <= s2) |
| 2925 | return 0; |
| 2926 | if (s2+l2 <= s1) |
| 2927 | return 0; |
| 2928 | return 1; |
| 2929 | } |
| 2930 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2931 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2932 | { |
| 2933 | unsigned long long blocks; |
| 2934 | sector_t new; |
| 2935 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2936 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2937 | return -EINVAL; |
| 2938 | |
| 2939 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2940 | return -EINVAL; /* sector conversion overflow */ |
| 2941 | |
| 2942 | new = blocks * 2; |
| 2943 | if (new != blocks * 2) |
| 2944 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2945 | |
| 2946 | *sectors = new; |
| 2947 | return 0; |
| 2948 | } |
| 2949 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2950 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2951 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2952 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2953 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2954 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2955 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2956 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2957 | if (test_bit(Journal, &rdev->flags)) |
| 2958 | return -EBUSY; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2959 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2960 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2961 | if (rdev->data_offset != rdev->new_data_offset) |
| 2962 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2963 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2964 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2965 | sectors = super_types[my_mddev->major_version]. |
| 2966 | rdev_size_change(rdev, sectors); |
| 2967 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2968 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2969 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2970 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2971 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 2972 | if (!my_mddev->pers->resize) |
| 2973 | /* Cannot change size for RAID0 or Linear etc */ |
| 2974 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2975 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2976 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2977 | return -EINVAL; /* component must fit device */ |
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 | rdev->sectors = sectors; |
| 2980 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2981 | /* Need to check that all other rdevs with the same |
| 2982 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 2983 | * the rdev lists safely. |
| 2984 | * This check does not provide a hard guarantee, it |
| 2985 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2986 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2987 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2988 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2989 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2990 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2991 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2992 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2993 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2994 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2995 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2996 | if (rdev->bdev == rdev2->bdev && |
| 2997 | rdev != rdev2 && |
| 2998 | overlaps(rdev->data_offset, rdev->sectors, |
| 2999 | rdev2->data_offset, |
| 3000 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3001 | overlap = 1; |
| 3002 | break; |
| 3003 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3004 | if (overlap) { |
| 3005 | mddev_put(mddev); |
| 3006 | break; |
| 3007 | } |
| 3008 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3009 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3010 | if (overlap) { |
| 3011 | /* Someone else could have slipped in a size |
| 3012 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3013 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3014 | * safe, and trust userspace not to race with |
| 3015 | * itself |
| 3016 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3017 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3018 | return -EBUSY; |
| 3019 | } |
| 3020 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3021 | return len; |
| 3022 | } |
| 3023 | |
| 3024 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3025 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3026 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3027 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3028 | { |
| 3029 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3030 | |
| 3031 | if (test_bit(In_sync, &rdev->flags) || |
| 3032 | recovery_start == MaxSector) |
| 3033 | return sprintf(page, "none\n"); |
| 3034 | |
| 3035 | return sprintf(page, "%llu\n", recovery_start); |
| 3036 | } |
| 3037 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3038 | 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] | 3039 | { |
| 3040 | unsigned long long recovery_start; |
| 3041 | |
| 3042 | if (cmd_match(buf, "none")) |
| 3043 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3044 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3045 | return -EINVAL; |
| 3046 | |
| 3047 | if (rdev->mddev->pers && |
| 3048 | rdev->raid_disk >= 0) |
| 3049 | return -EBUSY; |
| 3050 | |
| 3051 | rdev->recovery_offset = recovery_start; |
| 3052 | if (recovery_start == MaxSector) |
| 3053 | set_bit(In_sync, &rdev->flags); |
| 3054 | else |
| 3055 | clear_bit(In_sync, &rdev->flags); |
| 3056 | return len; |
| 3057 | } |
| 3058 | |
| 3059 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3060 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3061 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3062 | /* sysfs access to bad-blocks list. |
| 3063 | * We present two files. |
| 3064 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 3065 | * are recorded as bad. The list is truncated to fit within |
| 3066 | * the one-page limit of sysfs. |
| 3067 | * Writing "sector length" to this file adds an acknowledged |
| 3068 | * bad block list. |
| 3069 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 3070 | * been acknowledged. Writing to this file adds bad blocks |
| 3071 | * without acknowledging them. This is largely for testing. |
| 3072 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3073 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3074 | { |
| 3075 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3076 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3077 | 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] | 3078 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3079 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3080 | /* Maybe that ack was all we needed */ |
| 3081 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3082 | wake_up(&rdev->blocked_wait); |
| 3083 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3084 | } |
| 3085 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3086 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3087 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3088 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3089 | { |
| 3090 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3091 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3092 | 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] | 3093 | { |
| 3094 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3095 | } |
| 3096 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3097 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3098 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3099 | static struct attribute *rdev_default_attrs[] = { |
| 3100 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3101 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3102 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3103 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3104 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3105 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3106 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3107 | &rdev_bad_blocks.attr, |
| 3108 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3109 | NULL, |
| 3110 | }; |
| 3111 | static ssize_t |
| 3112 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3113 | { |
| 3114 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3115 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3116 | |
| 3117 | if (!entry->show) |
| 3118 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3119 | if (!rdev->mddev) |
| 3120 | return -EBUSY; |
| 3121 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | static ssize_t |
| 3125 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3126 | const char *page, size_t length) |
| 3127 | { |
| 3128 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3129 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3130 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3131 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3132 | |
| 3133 | if (!entry->store) |
| 3134 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3135 | if (!capable(CAP_SYS_ADMIN)) |
| 3136 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3137 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3138 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3139 | if (rdev->mddev == NULL) |
| 3140 | rv = -EBUSY; |
| 3141 | else |
| 3142 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3143 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3144 | } |
| 3145 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | static void rdev_free(struct kobject *ko) |
| 3149 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3150 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3151 | kfree(rdev); |
| 3152 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3153 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3154 | .show = rdev_attr_show, |
| 3155 | .store = rdev_attr_store, |
| 3156 | }; |
| 3157 | static struct kobj_type rdev_ktype = { |
| 3158 | .release = rdev_free, |
| 3159 | .sysfs_ops = &rdev_sysfs_ops, |
| 3160 | .default_attrs = rdev_default_attrs, |
| 3161 | }; |
| 3162 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3163 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3164 | { |
| 3165 | rdev->desc_nr = -1; |
| 3166 | rdev->saved_raid_disk = -1; |
| 3167 | rdev->raid_disk = -1; |
| 3168 | rdev->flags = 0; |
| 3169 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3170 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3171 | rdev->sb_events = 0; |
| 3172 | rdev->last_read_error.tv_sec = 0; |
| 3173 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3174 | rdev->sb_loaded = 0; |
| 3175 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3176 | atomic_set(&rdev->nr_pending, 0); |
| 3177 | atomic_set(&rdev->read_errors, 0); |
| 3178 | atomic_set(&rdev->corrected_errors, 0); |
| 3179 | |
| 3180 | INIT_LIST_HEAD(&rdev->same_set); |
| 3181 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3182 | |
| 3183 | /* Add space to store bad block list. |
| 3184 | * This reserves the space even on arrays where it cannot |
| 3185 | * be used - I wonder if that matters |
| 3186 | */ |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3187 | return badblocks_init(&rdev->badblocks, 0); |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3188 | } |
| 3189 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | /* |
| 3191 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3192 | * |
| 3193 | * mark the device faulty if: |
| 3194 | * |
| 3195 | * - the device is nonexistent (zero size) |
| 3196 | * - the device has no valid superblock |
| 3197 | * |
| 3198 | * a faulty rdev _never_ has rdev->sb set. |
| 3199 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3200 | 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] | 3201 | { |
| 3202 | char b[BDEVNAME_SIZE]; |
| 3203 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3204 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3205 | sector_t size; |
| 3206 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3207 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | if (!rdev) { |
| 3209 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3210 | return ERR_PTR(-ENOMEM); |
| 3211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3213 | err = md_rdev_init(rdev); |
| 3214 | if (err) |
| 3215 | goto abort_free; |
| 3216 | err = alloc_disk_sb(rdev); |
| 3217 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3218 | goto abort_free; |
| 3219 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3220 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | if (err) |
| 3222 | goto abort_free; |
| 3223 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3224 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3225 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3226 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3227 | if (!size) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3228 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3229 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3230 | bdevname(rdev->bdev,b)); |
| 3231 | err = -EINVAL; |
| 3232 | goto abort_free; |
| 3233 | } |
| 3234 | |
| 3235 | if (super_format >= 0) { |
| 3236 | err = super_types[super_format]. |
| 3237 | load_super(rdev, NULL, super_minor); |
| 3238 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3239 | printk(KERN_WARNING |
| 3240 | "md: %s does not have a valid v%d.%d " |
| 3241 | "superblock, not importing!\n", |
| 3242 | bdevname(rdev->bdev,b), |
| 3243 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | goto abort_free; |
| 3245 | } |
| 3246 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3247 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | "md: could not read %s's sb, not importing!\n", |
| 3249 | bdevname(rdev->bdev,b)); |
| 3250 | goto abort_free; |
| 3251 | } |
| 3252 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | return rdev; |
| 3255 | |
| 3256 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3257 | if (rdev->bdev) |
| 3258 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3259 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | kfree(rdev); |
| 3261 | return ERR_PTR(err); |
| 3262 | } |
| 3263 | |
| 3264 | /* |
| 3265 | * Check a full RAID array for plausibility |
| 3266 | */ |
| 3267 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3268 | static void analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | { |
| 3270 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3271 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | char b[BDEVNAME_SIZE]; |
| 3273 | |
| 3274 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3275 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | switch (super_types[mddev->major_version]. |
| 3277 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3278 | case 1: |
| 3279 | freshest = rdev; |
| 3280 | break; |
| 3281 | case 0: |
| 3282 | break; |
| 3283 | default: |
| 3284 | printk( KERN_ERR \ |
| 3285 | "md: fatal superblock inconsistency in %s" |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3286 | " -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3288 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | } |
| 3290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | super_types[mddev->major_version]. |
| 3292 | validate_super(mddev, freshest); |
| 3293 | |
| 3294 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3295 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3296 | if (mddev->max_disks && |
| 3297 | (rdev->desc_nr >= mddev->max_disks || |
| 3298 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3299 | printk(KERN_WARNING |
| 3300 | "md: %s: %s: only %d devices permitted\n", |
| 3301 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3302 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3303 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3304 | continue; |
| 3305 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3306 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | if (super_types[mddev->major_version]. |
| 3308 | validate_super(mddev, rdev)) { |
| 3309 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3310 | " from array!\n", |
| 3311 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3312 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3313 | continue; |
| 3314 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3315 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3317 | rdev->desc_nr = i++; |
| 3318 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3319 | set_bit(In_sync, &rdev->flags); |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3320 | } else if (rdev->raid_disk >= |
| 3321 | (mddev->raid_disks - min(0, mddev->delta_disks)) && |
| 3322 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3323 | rdev->raid_disk = -1; |
| 3324 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3325 | } |
| 3326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3327 | } |
| 3328 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3329 | /* Read a fixed-point number. |
| 3330 | * Numbers in sysfs attributes should be in "standard" units where |
| 3331 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3332 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3333 | * milliseconds or jiffies. |
| 3334 | * This function takes a decimal number with a possible fractional |
| 3335 | * component, and produces an integer which is the result of |
| 3336 | * multiplying that number by 10^'scale'. |
| 3337 | * all without any floating-point arithmetic. |
| 3338 | */ |
| 3339 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3340 | { |
| 3341 | unsigned long result = 0; |
| 3342 | long decimals = -1; |
| 3343 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3344 | if (*cp == '.') |
| 3345 | decimals = 0; |
| 3346 | else if (decimals < scale) { |
| 3347 | unsigned int value; |
| 3348 | value = *cp - '0'; |
| 3349 | result = result * 10 + value; |
| 3350 | if (decimals >= 0) |
| 3351 | decimals++; |
| 3352 | } |
| 3353 | cp++; |
| 3354 | } |
| 3355 | if (*cp == '\n') |
| 3356 | cp++; |
| 3357 | if (*cp) |
| 3358 | return -EINVAL; |
| 3359 | if (decimals < 0) |
| 3360 | decimals = 0; |
| 3361 | while (decimals < scale) { |
| 3362 | result *= 10; |
| 3363 | decimals ++; |
| 3364 | } |
| 3365 | *res = result; |
| 3366 | return 0; |
| 3367 | } |
| 3368 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3369 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3370 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3371 | { |
| 3372 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3373 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3374 | } |
| 3375 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3376 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3377 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3378 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3379 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3380 | if (mddev_is_clustered(mddev)) { |
| 3381 | pr_info("md: Safemode is disabled for clustered mode\n"); |
| 3382 | return -EINVAL; |
| 3383 | } |
| 3384 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3385 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3386 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3387 | if (msec == 0) |
| 3388 | mddev->safemode_delay = 0; |
| 3389 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3390 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3391 | unsigned long new_delay = (msec*HZ)/1000; |
| 3392 | |
| 3393 | if (new_delay == 0) |
| 3394 | new_delay = 1; |
| 3395 | mddev->safemode_delay = new_delay; |
| 3396 | if (new_delay < old_delay || old_delay == 0) |
| 3397 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3398 | } |
| 3399 | return len; |
| 3400 | } |
| 3401 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3402 | __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] | 3403 | |
| 3404 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3405 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3406 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3407 | struct md_personality *p; |
| 3408 | int ret; |
| 3409 | spin_lock(&mddev->lock); |
| 3410 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3411 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3412 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3413 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3414 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3415 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3416 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3417 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3418 | ret = 0; |
| 3419 | spin_unlock(&mddev->lock); |
| 3420 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3421 | } |
| 3422 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3423 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3424 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3425 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3426 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3427 | ssize_t rv; |
| 3428 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3429 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3430 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3431 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3432 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3433 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3434 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3435 | return -EINVAL; |
| 3436 | |
| 3437 | rv = mddev_lock(mddev); |
| 3438 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3439 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3440 | |
| 3441 | if (mddev->pers == NULL) { |
| 3442 | strncpy(mddev->clevel, buf, slen); |
| 3443 | if (mddev->clevel[slen-1] == '\n') |
| 3444 | slen--; |
| 3445 | mddev->clevel[slen] = 0; |
| 3446 | mddev->level = LEVEL_NONE; |
| 3447 | rv = len; |
| 3448 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3449 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3450 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3451 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3452 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3453 | |
| 3454 | /* request to change the personality. Need to ensure: |
| 3455 | * - array is not engaged in resync/recovery/reshape |
| 3456 | * - old personality can be suspended |
| 3457 | * - new personality will access other array. |
| 3458 | */ |
| 3459 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3460 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3461 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3462 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3463 | mddev->reshape_position != MaxSector || |
| 3464 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3465 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3466 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3467 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3468 | if (!mddev->pers->quiesce) { |
| 3469 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3470 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3471 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3472 | } |
| 3473 | |
| 3474 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3475 | strncpy(clevel, buf, slen); |
| 3476 | if (clevel[slen-1] == '\n') |
| 3477 | slen--; |
| 3478 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3479 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3480 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3481 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3482 | if (request_module("md-%s", clevel) != 0) |
| 3483 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3484 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3485 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3486 | if (!pers || !try_module_get(pers->owner)) { |
| 3487 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3488 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3489 | rv = -EINVAL; |
| 3490 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3491 | } |
| 3492 | spin_unlock(&pers_lock); |
| 3493 | |
| 3494 | if (pers == mddev->pers) { |
| 3495 | /* Nothing to do! */ |
| 3496 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3497 | rv = len; |
| 3498 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3499 | } |
| 3500 | if (!pers->takeover) { |
| 3501 | module_put(pers->owner); |
| 3502 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3503 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3504 | rv = -EINVAL; |
| 3505 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3506 | } |
| 3507 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3508 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3509 | rdev->new_raid_disk = rdev->raid_disk; |
| 3510 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3511 | /* ->takeover must set new_* and/or delta_disks |
| 3512 | * if it succeeds, and may set them when it fails. |
| 3513 | */ |
| 3514 | priv = pers->takeover(mddev); |
| 3515 | if (IS_ERR(priv)) { |
| 3516 | mddev->new_level = mddev->level; |
| 3517 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3518 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3519 | mddev->raid_disks -= mddev->delta_disks; |
| 3520 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3521 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3522 | module_put(pers->owner); |
| 3523 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3524 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3525 | rv = PTR_ERR(priv); |
| 3526 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3527 | } |
| 3528 | |
| 3529 | /* Looks like we have a winner */ |
| 3530 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 3531 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3532 | |
| 3533 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3534 | oldpers = mddev->pers; |
| 3535 | oldpriv = mddev->private; |
| 3536 | mddev->pers = pers; |
| 3537 | mddev->private = priv; |
| 3538 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3539 | mddev->level = mddev->new_level; |
| 3540 | mddev->layout = mddev->new_layout; |
| 3541 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 3542 | mddev->delta_disks = 0; |
| 3543 | mddev->reshape_backwards = 0; |
| 3544 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3545 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3546 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3547 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3548 | mddev->external) { |
| 3549 | /* We are converting from a no-redundancy array |
| 3550 | * to a redundancy array and metadata is managed |
| 3551 | * externally so we need to be sure that writes |
| 3552 | * won't block due to a need to transition |
| 3553 | * clean->dirty |
| 3554 | * until external management is started. |
| 3555 | */ |
| 3556 | mddev->in_sync = 0; |
| 3557 | mddev->safemode_delay = 0; |
| 3558 | mddev->safemode = 0; |
| 3559 | } |
| 3560 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3561 | oldpers->free(mddev, oldpriv); |
| 3562 | |
| 3563 | if (oldpers->sync_request == NULL && |
| 3564 | pers->sync_request != NULL) { |
| 3565 | /* need to add the md_redundancy_group */ |
| 3566 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3567 | printk(KERN_WARNING |
| 3568 | "md: cannot register extra attributes for %s\n", |
| 3569 | mdname(mddev)); |
| 3570 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3571 | } |
| 3572 | if (oldpers->sync_request != NULL && |
| 3573 | pers->sync_request == NULL) { |
| 3574 | /* need to remove the md_redundancy_group */ |
| 3575 | if (mddev->to_remove == NULL) |
| 3576 | mddev->to_remove = &md_redundancy_group; |
| 3577 | } |
| 3578 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3579 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3580 | if (rdev->raid_disk < 0) |
| 3581 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3582 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3583 | rdev->new_raid_disk = -1; |
| 3584 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3585 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3586 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3587 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3588 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3589 | if (rdev->raid_disk < 0) |
| 3590 | continue; |
| 3591 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3592 | continue; |
| 3593 | rdev->raid_disk = rdev->new_raid_disk; |
| 3594 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3595 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3596 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3597 | if (sysfs_link_rdev(mddev, rdev)) |
| 3598 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3599 | " for %s after level change\n", |
| 3600 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3601 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3602 | } |
| 3603 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3604 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3605 | /* this is now an array without redundancy, so |
| 3606 | * it must always be in_sync |
| 3607 | */ |
| 3608 | mddev->in_sync = 1; |
| 3609 | del_timer_sync(&mddev->safemode_timer); |
| 3610 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 3611 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3612 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3613 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3614 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 3615 | if (!mddev->thread) |
| 3616 | md_update_sb(mddev, 1); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3617 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3618 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3619 | rv = len; |
| 3620 | out_unlock: |
| 3621 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3622 | return rv; |
| 3623 | } |
| 3624 | |
| 3625 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3626 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3627 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3628 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3629 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3630 | { |
| 3631 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3632 | if (mddev->reshape_position != MaxSector && |
| 3633 | mddev->layout != mddev->new_layout) |
| 3634 | return sprintf(page, "%d (%d)\n", |
| 3635 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3636 | return sprintf(page, "%d\n", mddev->layout); |
| 3637 | } |
| 3638 | |
| 3639 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3640 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3641 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3642 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3643 | int err; |
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 | err = kstrtouint(buf, 10, &n); |
| 3646 | if (err < 0) |
| 3647 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3648 | err = mddev_lock(mddev); |
| 3649 | if (err) |
| 3650 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3651 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3652 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3653 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3654 | err = -EBUSY; |
| 3655 | else if (mddev->ro) |
| 3656 | err = -EROFS; |
| 3657 | else { |
| 3658 | mddev->new_layout = n; |
| 3659 | err = mddev->pers->check_reshape(mddev); |
| 3660 | if (err) |
| 3661 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3662 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3663 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3664 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3665 | if (mddev->reshape_position == MaxSector) |
| 3666 | mddev->layout = n; |
| 3667 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3668 | mddev_unlock(mddev); |
| 3669 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3670 | } |
| 3671 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3672 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3673 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3674 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3675 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3676 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3677 | if (mddev->raid_disks == 0) |
| 3678 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3679 | if (mddev->reshape_position != MaxSector && |
| 3680 | mddev->delta_disks != 0) |
| 3681 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3682 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3683 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3684 | } |
| 3685 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3686 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3687 | |
| 3688 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3689 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3690 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3691 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3692 | int err; |
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 | err = kstrtouint(buf, 10, &n); |
| 3695 | if (err < 0) |
| 3696 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3697 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3698 | err = mddev_lock(mddev); |
| 3699 | if (err) |
| 3700 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3701 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3702 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3703 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3704 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3705 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3706 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3707 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3708 | rdev_for_each(rdev, mddev) { |
| 3709 | if (olddisks < n && |
| 3710 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3711 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 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 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3716 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3717 | mddev->delta_disks = n - olddisks; |
| 3718 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3719 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3720 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3721 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3722 | out_unlock: |
| 3723 | mddev_unlock(mddev); |
| 3724 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3725 | } |
| 3726 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3727 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3728 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3729 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3730 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3731 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3732 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3733 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3734 | return sprintf(page, "%d (%d)\n", |
| 3735 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3736 | mddev->chunk_sectors << 9); |
| 3737 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3738 | } |
| 3739 | |
| 3740 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3741 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3742 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3743 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3744 | int err; |
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 | err = kstrtoul(buf, 10, &n); |
| 3747 | if (err < 0) |
| 3748 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3749 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3750 | err = mddev_lock(mddev); |
| 3751 | if (err) |
| 3752 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3753 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3754 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3755 | err = -EBUSY; |
| 3756 | else if (mddev->ro) |
| 3757 | err = -EROFS; |
| 3758 | else { |
| 3759 | mddev->new_chunk_sectors = n >> 9; |
| 3760 | err = mddev->pers->check_reshape(mddev); |
| 3761 | if (err) |
| 3762 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3763 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3764 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3765 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3766 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3767 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3768 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3769 | mddev_unlock(mddev); |
| 3770 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3771 | } |
| 3772 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3773 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3774 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3775 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3776 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3777 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3778 | if (mddev->recovery_cp == MaxSector) |
| 3779 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3780 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3781 | } |
| 3782 | |
| 3783 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3784 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3785 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3786 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3787 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3788 | |
| 3789 | if (cmd_match(buf, "none")) |
| 3790 | n = MaxSector; |
| 3791 | else { |
| 3792 | err = kstrtoull(buf, 10, &n); |
| 3793 | if (err < 0) |
| 3794 | return err; |
| 3795 | if (n != (sector_t)n) |
| 3796 | return -EINVAL; |
| 3797 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3798 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3799 | err = mddev_lock(mddev); |
| 3800 | if (err) |
| 3801 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3802 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3803 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3804 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3805 | if (!err) { |
| 3806 | mddev->recovery_cp = n; |
| 3807 | if (mddev->pers) |
| 3808 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3809 | } |
| 3810 | mddev_unlock(mddev); |
| 3811 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3812 | } |
| 3813 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3814 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 3815 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3816 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3817 | /* |
| 3818 | * The array state can be: |
| 3819 | * |
| 3820 | * clear |
| 3821 | * No devices, no size, no level |
| 3822 | * Equivalent to STOP_ARRAY ioctl |
| 3823 | * inactive |
| 3824 | * May have some settings, but array is not active |
| 3825 | * all IO results in error |
| 3826 | * When written, doesn't tear down array, but just stops it |
| 3827 | * suspended (not supported yet) |
| 3828 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3829 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3830 | * readonly |
| 3831 | * no resync can happen. no superblocks get written. |
| 3832 | * write requests fail |
| 3833 | * read-auto |
| 3834 | * like readonly, but behaves like 'clean' on a write request. |
| 3835 | * |
| 3836 | * clean - no pending writes, but otherwise active. |
| 3837 | * When written to inactive array, starts without resync |
| 3838 | * If a write request arrives then |
| 3839 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3840 | * if not known, block and switch to write-pending |
| 3841 | * If written to an active array that has pending writes, then fails. |
| 3842 | * active |
| 3843 | * fully active: IO and resync can be happening. |
| 3844 | * When written to inactive array, starts with resync |
| 3845 | * |
| 3846 | * write-pending |
| 3847 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3848 | * |
| 3849 | * active-idle |
| 3850 | * like active, but no writes have been seen for a while (100msec). |
| 3851 | * |
| 3852 | */ |
| 3853 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3854 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3855 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3856 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3857 | "write-pending", "active-idle", NULL }; |
| 3858 | |
| 3859 | static int match_word(const char *word, char **list) |
| 3860 | { |
| 3861 | int n; |
| 3862 | for (n=0; list[n]; n++) |
| 3863 | if (cmd_match(word, list[n])) |
| 3864 | break; |
| 3865 | return n; |
| 3866 | } |
| 3867 | |
| 3868 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3869 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3870 | { |
| 3871 | enum array_state st = inactive; |
| 3872 | |
| 3873 | if (mddev->pers) |
| 3874 | switch(mddev->ro) { |
| 3875 | case 1: |
| 3876 | st = readonly; |
| 3877 | break; |
| 3878 | case 2: |
| 3879 | st = read_auto; |
| 3880 | break; |
| 3881 | case 0: |
| 3882 | if (mddev->in_sync) |
| 3883 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3884 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3885 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3886 | else if (mddev->safemode) |
| 3887 | st = active_idle; |
| 3888 | else |
| 3889 | st = active; |
| 3890 | } |
| 3891 | else { |
| 3892 | if (list_empty(&mddev->disks) && |
| 3893 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3894 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3895 | st = clear; |
| 3896 | else |
| 3897 | st = inactive; |
| 3898 | } |
| 3899 | return sprintf(page, "%s\n", array_states[st]); |
| 3900 | } |
| 3901 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3902 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 3903 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 3904 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3905 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3906 | |
| 3907 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3908 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3909 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3910 | int err; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3911 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3912 | |
| 3913 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 3914 | /* don't take reconfig_mutex when toggling between |
| 3915 | * clean and active |
| 3916 | */ |
| 3917 | spin_lock(&mddev->lock); |
| 3918 | if (st == active) { |
| 3919 | restart_array(mddev); |
| 3920 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 3921 | wake_up(&mddev->sb_wait); |
| 3922 | err = 0; |
| 3923 | } else /* st == clean */ { |
| 3924 | restart_array(mddev); |
| 3925 | if (atomic_read(&mddev->writes_pending) == 0) { |
| 3926 | if (mddev->in_sync == 0) { |
| 3927 | mddev->in_sync = 1; |
| 3928 | if (mddev->safemode == 1) |
| 3929 | mddev->safemode = 0; |
| 3930 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3931 | } |
| 3932 | err = 0; |
| 3933 | } else |
| 3934 | err = -EBUSY; |
| 3935 | } |
| 3936 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 3937 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3938 | } |
| 3939 | err = mddev_lock(mddev); |
| 3940 | if (err) |
| 3941 | return err; |
| 3942 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3943 | switch(st) { |
| 3944 | case bad_word: |
| 3945 | break; |
| 3946 | case clear: |
| 3947 | /* stopping an active array */ |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3948 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3949 | break; |
| 3950 | case inactive: |
| 3951 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3952 | if (mddev->pers) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3953 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3954 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3955 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3956 | break; |
| 3957 | case suspended: |
| 3958 | break; /* not supported yet */ |
| 3959 | case readonly: |
| 3960 | if (mddev->pers) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3961 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3962 | else { |
| 3963 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3964 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3965 | err = do_md_run(mddev); |
| 3966 | } |
| 3967 | break; |
| 3968 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3969 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3970 | if (mddev->ro == 0) |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3971 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3972 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3973 | err = restart_array(mddev); |
| 3974 | if (err == 0) { |
| 3975 | mddev->ro = 2; |
| 3976 | set_disk_ro(mddev->gendisk, 0); |
| 3977 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3978 | } else { |
| 3979 | mddev->ro = 2; |
| 3980 | err = do_md_run(mddev); |
| 3981 | } |
| 3982 | break; |
| 3983 | case clean: |
| 3984 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 3985 | err = restart_array(mddev); |
| 3986 | if (err) |
| 3987 | break; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3988 | spin_lock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3989 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3990 | if (mddev->in_sync == 0) { |
| 3991 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3992 | if (mddev->safemode == 1) |
| 3993 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3994 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3995 | } |
| 3996 | err = 0; |
| 3997 | } else |
| 3998 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3999 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 4000 | } else |
| 4001 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4002 | break; |
| 4003 | case active: |
| 4004 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4005 | err = restart_array(mddev); |
| 4006 | if (err) |
| 4007 | break; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 4008 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4009 | wake_up(&mddev->sb_wait); |
| 4010 | err = 0; |
| 4011 | } else { |
| 4012 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4013 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4014 | err = do_md_run(mddev); |
| 4015 | } |
| 4016 | break; |
| 4017 | case write_pending: |
| 4018 | case active_idle: |
| 4019 | /* these cannot be set */ |
| 4020 | break; |
| 4021 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4022 | |
| 4023 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 4024 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4025 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4026 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4027 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4028 | mddev_unlock(mddev); |
| 4029 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4030 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4031 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4032 | __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] | 4033 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4034 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4035 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4036 | return sprintf(page, "%d\n", |
| 4037 | atomic_read(&mddev->max_corr_read_errors)); |
| 4038 | } |
| 4039 | |
| 4040 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4041 | 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] | 4042 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4043 | unsigned int n; |
| 4044 | int rv; |
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 | rv = kstrtouint(buf, 10, &n); |
| 4047 | if (rv < 0) |
| 4048 | return rv; |
| 4049 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4050 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | static struct md_sysfs_entry max_corr_read_errors = |
| 4054 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4055 | max_corrected_read_errors_store); |
| 4056 | |
| 4057 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4058 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4059 | { |
| 4060 | return -EINVAL; |
| 4061 | } |
| 4062 | |
| 4063 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4064 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4065 | { |
| 4066 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4067 | /* The new device is added to the array. |
| 4068 | * If the array has a persistent superblock, we read the |
| 4069 | * superblock to initialise info and check validity. |
| 4070 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4071 | * which mainly checks size. |
| 4072 | */ |
| 4073 | char *e; |
| 4074 | int major = simple_strtoul(buf, &e, 10); |
| 4075 | int minor; |
| 4076 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4077 | struct md_rdev *rdev; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4078 | int err; |
| 4079 | |
| 4080 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4081 | return -EINVAL; |
| 4082 | minor = simple_strtoul(e+1, &e, 10); |
| 4083 | if (*e && *e != '\n') |
| 4084 | return -EINVAL; |
| 4085 | dev = MKDEV(major, minor); |
| 4086 | if (major != MAJOR(dev) || |
| 4087 | minor != MINOR(dev)) |
| 4088 | return -EOVERFLOW; |
| 4089 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4090 | flush_workqueue(md_misc_wq); |
| 4091 | |
| 4092 | err = mddev_lock(mddev); |
| 4093 | if (err) |
| 4094 | return err; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4095 | if (mddev->persistent) { |
| 4096 | rdev = md_import_device(dev, mddev->major_version, |
| 4097 | mddev->minor_version); |
| 4098 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4099 | struct md_rdev *rdev0 |
| 4100 | = list_entry(mddev->disks.next, |
| 4101 | struct md_rdev, same_set); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4102 | err = super_types[mddev->major_version] |
| 4103 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4104 | if (err < 0) |
| 4105 | goto out; |
| 4106 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4107 | } else if (mddev->external) |
| 4108 | rdev = md_import_device(dev, -2, -1); |
| 4109 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4110 | rdev = md_import_device(dev, -1, -1); |
| 4111 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4112 | if (IS_ERR(rdev)) { |
| 4113 | mddev_unlock(mddev); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4114 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4115 | } |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4116 | err = bind_rdev_to_array(rdev, mddev); |
| 4117 | out: |
| 4118 | if (err) |
| 4119 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4120 | mddev_unlock(mddev); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4121 | return err ? err : len; |
| 4122 | } |
| 4123 | |
| 4124 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4125 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4126 | |
| 4127 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4128 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4129 | { |
| 4130 | char *end; |
| 4131 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4132 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4133 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4134 | err = mddev_lock(mddev); |
| 4135 | if (err) |
| 4136 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4137 | if (!mddev->bitmap) |
| 4138 | goto out; |
| 4139 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4140 | while (*buf) { |
| 4141 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4142 | if (buf == end) break; |
| 4143 | if (*end == '-') { /* range */ |
| 4144 | buf = end + 1; |
| 4145 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4146 | if (buf == end) break; |
| 4147 | } |
| 4148 | if (*end && !isspace(*end)) break; |
| 4149 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4150 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4151 | } |
| 4152 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4153 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4154 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4155 | return len; |
| 4156 | } |
| 4157 | |
| 4158 | static struct md_sysfs_entry md_bitmap = |
| 4159 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4160 | |
| 4161 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4162 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4163 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4164 | return sprintf(page, "%llu\n", |
| 4165 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4166 | } |
| 4167 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4168 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4169 | |
| 4170 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4171 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4172 | { |
| 4173 | /* If array is inactive, we can reduce the component size, but |
| 4174 | * not increase it (except from 0). |
| 4175 | * If array is active, we can try an on-line resize |
| 4176 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4177 | sector_t sectors; |
| 4178 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4179 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4180 | if (err < 0) |
| 4181 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4182 | err = mddev_lock(mddev); |
| 4183 | if (err) |
| 4184 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4185 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4186 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4187 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4188 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4189 | if (mddev->dev_sectors == 0 || |
| 4190 | mddev->dev_sectors > sectors) |
| 4191 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4192 | else |
| 4193 | err = -ENOSPC; |
| 4194 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4195 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4196 | return err ? err : len; |
| 4197 | } |
| 4198 | |
| 4199 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4200 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4201 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4202 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4203 | * This is one of |
| 4204 | * 'none' for arrays with no metadata (good luck...) |
| 4205 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4206 | * or N.M for internally known formats |
| 4207 | */ |
| 4208 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4209 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4210 | { |
| 4211 | if (mddev->persistent) |
| 4212 | return sprintf(page, "%d.%d\n", |
| 4213 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4214 | else if (mddev->external) |
| 4215 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4216 | else |
| 4217 | return sprintf(page, "none\n"); |
| 4218 | } |
| 4219 | |
| 4220 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4221 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4222 | { |
| 4223 | int major, minor; |
| 4224 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4225 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4226 | /* Changing the details of 'external' metadata is |
| 4227 | * always permitted. Otherwise there must be |
| 4228 | * no devices attached to the array. |
| 4229 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4230 | |
| 4231 | err = mddev_lock(mddev); |
| 4232 | if (err) |
| 4233 | return err; |
| 4234 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4235 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4236 | ; |
| 4237 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4238 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4239 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4240 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4241 | if (cmd_match(buf, "none")) { |
| 4242 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4243 | mddev->external = 0; |
| 4244 | mddev->major_version = 0; |
| 4245 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4246 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4247 | } |
| 4248 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4249 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4250 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4251 | namelen = sizeof(mddev->metadata_type)-1; |
| 4252 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4253 | mddev->metadata_type[namelen] = 0; |
| 4254 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4255 | mddev->metadata_type[--namelen] = 0; |
| 4256 | mddev->persistent = 0; |
| 4257 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4258 | mddev->major_version = 0; |
| 4259 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4260 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4261 | } |
| 4262 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4263 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4264 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4265 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4266 | buf = e+1; |
| 4267 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4268 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4269 | goto out_unlock; |
| 4270 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4271 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4272 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4273 | mddev->major_version = major; |
| 4274 | mddev->minor_version = minor; |
| 4275 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4276 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4277 | err = 0; |
| 4278 | out_unlock: |
| 4279 | mddev_unlock(mddev); |
| 4280 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4284 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4285 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4286 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4287 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4288 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4289 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4290 | unsigned long recovery = mddev->recovery; |
| 4291 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4292 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4293 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4294 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4295 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4296 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4297 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4298 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4299 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4300 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4301 | type = "check"; |
| 4302 | else |
| 4303 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4304 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4305 | type = "recover"; |
NeilBrown | 985ca97 | 2015-07-06 12:26:57 +1000 | [diff] [blame] | 4306 | else if (mddev->reshape_position != MaxSector) |
| 4307 | type = "reshape"; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4308 | } |
| 4309 | return sprintf(page, "%s\n", type); |
| 4310 | } |
| 4311 | |
| 4312 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4313 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4314 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4315 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4316 | return -EINVAL; |
| 4317 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4318 | |
| 4319 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4320 | if (cmd_match(page, "frozen")) |
| 4321 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4322 | else |
| 4323 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4324 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4325 | mddev_lock(mddev) == 0) { |
| 4326 | flush_workqueue(md_misc_wq); |
| 4327 | if (mddev->sync_thread) { |
| 4328 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4329 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4330 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4331 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4332 | } |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4333 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4334 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4335 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4336 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4337 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4338 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4339 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4340 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4341 | int err; |
| 4342 | if (mddev->pers->start_reshape == NULL) |
| 4343 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4344 | err = mddev_lock(mddev); |
| 4345 | if (!err) { |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4346 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4347 | err = -EBUSY; |
| 4348 | else { |
| 4349 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4350 | err = mddev->pers->start_reshape(mddev); |
| 4351 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4352 | mddev_unlock(mddev); |
| 4353 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4354 | if (err) |
| 4355 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4356 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4357 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4358 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4359 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4360 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4361 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4362 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4363 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4364 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4365 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4366 | if (mddev->ro == 2) { |
| 4367 | /* A write to sync_action is enough to justify |
| 4368 | * canceling read-auto mode |
| 4369 | */ |
| 4370 | mddev->ro = 0; |
| 4371 | md_wakeup_thread(mddev->sync_thread); |
| 4372 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4373 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4374 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4375 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4376 | return len; |
| 4377 | } |
| 4378 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4379 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4380 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4381 | |
| 4382 | static ssize_t |
| 4383 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4384 | { |
| 4385 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4386 | } |
| 4387 | |
| 4388 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4389 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4390 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4391 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4392 | { |
| 4393 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4394 | (unsigned long long) |
| 4395 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4396 | } |
| 4397 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4398 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4399 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4400 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4401 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4402 | { |
| 4403 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4404 | mddev->sync_speed_min ? "local": "system"); |
| 4405 | } |
| 4406 | |
| 4407 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4408 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4409 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4410 | unsigned int min; |
| 4411 | int rv; |
| 4412 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4413 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4414 | min = 0; |
| 4415 | } else { |
| 4416 | rv = kstrtouint(buf, 10, &min); |
| 4417 | if (rv < 0) |
| 4418 | return rv; |
| 4419 | if (min == 0) |
| 4420 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4421 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4422 | mddev->sync_speed_min = min; |
| 4423 | return len; |
| 4424 | } |
| 4425 | |
| 4426 | static struct md_sysfs_entry md_sync_min = |
| 4427 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4428 | |
| 4429 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4430 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4431 | { |
| 4432 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4433 | mddev->sync_speed_max ? "local": "system"); |
| 4434 | } |
| 4435 | |
| 4436 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4437 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4438 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4439 | unsigned int max; |
| 4440 | int rv; |
| 4441 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4442 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4443 | max = 0; |
| 4444 | } else { |
| 4445 | rv = kstrtouint(buf, 10, &max); |
| 4446 | if (rv < 0) |
| 4447 | return rv; |
| 4448 | if (max == 0) |
| 4449 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4450 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4451 | mddev->sync_speed_max = max; |
| 4452 | return len; |
| 4453 | } |
| 4454 | |
| 4455 | static struct md_sysfs_entry md_sync_max = |
| 4456 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4457 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4458 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4459 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4460 | { |
| 4461 | return sprintf(page, "%d\n", mddev->degraded); |
| 4462 | } |
| 4463 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4464 | |
| 4465 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4466 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4467 | { |
| 4468 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4469 | } |
| 4470 | |
| 4471 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4472 | 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] | 4473 | { |
| 4474 | long n; |
| 4475 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4476 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4477 | return -EINVAL; |
| 4478 | |
| 4479 | if (n != 0 && n != 1) |
| 4480 | return -EINVAL; |
| 4481 | |
| 4482 | mddev->parallel_resync = n; |
| 4483 | |
| 4484 | if (mddev->sync_thread) |
| 4485 | wake_up(&resync_wait); |
| 4486 | |
| 4487 | return len; |
| 4488 | } |
| 4489 | |
| 4490 | /* force parallel resync, even with shared block devices */ |
| 4491 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4492 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4493 | sync_force_parallel_show, sync_force_parallel_store); |
| 4494 | |
| 4495 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4496 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4497 | { |
| 4498 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4499 | if (mddev->curr_resync == 0) |
| 4500 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4501 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4502 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4503 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4504 | db = resync - mddev->resync_mark_cnt; |
| 4505 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4506 | } |
| 4507 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4508 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4509 | |
| 4510 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4511 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4512 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4513 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4514 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4515 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4516 | return sprintf(page, "none\n"); |
| 4517 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4518 | if (mddev->curr_resync == 1 || |
| 4519 | mddev->curr_resync == 2) |
| 4520 | return sprintf(page, "delayed\n"); |
| 4521 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4522 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4523 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4524 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4525 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4526 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4527 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4528 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4529 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4530 | } |
| 4531 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4532 | static struct md_sysfs_entry md_sync_completed = |
| 4533 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4534 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4535 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4536 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4537 | { |
| 4538 | return sprintf(page, "%llu\n", |
| 4539 | (unsigned long long)mddev->resync_min); |
| 4540 | } |
| 4541 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4542 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4543 | { |
| 4544 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4545 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4546 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4547 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4548 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4549 | |
| 4550 | spin_lock(&mddev->lock); |
| 4551 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4552 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4553 | goto out_unlock; |
| 4554 | |
| 4555 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4556 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4557 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4558 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 4559 | /* Round down to multiple of 4K for safety */ |
| 4560 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4561 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4562 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4563 | out_unlock: |
| 4564 | spin_unlock(&mddev->lock); |
| 4565 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4566 | } |
| 4567 | |
| 4568 | static struct md_sysfs_entry md_min_sync = |
| 4569 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4570 | |
| 4571 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4572 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4573 | { |
| 4574 | if (mddev->resync_max == MaxSector) |
| 4575 | return sprintf(page, "max\n"); |
| 4576 | else |
| 4577 | return sprintf(page, "%llu\n", |
| 4578 | (unsigned long long)mddev->resync_max); |
| 4579 | } |
| 4580 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4581 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4582 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4583 | int err; |
| 4584 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4585 | if (strncmp(buf, "max", 3) == 0) |
| 4586 | mddev->resync_max = MaxSector; |
| 4587 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4588 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4589 | int chunk; |
| 4590 | |
| 4591 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4592 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4593 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4594 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4595 | goto out_unlock; |
| 4596 | |
| 4597 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4598 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4599 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4600 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4601 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4602 | |
| 4603 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4604 | chunk = mddev->chunk_sectors; |
| 4605 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4606 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4607 | |
| 4608 | err = -EINVAL; |
| 4609 | if (sector_div(temp, chunk)) |
| 4610 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4611 | } |
| 4612 | mddev->resync_max = max; |
| 4613 | } |
| 4614 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4615 | err = 0; |
| 4616 | out_unlock: |
| 4617 | spin_unlock(&mddev->lock); |
| 4618 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4619 | } |
| 4620 | |
| 4621 | static struct md_sysfs_entry md_max_sync = |
| 4622 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4623 | |
| 4624 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4625 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4626 | { |
| 4627 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4628 | } |
| 4629 | |
| 4630 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4631 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4632 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4633 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4634 | int err; |
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 | err = kstrtoull(buf, 10, &new); |
| 4637 | if (err < 0) |
| 4638 | return err; |
| 4639 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4640 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4641 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4642 | err = mddev_lock(mddev); |
| 4643 | if (err) |
| 4644 | return err; |
| 4645 | err = -EINVAL; |
| 4646 | if (mddev->pers == NULL || |
| 4647 | mddev->pers->quiesce == NULL) |
| 4648 | goto unlock; |
| 4649 | old = mddev->suspend_lo; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4650 | mddev->suspend_lo = new; |
| 4651 | if (new >= old) |
| 4652 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4653 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4654 | else { |
| 4655 | /* Expanding suspended region - need to wait */ |
| 4656 | mddev->pers->quiesce(mddev, 1); |
| 4657 | mddev->pers->quiesce(mddev, 0); |
| 4658 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4659 | err = 0; |
| 4660 | unlock: |
| 4661 | mddev_unlock(mddev); |
| 4662 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4663 | } |
| 4664 | static struct md_sysfs_entry md_suspend_lo = |
| 4665 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4666 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4667 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4668 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4669 | { |
| 4670 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4671 | } |
| 4672 | |
| 4673 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4674 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4675 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4676 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4677 | int err; |
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 | err = kstrtoull(buf, 10, &new); |
| 4680 | if (err < 0) |
| 4681 | return err; |
| 4682 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4683 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4684 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4685 | err = mddev_lock(mddev); |
| 4686 | if (err) |
| 4687 | return err; |
| 4688 | err = -EINVAL; |
| 4689 | if (mddev->pers == NULL || |
| 4690 | mddev->pers->quiesce == NULL) |
| 4691 | goto unlock; |
| 4692 | old = mddev->suspend_hi; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4693 | mddev->suspend_hi = new; |
| 4694 | if (new <= old) |
| 4695 | /* Shrinking suspended region */ |
| 4696 | mddev->pers->quiesce(mddev, 2); |
| 4697 | else { |
| 4698 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4699 | mddev->pers->quiesce(mddev, 1); |
| 4700 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4701 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4702 | err = 0; |
| 4703 | unlock: |
| 4704 | mddev_unlock(mddev); |
| 4705 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4706 | } |
| 4707 | static struct md_sysfs_entry md_suspend_hi = |
| 4708 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4709 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4710 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4711 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4712 | { |
| 4713 | if (mddev->reshape_position != MaxSector) |
| 4714 | return sprintf(page, "%llu\n", |
| 4715 | (unsigned long long)mddev->reshape_position); |
| 4716 | strcpy(page, "none\n"); |
| 4717 | return 5; |
| 4718 | } |
| 4719 | |
| 4720 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4721 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4722 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4723 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4724 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4725 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4726 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4727 | err = kstrtoull(buf, 10, &new); |
| 4728 | if (err < 0) |
| 4729 | return err; |
| 4730 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4731 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4732 | err = mddev_lock(mddev); |
| 4733 | if (err) |
| 4734 | return err; |
| 4735 | err = -EBUSY; |
| 4736 | if (mddev->pers) |
| 4737 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4738 | mddev->reshape_position = new; |
| 4739 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4740 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4741 | mddev->new_level = mddev->level; |
| 4742 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4743 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4744 | rdev_for_each(rdev, mddev) |
| 4745 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4746 | err = 0; |
| 4747 | unlock: |
| 4748 | mddev_unlock(mddev); |
| 4749 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4750 | } |
| 4751 | |
| 4752 | static struct md_sysfs_entry md_reshape_position = |
| 4753 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4754 | reshape_position_store); |
| 4755 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4756 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4757 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4758 | { |
| 4759 | return sprintf(page, "%s\n", |
| 4760 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4761 | } |
| 4762 | |
| 4763 | static ssize_t |
| 4764 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4765 | { |
| 4766 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4767 | int err; |
| 4768 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4769 | if (cmd_match(buf, "forwards")) |
| 4770 | backwards = 0; |
| 4771 | else if (cmd_match(buf, "backwards")) |
| 4772 | backwards = 1; |
| 4773 | else |
| 4774 | return -EINVAL; |
| 4775 | if (mddev->reshape_backwards == backwards) |
| 4776 | return len; |
| 4777 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4778 | err = mddev_lock(mddev); |
| 4779 | if (err) |
| 4780 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4781 | /* check if we are allowed to change */ |
| 4782 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4783 | err = -EBUSY; |
| 4784 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4785 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4786 | err = -EINVAL; |
| 4787 | else |
| 4788 | mddev->reshape_backwards = backwards; |
| 4789 | mddev_unlock(mddev); |
| 4790 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | static struct md_sysfs_entry md_reshape_direction = |
| 4794 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4795 | reshape_direction_store); |
| 4796 | |
| 4797 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4798 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4799 | { |
| 4800 | if (mddev->external_size) |
| 4801 | return sprintf(page, "%llu\n", |
| 4802 | (unsigned long long)mddev->array_sectors/2); |
| 4803 | else |
| 4804 | return sprintf(page, "default\n"); |
| 4805 | } |
| 4806 | |
| 4807 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4808 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4809 | { |
| 4810 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4811 | int err; |
| 4812 | |
| 4813 | err = mddev_lock(mddev); |
| 4814 | if (err) |
| 4815 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4816 | |
| 4817 | if (strncmp(buf, "default", 7) == 0) { |
| 4818 | if (mddev->pers) |
| 4819 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4820 | else |
| 4821 | sectors = mddev->array_sectors; |
| 4822 | |
| 4823 | mddev->external_size = 0; |
| 4824 | } else { |
| 4825 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4826 | err = -EINVAL; |
| 4827 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 4828 | err = -E2BIG; |
| 4829 | else |
| 4830 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4831 | } |
| 4832 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4833 | if (!err) { |
| 4834 | mddev->array_sectors = sectors; |
| 4835 | if (mddev->pers) { |
| 4836 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4837 | revalidate_disk(mddev->gendisk); |
| 4838 | } |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4839 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4840 | mddev_unlock(mddev); |
| 4841 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4842 | } |
| 4843 | |
| 4844 | static struct md_sysfs_entry md_array_size = |
| 4845 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4846 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4847 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4848 | static struct attribute *md_default_attrs[] = { |
| 4849 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4850 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4851 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4852 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4853 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4854 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4855 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4856 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4857 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4858 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4859 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4860 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4861 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4862 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4863 | NULL, |
| 4864 | }; |
| 4865 | |
| 4866 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4867 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4868 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4869 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4870 | &md_sync_min.attr, |
| 4871 | &md_sync_max.attr, |
| 4872 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4873 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4874 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4875 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4876 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4877 | &md_suspend_lo.attr, |
| 4878 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4879 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4880 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4881 | NULL, |
| 4882 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4883 | static struct attribute_group md_redundancy_group = { |
| 4884 | .name = NULL, |
| 4885 | .attrs = md_redundancy_attrs, |
| 4886 | }; |
| 4887 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4888 | static ssize_t |
| 4889 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4890 | { |
| 4891 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4892 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4893 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4894 | |
| 4895 | if (!entry->show) |
| 4896 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4897 | spin_lock(&all_mddevs_lock); |
| 4898 | if (list_empty(&mddev->all_mddevs)) { |
| 4899 | spin_unlock(&all_mddevs_lock); |
| 4900 | return -EBUSY; |
| 4901 | } |
| 4902 | mddev_get(mddev); |
| 4903 | spin_unlock(&all_mddevs_lock); |
| 4904 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4905 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4906 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4907 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | static ssize_t |
| 4911 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4912 | const char *page, size_t length) |
| 4913 | { |
| 4914 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4915 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4916 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4917 | |
| 4918 | if (!entry->store) |
| 4919 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4920 | if (!capable(CAP_SYS_ADMIN)) |
| 4921 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4922 | spin_lock(&all_mddevs_lock); |
| 4923 | if (list_empty(&mddev->all_mddevs)) { |
| 4924 | spin_unlock(&all_mddevs_lock); |
| 4925 | return -EBUSY; |
| 4926 | } |
| 4927 | mddev_get(mddev); |
| 4928 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4929 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4930 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4931 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | static void md_free(struct kobject *ko) |
| 4935 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4936 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4937 | |
| 4938 | if (mddev->sysfs_state) |
| 4939 | sysfs_put(mddev->sysfs_state); |
| 4940 | |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 4941 | if (mddev->queue) |
| 4942 | blk_cleanup_queue(mddev->queue); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4943 | if (mddev->gendisk) { |
| 4944 | del_gendisk(mddev->gendisk); |
| 4945 | put_disk(mddev->gendisk); |
| 4946 | } |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4947 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4948 | kfree(mddev); |
| 4949 | } |
| 4950 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4951 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4952 | .show = md_attr_show, |
| 4953 | .store = md_attr_store, |
| 4954 | }; |
| 4955 | static struct kobj_type md_ktype = { |
| 4956 | .release = md_free, |
| 4957 | .sysfs_ops = &md_sysfs_ops, |
| 4958 | .default_attrs = md_default_attrs, |
| 4959 | }; |
| 4960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | int mdp_major = 0; |
| 4962 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4963 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4964 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4965 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4966 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4967 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4968 | kobject_del(&mddev->kobj); |
| 4969 | kobject_put(&mddev->kobj); |
| 4970 | } |
| 4971 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4972 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4974 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4975 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4977 | int partitioned; |
| 4978 | int shift; |
| 4979 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4980 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | |
| 4982 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4983 | return -ENODEV; |
| 4984 | |
| 4985 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4986 | shift = partitioned ? MdpMinorShift : 0; |
| 4987 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4988 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4989 | /* wait for any previous instance of this device to be |
| 4990 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4991 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4992 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4993 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4994 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4995 | error = -EEXIST; |
| 4996 | if (mddev->gendisk) |
| 4997 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4998 | |
| 4999 | if (name) { |
| 5000 | /* Need to ensure that 'name' is not a duplicate. |
| 5001 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5002 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5003 | spin_lock(&all_mddevs_lock); |
| 5004 | |
| 5005 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 5006 | if (mddev2->gendisk && |
| 5007 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 5008 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5009 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5010 | } |
| 5011 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5013 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5014 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5015 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5016 | if (!mddev->queue) |
| 5017 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5018 | mddev->queue->queuedata = mddev; |
| 5019 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5020 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 5021 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | disk = alloc_disk(1 << shift); |
| 5024 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5025 | blk_cleanup_queue(mddev->queue); |
| 5026 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5027 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5029 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5031 | if (name) |
| 5032 | strcpy(disk->disk_name, name); |
| 5033 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5035 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | disk->fops = &md_fops; |
| 5038 | disk->private_data = mddev; |
| 5039 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5040 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5041 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5042 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5043 | * remove it now. |
| 5044 | */ |
| 5045 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5047 | /* As soon as we call add_disk(), another thread could get |
| 5048 | * through to md_open, so make sure it doesn't get too far |
| 5049 | */ |
| 5050 | mutex_lock(&mddev->open_mutex); |
| 5051 | add_disk(disk); |
| 5052 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 5053 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 5054 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5055 | if (error) { |
| 5056 | /* This isn't possible, but as kobject_init_and_add is marked |
| 5057 | * __must_check, we must do something with the result |
| 5058 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5059 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 5060 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5061 | error = 0; |
| 5062 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5063 | if (mddev->kobj.sd && |
| 5064 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5065 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5066 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5067 | abort: |
| 5068 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5069 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5070 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5071 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5072 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5073 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5074 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5075 | } |
| 5076 | |
| 5077 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 5078 | { |
| 5079 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5080 | return NULL; |
| 5081 | } |
| 5082 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5083 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 5084 | { |
| 5085 | /* val must be "md_*" where * is not all digits. |
| 5086 | * We allocate an array with a large free minor number, and |
| 5087 | * set the name to val. val must not already be an active name. |
| 5088 | */ |
| 5089 | int len = strlen(val); |
| 5090 | char buf[DISK_NAME_LEN]; |
| 5091 | |
| 5092 | while (len && val[len-1] == '\n') |
| 5093 | len--; |
| 5094 | if (len >= DISK_NAME_LEN) |
| 5095 | return -E2BIG; |
| 5096 | strlcpy(buf, val, len+1); |
| 5097 | if (strncmp(buf, "md_", 3) != 0) |
| 5098 | return -EINVAL; |
| 5099 | return md_alloc(0, buf); |
| 5100 | } |
| 5101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | static void md_safemode_timeout(unsigned long data) |
| 5103 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5104 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5105 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5106 | if (!atomic_read(&mddev->writes_pending)) { |
| 5107 | mddev->safemode = 1; |
| 5108 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5109 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | md_wakeup_thread(mddev->thread); |
| 5112 | } |
| 5113 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5114 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5116 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5118 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5119 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5120 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5122 | if (list_empty(&mddev->disks)) |
| 5123 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5124 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5125 | |
| 5126 | if (mddev->pers) |
| 5127 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5128 | /* Cannot run until previous stop completes properly */ |
| 5129 | if (mddev->sysfs_active) |
| 5130 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | /* |
| 5133 | * Analyze all RAID superblock(s) |
| 5134 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5135 | if (!mddev->raid_disks) { |
| 5136 | if (!mddev->persistent) |
| 5137 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5138 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5141 | if (mddev->level != LEVEL_NONE) |
| 5142 | request_module("md-level-%d", mddev->level); |
| 5143 | else if (mddev->clevel[0]) |
| 5144 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | |
| 5146 | /* |
| 5147 | * Drop all container device buffers, from now on |
| 5148 | * the only valid external interface is through the md |
| 5149 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5151 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5152 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | continue; |
| 5154 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5155 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5156 | |
| 5157 | /* perform some consistency tests on the device. |
| 5158 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5159 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5160 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5161 | if (rdev->meta_bdev) { |
| 5162 | /* Nothing to check */; |
| 5163 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5164 | if (mddev->dev_sectors && |
| 5165 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5166 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5167 | printk("md: %s: data overlaps metadata\n", |
| 5168 | mdname(mddev)); |
| 5169 | return -EINVAL; |
| 5170 | } |
| 5171 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5172 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5173 | > rdev->data_offset) { |
| 5174 | printk("md: %s: metadata overlaps data\n", |
| 5175 | mdname(mddev)); |
| 5176 | return -EINVAL; |
| 5177 | } |
| 5178 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5179 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | } |
| 5181 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5182 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 5183 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5185 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5186 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5187 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5188 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5189 | if (mddev->level != LEVEL_NONE) |
| 5190 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5191 | mddev->level); |
| 5192 | else |
| 5193 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5194 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | return -EINVAL; |
| 5196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5197 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5198 | if (mddev->level != pers->level) { |
| 5199 | mddev->level = pers->level; |
| 5200 | mddev->new_level = pers->level; |
| 5201 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5202 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5204 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5205 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5206 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5207 | module_put(pers->owner); |
| 5208 | return -EINVAL; |
| 5209 | } |
| 5210 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5211 | if (pers->sync_request) { |
| 5212 | /* Warn if this is a potentially silly |
| 5213 | * configuration. |
| 5214 | */ |
| 5215 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5216 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5217 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5218 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5219 | rdev_for_each(rdev, mddev) |
| 5220 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5221 | if (rdev < rdev2 && |
| 5222 | rdev->bdev->bd_contains == |
| 5223 | rdev2->bdev->bd_contains) { |
| 5224 | printk(KERN_WARNING |
| 5225 | "%s: WARNING: %s appears to be" |
| 5226 | " on the same physical disk as" |
| 5227 | " %s.\n", |
| 5228 | mdname(mddev), |
| 5229 | bdevname(rdev->bdev,b), |
| 5230 | bdevname(rdev2->bdev,b2)); |
| 5231 | warned = 1; |
| 5232 | } |
| 5233 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5234 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5235 | if (warned) |
| 5236 | printk(KERN_WARNING |
| 5237 | "True protection against single-disk" |
| 5238 | " failure might be compromised.\n"); |
| 5239 | } |
| 5240 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5241 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5242 | /* may be over-ridden by personality */ |
| 5243 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5244 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5245 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5247 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5248 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5249 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5250 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5251 | if (err) |
| 5252 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5253 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5254 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5255 | " but 'external_size' not in effect?\n", __func__); |
| 5256 | printk(KERN_ERR |
| 5257 | "md: invalid array_size %llu > default size %llu\n", |
| 5258 | (unsigned long long)mddev->array_sectors / 2, |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5259 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5260 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5261 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5262 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5263 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5264 | struct bitmap *bitmap; |
| 5265 | |
| 5266 | bitmap = bitmap_create(mddev, -1); |
| 5267 | if (IS_ERR(bitmap)) { |
| 5268 | err = PTR_ERR(bitmap); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5269 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5270 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5271 | } else |
| 5272 | mddev->bitmap = bitmap; |
| 5273 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5274 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | if (err) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5276 | mddev_detach(mddev); |
NeilBrown | 0c35bd4 | 2015-03-13 11:51:18 +1100 | [diff] [blame] | 5277 | if (mddev->private) |
| 5278 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5279 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5280 | module_put(pers->owner); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5281 | bitmap_destroy(mddev); |
| 5282 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5284 | if (mddev->queue) { |
| 5285 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5286 | mddev->queue->backing_dev_info.congested_fn = md_congested; |
| 5287 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5288 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5289 | if (mddev->kobj.sd && |
| 5290 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5291 | printk(KERN_WARNING |
| 5292 | "md: cannot register extra attributes for %s\n", |
| 5293 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5294 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5295 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5296 | mddev->ro = 0; |
| 5297 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5298 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5299 | atomic_set(&mddev->max_corr_read_errors, |
| 5300 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5301 | mddev->safemode = 0; |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5302 | if (mddev_is_clustered(mddev)) |
| 5303 | mddev->safemode_delay = 0; |
| 5304 | else |
| 5305 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5307 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5308 | spin_lock(&mddev->lock); |
| 5309 | mddev->pers = pers; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5310 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5311 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5312 | if (rdev->raid_disk >= 0) |
| 5313 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5314 | /* failure here is OK */; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5315 | |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 5316 | if (mddev->degraded && !mddev->ro) |
| 5317 | /* This ensures that recovering status is reported immediately |
| 5318 | * via sysfs - until a lack of spares is confirmed. |
| 5319 | */ |
| 5320 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5321 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5322 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5323 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5324 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5326 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5327 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5328 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5329 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5330 | return 0; |
| 5331 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5332 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5333 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5334 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5335 | { |
| 5336 | int err; |
| 5337 | |
| 5338 | err = md_run(mddev); |
| 5339 | if (err) |
| 5340 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5341 | err = bitmap_load(mddev); |
| 5342 | if (err) { |
| 5343 | bitmap_destroy(mddev); |
| 5344 | goto out; |
| 5345 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5346 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5347 | if (mddev_is_clustered(mddev)) |
| 5348 | md_allow_write(mddev); |
| 5349 | |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5350 | md_wakeup_thread(mddev->thread); |
| 5351 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5352 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5353 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5354 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5355 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5356 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5357 | out: |
| 5358 | return err; |
| 5359 | } |
| 5360 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5361 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5362 | { |
| 5363 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5364 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5365 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5367 | return -ENXIO; |
| 5368 | if (!mddev->pers) |
| 5369 | return -EINVAL; |
| 5370 | if (!mddev->ro) |
| 5371 | return -EBUSY; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 5372 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 5373 | struct md_rdev *rdev; |
| 5374 | bool has_journal = false; |
| 5375 | |
| 5376 | rcu_read_lock(); |
| 5377 | rdev_for_each_rcu(rdev, mddev) { |
| 5378 | if (test_bit(Journal, &rdev->flags) && |
| 5379 | !test_bit(Faulty, &rdev->flags)) { |
| 5380 | has_journal = true; |
| 5381 | break; |
| 5382 | } |
| 5383 | } |
| 5384 | rcu_read_unlock(); |
| 5385 | |
| 5386 | /* Don't restart rw with journal missing/faulty */ |
| 5387 | if (!has_journal) |
| 5388 | return -EINVAL; |
| 5389 | } |
| 5390 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5391 | mddev->safemode = 0; |
| 5392 | mddev->ro = 0; |
| 5393 | set_disk_ro(disk, 0); |
| 5394 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5395 | mdname(mddev)); |
| 5396 | /* Kick recovery or resync if necessary */ |
| 5397 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5398 | md_wakeup_thread(mddev->thread); |
| 5399 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5400 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5401 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | } |
| 5403 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5404 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5405 | { |
| 5406 | mddev->array_sectors = 0; |
| 5407 | mddev->external_size = 0; |
| 5408 | mddev->dev_sectors = 0; |
| 5409 | mddev->raid_disks = 0; |
| 5410 | mddev->recovery_cp = 0; |
| 5411 | mddev->resync_min = 0; |
| 5412 | mddev->resync_max = MaxSector; |
| 5413 | mddev->reshape_position = MaxSector; |
| 5414 | mddev->external = 0; |
| 5415 | mddev->persistent = 0; |
| 5416 | mddev->level = LEVEL_NONE; |
| 5417 | mddev->clevel[0] = 0; |
| 5418 | mddev->flags = 0; |
| 5419 | mddev->ro = 0; |
| 5420 | mddev->metadata_type[0] = 0; |
| 5421 | mddev->chunk_sectors = 0; |
| 5422 | mddev->ctime = mddev->utime = 0; |
| 5423 | mddev->layout = 0; |
| 5424 | mddev->max_disks = 0; |
| 5425 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5426 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5427 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5428 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5429 | mddev->new_level = LEVEL_NONE; |
| 5430 | mddev->new_layout = 0; |
| 5431 | mddev->new_chunk_sectors = 0; |
| 5432 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5433 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5434 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5435 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5436 | mddev->recovery = 0; |
| 5437 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5438 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5439 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5440 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5441 | mddev->private = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5442 | mddev->bitmap_info.offset = 0; |
| 5443 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5444 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5445 | mddev->bitmap_info.chunksize = 0; |
| 5446 | mddev->bitmap_info.daemon_sleep = 0; |
| 5447 | mddev->bitmap_info.max_write_behind = 0; |
| 5448 | } |
| 5449 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5450 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5451 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 5452 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5453 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5454 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5455 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5456 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5457 | } |
| 5458 | |
| 5459 | del_timer_sync(&mddev->safemode_timer); |
| 5460 | |
| 5461 | bitmap_flush(mddev); |
| 5462 | md_super_wait(mddev); |
| 5463 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5464 | if (mddev->ro == 0 && |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5465 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
| 5466 | (mddev->flags & MD_UPDATE_SB_FLAGS))) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5467 | /* mark array as shutdown cleanly */ |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5468 | if (!mddev_is_clustered(mddev)) |
| 5469 | mddev->in_sync = 1; |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5470 | md_update_sb(mddev, 1); |
| 5471 | } |
| 5472 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5473 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5474 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5475 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 5476 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5477 | __md_stop_writes(mddev); |
| 5478 | mddev_unlock(mddev); |
| 5479 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5480 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5481 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5482 | static void mddev_detach(struct mddev *mddev) |
| 5483 | { |
| 5484 | struct bitmap *bitmap = mddev->bitmap; |
| 5485 | /* wait for behind writes to complete */ |
| 5486 | if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { |
| 5487 | printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n", |
| 5488 | mdname(mddev)); |
| 5489 | /* need to kick something here to make sure I/O goes? */ |
| 5490 | wait_event(bitmap->behind_wait, |
| 5491 | atomic_read(&bitmap->behind_writes) == 0); |
| 5492 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5493 | if (mddev->pers && mddev->pers->quiesce) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5494 | mddev->pers->quiesce(mddev, 1); |
| 5495 | mddev->pers->quiesce(mddev, 0); |
| 5496 | } |
| 5497 | md_unregister_thread(&mddev->thread); |
| 5498 | if (mddev->queue) |
| 5499 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 5500 | } |
| 5501 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5502 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5503 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5504 | struct md_personality *pers = mddev->pers; |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5505 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 5506 | /* Ensure ->event_work is done */ |
| 5507 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5508 | spin_lock(&mddev->lock); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5509 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5510 | spin_unlock(&mddev->lock); |
| 5511 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5512 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5513 | if (pers->sync_request && mddev->to_remove == NULL) |
| 5514 | mddev->to_remove = &md_redundancy_group; |
| 5515 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5516 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5517 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5518 | |
| 5519 | void md_stop(struct mddev *mddev) |
| 5520 | { |
| 5521 | /* stop the array and free an attached data structures. |
| 5522 | * This is called from dm-raid |
| 5523 | */ |
| 5524 | __md_stop(mddev); |
| 5525 | bitmap_destroy(mddev); |
| 5526 | if (mddev->bio_set) |
| 5527 | bioset_free(mddev->bio_set); |
| 5528 | } |
| 5529 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5530 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5531 | |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5532 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5533 | { |
| 5534 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5535 | int did_freeze = 0; |
| 5536 | |
| 5537 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5538 | did_freeze = 1; |
| 5539 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5540 | md_wakeup_thread(mddev->thread); |
| 5541 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5542 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5543 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5544 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5545 | /* Thread might be blocked waiting for metadata update |
| 5546 | * which will now never happen */ |
| 5547 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5548 | |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5549 | if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
| 5550 | return -EBUSY; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5551 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5552 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 5553 | &mddev->recovery)); |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5554 | wait_event(mddev->sb_wait, |
| 5555 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5556 | mddev_lock_nointr(mddev); |
| 5557 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5558 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5559 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5560 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5561 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5562 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5563 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5564 | if (did_freeze) { |
| 5565 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5566 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5567 | md_wakeup_thread(mddev->thread); |
| 5568 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5569 | err = -EBUSY; |
| 5570 | goto out; |
| 5571 | } |
| 5572 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5573 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5574 | |
| 5575 | err = -ENXIO; |
| 5576 | if (mddev->ro==1) |
| 5577 | goto out; |
| 5578 | mddev->ro = 1; |
| 5579 | set_disk_ro(mddev->gendisk, 1); |
| 5580 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5581 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5582 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5583 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5584 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5585 | } |
| 5586 | out: |
| 5587 | mutex_unlock(&mddev->open_mutex); |
| 5588 | return err; |
| 5589 | } |
| 5590 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5591 | /* mode: |
| 5592 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5593 | * 2 - stop but do not disassemble array |
| 5594 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5595 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5596 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5597 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5598 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5599 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5600 | int did_freeze = 0; |
| 5601 | |
| 5602 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5603 | did_freeze = 1; |
| 5604 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5605 | md_wakeup_thread(mddev->thread); |
| 5606 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5607 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5608 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5609 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5610 | /* Thread might be blocked waiting for metadata update |
| 5611 | * which will now never happen */ |
| 5612 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5613 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5614 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5615 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 5616 | !test_bit(MD_RECOVERY_RUNNING, |
| 5617 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5618 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5619 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5620 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5621 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5622 | mddev->sysfs_active || |
| 5623 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5624 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5625 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5626 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5627 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5628 | if (did_freeze) { |
| 5629 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5630 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5631 | md_wakeup_thread(mddev->thread); |
| 5632 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 5633 | return -EBUSY; |
| 5634 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5635 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5636 | if (mddev->ro) |
| 5637 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5638 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5639 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5640 | __md_stop(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5641 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5642 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5643 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5644 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5645 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5646 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5647 | if (rdev->raid_disk >= 0) |
| 5648 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5649 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5650 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5651 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5652 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5653 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5654 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5655 | if (mddev->ro) |
| 5656 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5657 | } else |
| 5658 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | /* |
| 5660 | * Free resources if final stop |
| 5661 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5662 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5663 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5664 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5665 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5666 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5667 | struct file *f = mddev->bitmap_info.file; |
| 5668 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5669 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5670 | spin_unlock(&mddev->lock); |
| 5671 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5672 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5673 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5675 | export_array(mddev); |
| 5676 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5677 | md_clean(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5678 | if (mddev->hold_active == UNTIL_STOP) |
| 5679 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5680 | } |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5681 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5682 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5683 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5684 | } |
| 5685 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5686 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5687 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5689 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5690 | int err; |
| 5691 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5692 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5693 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | |
| 5695 | printk(KERN_INFO "md: running: "); |
| 5696 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5697 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5698 | char b[BDEVNAME_SIZE]; |
| 5699 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5700 | } |
| 5701 | printk("\n"); |
| 5702 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5703 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5704 | if (err) { |
| 5705 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea0 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5706 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5707 | } |
| 5708 | } |
| 5709 | |
| 5710 | /* |
| 5711 | * lets try to run arrays based on all disks that have arrived |
| 5712 | * until now. (those are in pending_raid_disks) |
| 5713 | * |
| 5714 | * the method: pick the first pending disk, collect all disks with |
| 5715 | * the same UUID, remove all from the pending list and put them into |
| 5716 | * the 'same_array' list. Then order this list based on superblock |
| 5717 | * update time (freshest comes first), kick out 'old' disks and |
| 5718 | * compare superblocks. If everything's fine then run it. |
| 5719 | * |
| 5720 | * If "unit" is allocated, then bump its reference count |
| 5721 | */ |
| 5722 | static void autorun_devices(int part) |
| 5723 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5724 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5725 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5726 | char b[BDEVNAME_SIZE]; |
| 5727 | |
| 5728 | printk(KERN_INFO "md: autorun ...\n"); |
| 5729 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5730 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5732 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5733 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5734 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | |
| 5736 | printk(KERN_INFO "md: considering %s ...\n", |
| 5737 | bdevname(rdev0->bdev,b)); |
| 5738 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5739 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5741 | printk(KERN_INFO "md: adding %s ...\n", |
| 5742 | bdevname(rdev->bdev,b)); |
| 5743 | list_move(&rdev->same_set, &candidates); |
| 5744 | } |
| 5745 | /* |
| 5746 | * now we have a set of devices, with all of them having |
| 5747 | * mostly sane superblocks. It's time to allocate the |
| 5748 | * mddev. |
| 5749 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5750 | if (part) { |
| 5751 | dev = MKDEV(mdp_major, |
| 5752 | rdev0->preferred_minor << MdpMinorShift); |
| 5753 | unit = MINOR(dev) >> MdpMinorShift; |
| 5754 | } else { |
| 5755 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5756 | unit = MINOR(dev); |
| 5757 | } |
| 5758 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5759 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5760 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5761 | break; |
| 5762 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5763 | |
| 5764 | md_probe(dev, NULL, NULL); |
| 5765 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5766 | if (!mddev || !mddev->gendisk) { |
| 5767 | if (mddev) |
| 5768 | mddev_put(mddev); |
| 5769 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5770 | "md: cannot allocate memory for md drive.\n"); |
| 5771 | break; |
| 5772 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5773 | if (mddev_lock(mddev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5775 | mdname(mddev)); |
| 5776 | else if (mddev->raid_disks || mddev->major_version |
| 5777 | || !list_empty(&mddev->disks)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5778 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | "md: %s already running, cannot run %s\n", |
| 5780 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5781 | mddev_unlock(mddev); |
| 5782 | } else { |
| 5783 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5784 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5785 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5786 | list_del_init(&rdev->same_set); |
| 5787 | if (bind_rdev_to_array(rdev, mddev)) |
| 5788 | export_rdev(rdev); |
| 5789 | } |
| 5790 | autorun_array(mddev); |
| 5791 | mddev_unlock(mddev); |
| 5792 | } |
| 5793 | /* on success, candidates will be empty, on error |
| 5794 | * it won't... |
| 5795 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5796 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5797 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5798 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5799 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5800 | mddev_put(mddev); |
| 5801 | } |
| 5802 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5803 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5804 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5806 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | { |
| 5808 | mdu_version_t ver; |
| 5809 | |
| 5810 | ver.major = MD_MAJOR_VERSION; |
| 5811 | ver.minor = MD_MINOR_VERSION; |
| 5812 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5813 | |
| 5814 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5815 | return -EFAULT; |
| 5816 | |
| 5817 | return 0; |
| 5818 | } |
| 5819 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5820 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | { |
| 5822 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5823 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5824 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5826 | nr = working = insync = failed = spare = 0; |
| 5827 | rcu_read_lock(); |
| 5828 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5829 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5830 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | failed++; |
| 5832 | else { |
| 5833 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5834 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5835 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5836 | else |
| 5837 | spare++; |
| 5838 | } |
| 5839 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5840 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5841 | |
| 5842 | info.major_version = mddev->major_version; |
| 5843 | info.minor_version = mddev->minor_version; |
| 5844 | info.patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 5845 | info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5846 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5847 | info.size = mddev->dev_sectors / 2; |
| 5848 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5849 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | info.nr_disks = nr; |
| 5851 | info.raid_disks = mddev->raid_disks; |
| 5852 | info.md_minor = mddev->md_minor; |
| 5853 | info.not_persistent= !mddev->persistent; |
| 5854 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 5855 | info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | info.state = 0; |
| 5857 | if (mddev->in_sync) |
| 5858 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5859 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 5860 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 5861 | if (mddev_is_clustered(mddev)) |
| 5862 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5863 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 | info.working_disks = working; |
| 5865 | info.failed_disks = failed; |
| 5866 | info.spare_disks = spare; |
| 5867 | |
| 5868 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5869 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5870 | |
| 5871 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5872 | return -EFAULT; |
| 5873 | |
| 5874 | return 0; |
| 5875 | } |
| 5876 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5877 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5878 | { |
| 5879 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5880 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5881 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5882 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5883 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5884 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5885 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5886 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5887 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5888 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5889 | /* bitmap enabled */ |
| 5890 | if (mddev->bitmap_info.file) { |
| 5891 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 5892 | sizeof(file->pathname)); |
| 5893 | if (IS_ERR(ptr)) |
| 5894 | err = PTR_ERR(ptr); |
| 5895 | else |
| 5896 | memmove(file->pathname, ptr, |
| 5897 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 5898 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5899 | spin_unlock(&mddev->lock); |
| 5900 | |
| 5901 | if (err == 0 && |
| 5902 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5903 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5904 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5905 | kfree(file); |
| 5906 | return err; |
| 5907 | } |
| 5908 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5909 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | { |
| 5911 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5912 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5913 | |
| 5914 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5915 | return -EFAULT; |
| 5916 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5917 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 5918 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5919 | if (rdev) { |
| 5920 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5921 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5922 | info.raid_disk = rdev->raid_disk; |
| 5923 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5924 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5925 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5926 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5927 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5928 | info.state |= (1<<MD_DISK_SYNC); |
| 5929 | } |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 5930 | if (test_bit(Journal, &rdev->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 5931 | info.state |= (1<<MD_DISK_JOURNAL); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5932 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5933 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | } else { |
| 5935 | info.major = info.minor = 0; |
| 5936 | info.raid_disk = -1; |
| 5937 | info.state = (1<<MD_DISK_REMOVED); |
| 5938 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5939 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5940 | |
| 5941 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5942 | return -EFAULT; |
| 5943 | |
| 5944 | return 0; |
| 5945 | } |
| 5946 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5947 | 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] | 5948 | { |
| 5949 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5950 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | dev_t dev = MKDEV(info->major,info->minor); |
| 5952 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5953 | if (mddev_is_clustered(mddev) && |
| 5954 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 5955 | pr_err("%s: Cannot add to clustered mddev.\n", |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5956 | mdname(mddev)); |
| 5957 | return -EINVAL; |
| 5958 | } |
| 5959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5961 | return -EOVERFLOW; |
| 5962 | |
| 5963 | if (!mddev->raid_disks) { |
| 5964 | int err; |
| 5965 | /* expecting a device which has a superblock */ |
| 5966 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5967 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5968 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5969 | "md: md_import_device returned %ld\n", |
| 5970 | PTR_ERR(rdev)); |
| 5971 | return PTR_ERR(rdev); |
| 5972 | } |
| 5973 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5974 | struct md_rdev *rdev0 |
| 5975 | = list_entry(mddev->disks.next, |
| 5976 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5977 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5978 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5979 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5980 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5981 | "md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5982 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5983 | bdevname(rdev0->bdev,b2)); |
| 5984 | export_rdev(rdev); |
| 5985 | return -EINVAL; |
| 5986 | } |
| 5987 | } |
| 5988 | err = bind_rdev_to_array(rdev, mddev); |
| 5989 | if (err) |
| 5990 | export_rdev(rdev); |
| 5991 | return err; |
| 5992 | } |
| 5993 | |
| 5994 | /* |
| 5995 | * add_new_disk can be used once the array is assembled |
| 5996 | * to add "hot spares". They must already have a superblock |
| 5997 | * written |
| 5998 | */ |
| 5999 | if (mddev->pers) { |
| 6000 | int err; |
| 6001 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6002 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6003 | "%s: personality does not support diskops!\n", |
| 6004 | mdname(mddev)); |
| 6005 | return -EINVAL; |
| 6006 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 6007 | if (mddev->persistent) |
| 6008 | rdev = md_import_device(dev, mddev->major_version, |
| 6009 | mddev->minor_version); |
| 6010 | else |
| 6011 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6012 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6013 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | "md: md_import_device returned %ld\n", |
| 6015 | PTR_ERR(rdev)); |
| 6016 | return PTR_ERR(rdev); |
| 6017 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 6018 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6019 | if (!mddev->persistent) { |
| 6020 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6021 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6022 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6023 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 6024 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6025 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6026 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 6027 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6028 | } else |
| 6029 | super_types[mddev->major_version]. |
| 6030 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6031 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 6032 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6033 | /* This was a hot-add request, but events doesn't |
| 6034 | * match, so reject it. |
| 6035 | */ |
| 6036 | export_rdev(rdev); |
| 6037 | return -EINVAL; |
| 6038 | } |
| 6039 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6040 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6041 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6042 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6043 | else |
| 6044 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6045 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6046 | if (info->state & (1<<MD_DISK_JOURNAL)) { |
| 6047 | struct md_rdev *rdev2; |
| 6048 | bool has_journal = false; |
| 6049 | |
| 6050 | /* make sure no existing journal disk */ |
| 6051 | rdev_for_each(rdev2, mddev) { |
| 6052 | if (test_bit(Journal, &rdev2->flags)) { |
| 6053 | has_journal = true; |
| 6054 | break; |
| 6055 | } |
| 6056 | } |
| 6057 | if (has_journal) { |
| 6058 | export_rdev(rdev); |
| 6059 | return -EBUSY; |
| 6060 | } |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6061 | set_bit(Journal, &rdev->flags); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6062 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6063 | /* |
| 6064 | * check whether the device shows up in other nodes |
| 6065 | */ |
| 6066 | if (mddev_is_clustered(mddev)) { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6067 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6068 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6069 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6070 | /* --add initiated by this node */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6071 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6072 | if (err) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6073 | export_rdev(rdev); |
| 6074 | return err; |
| 6075 | } |
| 6076 | } |
| 6077 | } |
| 6078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6079 | rdev->raid_disk = -1; |
| 6080 | err = bind_rdev_to_array(rdev, mddev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | if (err) |
| 6083 | export_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6084 | |
| 6085 | if (mddev_is_clustered(mddev)) { |
| 6086 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
| 6087 | md_cluster_ops->new_disk_ack(mddev, (err == 0)); |
| 6088 | else { |
| 6089 | if (err) |
| 6090 | md_cluster_ops->add_new_disk_cancel(mddev); |
| 6091 | else |
| 6092 | err = add_bound_rdev(rdev); |
| 6093 | } |
| 6094 | |
| 6095 | } else if (!err) |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 6096 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6098 | return err; |
| 6099 | } |
| 6100 | |
| 6101 | /* otherwise, add_new_disk is only allowed |
| 6102 | * for major_version==0 superblocks |
| 6103 | */ |
| 6104 | if (mddev->major_version != 0) { |
| 6105 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 6106 | mdname(mddev)); |
| 6107 | return -EINVAL; |
| 6108 | } |
| 6109 | |
| 6110 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 6111 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6112 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6113 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6114 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | "md: error, md_import_device() returned %ld\n", |
| 6116 | PTR_ERR(rdev)); |
| 6117 | return PTR_ERR(rdev); |
| 6118 | } |
| 6119 | rdev->desc_nr = info->number; |
| 6120 | if (info->raid_disk < mddev->raid_disks) |
| 6121 | rdev->raid_disk = info->raid_disk; |
| 6122 | else |
| 6123 | rdev->raid_disk = -1; |
| 6124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6126 | if (info->state & (1<<MD_DISK_SYNC)) |
| 6127 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6129 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6130 | set_bit(WriteMostly, &rdev->flags); |
| 6131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6132 | if (!mddev->persistent) { |
| 6133 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6134 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 6135 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6136 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6137 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6138 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6139 | err = bind_rdev_to_array(rdev, mddev); |
| 6140 | if (err) { |
| 6141 | export_rdev(rdev); |
| 6142 | return err; |
| 6143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6144 | } |
| 6145 | |
| 6146 | return 0; |
| 6147 | } |
| 6148 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6149 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6150 | { |
| 6151 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6152 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6154 | rdev = find_rdev(mddev, dev); |
| 6155 | if (!rdev) |
| 6156 | return -ENXIO; |
| 6157 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6158 | if (rdev->raid_disk < 0) |
| 6159 | goto kick_rdev; |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6160 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6161 | clear_bit(Blocked, &rdev->flags); |
| 6162 | remove_and_add_spares(mddev, rdev); |
| 6163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6164 | if (rdev->raid_disk >= 0) |
| 6165 | goto busy; |
| 6166 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6167 | kick_rdev: |
Goldwyn Rodrigues | 54a8839 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6168 | if (mddev_is_clustered(mddev)) |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6169 | md_cluster_ops->remove_disk(mddev, rdev); |
| 6170 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6171 | md_kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6172 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6173 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6174 | |
| 6175 | return 0; |
| 6176 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 6177 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6178 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6179 | return -EBUSY; |
| 6180 | } |
| 6181 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6182 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6183 | { |
| 6184 | char b[BDEVNAME_SIZE]; |
| 6185 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6186 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6187 | |
| 6188 | if (!mddev->pers) |
| 6189 | return -ENODEV; |
| 6190 | |
| 6191 | if (mddev->major_version != 0) { |
| 6192 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 6193 | " version-0 superblocks.\n", |
| 6194 | mdname(mddev)); |
| 6195 | return -EINVAL; |
| 6196 | } |
| 6197 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6198 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6199 | "%s: personality does not support diskops!\n", |
| 6200 | mdname(mddev)); |
| 6201 | return -EINVAL; |
| 6202 | } |
| 6203 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6204 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6205 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6206 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6207 | "md: error, md_import_device() returned %ld\n", |
| 6208 | PTR_ERR(rdev)); |
| 6209 | return -EINVAL; |
| 6210 | } |
| 6211 | |
| 6212 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6213 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6214 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6215 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6216 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6217 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6218 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6219 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6220 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6221 | "md: can not hot-add faulty %s disk to %s!\n", |
| 6222 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6223 | err = -EINVAL; |
| 6224 | goto abort_export; |
| 6225 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6226 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6227 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6228 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 6229 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6230 | err = bind_rdev_to_array(rdev, mddev); |
| 6231 | if (err) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 6232 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6233 | |
| 6234 | /* |
| 6235 | * The rest should better be atomic, we can have disk failures |
| 6236 | * noticed in interrupt contexts ... |
| 6237 | */ |
| 6238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6239 | rdev->raid_disk = -1; |
| 6240 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6241 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6242 | /* |
| 6243 | * Kick recovery, maybe this spare has to be added to the |
| 6244 | * array immediately. |
| 6245 | */ |
| 6246 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6247 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6248 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | return 0; |
| 6250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6251 | abort_export: |
| 6252 | export_rdev(rdev); |
| 6253 | return err; |
| 6254 | } |
| 6255 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6256 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6257 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6258 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6259 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6260 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 6261 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6262 | return -EBUSY; |
| 6263 | if (mddev->recovery || mddev->sync_thread) |
| 6264 | return -EBUSY; |
| 6265 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6266 | } |
| 6267 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6268 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6269 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6270 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6271 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6272 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 6273 | return -EEXIST; /* cannot add when bitmap is present */ |
| 6274 | f = fget(fd); |
| 6275 | |
| 6276 | if (f == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6277 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 6278 | mdname(mddev)); |
| 6279 | return -EBADF; |
| 6280 | } |
| 6281 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6282 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6283 | if (!S_ISREG(inode->i_mode)) { |
| 6284 | printk(KERN_ERR "%s: error: bitmap file must be a regular file\n", |
| 6285 | mdname(mddev)); |
| 6286 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6287 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6288 | printk(KERN_ERR "%s: error: bitmap file must open for write\n", |
| 6289 | mdname(mddev)); |
| 6290 | err = -EBADF; |
| 6291 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6292 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 6293 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6294 | err = -EBUSY; |
| 6295 | } |
| 6296 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6297 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6298 | return err; |
| 6299 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6300 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6301 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6302 | } else if (mddev->bitmap == NULL) |
| 6303 | return -ENOENT; /* cannot remove what isn't there */ |
| 6304 | err = 0; |
| 6305 | if (mddev->pers) { |
| 6306 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6307 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6308 | struct bitmap *bitmap; |
| 6309 | |
| 6310 | bitmap = bitmap_create(mddev, -1); |
| 6311 | if (!IS_ERR(bitmap)) { |
| 6312 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6313 | err = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6314 | } else |
| 6315 | err = PTR_ERR(bitmap); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6316 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6317 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6318 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6319 | fd = -1; /* make sure to put the file */ |
| 6320 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6321 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6322 | } |
| 6323 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6324 | struct file *f = mddev->bitmap_info.file; |
| 6325 | if (f) { |
| 6326 | spin_lock(&mddev->lock); |
| 6327 | mddev->bitmap_info.file = NULL; |
| 6328 | spin_unlock(&mddev->lock); |
| 6329 | fput(f); |
| 6330 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6331 | } |
| 6332 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6333 | return err; |
| 6334 | } |
| 6335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6336 | /* |
| 6337 | * set_array_info is used two different ways |
| 6338 | * The original usage is when creating a new array. |
| 6339 | * In this usage, raid_disks is > 0 and it together with |
| 6340 | * level, size, not_persistent,layout,chunksize determine the |
| 6341 | * shape of the array. |
| 6342 | * This will always create an array with a type-0.90.0 superblock. |
| 6343 | * The newer usage is when assembling an array. |
| 6344 | * In this case raid_disks will be 0, and the major_version field is |
| 6345 | * use to determine which style super-blocks are to be found on the devices. |
| 6346 | * The minor and patch _version numbers are also kept incase the |
| 6347 | * super_block handler wishes to interpret them. |
| 6348 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6349 | 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] | 6350 | { |
| 6351 | |
| 6352 | if (info->raid_disks == 0) { |
| 6353 | /* just setting version number for superblock loading */ |
| 6354 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6355 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6356 | super_types[info->major_version].name == NULL) { |
| 6357 | /* maybe try to auto-load a module? */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6358 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6359 | "md: superblock version %d not known\n", |
| 6360 | info->major_version); |
| 6361 | return -EINVAL; |
| 6362 | } |
| 6363 | mddev->major_version = info->major_version; |
| 6364 | mddev->minor_version = info->minor_version; |
| 6365 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6366 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6367 | /* ensure mddev_put doesn't delete this now that there |
| 6368 | * is some minimal configuration. |
| 6369 | */ |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6370 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | return 0; |
| 6372 | } |
| 6373 | mddev->major_version = MD_MAJOR_VERSION; |
| 6374 | mddev->minor_version = MD_MINOR_VERSION; |
| 6375 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6376 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6377 | |
| 6378 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6379 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6380 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6381 | mddev->raid_disks = info->raid_disks; |
| 6382 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6383 | * openned |
| 6384 | */ |
| 6385 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6386 | mddev->recovery_cp = MaxSector; |
| 6387 | else |
| 6388 | mddev->recovery_cp = 0; |
| 6389 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6390 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6391 | |
| 6392 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6393 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | |
| 6395 | mddev->max_disks = MD_SB_DISKS; |
| 6396 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6397 | if (mddev->persistent) |
| 6398 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6399 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6400 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6401 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6402 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6403 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6404 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6405 | mddev->reshape_position = MaxSector; |
| 6406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6407 | /* |
| 6408 | * Generate a 128 bit UUID |
| 6409 | */ |
| 6410 | get_random_bytes(mddev->uuid, 16); |
| 6411 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6412 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6413 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6414 | mddev->new_layout = mddev->layout; |
| 6415 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6416 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6418 | return 0; |
| 6419 | } |
| 6420 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6421 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6422 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6423 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6424 | |
| 6425 | if (mddev->external_size) |
| 6426 | return; |
| 6427 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6428 | mddev->array_sectors = array_sectors; |
| 6429 | } |
| 6430 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6431 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6432 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6433 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6434 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6435 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6436 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6437 | |
| 6438 | if (mddev->pers->resize == NULL) |
| 6439 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6440 | /* The "num_sectors" is the number of sectors of each device that |
| 6441 | * is used. This can only make sense for arrays with redundancy. |
| 6442 | * linear and raid0 always use whatever space is available. We can only |
| 6443 | * consider changing this number if no resync or reconstruction is |
| 6444 | * 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] | 6445 | * 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] | 6446 | * of each device. If num_sectors is zero, we find the largest size |
| 6447 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6448 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6449 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6450 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6451 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6452 | if (mddev->ro) |
| 6453 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6454 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6455 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6456 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6457 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6458 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6459 | num_sectors = avail; |
| 6460 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6461 | return -ENOSPC; |
| 6462 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6463 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6464 | if (!rv) |
| 6465 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6466 | return rv; |
| 6467 | } |
| 6468 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6469 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6470 | { |
| 6471 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6472 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6473 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6474 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6475 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6476 | if (mddev->ro) |
| 6477 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6478 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6479 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6480 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6481 | if (mddev->sync_thread || |
| 6482 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6483 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6484 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6485 | |
| 6486 | rdev_for_each(rdev, mddev) { |
| 6487 | if (mddev->raid_disks < raid_disks && |
| 6488 | rdev->data_offset < rdev->new_data_offset) |
| 6489 | return -EINVAL; |
| 6490 | if (mddev->raid_disks > raid_disks && |
| 6491 | rdev->data_offset > rdev->new_data_offset) |
| 6492 | return -EINVAL; |
| 6493 | } |
| 6494 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6495 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6496 | if (mddev->delta_disks < 0) |
| 6497 | mddev->reshape_backwards = 1; |
| 6498 | else if (mddev->delta_disks > 0) |
| 6499 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6500 | |
| 6501 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6502 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6503 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6504 | mddev->reshape_backwards = 0; |
| 6505 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6506 | return rv; |
| 6507 | } |
| 6508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6509 | /* |
| 6510 | * update_array_info is used to change the configuration of an |
| 6511 | * on-line array. |
| 6512 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6513 | * fields in the info are checked against the array. |
| 6514 | * Any differences that cannot be handled will cause an error. |
| 6515 | * Normally, only one change can be managed at a time. |
| 6516 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6517 | 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] | 6518 | { |
| 6519 | int rv = 0; |
| 6520 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6521 | int state = 0; |
| 6522 | |
| 6523 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6524 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6525 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6526 | |
| 6527 | if (mddev->major_version != info->major_version || |
| 6528 | mddev->minor_version != info->minor_version || |
| 6529 | /* mddev->patch_version != info->patch_version || */ |
| 6530 | mddev->ctime != info->ctime || |
| 6531 | mddev->level != info->level || |
| 6532 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 6533 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6534 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6535 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6536 | ((state^info->state) & 0xfffffe00) |
| 6537 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6538 | return -EINVAL; |
| 6539 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6540 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6541 | cnt++; |
| 6542 | if (mddev->raid_disks != info->raid_disks) |
| 6543 | cnt++; |
| 6544 | if (mddev->layout != info->layout) |
| 6545 | cnt++; |
| 6546 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6547 | cnt++; |
| 6548 | if (cnt == 0) |
| 6549 | return 0; |
| 6550 | if (cnt > 1) |
| 6551 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6552 | |
| 6553 | if (mddev->layout != info->layout) { |
| 6554 | /* Change layout |
| 6555 | * we don't need to do anything at the md level, the |
| 6556 | * personality will take care of it all. |
| 6557 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6558 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6559 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6560 | else { |
| 6561 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6562 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6563 | if (rv) |
| 6564 | mddev->new_layout = mddev->layout; |
| 6565 | return rv; |
| 6566 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6567 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6568 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6569 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6570 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6571 | if (mddev->raid_disks != info->raid_disks) |
| 6572 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6573 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6574 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6575 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 6576 | rv = -EINVAL; |
| 6577 | goto err; |
| 6578 | } |
| 6579 | if (mddev->recovery || mddev->sync_thread) { |
| 6580 | rv = -EBUSY; |
| 6581 | goto err; |
| 6582 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6583 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6584 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6585 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6586 | if (mddev->bitmap) { |
| 6587 | rv = -EEXIST; |
| 6588 | goto err; |
| 6589 | } |
| 6590 | if (mddev->bitmap_info.default_offset == 0) { |
| 6591 | rv = -EINVAL; |
| 6592 | goto err; |
| 6593 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6594 | mddev->bitmap_info.offset = |
| 6595 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6596 | mddev->bitmap_info.space = |
| 6597 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6598 | mddev->pers->quiesce(mddev, 1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6599 | bitmap = bitmap_create(mddev, -1); |
| 6600 | if (!IS_ERR(bitmap)) { |
| 6601 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6602 | rv = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6603 | } else |
| 6604 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6605 | if (rv) |
| 6606 | bitmap_destroy(mddev); |
| 6607 | mddev->pers->quiesce(mddev, 0); |
| 6608 | } else { |
| 6609 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6610 | if (!mddev->bitmap) { |
| 6611 | rv = -ENOENT; |
| 6612 | goto err; |
| 6613 | } |
| 6614 | if (mddev->bitmap->storage.file) { |
| 6615 | rv = -EINVAL; |
| 6616 | goto err; |
| 6617 | } |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6618 | if (mddev->bitmap_info.nodes) { |
| 6619 | /* hold PW on all the bitmap lock */ |
| 6620 | if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) { |
| 6621 | printk("md: can't change bitmap to none since the" |
| 6622 | " array is in use by more than one node\n"); |
| 6623 | rv = -EPERM; |
| 6624 | md_cluster_ops->unlock_all_bitmaps(mddev); |
| 6625 | goto err; |
| 6626 | } |
| 6627 | |
| 6628 | mddev->bitmap_info.nodes = 0; |
| 6629 | md_cluster_ops->leave(mddev); |
| 6630 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6631 | mddev->pers->quiesce(mddev, 1); |
| 6632 | bitmap_destroy(mddev); |
| 6633 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6634 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6635 | } |
| 6636 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6637 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6638 | return rv; |
| 6639 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6640 | return rv; |
| 6641 | } |
| 6642 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6643 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6644 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6645 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6646 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6647 | |
| 6648 | if (mddev->pers == NULL) |
| 6649 | return -ENODEV; |
| 6650 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6651 | rcu_read_lock(); |
| 6652 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6653 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6654 | err = -ENODEV; |
| 6655 | else { |
| 6656 | md_error(mddev, rdev); |
| 6657 | if (!test_bit(Faulty, &rdev->flags)) |
| 6658 | err = -EBUSY; |
| 6659 | } |
| 6660 | rcu_read_unlock(); |
| 6661 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | } |
| 6663 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6664 | /* |
| 6665 | * We have a problem here : there is no easy way to give a CHS |
| 6666 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6667 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6668 | * dosfs just mad... ;-) |
| 6669 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6670 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6671 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6672 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6673 | |
| 6674 | geo->heads = 2; |
| 6675 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6676 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6677 | return 0; |
| 6678 | } |
| 6679 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6680 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 6681 | { |
| 6682 | switch (cmd) { |
| 6683 | case ADD_NEW_DISK: |
| 6684 | case BLKROSET: |
| 6685 | case GET_ARRAY_INFO: |
| 6686 | case GET_BITMAP_FILE: |
| 6687 | case GET_DISK_INFO: |
| 6688 | case HOT_ADD_DISK: |
| 6689 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6690 | case RAID_AUTORUN: |
| 6691 | case RAID_VERSION: |
| 6692 | case RESTART_ARRAY_RW: |
| 6693 | case RUN_ARRAY: |
| 6694 | case SET_ARRAY_INFO: |
| 6695 | case SET_BITMAP_FILE: |
| 6696 | case SET_DISK_FAULTY: |
| 6697 | case STOP_ARRAY: |
| 6698 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6699 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6700 | return true; |
| 6701 | default: |
| 6702 | return false; |
| 6703 | } |
| 6704 | } |
| 6705 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6706 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6707 | unsigned int cmd, unsigned long arg) |
| 6708 | { |
| 6709 | int err = 0; |
| 6710 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6711 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6712 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6713 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6714 | if (!md_ioctl_valid(cmd)) |
| 6715 | return -ENOTTY; |
| 6716 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6717 | switch (cmd) { |
| 6718 | case RAID_VERSION: |
| 6719 | case GET_ARRAY_INFO: |
| 6720 | case GET_DISK_INFO: |
| 6721 | break; |
| 6722 | default: |
| 6723 | if (!capable(CAP_SYS_ADMIN)) |
| 6724 | return -EACCES; |
| 6725 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6726 | |
| 6727 | /* |
| 6728 | * Commands dealing with the RAID driver but not any |
| 6729 | * particular array: |
| 6730 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6731 | switch (cmd) { |
| 6732 | case RAID_VERSION: |
| 6733 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6734 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6736 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6737 | case RAID_AUTORUN: |
| 6738 | err = 0; |
| 6739 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6740 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6741 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6742 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6743 | } |
| 6744 | |
| 6745 | /* |
| 6746 | * Commands creating/starting a new array: |
| 6747 | */ |
| 6748 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6749 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6750 | |
| 6751 | if (!mddev) { |
| 6752 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6753 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6754 | } |
| 6755 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6756 | /* Some actions do not requires the mutex */ |
| 6757 | switch (cmd) { |
| 6758 | case GET_ARRAY_INFO: |
| 6759 | if (!mddev->raid_disks && !mddev->external) |
| 6760 | err = -ENODEV; |
| 6761 | else |
| 6762 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6763 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6764 | |
| 6765 | case GET_DISK_INFO: |
| 6766 | if (!mddev->raid_disks && !mddev->external) |
| 6767 | err = -ENODEV; |
| 6768 | else |
| 6769 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6770 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6771 | |
| 6772 | case SET_DISK_FAULTY: |
| 6773 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6774 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6775 | |
| 6776 | case GET_BITMAP_FILE: |
| 6777 | err = get_bitmap_file(mddev, argp); |
| 6778 | goto out; |
| 6779 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6780 | } |
| 6781 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 6782 | if (cmd == ADD_NEW_DISK) |
| 6783 | /* need to ensure md_delayed_delete() has completed */ |
| 6784 | flush_workqueue(md_misc_wq); |
| 6785 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 6786 | if (cmd == HOT_REMOVE_DISK) |
| 6787 | /* need to ensure recovery thread has run */ |
| 6788 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 6789 | !test_bit(MD_RECOVERY_NEEDED, |
| 6790 | &mddev->flags), |
| 6791 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6792 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 6793 | /* Need to flush page cache, and ensure no-one else opens |
| 6794 | * and writes |
| 6795 | */ |
| 6796 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6797 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6798 | mutex_unlock(&mddev->open_mutex); |
| 6799 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6800 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6801 | } |
| 6802 | set_bit(MD_STILL_CLOSED, &mddev->flags); |
| 6803 | mutex_unlock(&mddev->open_mutex); |
| 6804 | sync_blockdev(bdev); |
| 6805 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6806 | err = mddev_lock(mddev); |
| 6807 | if (err) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6808 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6809 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6810 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6811 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6812 | } |
| 6813 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6814 | if (cmd == SET_ARRAY_INFO) { |
| 6815 | mdu_array_info_t info; |
| 6816 | if (!arg) |
| 6817 | memset(&info, 0, sizeof(info)); |
| 6818 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6819 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6820 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6821 | } |
| 6822 | if (mddev->pers) { |
| 6823 | err = update_array_info(mddev, &info); |
| 6824 | if (err) { |
| 6825 | printk(KERN_WARNING "md: couldn't update" |
| 6826 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6827 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6828 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6829 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6830 | } |
| 6831 | if (!list_empty(&mddev->disks)) { |
| 6832 | printk(KERN_WARNING |
| 6833 | "md: array %s already has disks!\n", |
| 6834 | mdname(mddev)); |
| 6835 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6836 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6837 | } |
| 6838 | if (mddev->raid_disks) { |
| 6839 | printk(KERN_WARNING |
| 6840 | "md: array %s already initialised!\n", |
| 6841 | mdname(mddev)); |
| 6842 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6843 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6844 | } |
| 6845 | err = set_array_info(mddev, &info); |
| 6846 | if (err) { |
| 6847 | printk(KERN_WARNING "md: couldn't set" |
| 6848 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6849 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6850 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6851 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6852 | } |
| 6853 | |
| 6854 | /* |
| 6855 | * Commands querying/configuring an existing array: |
| 6856 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6857 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6858 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6859 | if ((!mddev->raid_disks && !mddev->external) |
| 6860 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6861 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6862 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6863 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6864 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6865 | } |
| 6866 | |
| 6867 | /* |
| 6868 | * Commands even a read-only array can execute: |
| 6869 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6870 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6871 | case RESTART_ARRAY_RW: |
| 6872 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6873 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6874 | |
| 6875 | case STOP_ARRAY: |
| 6876 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6877 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6878 | |
| 6879 | case STOP_ARRAY_RO: |
| 6880 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6881 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6882 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6883 | case HOT_REMOVE_DISK: |
| 6884 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6885 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6886 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6887 | case ADD_NEW_DISK: |
| 6888 | /* We can support ADD_NEW_DISK on read-only arrays |
Wei Fang | 466ad29 | 2016-03-21 19:19:30 +0800 | [diff] [blame] | 6889 | * only if we are re-adding a preexisting device. |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6890 | * So require mddev->pers and MD_DISK_SYNC. |
| 6891 | */ |
| 6892 | if (mddev->pers) { |
| 6893 | mdu_disk_info_t info; |
| 6894 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6895 | err = -EFAULT; |
| 6896 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 6897 | /* Need to clear read-only for this */ |
| 6898 | break; |
| 6899 | else |
| 6900 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6901 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6902 | } |
| 6903 | break; |
| 6904 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6905 | case BLKROSET: |
| 6906 | if (get_user(ro, (int __user *)(arg))) { |
| 6907 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6908 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6909 | } |
| 6910 | err = -EINVAL; |
| 6911 | |
| 6912 | /* if the bdev is going readonly the value of mddev->ro |
| 6913 | * does not matter, no writes are coming |
| 6914 | */ |
| 6915 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6916 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6917 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6918 | /* are we are already prepared for writes? */ |
| 6919 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6920 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6921 | |
| 6922 | /* transitioning to readauto need only happen for |
| 6923 | * arrays that call md_write_start |
| 6924 | */ |
| 6925 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6926 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6927 | if (err == 0) { |
| 6928 | mddev->ro = 2; |
| 6929 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6930 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6931 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6932 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6933 | } |
| 6934 | |
| 6935 | /* |
| 6936 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6937 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6938 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 6939 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6940 | if (mddev->ro == 2) { |
| 6941 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6942 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6943 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6944 | /* mddev_unlock will wake thread */ |
| 6945 | /* If a device failed while we were read-only, we |
| 6946 | * need to make sure the metadata is updated now. |
| 6947 | */ |
| 6948 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
| 6949 | mddev_unlock(mddev); |
| 6950 | wait_event(mddev->sb_wait, |
| 6951 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && |
| 6952 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6953 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6954 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6955 | } else { |
| 6956 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6957 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6958 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6959 | } |
| 6960 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6961 | switch (cmd) { |
| 6962 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6963 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6964 | mdu_disk_info_t info; |
| 6965 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6966 | err = -EFAULT; |
| 6967 | else |
| 6968 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6969 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6970 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6971 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6972 | case CLUSTERED_DISK_NACK: |
| 6973 | if (mddev_is_clustered(mddev)) |
| 6974 | md_cluster_ops->new_disk_ack(mddev, false); |
| 6975 | else |
| 6976 | err = -EINVAL; |
| 6977 | goto unlock; |
| 6978 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6979 | case HOT_ADD_DISK: |
| 6980 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6981 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6982 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6983 | case RUN_ARRAY: |
| 6984 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6985 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6986 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6987 | case SET_BITMAP_FILE: |
| 6988 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6989 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6990 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6991 | default: |
| 6992 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6993 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6994 | } |
| 6995 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6996 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6997 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6998 | err != -EINVAL) |
| 6999 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7000 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7001 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7002 | return err; |
| 7003 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7004 | #ifdef CONFIG_COMPAT |
| 7005 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 7006 | unsigned int cmd, unsigned long arg) |
| 7007 | { |
| 7008 | switch (cmd) { |
| 7009 | case HOT_REMOVE_DISK: |
| 7010 | case HOT_ADD_DISK: |
| 7011 | case SET_DISK_FAULTY: |
| 7012 | case SET_BITMAP_FILE: |
| 7013 | /* These take in integer arg, do not convert */ |
| 7014 | break; |
| 7015 | default: |
| 7016 | arg = (unsigned long)compat_ptr(arg); |
| 7017 | break; |
| 7018 | } |
| 7019 | |
| 7020 | return md_ioctl(bdev, mode, cmd, arg); |
| 7021 | } |
| 7022 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7023 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7024 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7025 | { |
| 7026 | /* |
| 7027 | * Succeed if we can lock the mddev, which confirms that |
| 7028 | * it isn't being stopped right now. |
| 7029 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7030 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7031 | int err; |
| 7032 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 7033 | if (!mddev) |
| 7034 | return -ENODEV; |
| 7035 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7036 | if (mddev->gendisk != bdev->bd_disk) { |
| 7037 | /* we are racing with mddev_put which is discarding this |
| 7038 | * bd_disk. |
| 7039 | */ |
| 7040 | mddev_put(mddev); |
| 7041 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7042 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7043 | /* Then retry the open from the top */ |
| 7044 | return -ERESTARTSYS; |
| 7045 | } |
| 7046 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 7047 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7048 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7049 | goto out; |
| 7050 | |
| 7051 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7052 | atomic_inc(&mddev->openers); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7053 | clear_bit(MD_STILL_CLOSED, &mddev->flags); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7054 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7055 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7056 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7057 | out: |
| 7058 | return err; |
| 7059 | } |
| 7060 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 7061 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7062 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7063 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7064 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 7065 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7066 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7067 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7068 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7069 | |
| 7070 | static int md_media_changed(struct gendisk *disk) |
| 7071 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7072 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7073 | |
| 7074 | return mddev->changed; |
| 7075 | } |
| 7076 | |
| 7077 | static int md_revalidate(struct gendisk *disk) |
| 7078 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7079 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7080 | |
| 7081 | mddev->changed = 0; |
| 7082 | return 0; |
| 7083 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 7084 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7085 | { |
| 7086 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7087 | .open = md_open, |
| 7088 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 7089 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7090 | #ifdef CONFIG_COMPAT |
| 7091 | .compat_ioctl = md_compat_ioctl, |
| 7092 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7093 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7094 | .media_changed = md_media_changed, |
| 7095 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7096 | }; |
| 7097 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7098 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7099 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7100 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7102 | /* |
| 7103 | * md_thread is a 'system-thread', it's priority should be very |
| 7104 | * high. We avoid resource deadlocks individually in each |
| 7105 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 7106 | * the very same RT priority as kswapd, thus we will never get |
| 7107 | * into a priority inversion deadlock. |
| 7108 | * |
| 7109 | * we definitely have to have equal or higher priority than |
| 7110 | * bdflush, otherwise bdflush will deadlock if there are too |
| 7111 | * many dirty RAID5 blocks. |
| 7112 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7113 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7114 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7115 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7116 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7117 | /* We need to wait INTERRUPTIBLE so that |
| 7118 | * we don't add to the load-average. |
| 7119 | * That means we need to be sure no signals are |
| 7120 | * pending |
| 7121 | */ |
| 7122 | if (signal_pending(current)) |
| 7123 | flush_signals(current); |
| 7124 | |
| 7125 | wait_event_interruptible_timeout |
| 7126 | (thread->wqueue, |
| 7127 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 7128 | || kthread_should_stop(), |
| 7129 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7130 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7131 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 7132 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7133 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7134 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7136 | return 0; |
| 7137 | } |
| 7138 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7139 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | { |
| 7141 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7142 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7143 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 7144 | wake_up(&thread->wqueue); |
| 7145 | } |
| 7146 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7147 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7148 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7149 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7150 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7151 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7152 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7153 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7154 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7155 | if (!thread) |
| 7156 | return NULL; |
| 7157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7158 | init_waitqueue_head(&thread->wqueue); |
| 7159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7160 | thread->run = run; |
| 7161 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7162 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7163 | thread->tsk = kthread_run(md_thread, thread, |
| 7164 | "%s_%s", |
| 7165 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7166 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7167 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7168 | kfree(thread); |
| 7169 | return NULL; |
| 7170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7171 | return thread; |
| 7172 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7173 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7174 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7175 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7176 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7177 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7178 | if (!thread) |
| 7179 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7180 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7181 | /* Locking ensures that mddev_unlock does not wake_up a |
| 7182 | * non-existent thread |
| 7183 | */ |
| 7184 | spin_lock(&pers_lock); |
| 7185 | *threadp = NULL; |
| 7186 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7187 | |
| 7188 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7189 | kfree(thread); |
| 7190 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7191 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7192 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7193 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7194 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7195 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7196 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7197 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7198 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7199 | return; |
| 7200 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7201 | if (mddev->degraded) |
| 7202 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7203 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7204 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7205 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7206 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 7207 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7208 | queue_work(md_misc_wq, &mddev->event_work); |
Guoqing Jiang | bb9ef71 | 2015-12-28 10:46:38 +0800 | [diff] [blame] | 7209 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7210 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7211 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7212 | |
| 7213 | /* seq_file implementation /proc/mdstat */ |
| 7214 | |
| 7215 | static void status_unused(struct seq_file *seq) |
| 7216 | { |
| 7217 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7218 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7219 | |
| 7220 | seq_printf(seq, "unused devices: "); |
| 7221 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7222 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7223 | char b[BDEVNAME_SIZE]; |
| 7224 | i++; |
| 7225 | seq_printf(seq, "%s ", |
| 7226 | bdevname(rdev->bdev,b)); |
| 7227 | } |
| 7228 | if (!i) |
| 7229 | seq_printf(seq, "<none>"); |
| 7230 | |
| 7231 | seq_printf(seq, "\n"); |
| 7232 | } |
| 7233 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7234 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7235 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7236 | sector_t max_sectors, resync, res; |
| 7237 | unsigned long dt, db; |
| 7238 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7239 | int scale; |
| 7240 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7241 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7242 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 7243 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7244 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7245 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7246 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7247 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7248 | resync = mddev->curr_resync; |
| 7249 | if (resync <= 3) { |
| 7250 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7251 | /* Still cleaning up */ |
| 7252 | resync = max_sectors; |
| 7253 | } else |
| 7254 | resync -= atomic_read(&mddev->recovery_active); |
| 7255 | |
| 7256 | if (resync == 0) { |
| 7257 | if (mddev->recovery_cp < MaxSector) { |
| 7258 | seq_printf(seq, "\tresync=PENDING"); |
| 7259 | return 1; |
| 7260 | } |
| 7261 | return 0; |
| 7262 | } |
| 7263 | if (resync < 3) { |
| 7264 | seq_printf(seq, "\tresync=DELAYED"); |
| 7265 | return 1; |
| 7266 | } |
| 7267 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 7268 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7269 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7270 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7271 | * u32, as those are the requirements for sector_div. |
| 7272 | * Thus 'scale' must be at least 10 |
| 7273 | */ |
| 7274 | scale = 10; |
| 7275 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7276 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7277 | scale++; |
| 7278 | } |
| 7279 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7280 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7281 | |
| 7282 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7283 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7284 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7285 | seq_printf(seq, "["); |
| 7286 | for (i = 0; i < x; i++) |
| 7287 | seq_printf(seq, "="); |
| 7288 | seq_printf(seq, ">"); |
| 7289 | for (i = 0; i < y; i++) |
| 7290 | seq_printf(seq, "."); |
| 7291 | seq_printf(seq, "] "); |
| 7292 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7293 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7294 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 7295 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7296 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 7297 | "check" : |
| 7298 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 7299 | "resync" : "recovery"))), |
| 7300 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7301 | (unsigned long long) resync/2, |
| 7302 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7303 | |
| 7304 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7305 | * dt: time from mark until now |
| 7306 | * db: blocks written from mark until now |
| 7307 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7308 | * |
| 7309 | * rt is a sector_t, so could be 32bit or 64bit. |
| 7310 | * So we divide before multiply in case it is 32bit and close |
| 7311 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 7312 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7313 | * near the end of resync when the number of remaining sectors |
| 7314 | * is close to 'db'. |
| 7315 | * We then divide rt by 32 after multiplying by db to compensate. |
| 7316 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7317 | */ |
| 7318 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 7319 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7320 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 7321 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7322 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7323 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 7324 | sector_div(rt, db/32+1); |
| 7325 | rt *= dt; |
| 7326 | rt >>= 5; |
| 7327 | |
| 7328 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 7329 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7330 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7331 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7332 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7333 | } |
| 7334 | |
| 7335 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 7336 | { |
| 7337 | struct list_head *tmp; |
| 7338 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7339 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7340 | |
| 7341 | if (l >= 0x10000) |
| 7342 | return NULL; |
| 7343 | if (!l--) |
| 7344 | /* header */ |
| 7345 | return (void*)1; |
| 7346 | |
| 7347 | spin_lock(&all_mddevs_lock); |
| 7348 | list_for_each(tmp,&all_mddevs) |
| 7349 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7350 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7351 | mddev_get(mddev); |
| 7352 | spin_unlock(&all_mddevs_lock); |
| 7353 | return mddev; |
| 7354 | } |
| 7355 | spin_unlock(&all_mddevs_lock); |
| 7356 | if (!l--) |
| 7357 | return (void*)2;/* tail */ |
| 7358 | return NULL; |
| 7359 | } |
| 7360 | |
| 7361 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 7362 | { |
| 7363 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7364 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7366 | ++*pos; |
| 7367 | if (v == (void*)2) |
| 7368 | return NULL; |
| 7369 | |
| 7370 | spin_lock(&all_mddevs_lock); |
| 7371 | if (v == (void*)1) |
| 7372 | tmp = all_mddevs.next; |
| 7373 | else |
| 7374 | tmp = mddev->all_mddevs.next; |
| 7375 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7376 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7377 | else { |
| 7378 | next_mddev = (void*)2; |
| 7379 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7381 | spin_unlock(&all_mddevs_lock); |
| 7382 | |
| 7383 | if (v != (void*)1) |
| 7384 | mddev_put(mddev); |
| 7385 | return next_mddev; |
| 7386 | |
| 7387 | } |
| 7388 | |
| 7389 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 7390 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7391 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7392 | |
| 7393 | if (mddev && v != (void*)1 && v != (void*)2) |
| 7394 | mddev_put(mddev); |
| 7395 | } |
| 7396 | |
| 7397 | static int md_seq_show(struct seq_file *seq, void *v) |
| 7398 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7399 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7400 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7401 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7402 | |
| 7403 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7404 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7405 | seq_printf(seq, "Personalities : "); |
| 7406 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7407 | list_for_each_entry(pers, &pers_list, list) |
| 7408 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7409 | |
| 7410 | spin_unlock(&pers_lock); |
| 7411 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7412 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7413 | return 0; |
| 7414 | } |
| 7415 | if (v == (void*)2) { |
| 7416 | status_unused(seq); |
| 7417 | return 0; |
| 7418 | } |
| 7419 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7420 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7421 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7422 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7423 | mddev->pers ? "" : "in"); |
| 7424 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7425 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7426 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7427 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7428 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7429 | seq_printf(seq, " %s", mddev->pers->name); |
| 7430 | } |
| 7431 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7432 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7433 | rcu_read_lock(); |
| 7434 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7435 | char b[BDEVNAME_SIZE]; |
| 7436 | seq_printf(seq, " %s[%d]", |
| 7437 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7438 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7439 | seq_printf(seq, "(W)"); |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 7440 | if (test_bit(Journal, &rdev->flags)) |
| 7441 | seq_printf(seq, "(J)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7442 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7443 | seq_printf(seq, "(F)"); |
| 7444 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7445 | } |
| 7446 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7447 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7448 | if (test_bit(Replacement, &rdev->flags)) |
| 7449 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7450 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7451 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7452 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7453 | |
| 7454 | if (!list_empty(&mddev->disks)) { |
| 7455 | if (mddev->pers) |
| 7456 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7457 | (unsigned long long) |
| 7458 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7459 | else |
| 7460 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7461 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7462 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7463 | if (mddev->persistent) { |
| 7464 | if (mddev->major_version != 0 || |
| 7465 | mddev->minor_version != 90) { |
| 7466 | seq_printf(seq," super %d.%d", |
| 7467 | mddev->major_version, |
| 7468 | mddev->minor_version); |
| 7469 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7470 | } else if (mddev->external) |
| 7471 | seq_printf(seq, " super external:%s", |
| 7472 | mddev->metadata_type); |
| 7473 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7474 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7475 | |
| 7476 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7477 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7478 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7479 | if (mddev->pers->sync_request) { |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7480 | if (status_resync(seq, mddev)) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7481 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7482 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7483 | } else |
| 7484 | seq_printf(seq, "\n "); |
| 7485 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7486 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7487 | |
| 7488 | seq_printf(seq, "\n"); |
| 7489 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7490 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7492 | return 0; |
| 7493 | } |
| 7494 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7495 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7496 | .start = md_seq_start, |
| 7497 | .next = md_seq_next, |
| 7498 | .stop = md_seq_stop, |
| 7499 | .show = md_seq_show, |
| 7500 | }; |
| 7501 | |
| 7502 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7503 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7504 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7505 | int error; |
| 7506 | |
| 7507 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7508 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7509 | return error; |
| 7510 | |
| 7511 | seq = file->private_data; |
| 7512 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7513 | return error; |
| 7514 | } |
| 7515 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7516 | static int md_unloading; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7517 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7518 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7519 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7520 | int mask; |
| 7521 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7522 | if (md_unloading) |
kbuild test robot | 7d7e64f | 2014-12-03 16:07:59 +1100 | [diff] [blame] | 7523 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7524 | poll_wait(filp, &md_event_waiters, wait); |
| 7525 | |
| 7526 | /* always allow read */ |
| 7527 | mask = POLLIN | POLLRDNORM; |
| 7528 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7529 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7530 | mask |= POLLERR | POLLPRI; |
| 7531 | return mask; |
| 7532 | } |
| 7533 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7534 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7535 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7536 | .open = md_seq_open, |
| 7537 | .read = seq_read, |
| 7538 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7539 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7540 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7541 | }; |
| 7542 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7543 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7544 | { |
Chao Yu | 50bd377 | 2014-09-25 15:28:34 +0800 | [diff] [blame] | 7545 | printk(KERN_INFO "md: %s personality registered for level %d\n", |
| 7546 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7547 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7548 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7549 | spin_unlock(&pers_lock); |
| 7550 | return 0; |
| 7551 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7552 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7553 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7554 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7555 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7556 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7557 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7558 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7559 | spin_unlock(&pers_lock); |
| 7560 | return 0; |
| 7561 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7562 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7563 | |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7564 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
| 7565 | struct module *module) |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7566 | { |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7567 | int ret = 0; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7568 | spin_lock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7569 | if (md_cluster_ops != NULL) |
| 7570 | ret = -EALREADY; |
| 7571 | else { |
| 7572 | md_cluster_ops = ops; |
| 7573 | md_cluster_mod = module; |
| 7574 | } |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7575 | spin_unlock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7576 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7577 | } |
| 7578 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 7579 | |
| 7580 | int unregister_md_cluster_operations(void) |
| 7581 | { |
| 7582 | spin_lock(&pers_lock); |
| 7583 | md_cluster_ops = NULL; |
| 7584 | spin_unlock(&pers_lock); |
| 7585 | return 0; |
| 7586 | } |
| 7587 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 7588 | |
| 7589 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 7590 | { |
| 7591 | int err; |
| 7592 | |
| 7593 | err = request_module("md-cluster"); |
| 7594 | if (err) { |
| 7595 | pr_err("md-cluster module not found.\n"); |
Goldwyn Rodrigues | b0c26a7 | 2015-07-22 12:09:15 -0500 | [diff] [blame] | 7596 | return -ENOENT; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7597 | } |
| 7598 | |
| 7599 | spin_lock(&pers_lock); |
| 7600 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
| 7601 | spin_unlock(&pers_lock); |
| 7602 | return -ENOENT; |
| 7603 | } |
| 7604 | spin_unlock(&pers_lock); |
| 7605 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 7606 | return md_cluster_ops->join(mddev, nodes); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7607 | } |
| 7608 | |
| 7609 | void md_cluster_stop(struct mddev *mddev) |
| 7610 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 7611 | if (!md_cluster_ops) |
| 7612 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7613 | md_cluster_ops->leave(mddev); |
| 7614 | module_put(md_cluster_mod); |
| 7615 | } |
| 7616 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7617 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7618 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7619 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7620 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7621 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7622 | |
| 7623 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7624 | rcu_read_lock(); |
| 7625 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7626 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7627 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7628 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7629 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7630 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7631 | * as sync_io is counted when a request starts, and |
| 7632 | * disk_stats is counted when it completes. |
| 7633 | * So resync activity will cause curr_events to be smaller than |
| 7634 | * when there was no such activity. |
| 7635 | * non-sync IO will cause disk_stat to increase without |
| 7636 | * increasing sync_io so curr_events will (eventually) |
| 7637 | * be larger than it was before. Once it becomes |
| 7638 | * substantially larger, the test below will cause |
| 7639 | * the array to appear non-idle, and resync will slow |
| 7640 | * down. |
| 7641 | * If there is a lot of outstanding resync activity when |
| 7642 | * we set last_event to curr_events, then all that activity |
| 7643 | * completing might cause the array to appear non-idle |
| 7644 | * and resync will be slowed down even though there might |
| 7645 | * not have been non-resync activity. This will only |
| 7646 | * happen once though. 'last_events' will soon reflect |
| 7647 | * the state where there is little or no outstanding |
| 7648 | * resync requests, and further resync activity will |
| 7649 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7650 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7651 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7652 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7653 | rdev->last_events = curr_events; |
| 7654 | idle = 0; |
| 7655 | } |
| 7656 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7657 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7658 | return idle; |
| 7659 | } |
| 7660 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7661 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7662 | { |
| 7663 | /* another "blocks" (512byte) blocks have been synced */ |
| 7664 | atomic_sub(blocks, &mddev->recovery_active); |
| 7665 | wake_up(&mddev->recovery_wait); |
| 7666 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7667 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7668 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7669 | md_wakeup_thread(mddev->thread); |
| 7670 | // stop recovery, signal do_sync .... |
| 7671 | } |
| 7672 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7673 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7674 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7675 | /* md_write_start(mddev, bi) |
| 7676 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7677 | * in superblock) before writing, schedule a superblock update |
| 7678 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7679 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7680 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7681 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7682 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7683 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7684 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7685 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7686 | BUG_ON(mddev->ro == 1); |
| 7687 | if (mddev->ro == 2) { |
| 7688 | /* need to switch to read/write */ |
| 7689 | mddev->ro = 0; |
| 7690 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7691 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7692 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7693 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7694 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7695 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7696 | if (mddev->safemode == 1) |
| 7697 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7698 | if (mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7699 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7700 | if (mddev->in_sync) { |
| 7701 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7702 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7703 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7704 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7705 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7706 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7707 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7708 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7709 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7710 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7711 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7712 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7713 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7714 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7715 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7716 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7717 | { |
| 7718 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7719 | if (mddev->safemode == 2) |
| 7720 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7721 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7722 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7723 | } |
| 7724 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7725 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7726 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7727 | /* md_allow_write(mddev) |
| 7728 | * Calling this ensures that the array is marked 'active' so that writes |
| 7729 | * may proceed without blocking. It is important to call this before |
| 7730 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7731 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7732 | * |
Guoqing Jiang | abf3508 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 7733 | * 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] | 7734 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7735 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7736 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7737 | { |
| 7738 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7739 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7740 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7741 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7742 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7743 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7744 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7745 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7746 | if (mddev->in_sync) { |
| 7747 | mddev->in_sync = 0; |
| 7748 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7749 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7750 | if (mddev->safemode_delay && |
| 7751 | mddev->safemode == 0) |
| 7752 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7753 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7754 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7755 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7756 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7757 | spin_unlock(&mddev->lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7758 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7759 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7760 | return -EAGAIN; |
| 7761 | else |
| 7762 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7763 | } |
| 7764 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7766 | #define SYNC_MARKS 10 |
| 7767 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7768 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7769 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7770 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7771 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7772 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7773 | unsigned int currspeed = 0, |
| 7774 | window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7775 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7776 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7777 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7778 | sector_t mark_cnt[SYNC_MARKS]; |
| 7779 | int last_mark,m; |
| 7780 | struct list_head *tmp; |
| 7781 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7782 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7783 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7784 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7785 | struct blk_plug plug; |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 7786 | bool cluster_resync_finished = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7787 | |
| 7788 | /* just incase thread restarts... */ |
| 7789 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7790 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7791 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 7792 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7793 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7794 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7795 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7796 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7797 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7798 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7799 | action = "check"; |
| 7800 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7801 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7802 | action = "repair"; |
| 7803 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7804 | desc = "resync"; |
| 7805 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7806 | desc = "reshape"; |
| 7807 | else |
| 7808 | desc = "recovery"; |
| 7809 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7810 | mddev->last_sync_action = action ?: desc; |
| 7811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7812 | /* we overload curr_resync somewhat here. |
| 7813 | * 0 == not engaged in resync at all |
| 7814 | * 2 == checking that there is no conflict with another sync |
| 7815 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7816 | * commense |
| 7817 | * other == active in resync - this many blocks |
| 7818 | * |
| 7819 | * Before starting a resync we must have set curr_resync to |
| 7820 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7821 | * less than ours. When we find one that is the same or higher |
| 7822 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7823 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7824 | * This will mean we have to start checking from the beginning again. |
| 7825 | * |
| 7826 | */ |
| 7827 | |
| 7828 | do { |
| 7829 | mddev->curr_resync = 2; |
| 7830 | |
| 7831 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7832 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7833 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7834 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7835 | if (mddev2 == mddev) |
| 7836 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7837 | if (!mddev->parallel_resync |
| 7838 | && mddev2->curr_resync |
| 7839 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7840 | DEFINE_WAIT(wq); |
| 7841 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7842 | /* arbitrarily yield */ |
| 7843 | mddev->curr_resync = 1; |
| 7844 | wake_up(&resync_wait); |
| 7845 | } |
| 7846 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7847 | /* no need to wait here, we can wait the next |
| 7848 | * time 'round when curr_resync == 2 |
| 7849 | */ |
| 7850 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7851 | /* We need to wait 'interruptible' so as not to |
| 7852 | * contribute to the load average, and not to |
| 7853 | * be caught by 'softlockup' |
| 7854 | */ |
| 7855 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7856 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7857 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7858 | printk(KERN_INFO "md: delaying %s of %s" |
| 7859 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7860 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7861 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7862 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7863 | if (signal_pending(current)) |
| 7864 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7865 | schedule(); |
| 7866 | finish_wait(&resync_wait, &wq); |
| 7867 | goto try_again; |
| 7868 | } |
| 7869 | finish_wait(&resync_wait, &wq); |
| 7870 | } |
| 7871 | } |
| 7872 | } while (mddev->curr_resync < 2); |
| 7873 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7874 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7875 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7876 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7877 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7878 | */ |
| 7879 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 7880 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7881 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7882 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7883 | j = mddev->resync_min; |
| 7884 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7885 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7886 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7887 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7888 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7889 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7890 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7891 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7892 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7893 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7894 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7895 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 7896 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7897 | !test_bit(Faulty, &rdev->flags) && |
| 7898 | !test_bit(In_sync, &rdev->flags) && |
| 7899 | rdev->recovery_offset < j) |
| 7900 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7901 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 7902 | |
| 7903 | /* If there is a bitmap, we need to make sure all |
| 7904 | * writes that started before we added a spare |
| 7905 | * complete before we start doing a recovery. |
| 7906 | * Otherwise the write might complete and (via |
| 7907 | * bitmap_endwrite) set a bit in the bitmap after the |
| 7908 | * recovery has checked that bit and skipped that |
| 7909 | * region. |
| 7910 | */ |
| 7911 | if (mddev->bitmap) { |
| 7912 | mddev->pers->quiesce(mddev, 1); |
| 7913 | mddev->pers->quiesce(mddev, 0); |
| 7914 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7915 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7916 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7917 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7918 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7919 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7920 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7921 | "(but not more than %d KB/sec) for %s.\n", |
| 7922 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7923 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7924 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7925 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7926 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7927 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7928 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7929 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7930 | } |
| 7931 | last_mark = 0; |
| 7932 | mddev->resync_mark = mark[last_mark]; |
| 7933 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7934 | |
| 7935 | /* |
| 7936 | * Tune reconstruction: |
| 7937 | */ |
| 7938 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7939 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7940 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7941 | |
| 7942 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7943 | last_check = 0; |
| 7944 | |
| 7945 | if (j>2) { |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7946 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7947 | "md: resuming %s of %s from checkpoint.\n", |
| 7948 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7949 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7950 | } else |
| 7951 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7952 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7953 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 7954 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7955 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7956 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7957 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7958 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7959 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7960 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7961 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7962 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7963 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7964 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7965 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7966 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7967 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7968 | (j - mddev->curr_resync_completed)*2 |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 7969 | >= mddev->resync_max - mddev->curr_resync_completed || |
| 7970 | mddev->curr_resync_completed > mddev->resync_max |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7971 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7972 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7973 | wait_event(mddev->recovery_wait, |
| 7974 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7975 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7976 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 7977 | j > mddev->recovery_cp) |
| 7978 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7979 | update_time = jiffies; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7980 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7981 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7982 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7983 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7984 | while (j >= mddev->resync_max && |
| 7985 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7986 | /* As this condition is controlled by user-space, |
| 7987 | * we can block indefinitely, so use '_interruptible' |
| 7988 | * to avoid triggering warnings. |
| 7989 | */ |
| 7990 | flush_signals(current); /* just in case */ |
| 7991 | wait_event_interruptible(mddev->recovery_wait, |
| 7992 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7993 | || test_bit(MD_RECOVERY_INTR, |
| 7994 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7995 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7996 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7997 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7998 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7999 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8000 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8001 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8002 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8003 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8004 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8005 | |
| 8006 | if (!skipped) { /* actual IO requested */ |
| 8007 | io_sectors += sectors; |
| 8008 | atomic_add(sectors, &mddev->recovery_active); |
| 8009 | } |
| 8010 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8011 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8012 | break; |
| 8013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8014 | j += sectors; |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8015 | if (j > max_sectors) |
| 8016 | /* when skipping, extra large numbers can be returned. */ |
| 8017 | j = max_sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8018 | if (j > 2) |
| 8019 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8020 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8021 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8022 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8023 | * visible in /proc/mdstat |
| 8024 | */ |
| 8025 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8026 | |
| 8027 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8028 | continue; |
| 8029 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8030 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8031 | repeat: |
| 8032 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 8033 | /* step marks */ |
| 8034 | int next = (last_mark+1) % SYNC_MARKS; |
| 8035 | |
| 8036 | mddev->resync_mark = mark[next]; |
| 8037 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 8038 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8039 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8040 | last_mark = next; |
| 8041 | } |
| 8042 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8043 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8044 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8045 | |
| 8046 | /* |
| 8047 | * this loop exits only if either when we are slower than |
| 8048 | * the 'hard' speed limit, or the system was IO-idle for |
| 8049 | * a jiffy. |
| 8050 | * the system might be non-idle CPU-wise, but we only care |
| 8051 | * about not overloading the IO subsystem. (things like an |
| 8052 | * e2fsck being done on the RAID array should execute fast) |
| 8053 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8054 | cond_resched(); |
| 8055 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8056 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 8057 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8058 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8059 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 8060 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8061 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8062 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8063 | goto repeat; |
| 8064 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8065 | if (!is_mddev_idle(mddev, 0)) { |
| 8066 | /* |
| 8067 | * Give other IO more of a chance. |
| 8068 | * The faster the devices, the less we wait. |
| 8069 | */ |
| 8070 | wait_event(mddev->recovery_wait, |
| 8071 | !atomic_read(&mddev->recovery_active)); |
| 8072 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8073 | } |
| 8074 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8075 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
| 8076 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 8077 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8078 | /* |
| 8079 | * this also signals 'finished resyncing' to md_stop |
| 8080 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8081 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8082 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 8083 | |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8084 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8085 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8086 | mddev->curr_resync > 2) { |
| 8087 | mddev->curr_resync_completed = mddev->curr_resync; |
| 8088 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 8089 | } |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8090 | /* tell personality and other nodes that we are finished */ |
| 8091 | if (mddev_is_clustered(mddev)) { |
| 8092 | md_cluster_ops->resync_finish(mddev); |
| 8093 | cluster_resync_finished = true; |
| 8094 | } |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8095 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8096 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8097 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8098 | mddev->curr_resync > 2) { |
| 8099 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 8100 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8101 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 8102 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8103 | "md: checkpointing %s of %s.\n", |
| 8104 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8105 | if (test_bit(MD_RECOVERY_ERROR, |
| 8106 | &mddev->recovery)) |
| 8107 | mddev->recovery_cp = |
| 8108 | mddev->curr_resync_completed; |
| 8109 | else |
| 8110 | mddev->recovery_cp = |
| 8111 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8112 | } |
| 8113 | } else |
| 8114 | mddev->recovery_cp = MaxSector; |
| 8115 | } else { |
| 8116 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8117 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8118 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8119 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8120 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 8121 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8122 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8123 | !test_bit(Faulty, &rdev->flags) && |
| 8124 | !test_bit(In_sync, &rdev->flags) && |
| 8125 | rdev->recovery_offset < mddev->curr_resync) |
| 8126 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8127 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8129 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 8130 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 8131 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8132 | |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8133 | if (mddev_is_clustered(mddev) && |
| 8134 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8135 | !cluster_resync_finished) |
| 8136 | md_cluster_ops->resync_finish(mddev); |
| 8137 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8138 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 8139 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8140 | /* We completed so min/max setting can be forgotten if used. */ |
| 8141 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8142 | mddev->resync_min = 0; |
| 8143 | mddev->resync_max = MaxSector; |
| 8144 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8145 | mddev->resync_min = mddev->curr_resync_completed; |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8146 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8147 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8148 | spin_unlock(&mddev->lock); |
| 8149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8150 | wake_up(&resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8151 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 8152 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8153 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8154 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8155 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8156 | static int remove_and_add_spares(struct mddev *mddev, |
| 8157 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8158 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8159 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8160 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8161 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8162 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8163 | rdev_for_each(rdev, mddev) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8164 | if ((this == NULL || rdev == this) && |
| 8165 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8166 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8167 | (test_bit(Faulty, &rdev->flags) || |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8168 | (!test_bit(In_sync, &rdev->flags) && |
| 8169 | !test_bit(Journal, &rdev->flags))) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8170 | atomic_read(&rdev->nr_pending)==0) { |
| 8171 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8172 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 8173 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8174 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8175 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8176 | } |
| 8177 | } |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 8178 | if (removed && mddev->kobj.sd) |
| 8179 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8180 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8181 | if (this && removed) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8182 | goto no_add; |
| 8183 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8184 | rdev_for_each(rdev, mddev) { |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8185 | if (this && this != rdev) |
| 8186 | continue; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 8187 | if (test_bit(Candidate, &rdev->flags)) |
| 8188 | continue; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8189 | if (rdev->raid_disk >= 0 && |
| 8190 | !test_bit(In_sync, &rdev->flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8191 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8192 | !test_bit(Faulty, &rdev->flags)) |
| 8193 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8194 | if (rdev->raid_disk >= 0) |
| 8195 | continue; |
| 8196 | if (test_bit(Faulty, &rdev->flags)) |
| 8197 | continue; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8198 | if (!test_bit(Journal, &rdev->flags)) { |
| 8199 | if (mddev->ro && |
| 8200 | ! (rdev->saved_raid_disk >= 0 && |
| 8201 | !test_bit(Bitmap_sync, &rdev->flags))) |
| 8202 | continue; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8203 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8204 | rdev->recovery_offset = 0; |
| 8205 | } |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8206 | if (mddev->pers-> |
| 8207 | hot_add_disk(mddev, rdev) == 0) { |
| 8208 | if (sysfs_link_rdev(mddev, rdev)) |
| 8209 | /* failure here is OK */; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8210 | if (!test_bit(Journal, &rdev->flags)) |
| 8211 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8212 | md_new_event(mddev); |
| 8213 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8214 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8215 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8216 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 8217 | if (removed) |
| 8218 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8219 | return spares; |
| 8220 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 8221 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8222 | static void md_start_sync(struct work_struct *ws) |
| 8223 | { |
| 8224 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8225 | int ret = 0; |
| 8226 | |
| 8227 | if (mddev_is_clustered(mddev)) { |
| 8228 | ret = md_cluster_ops->resync_start(mddev); |
| 8229 | if (ret) { |
| 8230 | mddev->sync_thread = NULL; |
| 8231 | goto out; |
| 8232 | } |
| 8233 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8234 | |
| 8235 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 8236 | mddev, |
| 8237 | "resync"); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8238 | out: |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8239 | if (!mddev->sync_thread) { |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8240 | if (!(mddev_is_clustered(mddev) && ret == -EAGAIN)) |
| 8241 | printk(KERN_ERR "%s: could not start resync" |
| 8242 | " thread...\n", |
| 8243 | mdname(mddev)); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8244 | /* leave the spares where they are, it shouldn't hurt */ |
| 8245 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8246 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8247 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8248 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 8249 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8250 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8251 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8252 | &mddev->recovery)) |
| 8253 | if (mddev->sysfs_action) |
| 8254 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8255 | } else |
| 8256 | md_wakeup_thread(mddev->sync_thread); |
| 8257 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8258 | md_new_event(mddev); |
| 8259 | } |
| 8260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8261 | /* |
| 8262 | * This routine is regularly called by all per-raid-array threads to |
| 8263 | * deal with generic issues like resync and super-block update. |
| 8264 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 8265 | * need this as they never do any recovery or update the superblock. |
| 8266 | * |
| 8267 | * It does not do any resync itself, but rather "forks" off other threads |
| 8268 | * to do that as needed. |
| 8269 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 8270 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8271 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8272 | * and wakeups up this thread which will reap the thread and finish up. |
| 8273 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 8274 | * |
| 8275 | * The overall approach is: |
| 8276 | * 1/ if the superblock needs updating, update it. |
| 8277 | * 2/ If a recovery thread is running, don't do anything else. |
| 8278 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 8279 | * 4/ If there are any faulty devices, remove them. |
| 8280 | * 5/ If array is degraded, try to add spares devices |
| 8281 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 8282 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8283 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8284 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 8285 | if (mddev->suspended) |
| 8286 | return; |
| 8287 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 8288 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 8289 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8290 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8291 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8292 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8293 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 8294 | mdname(mddev)); |
| 8295 | mddev->safemode = 2; |
| 8296 | } |
| 8297 | flush_signals(current); |
| 8298 | } |
| 8299 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8300 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 8301 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8302 | if ( ! ( |
NeilBrown | 142d44c | 2013-11-28 10:34:18 +1100 | [diff] [blame] | 8303 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8304 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8305 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
Guoqing Jiang | 15858fa | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 8306 | test_bit(MD_RELOAD_SB, &mddev->flags) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8307 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8308 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 8309 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8310 | )) |
| 8311 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8312 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 8313 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8314 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8315 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8316 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 8317 | struct md_rdev *rdev; |
| 8318 | if (!mddev->external && mddev->in_sync) |
| 8319 | /* 'Blocked' flag not needed as failed devices |
| 8320 | * will be recorded if array switched to read/write. |
| 8321 | * Leaving it set will prevent the device |
| 8322 | * from being removed. |
| 8323 | */ |
| 8324 | rdev_for_each(rdev, mddev) |
| 8325 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8326 | /* On a read-only array we can: |
| 8327 | * - remove failed devices |
| 8328 | * - add already-in_sync devices if the array itself |
| 8329 | * is in-sync. |
| 8330 | * As we only add devices that are already in-sync, |
| 8331 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8332 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8333 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8334 | /* There is no thread, but we need to call |
| 8335 | * ->spare_active and clear saved_raid_disk |
| 8336 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 8337 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8338 | md_reap_sync_thread(mddev); |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 8339 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8340 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Shaohua Li | d4929ad | 2015-09-18 10:20:12 -0700 | [diff] [blame] | 8341 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8342 | goto unlock; |
| 8343 | } |
| 8344 | |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 8345 | if (mddev_is_clustered(mddev)) { |
| 8346 | struct md_rdev *rdev; |
| 8347 | /* kick the device if another node issued a |
| 8348 | * remove disk. |
| 8349 | */ |
| 8350 | rdev_for_each(rdev, mddev) { |
| 8351 | if (test_and_clear_bit(ClusterRemove, &rdev->flags) && |
| 8352 | rdev->raid_disk < 0) |
| 8353 | md_kick_rdev_from_array(rdev); |
| 8354 | } |
Guoqing Jiang | 15858fa | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 8355 | |
| 8356 | if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags)) |
| 8357 | md_reload_sb(mddev, mddev->good_device_nr); |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 8358 | } |
| 8359 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8360 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8361 | int did_change = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8362 | spin_lock(&mddev->lock); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8363 | if (mddev->safemode && |
| 8364 | !atomic_read(&mddev->writes_pending) && |
| 8365 | !mddev->in_sync && |
| 8366 | mddev->recovery_cp == MaxSector) { |
| 8367 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8368 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 8369 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8370 | } |
| 8371 | if (mddev->safemode == 1) |
| 8372 | mddev->safemode = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8373 | spin_unlock(&mddev->lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8374 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8375 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8376 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8377 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 8378 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 8379 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8381 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 8382 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 8383 | /* resync/recovery still happening */ |
| 8384 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8385 | goto unlock; |
| 8386 | } |
| 8387 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8388 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8389 | goto unlock; |
| 8390 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8391 | /* Set RUNNING before clearing NEEDED to avoid |
| 8392 | * any transients in the value of "sync_action". |
| 8393 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8394 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8395 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8396 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8397 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8398 | /* Clear some bits that don't mean anything, but |
| 8399 | * might be left set |
| 8400 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8401 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 8402 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8403 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 8404 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 8405 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8406 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8407 | /* no recovery is running. |
| 8408 | * remove any failed drives, then |
| 8409 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8410 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8411 | * the personality to fail the re-add. |
| 8412 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8413 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8414 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8415 | if (mddev->pers->check_reshape == NULL || |
| 8416 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8417 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8418 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8419 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8420 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8421 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8422 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8423 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 8424 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8425 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8426 | } else if (mddev->recovery_cp < MaxSector) { |
| 8427 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8428 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8429 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 8430 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8431 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8433 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 8434 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 8435 | /* We are adding a device or devices to an array |
| 8436 | * which has the bitmap stored on all devices. |
| 8437 | * So make sure all bitmap pages get written |
| 8438 | */ |
| 8439 | bitmap_write_all(mddev->bitmap); |
| 8440 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8441 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 8442 | queue_work(md_misc_wq, &mddev->del_work); |
| 8443 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8444 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8445 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8446 | if (!mddev->sync_thread) { |
| 8447 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8448 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8449 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8450 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 8451 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8452 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8453 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8454 | unlock: |
| 8455 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8456 | mddev_unlock(mddev); |
| 8457 | } |
| 8458 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8459 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8460 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8461 | void md_reap_sync_thread(struct mddev *mddev) |
| 8462 | { |
| 8463 | struct md_rdev *rdev; |
| 8464 | |
| 8465 | /* resync has finished, collect result */ |
| 8466 | md_unregister_thread(&mddev->sync_thread); |
| 8467 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8468 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 8469 | /* success...*/ |
| 8470 | /* activate any spares */ |
| 8471 | if (mddev->pers->spare_active(mddev)) { |
| 8472 | sysfs_notify(&mddev->kobj, NULL, |
| 8473 | "degraded"); |
| 8474 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 8475 | } |
| 8476 | } |
| 8477 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8478 | mddev->pers->finish_reshape) |
| 8479 | mddev->pers->finish_reshape(mddev); |
| 8480 | |
| 8481 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8482 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8483 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8484 | if (!mddev->degraded) |
| 8485 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8486 | rdev->saved_raid_disk = -1; |
| 8487 | |
| 8488 | md_update_sb(mddev, 1); |
| 8489 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 8490 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8491 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8492 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8493 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8494 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8495 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8496 | /* flag recovery needed just to double check */ |
| 8497 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8498 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8499 | md_new_event(mddev); |
| 8500 | if (mddev->event_work.func) |
| 8501 | queue_work(md_misc_wq, &mddev->event_work); |
| 8502 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8503 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8504 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8505 | 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] | 8506 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8507 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8508 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8509 | !test_bit(Blocked, &rdev->flags) && |
| 8510 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8511 | msecs_to_jiffies(5000)); |
| 8512 | rdev_dec_pending(rdev, mddev); |
| 8513 | } |
| 8514 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 8515 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8516 | void md_finish_reshape(struct mddev *mddev) |
| 8517 | { |
| 8518 | /* called be personality module when reshape completes. */ |
| 8519 | struct md_rdev *rdev; |
| 8520 | |
| 8521 | rdev_for_each(rdev, mddev) { |
| 8522 | if (rdev->data_offset > rdev->new_data_offset) |
| 8523 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 8524 | else |
| 8525 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 8526 | rdev->data_offset = rdev->new_data_offset; |
| 8527 | } |
| 8528 | } |
| 8529 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8530 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8531 | /* Bad block management */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8532 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8533 | /* Returns 1 on success, 0 on failure */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8534 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8535 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8536 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8537 | int rv; |
| 8538 | if (is_new) |
| 8539 | s += rdev->new_data_offset; |
| 8540 | else |
| 8541 | s += rdev->data_offset; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8542 | rv = badblocks_set(&rdev->badblocks, s, sectors, 0); |
| 8543 | if (rv == 0) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8544 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8545 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8546 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
NeilBrown | 55ce74d | 2015-08-14 11:11:10 +1000 | [diff] [blame] | 8547 | set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8548 | md_wakeup_thread(rdev->mddev->thread); |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8549 | return 1; |
| 8550 | } else |
| 8551 | return 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8552 | } |
| 8553 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8554 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8555 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8556 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8557 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8558 | if (is_new) |
| 8559 | s += rdev->new_data_offset; |
| 8560 | else |
| 8561 | s += rdev->data_offset; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 8562 | return badblocks_clear(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8563 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8564 | } |
| 8565 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8566 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8567 | static int md_notify_reboot(struct notifier_block *this, |
| 8568 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8569 | { |
| 8570 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8571 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8572 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8573 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8574 | for_each_mddev(mddev, tmp) { |
| 8575 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8576 | if (mddev->pers) |
| 8577 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 8578 | if (mddev->persistent) |
| 8579 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8580 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8581 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8582 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8583 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8584 | /* |
| 8585 | * certain more exotic SCSI devices are known to be |
| 8586 | * volatile wrt too early system reboots. While the |
| 8587 | * right place to handle this issue is the given |
| 8588 | * driver, we do want to have a safe RAID driver ... |
| 8589 | */ |
| 8590 | if (need_delay) |
| 8591 | mdelay(1000*1); |
| 8592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8593 | return NOTIFY_DONE; |
| 8594 | } |
| 8595 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8596 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8597 | .notifier_call = md_notify_reboot, |
| 8598 | .next = NULL, |
| 8599 | .priority = INT_MAX, /* before any real devices */ |
| 8600 | }; |
| 8601 | |
| 8602 | static void md_geninit(void) |
| 8603 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8604 | 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] | 8605 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8606 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8607 | } |
| 8608 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8609 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8610 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8611 | int ret = -ENOMEM; |
| 8612 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8613 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8614 | if (!md_wq) |
| 8615 | goto err_wq; |
| 8616 | |
| 8617 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8618 | if (!md_misc_wq) |
| 8619 | goto err_misc_wq; |
| 8620 | |
| 8621 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8622 | goto err_md; |
| 8623 | |
| 8624 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8625 | goto err_mdp; |
| 8626 | mdp_major = ret; |
| 8627 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8628 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8629 | md_probe, NULL, NULL); |
| 8630 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8631 | md_probe, NULL, NULL); |
| 8632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8633 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8634 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8635 | |
| 8636 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8637 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8638 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8639 | err_mdp: |
| 8640 | unregister_blkdev(MD_MAJOR, "md"); |
| 8641 | err_md: |
| 8642 | destroy_workqueue(md_misc_wq); |
| 8643 | err_misc_wq: |
| 8644 | destroy_workqueue(md_wq); |
| 8645 | err_wq: |
| 8646 | return ret; |
| 8647 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8648 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8649 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8650 | { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8651 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 8652 | struct md_rdev *rdev2; |
| 8653 | int role, ret; |
| 8654 | char b[BDEVNAME_SIZE]; |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8655 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8656 | /* Check for change of roles in the active devices */ |
| 8657 | rdev_for_each(rdev2, mddev) { |
| 8658 | if (test_bit(Faulty, &rdev2->flags)) |
| 8659 | continue; |
| 8660 | |
| 8661 | /* Check if the roles changed */ |
| 8662 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 8663 | |
| 8664 | if (test_bit(Candidate, &rdev2->flags)) { |
| 8665 | if (role == 0xfffe) { |
| 8666 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); |
| 8667 | md_kick_rdev_from_array(rdev2); |
| 8668 | continue; |
| 8669 | } |
| 8670 | else |
| 8671 | clear_bit(Candidate, &rdev2->flags); |
| 8672 | } |
| 8673 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8674 | if (role != rdev2->raid_disk) { |
| 8675 | /* got activated */ |
| 8676 | if (rdev2->raid_disk == -1 && role != 0xffff) { |
| 8677 | rdev2->saved_raid_disk = role; |
| 8678 | ret = remove_and_add_spares(mddev, rdev2); |
| 8679 | pr_info("Activated spare: %s\n", |
| 8680 | bdevname(rdev2->bdev,b)); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8681 | } |
| 8682 | /* device faulty |
| 8683 | * We just want to do the minimum to mark the disk |
| 8684 | * as faulty. The recovery is performed by the |
| 8685 | * one who initiated the error. |
| 8686 | */ |
| 8687 | if ((role == 0xfffe) || (role == 0xfffd)) { |
| 8688 | md_error(mddev, rdev2); |
| 8689 | clear_bit(Blocked, &rdev2->flags); |
| 8690 | } |
| 8691 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8692 | } |
| 8693 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 8694 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) |
| 8695 | update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 8696 | |
| 8697 | /* Finally set the event to be up to date */ |
| 8698 | mddev->events = le64_to_cpu(sb->events); |
| 8699 | } |
| 8700 | |
| 8701 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) |
| 8702 | { |
| 8703 | int err; |
| 8704 | struct page *swapout = rdev->sb_page; |
| 8705 | struct mdp_superblock_1 *sb; |
| 8706 | |
| 8707 | /* Store the sb page of the rdev in the swapout temporary |
| 8708 | * variable in case we err in the future |
| 8709 | */ |
| 8710 | rdev->sb_page = NULL; |
| 8711 | alloc_disk_sb(rdev); |
| 8712 | ClearPageUptodate(rdev->sb_page); |
| 8713 | rdev->sb_loaded = 0; |
| 8714 | err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version); |
| 8715 | |
| 8716 | if (err < 0) { |
| 8717 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", |
| 8718 | __func__, __LINE__, rdev->desc_nr, err); |
| 8719 | put_page(rdev->sb_page); |
| 8720 | rdev->sb_page = swapout; |
| 8721 | rdev->sb_loaded = 1; |
| 8722 | return err; |
| 8723 | } |
| 8724 | |
| 8725 | sb = page_address(rdev->sb_page); |
| 8726 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET |
| 8727 | * is not set |
| 8728 | */ |
| 8729 | |
| 8730 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) |
| 8731 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 8732 | |
| 8733 | /* The other node finished recovery, call spare_active to set |
| 8734 | * device In_sync and mddev->degraded |
| 8735 | */ |
| 8736 | if (rdev->recovery_offset == MaxSector && |
| 8737 | !test_bit(In_sync, &rdev->flags) && |
| 8738 | mddev->pers->spare_active(mddev)) |
| 8739 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
| 8740 | |
| 8741 | put_page(swapout); |
| 8742 | return 0; |
| 8743 | } |
| 8744 | |
| 8745 | void md_reload_sb(struct mddev *mddev, int nr) |
| 8746 | { |
| 8747 | struct md_rdev *rdev; |
| 8748 | int err; |
| 8749 | |
| 8750 | /* Find the rdev */ |
| 8751 | rdev_for_each_rcu(rdev, mddev) { |
| 8752 | if (rdev->desc_nr == nr) |
| 8753 | break; |
| 8754 | } |
| 8755 | |
| 8756 | if (!rdev || rdev->desc_nr != nr) { |
| 8757 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); |
| 8758 | return; |
| 8759 | } |
| 8760 | |
| 8761 | err = read_rdev(mddev, rdev); |
| 8762 | if (err < 0) |
| 8763 | return; |
| 8764 | |
| 8765 | check_sb_changes(mddev, rdev); |
| 8766 | |
| 8767 | /* Read all rdev's to update recovery_offset */ |
| 8768 | rdev_for_each_rcu(rdev, mddev) |
| 8769 | read_rdev(mddev, rdev); |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 8770 | } |
| 8771 | EXPORT_SYMBOL(md_reload_sb); |
| 8772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8773 | #ifndef MODULE |
| 8774 | |
| 8775 | /* |
| 8776 | * Searches all registered partitions for autorun RAID arrays |
| 8777 | * at boot time. |
| 8778 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8779 | |
| 8780 | static LIST_HEAD(all_detected_devices); |
| 8781 | struct detected_devices_node { |
| 8782 | struct list_head list; |
| 8783 | dev_t dev; |
| 8784 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8785 | |
| 8786 | void md_autodetect_dev(dev_t dev) |
| 8787 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8788 | struct detected_devices_node *node_detected_dev; |
| 8789 | |
| 8790 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8791 | if (node_detected_dev) { |
| 8792 | node_detected_dev->dev = dev; |
| 8793 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8794 | } else { |
| 8795 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8796 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8797 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8798 | } |
| 8799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8800 | static void autostart_arrays(int part) |
| 8801 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8802 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8803 | struct detected_devices_node *node_detected_dev; |
| 8804 | dev_t dev; |
| 8805 | int i_scanned, i_passed; |
| 8806 | |
| 8807 | i_scanned = 0; |
| 8808 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8809 | |
| 8810 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8811 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8812 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8813 | i_scanned++; |
| 8814 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8815 | struct detected_devices_node, list); |
| 8816 | list_del(&node_detected_dev->list); |
| 8817 | dev = node_detected_dev->dev; |
| 8818 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8819 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8820 | if (IS_ERR(rdev)) |
| 8821 | continue; |
| 8822 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8823 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8824 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8825 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8826 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8827 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8828 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8829 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8830 | |
| 8831 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8832 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8833 | |
| 8834 | autorun_devices(part); |
| 8835 | } |
| 8836 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8837 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8838 | |
| 8839 | static __exit void md_exit(void) |
| 8840 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8841 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8842 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8843 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8844 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8845 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8846 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8847 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8848 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8849 | unregister_blkdev(mdp_major, "mdp"); |
| 8850 | unregister_reboot_notifier(&md_notifier); |
| 8851 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8852 | |
| 8853 | /* We cannot unload the modules while some process is |
| 8854 | * waiting for us in select() or poll() - wake them up |
| 8855 | */ |
| 8856 | md_unloading = 1; |
| 8857 | while (waitqueue_active(&md_event_waiters)) { |
| 8858 | /* not safe to leave yet */ |
| 8859 | wake_up(&md_event_waiters); |
| 8860 | msleep(delay); |
| 8861 | delay += delay; |
| 8862 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8863 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8864 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8865 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8866 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8867 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8868 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8869 | destroy_workqueue(md_misc_wq); |
| 8870 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8871 | } |
| 8872 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8873 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8874 | module_exit(md_exit) |
| 8875 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8876 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8877 | { |
| 8878 | return sprintf(buffer, "%d", start_readonly); |
| 8879 | } |
| 8880 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8881 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 8882 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8883 | } |
| 8884 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8885 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8886 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8887 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8889 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8890 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8891 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8892 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |