Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 39 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 40 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 41 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 42 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 43 | #include <linux/hdreg.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/random.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 46 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 47 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 49 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 50 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/raid/md_p.h> |
| 52 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 58 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #endif |
| 60 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 61 | /* pers_list is a list of registered personalities protected |
| 62 | * by pers_lock. |
| 63 | * pers_lock does extra service to protect accesses to |
| 64 | * mddev->thread when the mutex cannot be held. |
| 65 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 66 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static DEFINE_SPINLOCK(pers_lock); |
| 68 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 69 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 70 | static struct workqueue_struct *md_wq; |
| 71 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 72 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 73 | static int remove_and_add_spares(struct mddev *mddev, |
| 74 | struct md_rdev *this); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 75 | static void mddev_detach(struct mddev *mddev); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 78 | * Default number of read corrections we'll attempt on an rdev |
| 79 | * before ejecting it from the array. We divide the read error |
| 80 | * count by 2 for every hour elapsed between read errors. |
| 81 | */ |
| 82 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 83 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 85 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 86 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 87 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 89 | * speed limit - in case reconstruction slows down your system despite |
| 90 | * idle IO detection. |
| 91 | * |
| 92 | * 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] | 93 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | */ |
| 95 | |
| 96 | static int sysctl_speed_limit_min = 1000; |
| 97 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 98 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 99 | { |
| 100 | return mddev->sync_speed_min ? |
| 101 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 102 | } |
| 103 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 104 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 105 | { |
| 106 | return mddev->sync_speed_max ? |
| 107 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | static struct ctl_table_header *raid_table_header; |
| 111 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 112 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | .procname = "speed_limit_min", |
| 115 | .data = &sysctl_speed_limit_min, |
| 116 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 117 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 118 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | }, |
| 120 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | .procname = "speed_limit_max", |
| 122 | .data = &sysctl_speed_limit_max, |
| 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 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 127 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 130 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | .procname = "raid", |
| 133 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 134 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | .child = raid_table, |
| 136 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 137 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 140 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | .procname = "dev", |
| 143 | .maxlen = 0, |
| 144 | .mode = 0555, |
| 145 | .child = raid_dir_table, |
| 146 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 147 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 150 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 152 | static int start_readonly; |
| 153 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 154 | /* bio_clone_mddev |
| 155 | * like bio_clone, but with a local bio set |
| 156 | */ |
| 157 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 158 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 159 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 160 | { |
| 161 | struct bio *b; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 162 | |
| 163 | if (!mddev || !mddev->bio_set) |
| 164 | return bio_alloc(gfp_mask, nr_iovecs); |
| 165 | |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 166 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 167 | if (!b) |
| 168 | return NULL; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 169 | return b; |
| 170 | } |
| 171 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 172 | |
| 173 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 174 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 175 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 176 | if (!mddev || !mddev->bio_set) |
| 177 | return bio_clone(bio, gfp_mask); |
| 178 | |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 179 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 180 | } |
| 181 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 184 | * We have a system wide 'event count' that is incremented |
| 185 | * on any 'interesting' event, and readers of /proc/mdstat |
| 186 | * can use 'poll' or 'select' to find out when the event |
| 187 | * count increases. |
| 188 | * |
| 189 | * Events are: |
| 190 | * start array, stop array, error, add device, remove device, |
| 191 | * start build, activate spare |
| 192 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 193 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 194 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 195 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 196 | { |
| 197 | atomic_inc(&md_event_count); |
| 198 | wake_up(&md_event_waiters); |
| 199 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 200 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 201 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 202 | /* Alternate version that can be called from interrupts |
| 203 | * when calling sysfs_notify isn't needed. |
| 204 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 205 | static void md_new_event_inintr(struct mddev *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 206 | { |
| 207 | atomic_inc(&md_event_count); |
| 208 | wake_up(&md_event_waiters); |
| 209 | } |
| 210 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 211 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | * Enables to iterate over all existing md arrays |
| 213 | * all_mddevs_lock protects this list. |
| 214 | */ |
| 215 | static LIST_HEAD(all_mddevs); |
| 216 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | /* |
| 219 | * iterates through all used mddevs in the system. |
| 220 | * We take care to grab the all_mddevs_lock whenever navigating |
| 221 | * the list, and to always hold a refcount when unlocked. |
| 222 | * Any code which breaks out of this loop while own |
| 223 | * a reference to the current mddev and must mddev_put it. |
| 224 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 225 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | \ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 227 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 228 | _tmp = all_mddevs.next; \ |
| 229 | _mddev = NULL;}); \ |
| 230 | ({ if (_tmp != &all_mddevs) \ |
| 231 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 233 | if (_mddev) mddev_put(_mddev); \ |
| 234 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 235 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 237 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | ) |
| 239 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 240 | /* Rather than calling directly into the personality make_request function, |
| 241 | * IO requests come here first so that we can check if the device is |
| 242 | * being suspended pending a reconfiguration. |
| 243 | * We hold a refcount over the call to ->make_request. By the time that |
| 244 | * call has finished, the bio has been linked into some internal structure |
| 245 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 246 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 247 | static void md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 249 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 250 | struct mddev *mddev = q->queuedata; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 251 | unsigned int sectors; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 252 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 253 | if (mddev == NULL || mddev->pers == NULL |
| 254 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 255 | bio_io_error(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 256 | return; |
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)) { |
| 259 | bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS); |
| 260 | return; |
| 261 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 262 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 263 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 264 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 265 | DEFINE_WAIT(__wait); |
| 266 | for (;;) { |
| 267 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 268 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 269 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 270 | break; |
| 271 | rcu_read_unlock(); |
| 272 | schedule(); |
| 273 | rcu_read_lock(); |
| 274 | } |
| 275 | finish_wait(&mddev->sb_wait, &__wait); |
| 276 | } |
| 277 | atomic_inc(&mddev->active_io); |
| 278 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 279 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 280 | /* |
| 281 | * save the sectors now since our bio can |
| 282 | * go away inside make_request |
| 283 | */ |
| 284 | sectors = bio_sectors(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 285 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 286 | |
Gu Zheng | 18c0b22 | 2014-11-24 11:05:26 +0800 | [diff] [blame] | 287 | generic_start_io_acct(rw, sectors, &mddev->gendisk->part0); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 288 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 289 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 290 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 293 | /* mddev_suspend makes sure no new requests are submitted |
| 294 | * to the device, and that any requests that have been submitted |
| 295 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 296 | * Once mddev_detach() is called and completes, the module will be |
| 297 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 298 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 299 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 300 | { |
| 301 | BUG_ON(mddev->suspended); |
| 302 | mddev->suspended = 1; |
| 303 | synchronize_rcu(); |
| 304 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 305 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 306 | |
| 307 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 308 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 309 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 310 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 311 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 312 | { |
| 313 | mddev->suspended = 0; |
| 314 | wake_up(&mddev->sb_wait); |
| 315 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 316 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 317 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 318 | md_wakeup_thread(mddev->thread); |
| 319 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 320 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 321 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 322 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 323 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 324 | { |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 325 | struct md_personality *pers = mddev->pers; |
| 326 | int ret = 0; |
| 327 | |
| 328 | rcu_read_lock(); |
| 329 | if (mddev->suspended) |
| 330 | ret = 1; |
| 331 | else if (pers && pers->congested) |
| 332 | ret = pers->congested(mddev, bits); |
| 333 | rcu_read_unlock(); |
| 334 | return ret; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 335 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 336 | EXPORT_SYMBOL_GPL(mddev_congested); |
| 337 | static int md_congested(void *data, int bits) |
| 338 | { |
| 339 | struct mddev *mddev = data; |
| 340 | return mddev_congested(mddev, bits); |
| 341 | } |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 342 | |
NeilBrown | 64590f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 343 | static int md_mergeable_bvec(struct request_queue *q, |
| 344 | struct bvec_merge_data *bvm, |
| 345 | struct bio_vec *biovec) |
| 346 | { |
| 347 | struct mddev *mddev = q->queuedata; |
| 348 | int ret; |
| 349 | rcu_read_lock(); |
| 350 | if (mddev->suspended) { |
| 351 | /* Must always allow one vec */ |
| 352 | if (bvm->bi_size == 0) |
| 353 | ret = biovec->bv_len; |
| 354 | else |
| 355 | ret = 0; |
| 356 | } else { |
| 357 | struct md_personality *pers = mddev->pers; |
| 358 | if (pers && pers->mergeable_bvec) |
| 359 | ret = pers->mergeable_bvec(mddev, bvm, biovec); |
| 360 | else |
| 361 | ret = biovec->bv_len; |
| 362 | } |
| 363 | rcu_read_unlock(); |
| 364 | return ret; |
| 365 | } |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 366 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 367 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 368 | */ |
| 369 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 370 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 371 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 372 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 373 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 374 | |
| 375 | rdev_dec_pending(rdev, mddev); |
| 376 | |
| 377 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 378 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 379 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 380 | } |
| 381 | bio_put(bio); |
| 382 | } |
| 383 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 384 | static void md_submit_flush_data(struct work_struct *ws); |
| 385 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 386 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 387 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 388 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 389 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 390 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 391 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 392 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 393 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 394 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 395 | if (rdev->raid_disk >= 0 && |
| 396 | !test_bit(Faulty, &rdev->flags)) { |
| 397 | /* Take two references, one is dropped |
| 398 | * when request finishes, one after |
| 399 | * we reclaim rcu_read_lock |
| 400 | */ |
| 401 | struct bio *bi; |
| 402 | atomic_inc(&rdev->nr_pending); |
| 403 | atomic_inc(&rdev->nr_pending); |
| 404 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 405 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 406 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 407 | bi->bi_private = rdev; |
| 408 | bi->bi_bdev = rdev->bdev; |
| 409 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 410 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 411 | rcu_read_lock(); |
| 412 | rdev_dec_pending(rdev, mddev); |
| 413 | } |
| 414 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 415 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 416 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 417 | } |
| 418 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 419 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 420 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 421 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 422 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 423 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 424 | if (bio->bi_iter.bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 425 | /* an empty barrier - all done */ |
| 426 | bio_endio(bio, 0); |
| 427 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 428 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 429 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 430 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 431 | |
| 432 | mddev->flush_bio = NULL; |
| 433 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 434 | } |
| 435 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 436 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 437 | { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 438 | spin_lock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 439 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 440 | !mddev->flush_bio, |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 441 | mddev->lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 442 | mddev->flush_bio = bio; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 443 | spin_unlock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 444 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 445 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 446 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 447 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 448 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 449 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 450 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 451 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 452 | struct mddev *mddev = cb->data; |
| 453 | md_wakeup_thread(mddev->thread); |
| 454 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 455 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 456 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 457 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 458 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | { |
| 460 | atomic_inc(&mddev->active); |
| 461 | return mddev; |
| 462 | } |
| 463 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 464 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 465 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 466 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 468 | struct bio_set *bs = NULL; |
| 469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 471 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 472 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 473 | mddev->ctime == 0 && !mddev->hold_active) { |
| 474 | /* Array is not configured at all, and not held active, |
| 475 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 476 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 477 | bs = mddev->bio_set; |
| 478 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 479 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 480 | /* We did a probe so need to clean up. Call |
| 481 | * queue_work inside the spinlock so that |
| 482 | * flush_workqueue() after mddev_find will |
| 483 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 484 | */ |
| 485 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 486 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 487 | } else |
| 488 | kfree(mddev); |
| 489 | } |
| 490 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 491 | if (bs) |
| 492 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 495 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 496 | { |
| 497 | mutex_init(&mddev->open_mutex); |
| 498 | mutex_init(&mddev->reconfig_mutex); |
| 499 | mutex_init(&mddev->bitmap_info.mutex); |
| 500 | INIT_LIST_HEAD(&mddev->disks); |
| 501 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 502 | init_timer(&mddev->safemode_timer); |
| 503 | atomic_set(&mddev->active, 1); |
| 504 | atomic_set(&mddev->openers, 0); |
| 505 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 506 | spin_lock_init(&mddev->lock); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 507 | atomic_set(&mddev->flush_pending, 0); |
| 508 | init_waitqueue_head(&mddev->sb_wait); |
| 509 | init_waitqueue_head(&mddev->recovery_wait); |
| 510 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 511 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 512 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 513 | mddev->resync_min = 0; |
| 514 | mddev->resync_max = MaxSector; |
| 515 | mddev->level = LEVEL_NONE; |
| 516 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 517 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 518 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 519 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 521 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 523 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 524 | unit &= ~((1<<MdpMinorShift)-1); |
| 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | retry: |
| 527 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 529 | if (unit) { |
| 530 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 531 | if (mddev->unit == unit) { |
| 532 | mddev_get(mddev); |
| 533 | spin_unlock(&all_mddevs_lock); |
| 534 | kfree(new); |
| 535 | return mddev; |
| 536 | } |
| 537 | |
| 538 | if (new) { |
| 539 | list_add(&new->all_mddevs, &all_mddevs); |
| 540 | spin_unlock(&all_mddevs_lock); |
| 541 | new->hold_active = UNTIL_IOCTL; |
| 542 | return new; |
| 543 | } |
| 544 | } else if (new) { |
| 545 | /* find an unused unit number */ |
| 546 | static int next_minor = 512; |
| 547 | int start = next_minor; |
| 548 | int is_free = 0; |
| 549 | int dev = 0; |
| 550 | while (!is_free) { |
| 551 | dev = MKDEV(MD_MAJOR, next_minor); |
| 552 | next_minor++; |
| 553 | if (next_minor > MINORMASK) |
| 554 | next_minor = 0; |
| 555 | if (next_minor == start) { |
| 556 | /* Oh dear, all in use. */ |
| 557 | spin_unlock(&all_mddevs_lock); |
| 558 | kfree(new); |
| 559 | return NULL; |
| 560 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 561 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 562 | is_free = 1; |
| 563 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 564 | if (mddev->unit == dev) { |
| 565 | is_free = 0; |
| 566 | break; |
| 567 | } |
| 568 | } |
| 569 | new->unit = dev; |
| 570 | new->md_minor = MINOR(dev); |
| 571 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | list_add(&new->all_mddevs, &all_mddevs); |
| 573 | spin_unlock(&all_mddevs_lock); |
| 574 | return new; |
| 575 | } |
| 576 | spin_unlock(&all_mddevs_lock); |
| 577 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 578 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | if (!new) |
| 580 | return NULL; |
| 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | new->unit = unit; |
| 583 | if (MAJOR(unit) == MD_MAJOR) |
| 584 | new->md_minor = MINOR(unit); |
| 585 | else |
| 586 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 587 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 588 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | goto retry; |
| 591 | } |
| 592 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 593 | static struct attribute_group md_redundancy_group; |
| 594 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 595 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 597 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 598 | /* These cannot be removed under reconfig_mutex as |
| 599 | * an access to the files will try to take reconfig_mutex |
| 600 | * while holding the file unremovable, which leads to |
| 601 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 602 | * So hold set sysfs_active while the remove in happeing, |
| 603 | * and anything else which might set ->to_remove or my |
| 604 | * otherwise change the sysfs namespace will fail with |
| 605 | * -EBUSY if sysfs_active is still set. |
| 606 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 607 | * test it under the same mutex to ensure its correct value |
| 608 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 609 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 610 | struct attribute_group *to_remove = mddev->to_remove; |
| 611 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 612 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 613 | mutex_unlock(&mddev->reconfig_mutex); |
| 614 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 615 | if (mddev->kobj.sd) { |
| 616 | if (to_remove != &md_redundancy_group) |
| 617 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 618 | if (mddev->pers == NULL || |
| 619 | mddev->pers->sync_request == NULL) { |
| 620 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 621 | if (mddev->sysfs_action) |
| 622 | sysfs_put(mddev->sysfs_action); |
| 623 | mddev->sysfs_action = NULL; |
| 624 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 625 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 626 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 627 | } else |
| 628 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 630 | /* As we've dropped the mutex we need a spinlock to |
| 631 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 632 | */ |
| 633 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 634 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 635 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 637 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 639 | static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr) |
| 640 | { |
| 641 | struct md_rdev *rdev; |
| 642 | |
| 643 | rdev_for_each_rcu(rdev, mddev) |
| 644 | if (rdev->desc_nr == nr) |
| 645 | return rdev; |
| 646 | |
| 647 | return NULL; |
| 648 | } |
| 649 | |
| 650 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 652 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 654 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | if (rdev->bdev->bd_dev == dev) |
| 656 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 657 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return NULL; |
| 659 | } |
| 660 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 661 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 662 | { |
| 663 | struct md_rdev *rdev; |
| 664 | |
| 665 | rdev_for_each_rcu(rdev, mddev) |
| 666 | if (rdev->bdev->bd_dev == dev) |
| 667 | return rdev; |
| 668 | |
| 669 | return NULL; |
| 670 | } |
| 671 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 672 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 673 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 674 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 675 | list_for_each_entry(pers, &pers_list, list) { |
| 676 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 677 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 678 | if (strcmp(pers->name, clevel)==0) |
| 679 | return pers; |
| 680 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 681 | return NULL; |
| 682 | } |
| 683 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 684 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 685 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 687 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 688 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
| 690 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 691 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 694 | if (!rdev->sb_page) { |
| 695 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 696 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 702 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | { |
| 704 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 705 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | rdev->sb_loaded = 0; |
| 707 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 708 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 709 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 711 | if (rdev->bb_page) { |
| 712 | put_page(rdev->bb_page); |
| 713 | rdev->bb_page = NULL; |
| 714 | } |
NeilBrown | 4fa2f32 | 2012-05-22 13:55:01 +1000 | [diff] [blame] | 715 | kfree(rdev->badblocks.page); |
| 716 | rdev->badblocks.page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 718 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 720 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 721 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 722 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 723 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 724 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 725 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 726 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 727 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 728 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 729 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 730 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 731 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 732 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 733 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 734 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 735 | } |
| 736 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 737 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 738 | sector_t sector, int size, struct page *page) |
| 739 | { |
| 740 | /* write first size bytes of page to sector of rdev |
| 741 | * Increment mddev->pending_writes before returning |
| 742 | * and decrement it on completion, waking up sb_wait |
| 743 | * if zero is reached. |
| 744 | * If an error occurred, call md_error |
| 745 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 746 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 747 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 748 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 749 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 750 | bio_add_page(bio, page, size, 0); |
| 751 | bio->bi_private = rdev; |
| 752 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 753 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 754 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 755 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 756 | } |
| 757 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 758 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 759 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 760 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 761 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 762 | } |
| 763 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 764 | 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] | 765 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 767 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | int ret; |
| 769 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 770 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 771 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 772 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 773 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 774 | else if (rdev->mddev->reshape_position != MaxSector && |
| 775 | (rdev->mddev->reshape_backwards == |
| 776 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 777 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 778 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 779 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | bio_add_page(bio, page, size, 0); |
Kent Overstreet | c170bbb | 2013-11-24 16:32:22 -0700 | [diff] [blame] | 781 | submit_bio_wait(rw, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
| 783 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 784 | bio_put(bio); |
| 785 | return ret; |
| 786 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 787 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 789 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | { |
| 791 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | if (rdev->sb_loaded) |
| 794 | return 0; |
| 795 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 796 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | goto fail; |
| 798 | rdev->sb_loaded = 1; |
| 799 | return 0; |
| 800 | |
| 801 | fail: |
| 802 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 803 | bdevname(rdev->bdev,b)); |
| 804 | return -EINVAL; |
| 805 | } |
| 806 | |
| 807 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 808 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 809 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 810 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 811 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 812 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 816 | { |
| 817 | int ret; |
| 818 | mdp_super_t *tmp1, *tmp2; |
| 819 | |
| 820 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 821 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 822 | |
| 823 | if (!tmp1 || !tmp2) { |
| 824 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 825 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | goto abort; |
| 827 | } |
| 828 | |
| 829 | *tmp1 = *sb1; |
| 830 | *tmp2 = *sb2; |
| 831 | |
| 832 | /* |
| 833 | * nr_disks is not constant |
| 834 | */ |
| 835 | tmp1->nr_disks = 0; |
| 836 | tmp2->nr_disks = 0; |
| 837 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 838 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 840 | kfree(tmp1); |
| 841 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | return ret; |
| 843 | } |
| 844 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 845 | static u32 md_csum_fold(u32 csum) |
| 846 | { |
| 847 | csum = (csum & 0xffff) + (csum >> 16); |
| 848 | return (csum & 0xffff) + (csum >> 16); |
| 849 | } |
| 850 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 851 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 853 | u64 newcsum = 0; |
| 854 | u32 *sb32 = (u32*)sb; |
| 855 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | unsigned int disk_csum, csum; |
| 857 | |
| 858 | disk_csum = sb->sb_csum; |
| 859 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 860 | |
| 861 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 862 | newcsum += sb32[i]; |
| 863 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 864 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 865 | #ifdef CONFIG_ALPHA |
| 866 | /* This used to use csum_partial, which was wrong for several |
| 867 | * reasons including that different results are returned on |
| 868 | * different architectures. It isn't critical that we get exactly |
| 869 | * the same return value as before (we always csum_fold before |
| 870 | * testing, and that removes any differences). However as we |
| 871 | * know that csum_partial always returned a 16bit value on |
| 872 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 873 | */ |
| 874 | sb->sb_csum = md_csum_fold(disk_csum); |
| 875 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 877 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | return csum; |
| 879 | } |
| 880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | /* |
| 882 | * Handle superblock details. |
| 883 | * We want to be able to handle multiple superblock formats |
| 884 | * so we have a common interface to them all, and an array of |
| 885 | * different handlers. |
| 886 | * We rely on user-space to write the initial superblock, and support |
| 887 | * reading and updating of superblocks. |
| 888 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 889 | * 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] | 890 | * loads and validates a superblock on dev. |
| 891 | * if refdev != NULL, compare superblocks on both devices |
| 892 | * Return: |
| 893 | * 0 - dev has a superblock that is compatible with refdev |
| 894 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 895 | * so dev should be used as the refdev in future |
| 896 | * -EINVAL superblock incompatible or invalid |
| 897 | * -othererror e.g. -EIO |
| 898 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 899 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | * Verify that dev is acceptable into mddev. |
| 901 | * The first time, mddev->raid_disks will be 0, and data from |
| 902 | * dev should be merged in. Subsequent calls check that dev |
| 903 | * is new enough. Return 0 or -EINVAL |
| 904 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 905 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | * Update the superblock for rdev with data in mddev |
| 907 | * This does not write to disc. |
| 908 | * |
| 909 | */ |
| 910 | |
| 911 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 912 | char *name; |
| 913 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 914 | int (*load_super)(struct md_rdev *rdev, |
| 915 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 916 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 917 | int (*validate_super)(struct mddev *mddev, |
| 918 | struct md_rdev *rdev); |
| 919 | void (*sync_super)(struct mddev *mddev, |
| 920 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 921 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 922 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 923 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 924 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | }; |
| 926 | |
| 927 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 928 | * Check that the given mddev has no bitmap. |
| 929 | * |
| 930 | * This function is called from the run method of all personalities that do not |
| 931 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 932 | * has a bitmap. Otherwise, it returns 0. |
| 933 | * |
| 934 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 935 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 936 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 937 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 938 | return 0; |
| 939 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 940 | mdname(mddev), mddev->pers->name); |
| 941 | return 1; |
| 942 | } |
| 943 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 944 | |
| 945 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 946 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 948 | 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] | 949 | { |
| 950 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 951 | mdp_super_t *sb; |
| 952 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
| 954 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 955 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | * it's at the end of the disk. |
| 957 | * |
| 958 | * It also happens to be a multiple of 4Kb. |
| 959 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 960 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 962 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | if (ret) return ret; |
| 964 | |
| 965 | ret = -EINVAL; |
| 966 | |
| 967 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 968 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | |
| 970 | if (sb->md_magic != MD_SB_MAGIC) { |
| 971 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 972 | b); |
| 973 | goto abort; |
| 974 | } |
| 975 | |
| 976 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 977 | sb->minor_version < 90 || |
| 978 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 980 | sb->major_version, sb->minor_version, |
| 981 | b); |
| 982 | goto abort; |
| 983 | } |
| 984 | |
| 985 | if (sb->raid_disks <= 0) |
| 986 | goto abort; |
| 987 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 988 | 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] | 989 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 990 | b); |
| 991 | goto abort; |
| 992 | } |
| 993 | |
| 994 | rdev->preferred_minor = sb->md_minor; |
| 995 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 996 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 997 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 998 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | |
| 1000 | if (sb->level == LEVEL_MULTIPATH) |
| 1001 | rdev->desc_nr = -1; |
| 1002 | else |
| 1003 | rdev->desc_nr = sb->this_disk.number; |
| 1004 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1005 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1007 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1009 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | if (!uuid_equal(refsb, sb)) { |
| 1011 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1012 | b, bdevname(refdev->bdev,b2)); |
| 1013 | goto abort; |
| 1014 | } |
| 1015 | if (!sb_equal(refsb, sb)) { |
| 1016 | printk(KERN_WARNING "md: %s has same UUID" |
| 1017 | " but different superblock to %s\n", |
| 1018 | b, bdevname(refdev->bdev, b2)); |
| 1019 | goto abort; |
| 1020 | } |
| 1021 | ev1 = md_event(sb); |
| 1022 | ev2 = md_event(refsb); |
| 1023 | if (ev1 > ev2) |
| 1024 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1025 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | ret = 0; |
| 1027 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1028 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1029 | /* Limit to 4TB as metadata cannot record more than that. |
| 1030 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1031 | * record this size) |
| 1032 | */ |
| 1033 | if (rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1034 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1036 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1037 | /* "this cannot possibly happen" ... */ |
| 1038 | ret = -EINVAL; |
| 1039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | abort: |
| 1041 | return ret; |
| 1042 | } |
| 1043 | |
| 1044 | /* |
| 1045 | * validate_super for 0.90.0 |
| 1046 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1047 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | { |
| 1049 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1050 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1051 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1053 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1054 | clear_bit(Faulty, &rdev->flags); |
| 1055 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1056 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1057 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | if (mddev->raid_disks == 0) { |
| 1060 | mddev->major_version = 0; |
| 1061 | mddev->minor_version = sb->minor_version; |
| 1062 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1063 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1064 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | mddev->ctime = sb->ctime; |
| 1066 | mddev->utime = sb->utime; |
| 1067 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1068 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | mddev->layout = sb->layout; |
| 1070 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1071 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1072 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1073 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1074 | mddev->bitmap_info.space = 0; |
| 1075 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1076 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1077 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1078 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1080 | if (mddev->minor_version >= 91) { |
| 1081 | mddev->reshape_position = sb->reshape_position; |
| 1082 | mddev->delta_disks = sb->delta_disks; |
| 1083 | mddev->new_level = sb->new_level; |
| 1084 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1085 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1086 | if (mddev->delta_disks < 0) |
| 1087 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1088 | } else { |
| 1089 | mddev->reshape_position = MaxSector; |
| 1090 | mddev->delta_disks = 0; |
| 1091 | mddev->new_level = mddev->level; |
| 1092 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1093 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1097 | mddev->recovery_cp = MaxSector; |
| 1098 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1099 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | sb->events_lo == sb->cp_events_lo) { |
| 1101 | mddev->recovery_cp = sb->recovery_cp; |
| 1102 | } else |
| 1103 | mddev->recovery_cp = 0; |
| 1104 | } |
| 1105 | |
| 1106 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1107 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1108 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1109 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1110 | |
| 1111 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1112 | |
| 1113 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1114 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1115 | mddev->bitmap_info.offset = |
| 1116 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1117 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1118 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1119 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1120 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1121 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1122 | /* Insist on good event counter while assembling, except |
| 1123 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1125 | if (sb->disks[rdev->desc_nr].state & ( |
| 1126 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1127 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1128 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1129 | } else if (mddev->bitmap) { |
| 1130 | /* if adding to array with a bitmap, then we can accept an |
| 1131 | * older device ... but not too old. |
| 1132 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1133 | if (ev1 < mddev->bitmap->events_cleared) |
| 1134 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1135 | if (ev1 < mddev->events) |
| 1136 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1137 | } else { |
| 1138 | if (ev1 < mddev->events) |
| 1139 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1140 | return 0; |
| 1141 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | desc = sb->disks + rdev->desc_nr; |
| 1145 | |
| 1146 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1147 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1148 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1149 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1150 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1152 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1153 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1154 | /* active but not in sync implies recovery up to |
| 1155 | * reshape position. We don't know exactly where |
| 1156 | * that is, so set to zero for now */ |
| 1157 | if (mddev->minor_version >= 91) { |
| 1158 | rdev->recovery_offset = 0; |
| 1159 | rdev->raid_disk = desc->raid_disk; |
| 1160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1162 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1163 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1164 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1165 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | /* |
| 1170 | * sync_super for 0.90.0 |
| 1171 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1172 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | { |
| 1174 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1175 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | /* make rdev->sb match mddev data.. |
| 1179 | * |
| 1180 | * 1/ zero out disks |
| 1181 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1182 | * 3/ any empty disks < next_spare become removed |
| 1183 | * |
| 1184 | * disks[0] gets initialised to REMOVED because |
| 1185 | * we cannot be sure from other fields if it has |
| 1186 | * been initialised or not. |
| 1187 | */ |
| 1188 | int i; |
| 1189 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1190 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1191 | rdev->sb_size = MD_SB_BYTES; |
| 1192 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1193 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | |
| 1195 | memset(sb, 0, sizeof(*sb)); |
| 1196 | |
| 1197 | sb->md_magic = MD_SB_MAGIC; |
| 1198 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | sb->patch_version = mddev->patch_version; |
| 1200 | sb->gvalid_words = 0; /* ignored */ |
| 1201 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1202 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1203 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1204 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1205 | |
| 1206 | sb->ctime = mddev->ctime; |
| 1207 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1208 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | sb->raid_disks = mddev->raid_disks; |
| 1210 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1211 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | sb->utime = mddev->utime; |
| 1213 | sb->state = 0; |
| 1214 | sb->events_hi = (mddev->events>>32); |
| 1215 | sb->events_lo = (u32)mddev->events; |
| 1216 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1217 | if (mddev->reshape_position == MaxSector) |
| 1218 | sb->minor_version = 90; |
| 1219 | else { |
| 1220 | sb->minor_version = 91; |
| 1221 | sb->reshape_position = mddev->reshape_position; |
| 1222 | sb->new_level = mddev->new_level; |
| 1223 | sb->delta_disks = mddev->delta_disks; |
| 1224 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1225 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1226 | } |
| 1227 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | if (mddev->in_sync) |
| 1229 | { |
| 1230 | sb->recovery_cp = mddev->recovery_cp; |
| 1231 | sb->cp_events_hi = (mddev->events>>32); |
| 1232 | sb->cp_events_lo = (u32)mddev->events; |
| 1233 | if (mddev->recovery_cp == MaxSector) |
| 1234 | sb->state = (1<< MD_SB_CLEAN); |
| 1235 | } else |
| 1236 | sb->recovery_cp = 0; |
| 1237 | |
| 1238 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1239 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1241 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1242 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1245 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1247 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1248 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1249 | |
| 1250 | if (rdev2->raid_disk >= 0 && |
| 1251 | sb->minor_version >= 91) |
| 1252 | /* we have nowhere to store the recovery_offset, |
| 1253 | * but if it is not below the reshape_position, |
| 1254 | * we can piggy-back on that. |
| 1255 | */ |
| 1256 | is_active = 1; |
| 1257 | if (rdev2->raid_disk < 0 || |
| 1258 | test_bit(Faulty, &rdev2->flags)) |
| 1259 | is_active = 0; |
| 1260 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1261 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1263 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1264 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | d = &sb->disks[rdev2->desc_nr]; |
| 1266 | nr_disks++; |
| 1267 | d->number = rdev2->desc_nr; |
| 1268 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1269 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1270 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | d->raid_disk = rdev2->raid_disk; |
| 1272 | else |
| 1273 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1274 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1276 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1278 | if (test_bit(In_sync, &rdev2->flags)) |
| 1279 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | active++; |
| 1281 | working++; |
| 1282 | } else { |
| 1283 | d->state = 0; |
| 1284 | spare++; |
| 1285 | working++; |
| 1286 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1287 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1288 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1291 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1292 | mdp_disk_t *d = &sb->disks[i]; |
| 1293 | if (d->state == 0 && d->number == 0) { |
| 1294 | d->number = i; |
| 1295 | d->raid_disk = i; |
| 1296 | d->state = (1<<MD_DISK_REMOVED); |
| 1297 | d->state |= (1<<MD_DISK_FAULTY); |
| 1298 | failed++; |
| 1299 | } |
| 1300 | } |
| 1301 | sb->nr_disks = nr_disks; |
| 1302 | sb->active_disks = active; |
| 1303 | sb->working_disks = working; |
| 1304 | sb->failed_disks = failed; |
| 1305 | sb->spare_disks = spare; |
| 1306 | |
| 1307 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1308 | sb->sb_csum = calc_sb_csum(sb); |
| 1309 | } |
| 1310 | |
| 1311 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1312 | * rdev_size_change for 0.90.0 |
| 1313 | */ |
| 1314 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1315 | 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] | 1316 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1317 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1318 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1319 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1320 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1321 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1322 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1323 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1324 | /* Limit to 4TB as metadata cannot record more than that. |
| 1325 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1326 | */ |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1327 | if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1328 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1329 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1330 | rdev->sb_page); |
| 1331 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1332 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1333 | } |
| 1334 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1335 | static int |
| 1336 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1337 | { |
| 1338 | /* non-zero offset changes not possible with v0.90 */ |
| 1339 | return new_offset == 0; |
| 1340 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1341 | |
| 1342 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | * version 1 superblock |
| 1344 | */ |
| 1345 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1346 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1348 | __le32 disk_csum; |
| 1349 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | unsigned long long newcsum; |
| 1351 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1352 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | |
| 1354 | disk_csum = sb->sb_csum; |
| 1355 | sb->sb_csum = 0; |
| 1356 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1357 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | newcsum += le32_to_cpu(*isuper++); |
| 1359 | |
| 1360 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1361 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
| 1363 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1364 | sb->sb_csum = disk_csum; |
| 1365 | return cpu_to_le32(csum); |
| 1366 | } |
| 1367 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1368 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1369 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1370 | 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] | 1371 | { |
| 1372 | struct mdp_superblock_1 *sb; |
| 1373 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1374 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1375 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1377 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
| 1379 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1380 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | * It is always aligned to a 4K boundary and |
| 1382 | * depeding on minor_version, it can be: |
| 1383 | * 0: At least 8K, but less than 12K, from end of device |
| 1384 | * 1: At start of device |
| 1385 | * 2: 4K from start of device. |
| 1386 | */ |
| 1387 | switch(minor_version) { |
| 1388 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1389 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1390 | sb_start -= 8*2; |
| 1391 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | break; |
| 1393 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1394 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | break; |
| 1396 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1397 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | break; |
| 1399 | default: |
| 1400 | return -EINVAL; |
| 1401 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1402 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1404 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1405 | * and it is safe to read 4k, so we do that |
| 1406 | */ |
| 1407 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | if (ret) return ret; |
| 1409 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1410 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | |
| 1412 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1413 | sb->major_version != cpu_to_le32(1) || |
| 1414 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1415 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1416 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | return -EINVAL; |
| 1418 | |
| 1419 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1420 | printk("md: invalid superblock checksum on %s\n", |
| 1421 | bdevname(rdev->bdev,b)); |
| 1422 | return -EINVAL; |
| 1423 | } |
| 1424 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1425 | printk("md: data_size too small on %s\n", |
| 1426 | bdevname(rdev->bdev,b)); |
| 1427 | return -EINVAL; |
| 1428 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1429 | if (sb->pad0 || |
| 1430 | sb->pad3[0] || |
| 1431 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1432 | /* Some padding is non-zero, might be a new feature */ |
| 1433 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | rdev->preferred_minor = 0xffff; |
| 1436 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1437 | rdev->new_data_offset = rdev->data_offset; |
| 1438 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1439 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1440 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1441 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1443 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1444 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1445 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1446 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1447 | |
| 1448 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1449 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1450 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1451 | if (minor_version |
| 1452 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1453 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1454 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1455 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1456 | rdev->desc_nr = -1; |
| 1457 | else |
| 1458 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1459 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1460 | if (!rdev->bb_page) { |
| 1461 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1462 | if (!rdev->bb_page) |
| 1463 | return -ENOMEM; |
| 1464 | } |
| 1465 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1466 | rdev->badblocks.count == 0) { |
| 1467 | /* need to load the bad block list. |
| 1468 | * Currently we limit it to one page. |
| 1469 | */ |
| 1470 | s32 offset; |
| 1471 | sector_t bb_sector; |
| 1472 | u64 *bbp; |
| 1473 | int i; |
| 1474 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1475 | if (sectors > (PAGE_SIZE / 512)) |
| 1476 | return -EINVAL; |
| 1477 | offset = le32_to_cpu(sb->bblog_offset); |
| 1478 | if (offset == 0) |
| 1479 | return -EINVAL; |
| 1480 | bb_sector = (long long)offset; |
| 1481 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1482 | rdev->bb_page, READ, true)) |
| 1483 | return -EIO; |
| 1484 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1485 | rdev->badblocks.shift = sb->bblog_shift; |
| 1486 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1487 | u64 bb = le64_to_cpu(*bbp); |
| 1488 | int count = bb & (0x3ff); |
| 1489 | u64 sector = bb >> 10; |
| 1490 | sector <<= sb->bblog_shift; |
| 1491 | count <<= sb->bblog_shift; |
| 1492 | if (bb + 1 == 0) |
| 1493 | break; |
| 1494 | if (md_set_badblocks(&rdev->badblocks, |
| 1495 | sector, count, 1) == 0) |
| 1496 | return -EINVAL; |
| 1497 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1498 | } else if (sb->bblog_offset != 0) |
| 1499 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1500 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1501 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1502 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1503 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1505 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | |
| 1507 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1508 | sb->level != refsb->level || |
| 1509 | sb->layout != refsb->layout || |
| 1510 | sb->chunksize != refsb->chunksize) { |
| 1511 | printk(KERN_WARNING "md: %s has strangely different" |
| 1512 | " superblock to %s\n", |
| 1513 | bdevname(rdev->bdev,b), |
| 1514 | bdevname(refdev->bdev,b2)); |
| 1515 | return -EINVAL; |
| 1516 | } |
| 1517 | ev1 = le64_to_cpu(sb->events); |
| 1518 | ev2 = le64_to_cpu(refsb->events); |
| 1519 | |
| 1520 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1521 | ret = 1; |
| 1522 | else |
| 1523 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1525 | if (minor_version) { |
| 1526 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1527 | sectors -= rdev->data_offset; |
| 1528 | } else |
| 1529 | sectors = rdev->sb_start; |
| 1530 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1532 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1533 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1536 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1538 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1539 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1541 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1542 | clear_bit(Faulty, &rdev->flags); |
| 1543 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1544 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1545 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | if (mddev->raid_disks == 0) { |
| 1548 | mddev->major_version = 1; |
| 1549 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1550 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1551 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1553 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1554 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1555 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | mddev->layout = le32_to_cpu(sb->layout); |
| 1557 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1558 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1559 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1560 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1561 | mddev->bitmap_info.space = 0; |
| 1562 | /* Default location for bitmap is 1K after superblock |
| 1563 | * using 3K - total of 4K |
| 1564 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1565 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1566 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1567 | mddev->reshape_backwards = 0; |
| 1568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1570 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1571 | |
| 1572 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1573 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1574 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1575 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1576 | mddev->bitmap_info.offset = |
| 1577 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1578 | /* Metadata doesn't record how much space is available. |
| 1579 | * For 1.0, we assume we can use up to the superblock |
| 1580 | * if before, else to 4K beyond superblock. |
| 1581 | * For others, assume no change is possible. |
| 1582 | */ |
| 1583 | if (mddev->minor_version > 0) |
| 1584 | mddev->bitmap_info.space = 0; |
| 1585 | else if (mddev->bitmap_info.offset > 0) |
| 1586 | mddev->bitmap_info.space = |
| 1587 | 8 - mddev->bitmap_info.offset; |
| 1588 | else |
| 1589 | mddev->bitmap_info.space = |
| 1590 | -mddev->bitmap_info.offset; |
| 1591 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1592 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1593 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1594 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1595 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1596 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1597 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1598 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1599 | if (mddev->delta_disks < 0 || |
| 1600 | (mddev->delta_disks == 0 && |
| 1601 | (le32_to_cpu(sb->feature_map) |
| 1602 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1603 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1604 | } else { |
| 1605 | mddev->reshape_position = MaxSector; |
| 1606 | mddev->delta_disks = 0; |
| 1607 | mddev->new_level = mddev->level; |
| 1608 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1609 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1610 | } |
| 1611 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1612 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1613 | /* Insist of good event counter while assembling, except for |
| 1614 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1616 | if (rdev->desc_nr >= 0 && |
| 1617 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1618 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1619 | if (ev1 < mddev->events) |
| 1620 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1621 | } else if (mddev->bitmap) { |
| 1622 | /* If adding to array with a bitmap, then we can accept an |
| 1623 | * older device, but not too old. |
| 1624 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1625 | if (ev1 < mddev->bitmap->events_cleared) |
| 1626 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1627 | if (ev1 < mddev->events) |
| 1628 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1629 | } else { |
| 1630 | if (ev1 < mddev->events) |
| 1631 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1632 | return 0; |
| 1633 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1635 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1636 | if (rdev->desc_nr < 0 || |
| 1637 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1638 | role = 0xffff; |
| 1639 | rdev->desc_nr = -1; |
| 1640 | } else |
| 1641 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | switch(role) { |
| 1643 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | break; |
| 1645 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1646 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | break; |
| 1648 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1649 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1650 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1651 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1652 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1653 | if (!(le32_to_cpu(sb->feature_map) & |
| 1654 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1655 | rdev->saved_raid_disk = -1; |
| 1656 | } else |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1657 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | rdev->raid_disk = role; |
| 1659 | break; |
| 1660 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1661 | if (sb->devflags & WriteMostly1) |
| 1662 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1663 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1664 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1665 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1666 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | return 0; |
| 1669 | } |
| 1670 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1671 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | { |
| 1673 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1674 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | int max_dev, i; |
| 1676 | /* make rdev->sb match mddev and rdev data. */ |
| 1677 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1678 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
| 1680 | sb->feature_map = 0; |
| 1681 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1682 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1684 | |
| 1685 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1686 | sb->events = cpu_to_le64(mddev->events); |
| 1687 | if (mddev->in_sync) |
| 1688 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1689 | else |
| 1690 | sb->resync_offset = cpu_to_le64(0); |
| 1691 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1692 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1693 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1694 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1695 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1696 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1697 | sb->level = cpu_to_le32(mddev->level); |
| 1698 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1699 | |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1700 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1701 | sb->devflags |= WriteMostly1; |
| 1702 | else |
| 1703 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1704 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1705 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1706 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1707 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1708 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1709 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1710 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1711 | |
| 1712 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1713 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1714 | sb->feature_map |= |
| 1715 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1716 | sb->recovery_offset = |
| 1717 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1718 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 1719 | sb->feature_map |= |
| 1720 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1721 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1722 | if (test_bit(Replacement, &rdev->flags)) |
| 1723 | sb->feature_map |= |
| 1724 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1725 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1726 | if (mddev->reshape_position != MaxSector) { |
| 1727 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1728 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1729 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1730 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1731 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1732 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1733 | if (mddev->delta_disks == 0 && |
| 1734 | mddev->reshape_backwards) |
| 1735 | sb->feature_map |
| 1736 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1737 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1738 | sb->feature_map |
| 1739 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1740 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1741 | - rdev->data_offset)); |
| 1742 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1743 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1744 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1745 | if (rdev->badblocks.count == 0) |
| 1746 | /* Nothing to do for bad blocks*/ ; |
| 1747 | else if (sb->bblog_offset == 0) |
| 1748 | /* Cannot record bad blocks on this device */ |
| 1749 | md_error(mddev, rdev); |
| 1750 | else { |
| 1751 | struct badblocks *bb = &rdev->badblocks; |
| 1752 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1753 | u64 *p = bb->page; |
| 1754 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1755 | if (bb->changed) { |
| 1756 | unsigned seq; |
| 1757 | |
| 1758 | retry: |
| 1759 | seq = read_seqbegin(&bb->lock); |
| 1760 | |
| 1761 | memset(bbp, 0xff, PAGE_SIZE); |
| 1762 | |
| 1763 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1764 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1765 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1766 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1767 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1768 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1769 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1770 | if (read_seqretry(&bb->lock, seq)) |
| 1771 | goto retry; |
| 1772 | |
| 1773 | bb->sector = (rdev->sb_start + |
| 1774 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1775 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1776 | } |
| 1777 | } |
| 1778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1780 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | if (rdev2->desc_nr+1 > max_dev) |
| 1782 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1783 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1784 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1785 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1786 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1787 | rdev->sb_size = max_dev * 2 + 256; |
| 1788 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1789 | if (rdev->sb_size & bmask) |
| 1790 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1791 | } else |
| 1792 | max_dev = le32_to_cpu(sb->max_dev); |
| 1793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | for (i=0; i<max_dev;i++) |
| 1795 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1796 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1797 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1799 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1801 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1803 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1804 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | else |
| 1806 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1807 | } |
| 1808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1810 | } |
| 1811 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1812 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1813 | 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] | 1814 | { |
| 1815 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1816 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1817 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1818 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1819 | if (rdev->data_offset != rdev->new_data_offset) |
| 1820 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1821 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1822 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1823 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1824 | max_sectors -= rdev->data_offset; |
| 1825 | if (!num_sectors || num_sectors > max_sectors) |
| 1826 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1827 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1828 | /* minor version 0 with bitmap we can't move */ |
| 1829 | return 0; |
| 1830 | } else { |
| 1831 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1832 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1833 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1834 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1835 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1836 | if (!num_sectors || num_sectors > max_sectors) |
| 1837 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1838 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1839 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1840 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1841 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1842 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1843 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1844 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1845 | rdev->sb_page); |
| 1846 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1847 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1848 | |
| 1849 | } |
| 1850 | |
| 1851 | static int |
| 1852 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1853 | unsigned long long new_offset) |
| 1854 | { |
| 1855 | /* All necessary checks on new >= old have been done */ |
| 1856 | struct bitmap *bitmap; |
| 1857 | if (new_offset >= rdev->data_offset) |
| 1858 | return 1; |
| 1859 | |
| 1860 | /* with 1.0 metadata, there is no metadata to tread on |
| 1861 | * so we can always move back */ |
| 1862 | if (rdev->mddev->minor_version == 0) |
| 1863 | return 1; |
| 1864 | |
| 1865 | /* otherwise we must be sure not to step on |
| 1866 | * any metadata, so stay: |
| 1867 | * 36K beyond start of superblock |
| 1868 | * beyond end of badblocks |
| 1869 | * beyond write-intent bitmap |
| 1870 | */ |
| 1871 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1872 | return 0; |
| 1873 | bitmap = rdev->mddev->bitmap; |
| 1874 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1875 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1876 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1877 | return 0; |
| 1878 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1879 | return 0; |
| 1880 | |
| 1881 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1882 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1884 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | [0] = { |
| 1886 | .name = "0.90.0", |
| 1887 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1888 | .load_super = super_90_load, |
| 1889 | .validate_super = super_90_validate, |
| 1890 | .sync_super = super_90_sync, |
| 1891 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1892 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | }, |
| 1894 | [1] = { |
| 1895 | .name = "md-1", |
| 1896 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1897 | .load_super = super_1_load, |
| 1898 | .validate_super = super_1_validate, |
| 1899 | .sync_super = super_1_sync, |
| 1900 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1901 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | }, |
| 1903 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1905 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1906 | { |
| 1907 | if (mddev->sync_super) { |
| 1908 | mddev->sync_super(mddev, rdev); |
| 1909 | return; |
| 1910 | } |
| 1911 | |
| 1912 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1913 | |
| 1914 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1915 | } |
| 1916 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1917 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1919 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1921 | rcu_read_lock(); |
| 1922 | rdev_for_each_rcu(rdev, mddev1) |
| 1923 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1924 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1925 | rdev2->bdev->bd_contains) { |
| 1926 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1927 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1928 | } |
| 1929 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | return 0; |
| 1931 | } |
| 1932 | |
| 1933 | static LIST_HEAD(pending_raid_disks); |
| 1934 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1935 | /* |
| 1936 | * Try to register data integrity profile for an mddev |
| 1937 | * |
| 1938 | * This is called when an array is started and after a disk has been kicked |
| 1939 | * from the array. It only succeeds if all working and active component devices |
| 1940 | * are integrity capable with matching profiles. |
| 1941 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1942 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1943 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1944 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1945 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1946 | if (list_empty(&mddev->disks)) |
| 1947 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1948 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1949 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1950 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1951 | /* skip spares and non-functional disks */ |
| 1952 | if (test_bit(Faulty, &rdev->flags)) |
| 1953 | continue; |
| 1954 | if (rdev->raid_disk < 0) |
| 1955 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1956 | if (!reference) { |
| 1957 | /* Use the first rdev as the reference */ |
| 1958 | reference = rdev; |
| 1959 | continue; |
| 1960 | } |
| 1961 | /* does this rdev's profile match the reference profile? */ |
| 1962 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1963 | rdev->bdev->bd_disk) < 0) |
| 1964 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1965 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1966 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1967 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1968 | /* |
| 1969 | * All component devices are integrity capable and have matching |
| 1970 | * profiles, register the common profile for the md device. |
| 1971 | */ |
| 1972 | if (blk_integrity_register(mddev->gendisk, |
| 1973 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1974 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1975 | mdname(mddev)); |
| 1976 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1977 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1978 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1979 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1980 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1981 | mdname(mddev)); |
| 1982 | return -EINVAL; |
| 1983 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1984 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1985 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1986 | EXPORT_SYMBOL(md_integrity_register); |
| 1987 | |
| 1988 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1989 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1990 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 1991 | struct blk_integrity *bi_rdev; |
| 1992 | struct blk_integrity *bi_mddev; |
| 1993 | |
| 1994 | if (!mddev->gendisk) |
| 1995 | return; |
| 1996 | |
| 1997 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1998 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1999 | |
| 2000 | if (!bi_mddev) /* nothing to do */ |
| 2001 | return; |
| 2002 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2003 | return; |
| 2004 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2005 | rdev->bdev->bd_disk) >= 0) |
| 2006 | return; |
| 2007 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2008 | blk_integrity_unregister(mddev->gendisk); |
| 2009 | } |
| 2010 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2011 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2012 | 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] | 2013 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2014 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2015 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2016 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2017 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2019 | /* prevent duplicates */ |
| 2020 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2021 | return -EEXIST; |
| 2022 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2023 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2024 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2025 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2026 | if (mddev->pers) { |
| 2027 | /* Cannot change size, so fail |
| 2028 | * If mddev->level <= 0, then we don't care |
| 2029 | * about aligning sizes (e.g. linear) |
| 2030 | */ |
| 2031 | if (mddev->level > 0) |
| 2032 | return -ENOSPC; |
| 2033 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2034 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2035 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | |
| 2037 | /* Verify rdev->desc_nr is unique. |
| 2038 | * If it is -1, assign a free number, else |
| 2039 | * check number is not in use |
| 2040 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2041 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | if (rdev->desc_nr < 0) { |
| 2043 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2044 | if (mddev->pers) |
| 2045 | choice = mddev->raid_disks; |
| 2046 | while (find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | choice++; |
| 2048 | rdev->desc_nr = choice; |
| 2049 | } else { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2050 | if (find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
| 2051 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2053 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2055 | rcu_read_unlock(); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2056 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2057 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2058 | mdname(mddev), mddev->max_disks); |
| 2059 | return -EBUSY; |
| 2060 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2061 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2062 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2063 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2066 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2067 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2068 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2069 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2070 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2071 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2072 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2073 | /* failure here is OK */; |
| 2074 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2075 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2076 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2077 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2078 | |
| 2079 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2080 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2083 | |
| 2084 | fail: |
| 2085 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2086 | b, mdname(mddev)); |
| 2087 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | } |
| 2089 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2090 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2091 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2092 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2093 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2094 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2097 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | { |
| 2099 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2100 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2101 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2102 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2104 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2105 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2106 | sysfs_put(rdev->sysfs_state); |
| 2107 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2108 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2109 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2110 | * writing to 'remove' to "dev/state". We also need |
| 2111 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2112 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2113 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2114 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2115 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2116 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | /* |
| 2120 | * prevent the device from being mounted, repartitioned or |
| 2121 | * otherwise reused by a RAID array (or any other kernel |
| 2122 | * subsystem), by bd_claiming the device. |
| 2123 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2124 | 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] | 2125 | { |
| 2126 | int err = 0; |
| 2127 | struct block_device *bdev; |
| 2128 | char b[BDEVNAME_SIZE]; |
| 2129 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2130 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2131 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | if (IS_ERR(bdev)) { |
| 2133 | printk(KERN_ERR "md: could not open %s.\n", |
| 2134 | __bdevname(dev, b)); |
| 2135 | return PTR_ERR(bdev); |
| 2136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | rdev->bdev = bdev; |
| 2138 | return err; |
| 2139 | } |
| 2140 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2141 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | { |
| 2143 | struct block_device *bdev = rdev->bdev; |
| 2144 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2145 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | void md_autodetect_dev(dev_t dev); |
| 2149 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2150 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | { |
| 2152 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2155 | bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2156 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2158 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2159 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | #endif |
| 2161 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2162 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | } |
| 2164 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2165 | static void kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | { |
| 2167 | unbind_rdev_from_array(rdev); |
| 2168 | export_rdev(rdev); |
| 2169 | } |
| 2170 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2171 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2173 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2175 | while (!list_empty(&mddev->disks)) { |
| 2176 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2177 | same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | kick_rdev_from_array(rdev); |
| 2179 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | mddev->raid_disks = 0; |
| 2181 | mddev->major_version = 0; |
| 2182 | } |
| 2183 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2184 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2186 | /* Update each superblock (in-memory image), but |
| 2187 | * if we are allowed to, skip spares which already |
| 2188 | * have the right event counter, or have one earlier |
| 2189 | * (which would mean they aren't being marked as dirty |
| 2190 | * with the rest of the array) |
| 2191 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2192 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2193 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2194 | if (rdev->sb_events == mddev->events || |
| 2195 | (nospares && |
| 2196 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2197 | rdev->sb_events+1 == mddev->events)) { |
| 2198 | /* Don't update this superblock */ |
| 2199 | rdev->sb_loaded = 2; |
| 2200 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2201 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2202 | rdev->sb_loaded = 1; |
| 2203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | } |
| 2205 | } |
| 2206 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2207 | static void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2209 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2210 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2211 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2212 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2214 | if (mddev->ro) { |
| 2215 | if (force_change) |
| 2216 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2217 | return; |
| 2218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2220 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2221 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2222 | if (rdev->raid_disk >= 0 && |
| 2223 | mddev->delta_disks >= 0 && |
| 2224 | !test_bit(In_sync, &rdev->flags) && |
| 2225 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2226 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2227 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2228 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2229 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2230 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2231 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2232 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2233 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2234 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2235 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2236 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2237 | md_ack_all_badblocks(&rdev->badblocks); |
| 2238 | md_error(mddev, rdev); |
| 2239 | } |
| 2240 | clear_bit(Blocked, &rdev->flags); |
| 2241 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2242 | wake_up(&rdev->blocked_wait); |
| 2243 | } |
| 2244 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2245 | wake_up(&mddev->sb_wait); |
| 2246 | return; |
| 2247 | } |
| 2248 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2249 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2250 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2251 | mddev->utime = get_seconds(); |
| 2252 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2253 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2254 | force_change = 1; |
| 2255 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2256 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2257 | * though if events isn't the right even/odd, we will have to do |
| 2258 | * spares after all |
| 2259 | */ |
| 2260 | nospares = 1; |
| 2261 | if (force_change) |
| 2262 | nospares = 0; |
| 2263 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2264 | /* If the array is degraded, then skipping spares is both |
| 2265 | * dangerous and fairly pointless. |
| 2266 | * Dangerous because a device that was removed from the array |
| 2267 | * might have a event_count that still looks up-to-date, |
| 2268 | * so it can be re-added without a resync. |
| 2269 | * Pointless because if there are any spares to skip, |
| 2270 | * then a recovery will happen and soon that array won't |
| 2271 | * be degraded any more and the spare can go back to sleep then. |
| 2272 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2273 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2274 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2275 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2276 | |
| 2277 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2278 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2279 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2280 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2281 | && mddev->can_decrease_events |
| 2282 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2283 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2284 | mddev->can_decrease_events = 0; |
| 2285 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2286 | /* otherwise we have to go forward and ... */ |
| 2287 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2288 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2291 | /* |
| 2292 | * This 64-bit counter should never wrap. |
| 2293 | * Either we are in around ~1 trillion A.C., assuming |
| 2294 | * 1 reboot per second, or we have a bug... |
| 2295 | */ |
| 2296 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2297 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2298 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2299 | if (rdev->badblocks.changed) |
| 2300 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2301 | if (test_bit(Faulty, &rdev->flags)) |
| 2302 | set_bit(FaultRecorded, &rdev->flags); |
| 2303 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2304 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2305 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2306 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2308 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2309 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2311 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2312 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2314 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2315 | if (rdev->sb_loaded != 1) |
| 2316 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2318 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2319 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2320 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2321 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2322 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2323 | bdevname(rdev->bdev, b), |
| 2324 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2325 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2326 | if (rdev->badblocks.size) { |
| 2327 | md_super_write(mddev, rdev, |
| 2328 | rdev->badblocks.sector, |
| 2329 | rdev->badblocks.size << 9, |
| 2330 | rdev->bb_page); |
| 2331 | rdev->badblocks.size = 0; |
| 2332 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2333 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2334 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2335 | pr_debug("md: %s (skipping faulty)\n", |
| 2336 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2337 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2338 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | /* only need to write one superblock... */ |
| 2340 | break; |
| 2341 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2342 | md_super_wait(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2343 | /* 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] | 2344 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2345 | spin_lock(&mddev->lock); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2346 | if (mddev->in_sync != sync_req || |
| 2347 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2348 | /* have to write it out again */ |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2349 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2350 | goto repeat; |
| 2351 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2352 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2353 | spin_unlock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2354 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2355 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2356 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2357 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2358 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2359 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2360 | clear_bit(Blocked, &rdev->flags); |
| 2361 | |
| 2362 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2363 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2364 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2365 | wake_up(&rdev->blocked_wait); |
| 2366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | } |
| 2368 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2369 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2370 | * We want to accept with case. For this we use cmd_match. |
| 2371 | */ |
| 2372 | static int cmd_match(const char *cmd, const char *str) |
| 2373 | { |
| 2374 | /* See if cmd, written into a sysfs file, matches |
| 2375 | * str. They must either be the same, or cmd can |
| 2376 | * have a trailing newline |
| 2377 | */ |
| 2378 | while (*cmd && *str && *cmd == *str) { |
| 2379 | cmd++; |
| 2380 | str++; |
| 2381 | } |
| 2382 | if (*cmd == '\n') |
| 2383 | cmd++; |
| 2384 | if (*str || *cmd) |
| 2385 | return 0; |
| 2386 | return 1; |
| 2387 | } |
| 2388 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2389 | struct rdev_sysfs_entry { |
| 2390 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2391 | ssize_t (*show)(struct md_rdev *, char *); |
| 2392 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2393 | }; |
| 2394 | |
| 2395 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2396 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2397 | { |
| 2398 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2399 | size_t len = 0; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2400 | unsigned long flags = ACCESS_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2401 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2402 | if (test_bit(Faulty, &flags) || |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2403 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2404 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2405 | sep = ","; |
| 2406 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2407 | if (test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2408 | len += sprintf(page+len, "%sin_sync",sep); |
| 2409 | sep = ","; |
| 2410 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2411 | if (test_bit(WriteMostly, &flags)) { |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2412 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2413 | sep = ","; |
| 2414 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2415 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2416 | (rdev->badblocks.unacked_exist |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2417 | && !test_bit(Faulty, &flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2418 | len += sprintf(page+len, "%sblocked", sep); |
| 2419 | sep = ","; |
| 2420 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2421 | if (!test_bit(Faulty, &flags) && |
| 2422 | !test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2423 | len += sprintf(page+len, "%sspare", sep); |
| 2424 | sep = ","; |
| 2425 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2426 | if (test_bit(WriteErrorSeen, &flags)) { |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2427 | len += sprintf(page+len, "%swrite_error", sep); |
| 2428 | sep = ","; |
| 2429 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2430 | if (test_bit(WantReplacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2431 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2432 | sep = ","; |
| 2433 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2434 | if (test_bit(Replacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2435 | len += sprintf(page+len, "%sreplacement", sep); |
| 2436 | sep = ","; |
| 2437 | } |
| 2438 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2439 | return len+sprintf(page+len, "\n"); |
| 2440 | } |
| 2441 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2442 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2443 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2444 | { |
| 2445 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2446 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2447 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2448 | * writemostly - sets write_mostly |
| 2449 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2450 | * blocked - sets the Blocked flags |
| 2451 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2452 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2453 | * -insync - clear Insync for a device with a slot assigned, |
| 2454 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2455 | * write_error - sets WriteErrorSeen |
| 2456 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2457 | */ |
| 2458 | int err = -EINVAL; |
| 2459 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2460 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2461 | if (test_bit(Faulty, &rdev->flags)) |
| 2462 | err = 0; |
| 2463 | else |
| 2464 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2465 | } else if (cmd_match(buf, "remove")) { |
| 2466 | if (rdev->raid_disk >= 0) |
| 2467 | err = -EBUSY; |
| 2468 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2469 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2470 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2471 | if (mddev->pers) |
| 2472 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2473 | md_new_event(mddev); |
| 2474 | err = 0; |
| 2475 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2476 | } else if (cmd_match(buf, "writemostly")) { |
| 2477 | set_bit(WriteMostly, &rdev->flags); |
| 2478 | err = 0; |
| 2479 | } else if (cmd_match(buf, "-writemostly")) { |
| 2480 | clear_bit(WriteMostly, &rdev->flags); |
| 2481 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2482 | } else if (cmd_match(buf, "blocked")) { |
| 2483 | set_bit(Blocked, &rdev->flags); |
| 2484 | err = 0; |
| 2485 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2486 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2487 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2488 | /* metadata handler doesn't understand badblocks, |
| 2489 | * so we need to fail the device |
| 2490 | */ |
| 2491 | md_error(rdev->mddev, rdev); |
| 2492 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2493 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2494 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2495 | wake_up(&rdev->blocked_wait); |
| 2496 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2497 | md_wakeup_thread(rdev->mddev->thread); |
| 2498 | |
| 2499 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2500 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2501 | set_bit(In_sync, &rdev->flags); |
| 2502 | err = 0; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2503 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 2504 | if (rdev->mddev->pers == NULL) { |
| 2505 | clear_bit(In_sync, &rdev->flags); |
| 2506 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2507 | rdev->raid_disk = -1; |
| 2508 | err = 0; |
| 2509 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2510 | } else if (cmd_match(buf, "write_error")) { |
| 2511 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2512 | err = 0; |
| 2513 | } else if (cmd_match(buf, "-write_error")) { |
| 2514 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2515 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2516 | } else if (cmd_match(buf, "want_replacement")) { |
| 2517 | /* Any non-spare device that is not a replacement can |
| 2518 | * become want_replacement at any time, but we then need to |
| 2519 | * check if recovery is needed. |
| 2520 | */ |
| 2521 | if (rdev->raid_disk >= 0 && |
| 2522 | !test_bit(Replacement, &rdev->flags)) |
| 2523 | set_bit(WantReplacement, &rdev->flags); |
| 2524 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2525 | md_wakeup_thread(rdev->mddev->thread); |
| 2526 | err = 0; |
| 2527 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2528 | /* Clearing 'want_replacement' is always allowed. |
| 2529 | * Once replacements starts it is too late though. |
| 2530 | */ |
| 2531 | err = 0; |
| 2532 | clear_bit(WantReplacement, &rdev->flags); |
| 2533 | } else if (cmd_match(buf, "replacement")) { |
| 2534 | /* Can only set a device as a replacement when array has not |
| 2535 | * yet been started. Once running, replacement is automatic |
| 2536 | * from spares, or by assigning 'slot'. |
| 2537 | */ |
| 2538 | if (rdev->mddev->pers) |
| 2539 | err = -EBUSY; |
| 2540 | else { |
| 2541 | set_bit(Replacement, &rdev->flags); |
| 2542 | err = 0; |
| 2543 | } |
| 2544 | } else if (cmd_match(buf, "-replacement")) { |
| 2545 | /* Similarly, can only clear Replacement before start */ |
| 2546 | if (rdev->mddev->pers) |
| 2547 | err = -EBUSY; |
| 2548 | else { |
| 2549 | clear_bit(Replacement, &rdev->flags); |
| 2550 | err = 0; |
| 2551 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2552 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2553 | if (!err) |
| 2554 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2555 | return err ? err : len; |
| 2556 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2557 | static struct rdev_sysfs_entry rdev_state = |
| 2558 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2559 | |
| 2560 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2561 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2562 | { |
| 2563 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2564 | } |
| 2565 | |
| 2566 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2567 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2568 | { |
| 2569 | char *e; |
| 2570 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2571 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2572 | atomic_set(&rdev->corrected_errors, n); |
| 2573 | return len; |
| 2574 | } |
| 2575 | return -EINVAL; |
| 2576 | } |
| 2577 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2578 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2579 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2580 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2581 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2582 | { |
| 2583 | if (rdev->raid_disk < 0) |
| 2584 | return sprintf(page, "none\n"); |
| 2585 | else |
| 2586 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2587 | } |
| 2588 | |
| 2589 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2590 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2591 | { |
| 2592 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2593 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2594 | int slot = simple_strtoul(buf, &e, 10); |
| 2595 | if (strncmp(buf, "none", 4)==0) |
| 2596 | slot = -1; |
| 2597 | else if (e==buf || (*e && *e!= '\n')) |
| 2598 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2599 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2600 | /* Setting 'slot' on an active array requires also |
| 2601 | * updating the 'rd%d' link, and communicating |
| 2602 | * with the personality with ->hot_*_disk. |
| 2603 | * For now we only support removing |
| 2604 | * failed/spare devices. This normally happens automatically, |
| 2605 | * but not when the metadata is externally managed. |
| 2606 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2607 | if (rdev->raid_disk == -1) |
| 2608 | return -EEXIST; |
| 2609 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2610 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2611 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2612 | clear_bit(Blocked, &rdev->flags); |
| 2613 | remove_and_add_spares(rdev->mddev, rdev); |
| 2614 | if (rdev->raid_disk >= 0) |
| 2615 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2616 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2617 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2618 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2619 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2620 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2621 | */ |
| 2622 | |
| 2623 | if (rdev->raid_disk != -1) |
| 2624 | return -EBUSY; |
| 2625 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2626 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2627 | return -EBUSY; |
| 2628 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2629 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2630 | return -EINVAL; |
| 2631 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2632 | if (slot >= rdev->mddev->raid_disks && |
| 2633 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2634 | return -ENOSPC; |
| 2635 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2636 | rdev->raid_disk = slot; |
| 2637 | if (test_bit(In_sync, &rdev->flags)) |
| 2638 | rdev->saved_raid_disk = slot; |
| 2639 | else |
| 2640 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2641 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 2642 | clear_bit(Bitmap_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2643 | err = rdev->mddev->pers-> |
| 2644 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2645 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2646 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2647 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2648 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2649 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2650 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2651 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2652 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2653 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2654 | if (slot >= rdev->mddev->raid_disks && |
| 2655 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2656 | return -ENOSPC; |
| 2657 | rdev->raid_disk = slot; |
| 2658 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2659 | clear_bit(Faulty, &rdev->flags); |
| 2660 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2661 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2662 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2663 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2664 | return len; |
| 2665 | } |
| 2666 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2667 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2668 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2669 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2670 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2671 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2672 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2673 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2677 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2678 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2679 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2680 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2681 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2682 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2683 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2684 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2685 | /* Must set offset before size, so overlap checks |
| 2686 | * can be sane */ |
| 2687 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2688 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2689 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2690 | return len; |
| 2691 | } |
| 2692 | |
| 2693 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2694 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2695 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2696 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2697 | { |
| 2698 | return sprintf(page, "%llu\n", |
| 2699 | (unsigned long long)rdev->new_data_offset); |
| 2700 | } |
| 2701 | |
| 2702 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2703 | const char *buf, size_t len) |
| 2704 | { |
| 2705 | unsigned long long new_offset; |
| 2706 | struct mddev *mddev = rdev->mddev; |
| 2707 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2708 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2709 | return -EINVAL; |
| 2710 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 2711 | if (mddev->sync_thread || |
| 2712 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2713 | return -EBUSY; |
| 2714 | if (new_offset == rdev->data_offset) |
| 2715 | /* reset is always permitted */ |
| 2716 | ; |
| 2717 | else if (new_offset > rdev->data_offset) { |
| 2718 | /* must not push array size beyond rdev_sectors */ |
| 2719 | if (new_offset - rdev->data_offset |
| 2720 | + mddev->dev_sectors > rdev->sectors) |
| 2721 | return -E2BIG; |
| 2722 | } |
| 2723 | /* Metadata worries about other space details. */ |
| 2724 | |
| 2725 | /* decreasing the offset is inconsistent with a backwards |
| 2726 | * reshape. |
| 2727 | */ |
| 2728 | if (new_offset < rdev->data_offset && |
| 2729 | mddev->reshape_backwards) |
| 2730 | return -EINVAL; |
| 2731 | /* Increasing offset is inconsistent with forwards |
| 2732 | * reshape. reshape_direction should be set to |
| 2733 | * 'backwards' first. |
| 2734 | */ |
| 2735 | if (new_offset > rdev->data_offset && |
| 2736 | !mddev->reshape_backwards) |
| 2737 | return -EINVAL; |
| 2738 | |
| 2739 | if (mddev->pers && mddev->persistent && |
| 2740 | !super_types[mddev->major_version] |
| 2741 | .allow_new_offset(rdev, new_offset)) |
| 2742 | return -E2BIG; |
| 2743 | rdev->new_data_offset = new_offset; |
| 2744 | if (new_offset > rdev->data_offset) |
| 2745 | mddev->reshape_backwards = 1; |
| 2746 | else if (new_offset < rdev->data_offset) |
| 2747 | mddev->reshape_backwards = 0; |
| 2748 | |
| 2749 | return len; |
| 2750 | } |
| 2751 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2752 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2753 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2754 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2755 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2756 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2757 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2758 | } |
| 2759 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2760 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2761 | { |
| 2762 | /* check if two start/length pairs overlap */ |
| 2763 | if (s1+l1 <= s2) |
| 2764 | return 0; |
| 2765 | if (s2+l2 <= s1) |
| 2766 | return 0; |
| 2767 | return 1; |
| 2768 | } |
| 2769 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2770 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2771 | { |
| 2772 | unsigned long long blocks; |
| 2773 | sector_t new; |
| 2774 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2775 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2776 | return -EINVAL; |
| 2777 | |
| 2778 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2779 | return -EINVAL; /* sector conversion overflow */ |
| 2780 | |
| 2781 | new = blocks * 2; |
| 2782 | if (new != blocks * 2) |
| 2783 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2784 | |
| 2785 | *sectors = new; |
| 2786 | return 0; |
| 2787 | } |
| 2788 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2789 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2790 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2791 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2792 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2793 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2794 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2795 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2796 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2797 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2798 | if (rdev->data_offset != rdev->new_data_offset) |
| 2799 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2800 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2801 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2802 | sectors = super_types[my_mddev->major_version]. |
| 2803 | rdev_size_change(rdev, sectors); |
| 2804 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2805 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2806 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2807 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2808 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 2809 | if (!my_mddev->pers->resize) |
| 2810 | /* Cannot change size for RAID0 or Linear etc */ |
| 2811 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2812 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2813 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2814 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2815 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2816 | rdev->sectors = sectors; |
| 2817 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2818 | /* Need to check that all other rdevs with the same |
| 2819 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 2820 | * the rdev lists safely. |
| 2821 | * This check does not provide a hard guarantee, it |
| 2822 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2823 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2824 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2825 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2826 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2827 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2828 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2829 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2830 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2831 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2832 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2833 | if (rdev->bdev == rdev2->bdev && |
| 2834 | rdev != rdev2 && |
| 2835 | overlaps(rdev->data_offset, rdev->sectors, |
| 2836 | rdev2->data_offset, |
| 2837 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2838 | overlap = 1; |
| 2839 | break; |
| 2840 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2841 | if (overlap) { |
| 2842 | mddev_put(mddev); |
| 2843 | break; |
| 2844 | } |
| 2845 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2846 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2847 | if (overlap) { |
| 2848 | /* Someone else could have slipped in a size |
| 2849 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2850 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2851 | * safe, and trust userspace not to race with |
| 2852 | * itself |
| 2853 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2854 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2855 | return -EBUSY; |
| 2856 | } |
| 2857 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2858 | return len; |
| 2859 | } |
| 2860 | |
| 2861 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2862 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2863 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2864 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2865 | { |
| 2866 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2867 | |
| 2868 | if (test_bit(In_sync, &rdev->flags) || |
| 2869 | recovery_start == MaxSector) |
| 2870 | return sprintf(page, "none\n"); |
| 2871 | |
| 2872 | return sprintf(page, "%llu\n", recovery_start); |
| 2873 | } |
| 2874 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2875 | 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] | 2876 | { |
| 2877 | unsigned long long recovery_start; |
| 2878 | |
| 2879 | if (cmd_match(buf, "none")) |
| 2880 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2881 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2882 | return -EINVAL; |
| 2883 | |
| 2884 | if (rdev->mddev->pers && |
| 2885 | rdev->raid_disk >= 0) |
| 2886 | return -EBUSY; |
| 2887 | |
| 2888 | rdev->recovery_offset = recovery_start; |
| 2889 | if (recovery_start == MaxSector) |
| 2890 | set_bit(In_sync, &rdev->flags); |
| 2891 | else |
| 2892 | clear_bit(In_sync, &rdev->flags); |
| 2893 | return len; |
| 2894 | } |
| 2895 | |
| 2896 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2897 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2898 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2899 | static ssize_t |
| 2900 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 2901 | static ssize_t |
| 2902 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 2903 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2904 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2905 | { |
| 2906 | return badblocks_show(&rdev->badblocks, page, 0); |
| 2907 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2908 | 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] | 2909 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2910 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 2911 | /* Maybe that ack was all we needed */ |
| 2912 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 2913 | wake_up(&rdev->blocked_wait); |
| 2914 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2915 | } |
| 2916 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 2917 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 2918 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2919 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2920 | { |
| 2921 | return badblocks_show(&rdev->badblocks, page, 1); |
| 2922 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2923 | 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] | 2924 | { |
| 2925 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 2926 | } |
| 2927 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 2928 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 2929 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2930 | static struct attribute *rdev_default_attrs[] = { |
| 2931 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2932 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2933 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2934 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2935 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2936 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2937 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2938 | &rdev_bad_blocks.attr, |
| 2939 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2940 | NULL, |
| 2941 | }; |
| 2942 | static ssize_t |
| 2943 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2944 | { |
| 2945 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2946 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2947 | |
| 2948 | if (!entry->show) |
| 2949 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2950 | if (!rdev->mddev) |
| 2951 | return -EBUSY; |
| 2952 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | static ssize_t |
| 2956 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2957 | const char *page, size_t length) |
| 2958 | { |
| 2959 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2960 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2961 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2962 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2963 | |
| 2964 | if (!entry->store) |
| 2965 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2966 | if (!capable(CAP_SYS_ADMIN)) |
| 2967 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2968 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2969 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2970 | if (rdev->mddev == NULL) |
| 2971 | rv = -EBUSY; |
| 2972 | else |
| 2973 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2974 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2975 | } |
| 2976 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | static void rdev_free(struct kobject *ko) |
| 2980 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2981 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2982 | kfree(rdev); |
| 2983 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2984 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2985 | .show = rdev_attr_show, |
| 2986 | .store = rdev_attr_store, |
| 2987 | }; |
| 2988 | static struct kobj_type rdev_ktype = { |
| 2989 | .release = rdev_free, |
| 2990 | .sysfs_ops = &rdev_sysfs_ops, |
| 2991 | .default_attrs = rdev_default_attrs, |
| 2992 | }; |
| 2993 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2994 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2995 | { |
| 2996 | rdev->desc_nr = -1; |
| 2997 | rdev->saved_raid_disk = -1; |
| 2998 | rdev->raid_disk = -1; |
| 2999 | rdev->flags = 0; |
| 3000 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3001 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3002 | rdev->sb_events = 0; |
| 3003 | rdev->last_read_error.tv_sec = 0; |
| 3004 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3005 | rdev->sb_loaded = 0; |
| 3006 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3007 | atomic_set(&rdev->nr_pending, 0); |
| 3008 | atomic_set(&rdev->read_errors, 0); |
| 3009 | atomic_set(&rdev->corrected_errors, 0); |
| 3010 | |
| 3011 | INIT_LIST_HEAD(&rdev->same_set); |
| 3012 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3013 | |
| 3014 | /* Add space to store bad block list. |
| 3015 | * This reserves the space even on arrays where it cannot |
| 3016 | * be used - I wonder if that matters |
| 3017 | */ |
| 3018 | rdev->badblocks.count = 0; |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 3019 | rdev->badblocks.shift = -1; /* disabled until explicitly enabled */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3020 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3021 | seqlock_init(&rdev->badblocks.lock); |
| 3022 | if (rdev->badblocks.page == NULL) |
| 3023 | return -ENOMEM; |
| 3024 | |
| 3025 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3026 | } |
| 3027 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | /* |
| 3029 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3030 | * |
| 3031 | * mark the device faulty if: |
| 3032 | * |
| 3033 | * - the device is nonexistent (zero size) |
| 3034 | * - the device has no valid superblock |
| 3035 | * |
| 3036 | * a faulty rdev _never_ has rdev->sb set. |
| 3037 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3038 | 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] | 3039 | { |
| 3040 | char b[BDEVNAME_SIZE]; |
| 3041 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3042 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | sector_t size; |
| 3044 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3045 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | if (!rdev) { |
| 3047 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3048 | return ERR_PTR(-ENOMEM); |
| 3049 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3050 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3051 | err = md_rdev_init(rdev); |
| 3052 | if (err) |
| 3053 | goto abort_free; |
| 3054 | err = alloc_disk_sb(rdev); |
| 3055 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | goto abort_free; |
| 3057 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3058 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | if (err) |
| 3060 | goto abort_free; |
| 3061 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3062 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3063 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3064 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | if (!size) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3066 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3068 | bdevname(rdev->bdev,b)); |
| 3069 | err = -EINVAL; |
| 3070 | goto abort_free; |
| 3071 | } |
| 3072 | |
| 3073 | if (super_format >= 0) { |
| 3074 | err = super_types[super_format]. |
| 3075 | load_super(rdev, NULL, super_minor); |
| 3076 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3077 | printk(KERN_WARNING |
| 3078 | "md: %s does not have a valid v%d.%d " |
| 3079 | "superblock, not importing!\n", |
| 3080 | bdevname(rdev->bdev,b), |
| 3081 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | goto abort_free; |
| 3083 | } |
| 3084 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3085 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | "md: could not read %s's sb, not importing!\n", |
| 3087 | bdevname(rdev->bdev,b)); |
| 3088 | goto abort_free; |
| 3089 | } |
| 3090 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | return rdev; |
| 3093 | |
| 3094 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3095 | if (rdev->bdev) |
| 3096 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3097 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | kfree(rdev); |
| 3099 | return ERR_PTR(err); |
| 3100 | } |
| 3101 | |
| 3102 | /* |
| 3103 | * Check a full RAID array for plausibility |
| 3104 | */ |
| 3105 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3106 | static void analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | { |
| 3108 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3109 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | char b[BDEVNAME_SIZE]; |
| 3111 | |
| 3112 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3113 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | switch (super_types[mddev->major_version]. |
| 3115 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3116 | case 1: |
| 3117 | freshest = rdev; |
| 3118 | break; |
| 3119 | case 0: |
| 3120 | break; |
| 3121 | default: |
| 3122 | printk( KERN_ERR \ |
| 3123 | "md: fatal superblock inconsistency in %s" |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3124 | " -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | bdevname(rdev->bdev,b)); |
| 3126 | kick_rdev_from_array(rdev); |
| 3127 | } |
| 3128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3129 | super_types[mddev->major_version]. |
| 3130 | validate_super(mddev, freshest); |
| 3131 | |
| 3132 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3133 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3134 | if (mddev->max_disks && |
| 3135 | (rdev->desc_nr >= mddev->max_disks || |
| 3136 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3137 | printk(KERN_WARNING |
| 3138 | "md: %s: %s: only %d devices permitted\n", |
| 3139 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3140 | mddev->max_disks); |
| 3141 | kick_rdev_from_array(rdev); |
| 3142 | continue; |
| 3143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | if (rdev != freshest) |
| 3145 | if (super_types[mddev->major_version]. |
| 3146 | validate_super(mddev, rdev)) { |
| 3147 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3148 | " from array!\n", |
| 3149 | bdevname(rdev->bdev,b)); |
| 3150 | kick_rdev_from_array(rdev); |
| 3151 | continue; |
| 3152 | } |
| 3153 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3154 | rdev->desc_nr = i++; |
| 3155 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3156 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3157 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3158 | rdev->raid_disk = -1; |
| 3159 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | } |
| 3161 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | } |
| 3163 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3164 | /* Read a fixed-point number. |
| 3165 | * Numbers in sysfs attributes should be in "standard" units where |
| 3166 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3167 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3168 | * milliseconds or jiffies. |
| 3169 | * This function takes a decimal number with a possible fractional |
| 3170 | * component, and produces an integer which is the result of |
| 3171 | * multiplying that number by 10^'scale'. |
| 3172 | * all without any floating-point arithmetic. |
| 3173 | */ |
| 3174 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3175 | { |
| 3176 | unsigned long result = 0; |
| 3177 | long decimals = -1; |
| 3178 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3179 | if (*cp == '.') |
| 3180 | decimals = 0; |
| 3181 | else if (decimals < scale) { |
| 3182 | unsigned int value; |
| 3183 | value = *cp - '0'; |
| 3184 | result = result * 10 + value; |
| 3185 | if (decimals >= 0) |
| 3186 | decimals++; |
| 3187 | } |
| 3188 | cp++; |
| 3189 | } |
| 3190 | if (*cp == '\n') |
| 3191 | cp++; |
| 3192 | if (*cp) |
| 3193 | return -EINVAL; |
| 3194 | if (decimals < 0) |
| 3195 | decimals = 0; |
| 3196 | while (decimals < scale) { |
| 3197 | result *= 10; |
| 3198 | decimals ++; |
| 3199 | } |
| 3200 | *res = result; |
| 3201 | return 0; |
| 3202 | } |
| 3203 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3204 | static void md_safemode_timeout(unsigned long data); |
| 3205 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3206 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3207 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3208 | { |
| 3209 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3210 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3211 | } |
| 3212 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3213 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3214 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3215 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3216 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3217 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3218 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3219 | if (msec == 0) |
| 3220 | mddev->safemode_delay = 0; |
| 3221 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3222 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3223 | unsigned long new_delay = (msec*HZ)/1000; |
| 3224 | |
| 3225 | if (new_delay == 0) |
| 3226 | new_delay = 1; |
| 3227 | mddev->safemode_delay = new_delay; |
| 3228 | if (new_delay < old_delay || old_delay == 0) |
| 3229 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3230 | } |
| 3231 | return len; |
| 3232 | } |
| 3233 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3234 | __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] | 3235 | |
| 3236 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3237 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3238 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3239 | struct md_personality *p; |
| 3240 | int ret; |
| 3241 | spin_lock(&mddev->lock); |
| 3242 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3243 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3244 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3245 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3246 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3247 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3248 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3249 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3250 | ret = 0; |
| 3251 | spin_unlock(&mddev->lock); |
| 3252 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3253 | } |
| 3254 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3255 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3256 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3257 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3258 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3259 | ssize_t rv; |
| 3260 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3261 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3262 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3263 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3264 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3265 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3266 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3267 | return -EINVAL; |
| 3268 | |
| 3269 | rv = mddev_lock(mddev); |
| 3270 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3271 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3272 | |
| 3273 | if (mddev->pers == NULL) { |
| 3274 | strncpy(mddev->clevel, buf, slen); |
| 3275 | if (mddev->clevel[slen-1] == '\n') |
| 3276 | slen--; |
| 3277 | mddev->clevel[slen] = 0; |
| 3278 | mddev->level = LEVEL_NONE; |
| 3279 | rv = len; |
| 3280 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3281 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3282 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3283 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3284 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3285 | |
| 3286 | /* request to change the personality. Need to ensure: |
| 3287 | * - array is not engaged in resync/recovery/reshape |
| 3288 | * - old personality can be suspended |
| 3289 | * - new personality will access other array. |
| 3290 | */ |
| 3291 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3292 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3293 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3294 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3295 | mddev->reshape_position != MaxSector || |
| 3296 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3297 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3298 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3299 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3300 | if (!mddev->pers->quiesce) { |
| 3301 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3302 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3303 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3307 | strncpy(clevel, buf, slen); |
| 3308 | if (clevel[slen-1] == '\n') |
| 3309 | slen--; |
| 3310 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3311 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3312 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3313 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3314 | if (request_module("md-%s", clevel) != 0) |
| 3315 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3316 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3317 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3318 | if (!pers || !try_module_get(pers->owner)) { |
| 3319 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3320 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3321 | rv = -EINVAL; |
| 3322 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3323 | } |
| 3324 | spin_unlock(&pers_lock); |
| 3325 | |
| 3326 | if (pers == mddev->pers) { |
| 3327 | /* Nothing to do! */ |
| 3328 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3329 | rv = len; |
| 3330 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3331 | } |
| 3332 | if (!pers->takeover) { |
| 3333 | module_put(pers->owner); |
| 3334 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3335 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3336 | rv = -EINVAL; |
| 3337 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3338 | } |
| 3339 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3340 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3341 | rdev->new_raid_disk = rdev->raid_disk; |
| 3342 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3343 | /* ->takeover must set new_* and/or delta_disks |
| 3344 | * if it succeeds, and may set them when it fails. |
| 3345 | */ |
| 3346 | priv = pers->takeover(mddev); |
| 3347 | if (IS_ERR(priv)) { |
| 3348 | mddev->new_level = mddev->level; |
| 3349 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3350 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3351 | mddev->raid_disks -= mddev->delta_disks; |
| 3352 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3353 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3354 | module_put(pers->owner); |
| 3355 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3356 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3357 | rv = PTR_ERR(priv); |
| 3358 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | /* Looks like we have a winner */ |
| 3362 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 3363 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3364 | |
| 3365 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3366 | oldpers = mddev->pers; |
| 3367 | oldpriv = mddev->private; |
| 3368 | mddev->pers = pers; |
| 3369 | mddev->private = priv; |
| 3370 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3371 | mddev->level = mddev->new_level; |
| 3372 | mddev->layout = mddev->new_layout; |
| 3373 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 3374 | mddev->delta_disks = 0; |
| 3375 | mddev->reshape_backwards = 0; |
| 3376 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3377 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3378 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3379 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3380 | mddev->external) { |
| 3381 | /* We are converting from a no-redundancy array |
| 3382 | * to a redundancy array and metadata is managed |
| 3383 | * externally so we need to be sure that writes |
| 3384 | * won't block due to a need to transition |
| 3385 | * clean->dirty |
| 3386 | * until external management is started. |
| 3387 | */ |
| 3388 | mddev->in_sync = 0; |
| 3389 | mddev->safemode_delay = 0; |
| 3390 | mddev->safemode = 0; |
| 3391 | } |
| 3392 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3393 | oldpers->free(mddev, oldpriv); |
| 3394 | |
| 3395 | if (oldpers->sync_request == NULL && |
| 3396 | pers->sync_request != NULL) { |
| 3397 | /* need to add the md_redundancy_group */ |
| 3398 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3399 | printk(KERN_WARNING |
| 3400 | "md: cannot register extra attributes for %s\n", |
| 3401 | mdname(mddev)); |
| 3402 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3403 | } |
| 3404 | if (oldpers->sync_request != NULL && |
| 3405 | pers->sync_request == NULL) { |
| 3406 | /* need to remove the md_redundancy_group */ |
| 3407 | if (mddev->to_remove == NULL) |
| 3408 | mddev->to_remove = &md_redundancy_group; |
| 3409 | } |
| 3410 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3411 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3412 | if (rdev->raid_disk < 0) |
| 3413 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3414 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3415 | rdev->new_raid_disk = -1; |
| 3416 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3417 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3418 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3419 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3420 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3421 | if (rdev->raid_disk < 0) |
| 3422 | continue; |
| 3423 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3424 | continue; |
| 3425 | rdev->raid_disk = rdev->new_raid_disk; |
| 3426 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3427 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3428 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3429 | if (sysfs_link_rdev(mddev, rdev)) |
| 3430 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3431 | " for %s after level change\n", |
| 3432 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3433 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3434 | } |
| 3435 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3436 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3437 | /* this is now an array without redundancy, so |
| 3438 | * it must always be in_sync |
| 3439 | */ |
| 3440 | mddev->in_sync = 1; |
| 3441 | del_timer_sync(&mddev->safemode_timer); |
| 3442 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 3443 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3444 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3445 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3446 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 3447 | if (!mddev->thread) |
| 3448 | md_update_sb(mddev, 1); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3449 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3450 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3451 | rv = len; |
| 3452 | out_unlock: |
| 3453 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3454 | return rv; |
| 3455 | } |
| 3456 | |
| 3457 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3458 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3459 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3460 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3461 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3462 | { |
| 3463 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3464 | if (mddev->reshape_position != MaxSector && |
| 3465 | mddev->layout != mddev->new_layout) |
| 3466 | return sprintf(page, "%d (%d)\n", |
| 3467 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3468 | return sprintf(page, "%d\n", mddev->layout); |
| 3469 | } |
| 3470 | |
| 3471 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3472 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3473 | { |
| 3474 | char *e; |
| 3475 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3476 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3477 | |
| 3478 | if (!*buf || (*e && *e != '\n')) |
| 3479 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3480 | err = mddev_lock(mddev); |
| 3481 | if (err) |
| 3482 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3483 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3484 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3485 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3486 | err = -EBUSY; |
| 3487 | else if (mddev->ro) |
| 3488 | err = -EROFS; |
| 3489 | else { |
| 3490 | mddev->new_layout = n; |
| 3491 | err = mddev->pers->check_reshape(mddev); |
| 3492 | if (err) |
| 3493 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3494 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3495 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3496 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3497 | if (mddev->reshape_position == MaxSector) |
| 3498 | mddev->layout = n; |
| 3499 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3500 | mddev_unlock(mddev); |
| 3501 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3502 | } |
| 3503 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3504 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3505 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3506 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3507 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3508 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3509 | if (mddev->raid_disks == 0) |
| 3510 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3511 | if (mddev->reshape_position != MaxSector && |
| 3512 | mddev->delta_disks != 0) |
| 3513 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3514 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3515 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3516 | } |
| 3517 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3518 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3519 | |
| 3520 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3521 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3522 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3523 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3524 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3525 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3526 | |
| 3527 | if (!*buf || (*e && *e != '\n')) |
| 3528 | return -EINVAL; |
| 3529 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3530 | err = mddev_lock(mddev); |
| 3531 | if (err) |
| 3532 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3533 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3534 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3535 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3536 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3537 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3538 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3539 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3540 | rdev_for_each(rdev, mddev) { |
| 3541 | if (olddisks < n && |
| 3542 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3543 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3544 | if (olddisks > n && |
| 3545 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3546 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3547 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3548 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3549 | mddev->delta_disks = n - olddisks; |
| 3550 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3551 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3552 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3553 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3554 | out_unlock: |
| 3555 | mddev_unlock(mddev); |
| 3556 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3557 | } |
| 3558 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3559 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3560 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3561 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3562 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3563 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3564 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3565 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3566 | return sprintf(page, "%d (%d)\n", |
| 3567 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3568 | mddev->chunk_sectors << 9); |
| 3569 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3570 | } |
| 3571 | |
| 3572 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3573 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3574 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3575 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3576 | char *e; |
| 3577 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3578 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3579 | if (!*buf || (*e && *e != '\n')) |
| 3580 | return -EINVAL; |
| 3581 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3582 | err = mddev_lock(mddev); |
| 3583 | if (err) |
| 3584 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3585 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3586 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3587 | err = -EBUSY; |
| 3588 | else if (mddev->ro) |
| 3589 | err = -EROFS; |
| 3590 | else { |
| 3591 | mddev->new_chunk_sectors = n >> 9; |
| 3592 | err = mddev->pers->check_reshape(mddev); |
| 3593 | if (err) |
| 3594 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3595 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3596 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3597 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3598 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3599 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3600 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3601 | mddev_unlock(mddev); |
| 3602 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3603 | } |
| 3604 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3605 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3606 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3607 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3608 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3609 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3610 | if (mddev->recovery_cp == MaxSector) |
| 3611 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3612 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3613 | } |
| 3614 | |
| 3615 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3616 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3617 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3618 | int err; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3619 | char *e; |
| 3620 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3621 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3622 | err = mddev_lock(mddev); |
| 3623 | if (err) |
| 3624 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3625 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3626 | err = -EBUSY; |
| 3627 | else if (cmd_match(buf, "none")) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3628 | n = MaxSector; |
| 3629 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3630 | err = -EINVAL; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3631 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3632 | if (!err) { |
| 3633 | mddev->recovery_cp = n; |
| 3634 | if (mddev->pers) |
| 3635 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3636 | } |
| 3637 | mddev_unlock(mddev); |
| 3638 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3639 | } |
| 3640 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3641 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3642 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3643 | /* |
| 3644 | * The array state can be: |
| 3645 | * |
| 3646 | * clear |
| 3647 | * No devices, no size, no level |
| 3648 | * Equivalent to STOP_ARRAY ioctl |
| 3649 | * inactive |
| 3650 | * May have some settings, but array is not active |
| 3651 | * all IO results in error |
| 3652 | * When written, doesn't tear down array, but just stops it |
| 3653 | * suspended (not supported yet) |
| 3654 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3655 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3656 | * readonly |
| 3657 | * no resync can happen. no superblocks get written. |
| 3658 | * write requests fail |
| 3659 | * read-auto |
| 3660 | * like readonly, but behaves like 'clean' on a write request. |
| 3661 | * |
| 3662 | * clean - no pending writes, but otherwise active. |
| 3663 | * When written to inactive array, starts without resync |
| 3664 | * If a write request arrives then |
| 3665 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3666 | * if not known, block and switch to write-pending |
| 3667 | * If written to an active array that has pending writes, then fails. |
| 3668 | * active |
| 3669 | * fully active: IO and resync can be happening. |
| 3670 | * When written to inactive array, starts with resync |
| 3671 | * |
| 3672 | * write-pending |
| 3673 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3674 | * |
| 3675 | * active-idle |
| 3676 | * like active, but no writes have been seen for a while (100msec). |
| 3677 | * |
| 3678 | */ |
| 3679 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3680 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3681 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3682 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3683 | "write-pending", "active-idle", NULL }; |
| 3684 | |
| 3685 | static int match_word(const char *word, char **list) |
| 3686 | { |
| 3687 | int n; |
| 3688 | for (n=0; list[n]; n++) |
| 3689 | if (cmd_match(word, list[n])) |
| 3690 | break; |
| 3691 | return n; |
| 3692 | } |
| 3693 | |
| 3694 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3695 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3696 | { |
| 3697 | enum array_state st = inactive; |
| 3698 | |
| 3699 | if (mddev->pers) |
| 3700 | switch(mddev->ro) { |
| 3701 | case 1: |
| 3702 | st = readonly; |
| 3703 | break; |
| 3704 | case 2: |
| 3705 | st = read_auto; |
| 3706 | break; |
| 3707 | case 0: |
| 3708 | if (mddev->in_sync) |
| 3709 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3710 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3711 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3712 | else if (mddev->safemode) |
| 3713 | st = active_idle; |
| 3714 | else |
| 3715 | st = active; |
| 3716 | } |
| 3717 | else { |
| 3718 | if (list_empty(&mddev->disks) && |
| 3719 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3720 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3721 | st = clear; |
| 3722 | else |
| 3723 | st = inactive; |
| 3724 | } |
| 3725 | return sprintf(page, "%s\n", array_states[st]); |
| 3726 | } |
| 3727 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3728 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 3729 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 3730 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3731 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3732 | |
| 3733 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3734 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3735 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3736 | int err; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3737 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3738 | |
| 3739 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 3740 | /* don't take reconfig_mutex when toggling between |
| 3741 | * clean and active |
| 3742 | */ |
| 3743 | spin_lock(&mddev->lock); |
| 3744 | if (st == active) { |
| 3745 | restart_array(mddev); |
| 3746 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 3747 | wake_up(&mddev->sb_wait); |
| 3748 | err = 0; |
| 3749 | } else /* st == clean */ { |
| 3750 | restart_array(mddev); |
| 3751 | if (atomic_read(&mddev->writes_pending) == 0) { |
| 3752 | if (mddev->in_sync == 0) { |
| 3753 | mddev->in_sync = 1; |
| 3754 | if (mddev->safemode == 1) |
| 3755 | mddev->safemode = 0; |
| 3756 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3757 | } |
| 3758 | err = 0; |
| 3759 | } else |
| 3760 | err = -EBUSY; |
| 3761 | } |
| 3762 | spin_unlock(&mddev->lock); |
| 3763 | return err; |
| 3764 | } |
| 3765 | err = mddev_lock(mddev); |
| 3766 | if (err) |
| 3767 | return err; |
| 3768 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3769 | switch(st) { |
| 3770 | case bad_word: |
| 3771 | break; |
| 3772 | case clear: |
| 3773 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3774 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3775 | break; |
| 3776 | case inactive: |
| 3777 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3778 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3779 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3780 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3781 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3782 | break; |
| 3783 | case suspended: |
| 3784 | break; /* not supported yet */ |
| 3785 | case readonly: |
| 3786 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3787 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3788 | else { |
| 3789 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3790 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3791 | err = do_md_run(mddev); |
| 3792 | } |
| 3793 | break; |
| 3794 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3795 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3796 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3797 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3798 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3799 | err = restart_array(mddev); |
| 3800 | if (err == 0) { |
| 3801 | mddev->ro = 2; |
| 3802 | set_disk_ro(mddev->gendisk, 0); |
| 3803 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3804 | } else { |
| 3805 | mddev->ro = 2; |
| 3806 | err = do_md_run(mddev); |
| 3807 | } |
| 3808 | break; |
| 3809 | case clean: |
| 3810 | if (mddev->pers) { |
| 3811 | restart_array(mddev); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3812 | spin_lock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3813 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3814 | if (mddev->in_sync == 0) { |
| 3815 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3816 | if (mddev->safemode == 1) |
| 3817 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3818 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3819 | } |
| 3820 | err = 0; |
| 3821 | } else |
| 3822 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3823 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3824 | } else |
| 3825 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3826 | break; |
| 3827 | case active: |
| 3828 | if (mddev->pers) { |
| 3829 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3830 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3831 | wake_up(&mddev->sb_wait); |
| 3832 | err = 0; |
| 3833 | } else { |
| 3834 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3835 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3836 | err = do_md_run(mddev); |
| 3837 | } |
| 3838 | break; |
| 3839 | case write_pending: |
| 3840 | case active_idle: |
| 3841 | /* these cannot be set */ |
| 3842 | break; |
| 3843 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3844 | |
| 3845 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 3846 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3847 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3848 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3849 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3850 | mddev_unlock(mddev); |
| 3851 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3852 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3853 | static struct md_sysfs_entry md_array_state = |
| 3854 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3855 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3856 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3857 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3858 | return sprintf(page, "%d\n", |
| 3859 | atomic_read(&mddev->max_corr_read_errors)); |
| 3860 | } |
| 3861 | |
| 3862 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3863 | 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] | 3864 | { |
| 3865 | char *e; |
| 3866 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3867 | |
| 3868 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3869 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3870 | return len; |
| 3871 | } |
| 3872 | return -EINVAL; |
| 3873 | } |
| 3874 | |
| 3875 | static struct md_sysfs_entry max_corr_read_errors = |
| 3876 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3877 | max_corrected_read_errors_store); |
| 3878 | |
| 3879 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3880 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3881 | { |
| 3882 | return -EINVAL; |
| 3883 | } |
| 3884 | |
| 3885 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3886 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3887 | { |
| 3888 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3889 | /* The new device is added to the array. |
| 3890 | * If the array has a persistent superblock, we read the |
| 3891 | * superblock to initialise info and check validity. |
| 3892 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3893 | * which mainly checks size. |
| 3894 | */ |
| 3895 | char *e; |
| 3896 | int major = simple_strtoul(buf, &e, 10); |
| 3897 | int minor; |
| 3898 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3899 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3900 | int err; |
| 3901 | |
| 3902 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3903 | return -EINVAL; |
| 3904 | minor = simple_strtoul(e+1, &e, 10); |
| 3905 | if (*e && *e != '\n') |
| 3906 | return -EINVAL; |
| 3907 | dev = MKDEV(major, minor); |
| 3908 | if (major != MAJOR(dev) || |
| 3909 | minor != MINOR(dev)) |
| 3910 | return -EOVERFLOW; |
| 3911 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3912 | flush_workqueue(md_misc_wq); |
| 3913 | |
| 3914 | err = mddev_lock(mddev); |
| 3915 | if (err) |
| 3916 | return err; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3917 | if (mddev->persistent) { |
| 3918 | rdev = md_import_device(dev, mddev->major_version, |
| 3919 | mddev->minor_version); |
| 3920 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3921 | struct md_rdev *rdev0 |
| 3922 | = list_entry(mddev->disks.next, |
| 3923 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3924 | err = super_types[mddev->major_version] |
| 3925 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3926 | if (err < 0) |
| 3927 | goto out; |
| 3928 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3929 | } else if (mddev->external) |
| 3930 | rdev = md_import_device(dev, -2, -1); |
| 3931 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3932 | rdev = md_import_device(dev, -1, -1); |
| 3933 | |
| 3934 | if (IS_ERR(rdev)) |
| 3935 | return PTR_ERR(rdev); |
| 3936 | err = bind_rdev_to_array(rdev, mddev); |
| 3937 | out: |
| 3938 | if (err) |
| 3939 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3940 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3941 | return err ? err : len; |
| 3942 | } |
| 3943 | |
| 3944 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3945 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3946 | |
| 3947 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3948 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3949 | { |
| 3950 | char *end; |
| 3951 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3952 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3953 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3954 | err = mddev_lock(mddev); |
| 3955 | if (err) |
| 3956 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3957 | if (!mddev->bitmap) |
| 3958 | goto out; |
| 3959 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3960 | while (*buf) { |
| 3961 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3962 | if (buf == end) break; |
| 3963 | if (*end == '-') { /* range */ |
| 3964 | buf = end + 1; |
| 3965 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3966 | if (buf == end) break; |
| 3967 | } |
| 3968 | if (*end && !isspace(*end)) break; |
| 3969 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3970 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3971 | } |
| 3972 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3973 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 3974 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3975 | return len; |
| 3976 | } |
| 3977 | |
| 3978 | static struct md_sysfs_entry md_bitmap = |
| 3979 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3980 | |
| 3981 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3982 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3983 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3984 | return sprintf(page, "%llu\n", |
| 3985 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3986 | } |
| 3987 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3988 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3989 | |
| 3990 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3991 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3992 | { |
| 3993 | /* If array is inactive, we can reduce the component size, but |
| 3994 | * not increase it (except from 0). |
| 3995 | * If array is active, we can try an on-line resize |
| 3996 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3997 | sector_t sectors; |
| 3998 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3999 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4000 | if (err < 0) |
| 4001 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4002 | err = mddev_lock(mddev); |
| 4003 | if (err) |
| 4004 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4005 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4006 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4007 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4008 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4009 | if (mddev->dev_sectors == 0 || |
| 4010 | mddev->dev_sectors > sectors) |
| 4011 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4012 | else |
| 4013 | err = -ENOSPC; |
| 4014 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4015 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4016 | return err ? err : len; |
| 4017 | } |
| 4018 | |
| 4019 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4020 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4021 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4022 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4023 | * This is one of |
| 4024 | * 'none' for arrays with no metadata (good luck...) |
| 4025 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4026 | * or N.M for internally known formats |
| 4027 | */ |
| 4028 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4029 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4030 | { |
| 4031 | if (mddev->persistent) |
| 4032 | return sprintf(page, "%d.%d\n", |
| 4033 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4034 | else if (mddev->external) |
| 4035 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4036 | else |
| 4037 | return sprintf(page, "none\n"); |
| 4038 | } |
| 4039 | |
| 4040 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4041 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4042 | { |
| 4043 | int major, minor; |
| 4044 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4045 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4046 | /* Changing the details of 'external' metadata is |
| 4047 | * always permitted. Otherwise there must be |
| 4048 | * no devices attached to the array. |
| 4049 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4050 | |
| 4051 | err = mddev_lock(mddev); |
| 4052 | if (err) |
| 4053 | return err; |
| 4054 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4055 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4056 | ; |
| 4057 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4058 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4059 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4060 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4061 | if (cmd_match(buf, "none")) { |
| 4062 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4063 | mddev->external = 0; |
| 4064 | mddev->major_version = 0; |
| 4065 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4066 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4067 | } |
| 4068 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4069 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4070 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4071 | namelen = sizeof(mddev->metadata_type)-1; |
| 4072 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4073 | mddev->metadata_type[namelen] = 0; |
| 4074 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4075 | mddev->metadata_type[--namelen] = 0; |
| 4076 | mddev->persistent = 0; |
| 4077 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4078 | mddev->major_version = 0; |
| 4079 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4080 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4081 | } |
| 4082 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4083 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4084 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4085 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4086 | buf = e+1; |
| 4087 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4088 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4089 | goto out_unlock; |
| 4090 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4091 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4092 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4093 | mddev->major_version = major; |
| 4094 | mddev->minor_version = minor; |
| 4095 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4096 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4097 | err = 0; |
| 4098 | out_unlock: |
| 4099 | mddev_unlock(mddev); |
| 4100 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4104 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4105 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4106 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4107 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4108 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4109 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4110 | unsigned long recovery = mddev->recovery; |
| 4111 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4112 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4113 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4114 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4115 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4116 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4117 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4118 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4119 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4120 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4121 | type = "check"; |
| 4122 | else |
| 4123 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4124 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4125 | type = "recover"; |
| 4126 | } |
| 4127 | return sprintf(page, "%s\n", type); |
| 4128 | } |
| 4129 | |
| 4130 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4131 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4132 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4133 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4134 | return -EINVAL; |
| 4135 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4136 | if (cmd_match(page, "frozen")) |
| 4137 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4138 | else |
| 4139 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4140 | |
| 4141 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 4142 | flush_workqueue(md_misc_wq); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4143 | if (mddev->sync_thread) { |
| 4144 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4145 | if (mddev_lock(mddev) == 0) { |
| 4146 | md_reap_sync_thread(mddev); |
| 4147 | mddev_unlock(mddev); |
| 4148 | } |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4149 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4150 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4151 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4152 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4153 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4154 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4155 | else if (cmd_match(page, "recover")) { |
| 4156 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4157 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4158 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4159 | int err; |
| 4160 | if (mddev->pers->start_reshape == NULL) |
| 4161 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4162 | err = mddev_lock(mddev); |
| 4163 | if (!err) { |
| 4164 | err = mddev->pers->start_reshape(mddev); |
| 4165 | mddev_unlock(mddev); |
| 4166 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4167 | if (err) |
| 4168 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4169 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4170 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4171 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4172 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4173 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4174 | return -EINVAL; |
| 4175 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4176 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4177 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4178 | if (mddev->ro == 2) { |
| 4179 | /* A write to sync_action is enough to justify |
| 4180 | * canceling read-auto mode |
| 4181 | */ |
| 4182 | mddev->ro = 0; |
| 4183 | md_wakeup_thread(mddev->sync_thread); |
| 4184 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4185 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4186 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4187 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4188 | return len; |
| 4189 | } |
| 4190 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4191 | static struct md_sysfs_entry md_scan_mode = |
| 4192 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
| 4193 | |
| 4194 | static ssize_t |
| 4195 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4196 | { |
| 4197 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4198 | } |
| 4199 | |
| 4200 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4201 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4202 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4203 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4204 | { |
| 4205 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4206 | (unsigned long long) |
| 4207 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4208 | } |
| 4209 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4210 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4211 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4212 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4213 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4214 | { |
| 4215 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4216 | mddev->sync_speed_min ? "local": "system"); |
| 4217 | } |
| 4218 | |
| 4219 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4220 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4221 | { |
| 4222 | int min; |
| 4223 | char *e; |
| 4224 | if (strncmp(buf, "system", 6)==0) { |
| 4225 | mddev->sync_speed_min = 0; |
| 4226 | return len; |
| 4227 | } |
| 4228 | min = simple_strtoul(buf, &e, 10); |
| 4229 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4230 | return -EINVAL; |
| 4231 | mddev->sync_speed_min = min; |
| 4232 | return len; |
| 4233 | } |
| 4234 | |
| 4235 | static struct md_sysfs_entry md_sync_min = |
| 4236 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4237 | |
| 4238 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4239 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4240 | { |
| 4241 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4242 | mddev->sync_speed_max ? "local": "system"); |
| 4243 | } |
| 4244 | |
| 4245 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4246 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4247 | { |
| 4248 | int max; |
| 4249 | char *e; |
| 4250 | if (strncmp(buf, "system", 6)==0) { |
| 4251 | mddev->sync_speed_max = 0; |
| 4252 | return len; |
| 4253 | } |
| 4254 | max = simple_strtoul(buf, &e, 10); |
| 4255 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4256 | return -EINVAL; |
| 4257 | mddev->sync_speed_max = max; |
| 4258 | return len; |
| 4259 | } |
| 4260 | |
| 4261 | static struct md_sysfs_entry md_sync_max = |
| 4262 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4263 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4264 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4265 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4266 | { |
| 4267 | return sprintf(page, "%d\n", mddev->degraded); |
| 4268 | } |
| 4269 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4270 | |
| 4271 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4272 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4273 | { |
| 4274 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4275 | } |
| 4276 | |
| 4277 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4278 | 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] | 4279 | { |
| 4280 | long n; |
| 4281 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4282 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4283 | return -EINVAL; |
| 4284 | |
| 4285 | if (n != 0 && n != 1) |
| 4286 | return -EINVAL; |
| 4287 | |
| 4288 | mddev->parallel_resync = n; |
| 4289 | |
| 4290 | if (mddev->sync_thread) |
| 4291 | wake_up(&resync_wait); |
| 4292 | |
| 4293 | return len; |
| 4294 | } |
| 4295 | |
| 4296 | /* force parallel resync, even with shared block devices */ |
| 4297 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4298 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4299 | sync_force_parallel_show, sync_force_parallel_store); |
| 4300 | |
| 4301 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4302 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4303 | { |
| 4304 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4305 | if (mddev->curr_resync == 0) |
| 4306 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4307 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4308 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4309 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4310 | db = resync - mddev->resync_mark_cnt; |
| 4311 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4312 | } |
| 4313 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4314 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4315 | |
| 4316 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4317 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4318 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4319 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4320 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4321 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4322 | return sprintf(page, "none\n"); |
| 4323 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4324 | if (mddev->curr_resync == 1 || |
| 4325 | mddev->curr_resync == 2) |
| 4326 | return sprintf(page, "delayed\n"); |
| 4327 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4328 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4329 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4330 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4331 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4332 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4333 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4334 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4335 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4336 | } |
| 4337 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4338 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4339 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4340 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4341 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4342 | { |
| 4343 | return sprintf(page, "%llu\n", |
| 4344 | (unsigned long long)mddev->resync_min); |
| 4345 | } |
| 4346 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4347 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4348 | { |
| 4349 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4350 | int err; |
| 4351 | int chunk; |
| 4352 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4353 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4354 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4355 | |
| 4356 | spin_lock(&mddev->lock); |
| 4357 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4358 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4359 | goto out_unlock; |
| 4360 | |
| 4361 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4362 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4363 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4364 | |
| 4365 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4366 | chunk = mddev->chunk_sectors; |
| 4367 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4368 | sector_t temp = min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4369 | |
| 4370 | err = -EINVAL; |
| 4371 | if (sector_div(temp, chunk)) |
| 4372 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4373 | } |
| 4374 | mddev->resync_min = min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4375 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4376 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4377 | out_unlock: |
| 4378 | spin_unlock(&mddev->lock); |
| 4379 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | static struct md_sysfs_entry md_min_sync = |
| 4383 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4384 | |
| 4385 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4386 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4387 | { |
| 4388 | if (mddev->resync_max == MaxSector) |
| 4389 | return sprintf(page, "max\n"); |
| 4390 | else |
| 4391 | return sprintf(page, "%llu\n", |
| 4392 | (unsigned long long)mddev->resync_max); |
| 4393 | } |
| 4394 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4395 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4396 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4397 | int err; |
| 4398 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4399 | if (strncmp(buf, "max", 3) == 0) |
| 4400 | mddev->resync_max = MaxSector; |
| 4401 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4402 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4403 | int chunk; |
| 4404 | |
| 4405 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4406 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4407 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4408 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4409 | goto out_unlock; |
| 4410 | |
| 4411 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4412 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4413 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4414 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4415 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4416 | |
| 4417 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4418 | chunk = mddev->chunk_sectors; |
| 4419 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4420 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4421 | |
| 4422 | err = -EINVAL; |
| 4423 | if (sector_div(temp, chunk)) |
| 4424 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4425 | } |
| 4426 | mddev->resync_max = max; |
| 4427 | } |
| 4428 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4429 | err = 0; |
| 4430 | out_unlock: |
| 4431 | spin_unlock(&mddev->lock); |
| 4432 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4433 | } |
| 4434 | |
| 4435 | static struct md_sysfs_entry md_max_sync = |
| 4436 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4437 | |
| 4438 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4439 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4440 | { |
| 4441 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4442 | } |
| 4443 | |
| 4444 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4445 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4446 | { |
| 4447 | char *e; |
| 4448 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4449 | unsigned long long old; |
| 4450 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4451 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4452 | if (buf == e || (*e && *e != '\n')) |
| 4453 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4454 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4455 | err = mddev_lock(mddev); |
| 4456 | if (err) |
| 4457 | return err; |
| 4458 | err = -EINVAL; |
| 4459 | if (mddev->pers == NULL || |
| 4460 | mddev->pers->quiesce == NULL) |
| 4461 | goto unlock; |
| 4462 | old = mddev->suspend_lo; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4463 | mddev->suspend_lo = new; |
| 4464 | if (new >= old) |
| 4465 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4466 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4467 | else { |
| 4468 | /* Expanding suspended region - need to wait */ |
| 4469 | mddev->pers->quiesce(mddev, 1); |
| 4470 | mddev->pers->quiesce(mddev, 0); |
| 4471 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4472 | err = 0; |
| 4473 | unlock: |
| 4474 | mddev_unlock(mddev); |
| 4475 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4476 | } |
| 4477 | static struct md_sysfs_entry md_suspend_lo = |
| 4478 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4479 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4480 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4481 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4482 | { |
| 4483 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4484 | } |
| 4485 | |
| 4486 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4487 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4488 | { |
| 4489 | char *e; |
| 4490 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4491 | unsigned long long old; |
| 4492 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4493 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4494 | if (buf == e || (*e && *e != '\n')) |
| 4495 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4496 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4497 | err = mddev_lock(mddev); |
| 4498 | if (err) |
| 4499 | return err; |
| 4500 | err = -EINVAL; |
| 4501 | if (mddev->pers == NULL || |
| 4502 | mddev->pers->quiesce == NULL) |
| 4503 | goto unlock; |
| 4504 | old = mddev->suspend_hi; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4505 | mddev->suspend_hi = new; |
| 4506 | if (new <= old) |
| 4507 | /* Shrinking suspended region */ |
| 4508 | mddev->pers->quiesce(mddev, 2); |
| 4509 | else { |
| 4510 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4511 | mddev->pers->quiesce(mddev, 1); |
| 4512 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4513 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4514 | err = 0; |
| 4515 | unlock: |
| 4516 | mddev_unlock(mddev); |
| 4517 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4518 | } |
| 4519 | static struct md_sysfs_entry md_suspend_hi = |
| 4520 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4521 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4522 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4523 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4524 | { |
| 4525 | if (mddev->reshape_position != MaxSector) |
| 4526 | return sprintf(page, "%llu\n", |
| 4527 | (unsigned long long)mddev->reshape_position); |
| 4528 | strcpy(page, "none\n"); |
| 4529 | return 5; |
| 4530 | } |
| 4531 | |
| 4532 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4533 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4534 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4535 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4536 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4537 | int err; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4538 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4539 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4540 | if (buf == e || (*e && *e != '\n')) |
| 4541 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4542 | err = mddev_lock(mddev); |
| 4543 | if (err) |
| 4544 | return err; |
| 4545 | err = -EBUSY; |
| 4546 | if (mddev->pers) |
| 4547 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4548 | mddev->reshape_position = new; |
| 4549 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4550 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4551 | mddev->new_level = mddev->level; |
| 4552 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4553 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4554 | rdev_for_each(rdev, mddev) |
| 4555 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4556 | err = 0; |
| 4557 | unlock: |
| 4558 | mddev_unlock(mddev); |
| 4559 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4560 | } |
| 4561 | |
| 4562 | static struct md_sysfs_entry md_reshape_position = |
| 4563 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4564 | reshape_position_store); |
| 4565 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4566 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4567 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4568 | { |
| 4569 | return sprintf(page, "%s\n", |
| 4570 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4571 | } |
| 4572 | |
| 4573 | static ssize_t |
| 4574 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4575 | { |
| 4576 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4577 | int err; |
| 4578 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4579 | if (cmd_match(buf, "forwards")) |
| 4580 | backwards = 0; |
| 4581 | else if (cmd_match(buf, "backwards")) |
| 4582 | backwards = 1; |
| 4583 | else |
| 4584 | return -EINVAL; |
| 4585 | if (mddev->reshape_backwards == backwards) |
| 4586 | return len; |
| 4587 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4588 | err = mddev_lock(mddev); |
| 4589 | if (err) |
| 4590 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4591 | /* check if we are allowed to change */ |
| 4592 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4593 | err = -EBUSY; |
| 4594 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4595 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4596 | err = -EINVAL; |
| 4597 | else |
| 4598 | mddev->reshape_backwards = backwards; |
| 4599 | mddev_unlock(mddev); |
| 4600 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4601 | } |
| 4602 | |
| 4603 | static struct md_sysfs_entry md_reshape_direction = |
| 4604 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4605 | reshape_direction_store); |
| 4606 | |
| 4607 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4608 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4609 | { |
| 4610 | if (mddev->external_size) |
| 4611 | return sprintf(page, "%llu\n", |
| 4612 | (unsigned long long)mddev->array_sectors/2); |
| 4613 | else |
| 4614 | return sprintf(page, "default\n"); |
| 4615 | } |
| 4616 | |
| 4617 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4618 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4619 | { |
| 4620 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4621 | int err; |
| 4622 | |
| 4623 | err = mddev_lock(mddev); |
| 4624 | if (err) |
| 4625 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4626 | |
| 4627 | if (strncmp(buf, "default", 7) == 0) { |
| 4628 | if (mddev->pers) |
| 4629 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4630 | else |
| 4631 | sectors = mddev->array_sectors; |
| 4632 | |
| 4633 | mddev->external_size = 0; |
| 4634 | } else { |
| 4635 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4636 | err = -EINVAL; |
| 4637 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 4638 | err = -E2BIG; |
| 4639 | else |
| 4640 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4641 | } |
| 4642 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4643 | if (!err) { |
| 4644 | mddev->array_sectors = sectors; |
| 4645 | if (mddev->pers) { |
| 4646 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4647 | revalidate_disk(mddev->gendisk); |
| 4648 | } |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4649 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4650 | mddev_unlock(mddev); |
| 4651 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | static struct md_sysfs_entry md_array_size = |
| 4655 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4656 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4657 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4658 | static struct attribute *md_default_attrs[] = { |
| 4659 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4660 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4661 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4662 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4663 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4664 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4665 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4666 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4667 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4668 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4669 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4670 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4671 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4672 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4673 | NULL, |
| 4674 | }; |
| 4675 | |
| 4676 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4677 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4678 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4679 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4680 | &md_sync_min.attr, |
| 4681 | &md_sync_max.attr, |
| 4682 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4683 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4684 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4685 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4686 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4687 | &md_suspend_lo.attr, |
| 4688 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4689 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4690 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4691 | NULL, |
| 4692 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4693 | static struct attribute_group md_redundancy_group = { |
| 4694 | .name = NULL, |
| 4695 | .attrs = md_redundancy_attrs, |
| 4696 | }; |
| 4697 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4698 | static ssize_t |
| 4699 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4700 | { |
| 4701 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4702 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4703 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4704 | |
| 4705 | if (!entry->show) |
| 4706 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4707 | spin_lock(&all_mddevs_lock); |
| 4708 | if (list_empty(&mddev->all_mddevs)) { |
| 4709 | spin_unlock(&all_mddevs_lock); |
| 4710 | return -EBUSY; |
| 4711 | } |
| 4712 | mddev_get(mddev); |
| 4713 | spin_unlock(&all_mddevs_lock); |
| 4714 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4715 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4716 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4717 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4718 | } |
| 4719 | |
| 4720 | static ssize_t |
| 4721 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4722 | const char *page, size_t length) |
| 4723 | { |
| 4724 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4725 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4726 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4727 | |
| 4728 | if (!entry->store) |
| 4729 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4730 | if (!capable(CAP_SYS_ADMIN)) |
| 4731 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4732 | spin_lock(&all_mddevs_lock); |
| 4733 | if (list_empty(&mddev->all_mddevs)) { |
| 4734 | spin_unlock(&all_mddevs_lock); |
| 4735 | return -EBUSY; |
| 4736 | } |
| 4737 | mddev_get(mddev); |
| 4738 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame^] | 4739 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4740 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4741 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4742 | } |
| 4743 | |
| 4744 | static void md_free(struct kobject *ko) |
| 4745 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4746 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4747 | |
| 4748 | if (mddev->sysfs_state) |
| 4749 | sysfs_put(mddev->sysfs_state); |
| 4750 | |
| 4751 | if (mddev->gendisk) { |
| 4752 | del_gendisk(mddev->gendisk); |
| 4753 | put_disk(mddev->gendisk); |
| 4754 | } |
| 4755 | if (mddev->queue) |
| 4756 | blk_cleanup_queue(mddev->queue); |
| 4757 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4758 | kfree(mddev); |
| 4759 | } |
| 4760 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4761 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4762 | .show = md_attr_show, |
| 4763 | .store = md_attr_store, |
| 4764 | }; |
| 4765 | static struct kobj_type md_ktype = { |
| 4766 | .release = md_free, |
| 4767 | .sysfs_ops = &md_sysfs_ops, |
| 4768 | .default_attrs = md_default_attrs, |
| 4769 | }; |
| 4770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | int mdp_major = 0; |
| 4772 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4773 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4774 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4775 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4776 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4777 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4778 | kobject_del(&mddev->kobj); |
| 4779 | kobject_put(&mddev->kobj); |
| 4780 | } |
| 4781 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4782 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4784 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4785 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4786 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4787 | int partitioned; |
| 4788 | int shift; |
| 4789 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4790 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | |
| 4792 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4793 | return -ENODEV; |
| 4794 | |
| 4795 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4796 | shift = partitioned ? MdpMinorShift : 0; |
| 4797 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4799 | /* wait for any previous instance of this device to be |
| 4800 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4801 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4802 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4803 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4804 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4805 | error = -EEXIST; |
| 4806 | if (mddev->gendisk) |
| 4807 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4808 | |
| 4809 | if (name) { |
| 4810 | /* Need to ensure that 'name' is not a duplicate. |
| 4811 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4812 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4813 | spin_lock(&all_mddevs_lock); |
| 4814 | |
| 4815 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4816 | if (mddev2->gendisk && |
| 4817 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4818 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4819 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4820 | } |
| 4821 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4823 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4824 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4825 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4826 | if (!mddev->queue) |
| 4827 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4828 | mddev->queue->queuedata = mddev; |
| 4829 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4830 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 4831 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | disk = alloc_disk(1 << shift); |
| 4834 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4835 | blk_cleanup_queue(mddev->queue); |
| 4836 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4837 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4838 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4839 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4841 | if (name) |
| 4842 | strcpy(disk->disk_name, name); |
| 4843 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4844 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4845 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | disk->fops = &md_fops; |
| 4848 | disk->private_data = mddev; |
| 4849 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4850 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4851 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4852 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4853 | * remove it now. |
| 4854 | */ |
| 4855 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4857 | /* As soon as we call add_disk(), another thread could get |
| 4858 | * through to md_open, so make sure it doesn't get too far |
| 4859 | */ |
| 4860 | mutex_lock(&mddev->open_mutex); |
| 4861 | add_disk(disk); |
| 4862 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4863 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4864 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4865 | if (error) { |
| 4866 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4867 | * __must_check, we must do something with the result |
| 4868 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4869 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4870 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4871 | error = 0; |
| 4872 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4873 | if (mddev->kobj.sd && |
| 4874 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4875 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4876 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4877 | abort: |
| 4878 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4879 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4880 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4881 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4882 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4883 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4884 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4885 | } |
| 4886 | |
| 4887 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4888 | { |
| 4889 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | return NULL; |
| 4891 | } |
| 4892 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4893 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4894 | { |
| 4895 | /* val must be "md_*" where * is not all digits. |
| 4896 | * We allocate an array with a large free minor number, and |
| 4897 | * set the name to val. val must not already be an active name. |
| 4898 | */ |
| 4899 | int len = strlen(val); |
| 4900 | char buf[DISK_NAME_LEN]; |
| 4901 | |
| 4902 | while (len && val[len-1] == '\n') |
| 4903 | len--; |
| 4904 | if (len >= DISK_NAME_LEN) |
| 4905 | return -E2BIG; |
| 4906 | strlcpy(buf, val, len+1); |
| 4907 | if (strncmp(buf, "md_", 3) != 0) |
| 4908 | return -EINVAL; |
| 4909 | return md_alloc(0, buf); |
| 4910 | } |
| 4911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | static void md_safemode_timeout(unsigned long data) |
| 4913 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4914 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4916 | if (!atomic_read(&mddev->writes_pending)) { |
| 4917 | mddev->safemode = 1; |
| 4918 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4919 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4920 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | md_wakeup_thread(mddev->thread); |
| 4922 | } |
| 4923 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4924 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4926 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4928 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4929 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4930 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4931 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4932 | if (list_empty(&mddev->disks)) |
| 4933 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4934 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | |
| 4936 | if (mddev->pers) |
| 4937 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4938 | /* Cannot run until previous stop completes properly */ |
| 4939 | if (mddev->sysfs_active) |
| 4940 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | /* |
| 4943 | * Analyze all RAID superblock(s) |
| 4944 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4945 | if (!mddev->raid_disks) { |
| 4946 | if (!mddev->persistent) |
| 4947 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4948 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4949 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4950 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4951 | if (mddev->level != LEVEL_NONE) |
| 4952 | request_module("md-level-%d", mddev->level); |
| 4953 | else if (mddev->clevel[0]) |
| 4954 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | |
| 4956 | /* |
| 4957 | * Drop all container device buffers, from now on |
| 4958 | * the only valid external interface is through the md |
| 4959 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4960 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4961 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4962 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4963 | continue; |
| 4964 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4965 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4966 | |
| 4967 | /* perform some consistency tests on the device. |
| 4968 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4969 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4970 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4971 | if (rdev->meta_bdev) { |
| 4972 | /* Nothing to check */; |
| 4973 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4974 | if (mddev->dev_sectors && |
| 4975 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4976 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4977 | printk("md: %s: data overlaps metadata\n", |
| 4978 | mdname(mddev)); |
| 4979 | return -EINVAL; |
| 4980 | } |
| 4981 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4982 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4983 | > rdev->data_offset) { |
| 4984 | printk("md: %s: metadata overlaps data\n", |
| 4985 | mdname(mddev)); |
| 4986 | return -EINVAL; |
| 4987 | } |
| 4988 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4989 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4990 | } |
| 4991 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4992 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 4993 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4996 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4997 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4998 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4999 | if (mddev->level != LEVEL_NONE) |
| 5000 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5001 | mddev->level); |
| 5002 | else |
| 5003 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5004 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5005 | return -EINVAL; |
| 5006 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5007 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5008 | if (mddev->level != pers->level) { |
| 5009 | mddev->level = pers->level; |
| 5010 | mddev->new_level = pers->level; |
| 5011 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5012 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5014 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5015 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5016 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5017 | module_put(pers->owner); |
| 5018 | return -EINVAL; |
| 5019 | } |
| 5020 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5021 | if (pers->sync_request) { |
| 5022 | /* Warn if this is a potentially silly |
| 5023 | * configuration. |
| 5024 | */ |
| 5025 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5026 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5027 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5028 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5029 | rdev_for_each(rdev, mddev) |
| 5030 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5031 | if (rdev < rdev2 && |
| 5032 | rdev->bdev->bd_contains == |
| 5033 | rdev2->bdev->bd_contains) { |
| 5034 | printk(KERN_WARNING |
| 5035 | "%s: WARNING: %s appears to be" |
| 5036 | " on the same physical disk as" |
| 5037 | " %s.\n", |
| 5038 | mdname(mddev), |
| 5039 | bdevname(rdev->bdev,b), |
| 5040 | bdevname(rdev2->bdev,b2)); |
| 5041 | warned = 1; |
| 5042 | } |
| 5043 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5044 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5045 | if (warned) |
| 5046 | printk(KERN_WARNING |
| 5047 | "True protection against single-disk" |
| 5048 | " failure might be compromised.\n"); |
| 5049 | } |
| 5050 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5051 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5052 | /* may be over-ridden by personality */ |
| 5053 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5054 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5055 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5056 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5057 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5058 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5059 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5060 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5061 | if (err) |
| 5062 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5063 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5064 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5065 | " but 'external_size' not in effect?\n", __func__); |
| 5066 | printk(KERN_ERR |
| 5067 | "md: invalid array_size %llu > default size %llu\n", |
| 5068 | (unsigned long long)mddev->array_sectors / 2, |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5069 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5070 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5071 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5072 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5073 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5074 | err = bitmap_create(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5075 | if (err) |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5076 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5077 | mdname(mddev), err); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5078 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5079 | if (err) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5080 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5081 | pers->free(mddev, mddev->private); |
| 5082 | module_put(pers->owner); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5083 | bitmap_destroy(mddev); |
| 5084 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5086 | if (mddev->queue) { |
| 5087 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5088 | mddev->queue->backing_dev_info.congested_fn = md_congested; |
NeilBrown | 64590f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5089 | blk_queue_merge_bvec(mddev->queue, md_mergeable_bvec); |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5090 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5091 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5092 | if (mddev->kobj.sd && |
| 5093 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5094 | printk(KERN_WARNING |
| 5095 | "md: cannot register extra attributes for %s\n", |
| 5096 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5097 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5098 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5099 | mddev->ro = 0; |
| 5100 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5101 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5102 | atomic_set(&mddev->max_corr_read_errors, |
| 5103 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | mddev->safemode = 0; |
| 5105 | mddev->safemode_timer.function = md_safemode_timeout; |
| 5106 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5107 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5109 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5110 | spin_lock(&mddev->lock); |
| 5111 | mddev->pers = pers; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5112 | mddev->ready = 1; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5113 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5114 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5115 | if (rdev->raid_disk >= 0) |
| 5116 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5117 | /* failure here is OK */; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5119 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5120 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5121 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5122 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5123 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5124 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5125 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5126 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5127 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | return 0; |
| 5129 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5130 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5132 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5133 | { |
| 5134 | int err; |
| 5135 | |
| 5136 | err = md_run(mddev); |
| 5137 | if (err) |
| 5138 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5139 | err = bitmap_load(mddev); |
| 5140 | if (err) { |
| 5141 | bitmap_destroy(mddev); |
| 5142 | goto out; |
| 5143 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5144 | |
| 5145 | md_wakeup_thread(mddev->thread); |
| 5146 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5147 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5148 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5149 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5150 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5151 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5152 | out: |
| 5153 | return err; |
| 5154 | } |
| 5155 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5156 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | { |
| 5158 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5160 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5162 | return -ENXIO; |
| 5163 | if (!mddev->pers) |
| 5164 | return -EINVAL; |
| 5165 | if (!mddev->ro) |
| 5166 | return -EBUSY; |
| 5167 | mddev->safemode = 0; |
| 5168 | mddev->ro = 0; |
| 5169 | set_disk_ro(disk, 0); |
| 5170 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5171 | mdname(mddev)); |
| 5172 | /* Kick recovery or resync if necessary */ |
| 5173 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5174 | md_wakeup_thread(mddev->thread); |
| 5175 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5176 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5177 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 | } |
| 5179 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5180 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5181 | { |
| 5182 | mddev->array_sectors = 0; |
| 5183 | mddev->external_size = 0; |
| 5184 | mddev->dev_sectors = 0; |
| 5185 | mddev->raid_disks = 0; |
| 5186 | mddev->recovery_cp = 0; |
| 5187 | mddev->resync_min = 0; |
| 5188 | mddev->resync_max = MaxSector; |
| 5189 | mddev->reshape_position = MaxSector; |
| 5190 | mddev->external = 0; |
| 5191 | mddev->persistent = 0; |
| 5192 | mddev->level = LEVEL_NONE; |
| 5193 | mddev->clevel[0] = 0; |
| 5194 | mddev->flags = 0; |
| 5195 | mddev->ro = 0; |
| 5196 | mddev->metadata_type[0] = 0; |
| 5197 | mddev->chunk_sectors = 0; |
| 5198 | mddev->ctime = mddev->utime = 0; |
| 5199 | mddev->layout = 0; |
| 5200 | mddev->max_disks = 0; |
| 5201 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5202 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5203 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5204 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5205 | mddev->new_level = LEVEL_NONE; |
| 5206 | mddev->new_layout = 0; |
| 5207 | mddev->new_chunk_sectors = 0; |
| 5208 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5209 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5210 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5211 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5212 | mddev->recovery = 0; |
| 5213 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5214 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5215 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5216 | mddev->safemode = 0; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5217 | mddev->merge_check_needed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5218 | mddev->bitmap_info.offset = 0; |
| 5219 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5220 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5221 | mddev->bitmap_info.chunksize = 0; |
| 5222 | mddev->bitmap_info.daemon_sleep = 0; |
| 5223 | mddev->bitmap_info.max_write_behind = 0; |
| 5224 | } |
| 5225 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5226 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5227 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 5228 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5229 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5230 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5231 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5232 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5233 | } |
| 5234 | |
| 5235 | del_timer_sync(&mddev->safemode_timer); |
| 5236 | |
| 5237 | bitmap_flush(mddev); |
| 5238 | md_super_wait(mddev); |
| 5239 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5240 | if (mddev->ro == 0 && |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5241 | (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5242 | /* mark array as shutdown cleanly */ |
| 5243 | mddev->in_sync = 1; |
| 5244 | md_update_sb(mddev, 1); |
| 5245 | } |
| 5246 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5247 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5248 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5249 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 5250 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5251 | __md_stop_writes(mddev); |
| 5252 | mddev_unlock(mddev); |
| 5253 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5254 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5255 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5256 | static void mddev_detach(struct mddev *mddev) |
| 5257 | { |
| 5258 | struct bitmap *bitmap = mddev->bitmap; |
| 5259 | /* wait for behind writes to complete */ |
| 5260 | if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { |
| 5261 | printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n", |
| 5262 | mdname(mddev)); |
| 5263 | /* need to kick something here to make sure I/O goes? */ |
| 5264 | wait_event(bitmap->behind_wait, |
| 5265 | atomic_read(&bitmap->behind_writes) == 0); |
| 5266 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5267 | if (mddev->pers && mddev->pers->quiesce) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5268 | mddev->pers->quiesce(mddev, 1); |
| 5269 | mddev->pers->quiesce(mddev, 0); |
| 5270 | } |
| 5271 | md_unregister_thread(&mddev->thread); |
| 5272 | if (mddev->queue) |
| 5273 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 5274 | } |
| 5275 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5276 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5277 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5278 | struct md_personality *pers = mddev->pers; |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5279 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5280 | spin_lock(&mddev->lock); |
| 5281 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5282 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5283 | spin_unlock(&mddev->lock); |
| 5284 | pers->free(mddev, mddev->private); |
| 5285 | if (pers->sync_request && mddev->to_remove == NULL) |
| 5286 | mddev->to_remove = &md_redundancy_group; |
| 5287 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5288 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5289 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5290 | |
| 5291 | void md_stop(struct mddev *mddev) |
| 5292 | { |
| 5293 | /* stop the array and free an attached data structures. |
| 5294 | * This is called from dm-raid |
| 5295 | */ |
| 5296 | __md_stop(mddev); |
| 5297 | bitmap_destroy(mddev); |
| 5298 | if (mddev->bio_set) |
| 5299 | bioset_free(mddev->bio_set); |
| 5300 | } |
| 5301 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5302 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5303 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5304 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5305 | { |
| 5306 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5307 | int did_freeze = 0; |
| 5308 | |
| 5309 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5310 | did_freeze = 1; |
| 5311 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5312 | md_wakeup_thread(mddev->thread); |
| 5313 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5314 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5315 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5316 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5317 | /* Thread might be blocked waiting for metadata update |
| 5318 | * which will now never happen */ |
| 5319 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5320 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5321 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5322 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 5323 | &mddev->recovery)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5324 | mddev_lock_nointr(mddev); |
| 5325 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5326 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5327 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5328 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5329 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5330 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5331 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5332 | if (did_freeze) { |
| 5333 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5334 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5335 | md_wakeup_thread(mddev->thread); |
| 5336 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5337 | err = -EBUSY; |
| 5338 | goto out; |
| 5339 | } |
| 5340 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5341 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5342 | |
| 5343 | err = -ENXIO; |
| 5344 | if (mddev->ro==1) |
| 5345 | goto out; |
| 5346 | mddev->ro = 1; |
| 5347 | set_disk_ro(mddev->gendisk, 1); |
| 5348 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5349 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5350 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5351 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5352 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5353 | } |
| 5354 | out: |
| 5355 | mutex_unlock(&mddev->open_mutex); |
| 5356 | return err; |
| 5357 | } |
| 5358 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5359 | /* mode: |
| 5360 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5361 | * 2 - stop but do not disassemble array |
| 5362 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5363 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5364 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5365 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5367 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5368 | int did_freeze = 0; |
| 5369 | |
| 5370 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5371 | did_freeze = 1; |
| 5372 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5373 | md_wakeup_thread(mddev->thread); |
| 5374 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5375 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5376 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5377 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5378 | /* Thread might be blocked waiting for metadata update |
| 5379 | * which will now never happen */ |
| 5380 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5381 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5382 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5383 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 5384 | !test_bit(MD_RECOVERY_RUNNING, |
| 5385 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5386 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5388 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5389 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5390 | mddev->sysfs_active || |
| 5391 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5392 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5393 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5394 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5395 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5396 | if (did_freeze) { |
| 5397 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5398 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5399 | md_wakeup_thread(mddev->thread); |
| 5400 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 5401 | return -EBUSY; |
| 5402 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5403 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5404 | if (mddev->ro) |
| 5405 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5406 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5407 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5408 | __md_stop(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5409 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5410 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5411 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5412 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5413 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5414 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5415 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5416 | if (rdev->raid_disk >= 0) |
| 5417 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5418 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5419 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5420 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5421 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5422 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5423 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5424 | if (mddev->ro) |
| 5425 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5426 | } else |
| 5427 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5428 | /* |
| 5429 | * Free resources if final stop |
| 5430 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5431 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5433 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5434 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5435 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5436 | struct file *f = mddev->bitmap_info.file; |
| 5437 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5438 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5439 | spin_unlock(&mddev->lock); |
| 5440 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5441 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5442 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | export_array(mddev); |
| 5445 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5446 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5447 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5448 | if (mddev->hold_active == UNTIL_STOP) |
| 5449 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5450 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5451 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5452 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5453 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5454 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | } |
| 5456 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5457 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5458 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5460 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5461 | int err; |
| 5462 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5463 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | |
| 5466 | printk(KERN_INFO "md: running: "); |
| 5467 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5468 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | char b[BDEVNAME_SIZE]; |
| 5470 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5471 | } |
| 5472 | printk("\n"); |
| 5473 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5474 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | if (err) { |
| 5476 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5477 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | } |
| 5479 | } |
| 5480 | |
| 5481 | /* |
| 5482 | * lets try to run arrays based on all disks that have arrived |
| 5483 | * until now. (those are in pending_raid_disks) |
| 5484 | * |
| 5485 | * the method: pick the first pending disk, collect all disks with |
| 5486 | * the same UUID, remove all from the pending list and put them into |
| 5487 | * the 'same_array' list. Then order this list based on superblock |
| 5488 | * update time (freshest comes first), kick out 'old' disks and |
| 5489 | * compare superblocks. If everything's fine then run it. |
| 5490 | * |
| 5491 | * If "unit" is allocated, then bump its reference count |
| 5492 | */ |
| 5493 | static void autorun_devices(int part) |
| 5494 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5495 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5496 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | char b[BDEVNAME_SIZE]; |
| 5498 | |
| 5499 | printk(KERN_INFO "md: autorun ...\n"); |
| 5500 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5501 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5503 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5504 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5505 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5506 | |
| 5507 | printk(KERN_INFO "md: considering %s ...\n", |
| 5508 | bdevname(rdev0->bdev,b)); |
| 5509 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5510 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5511 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5512 | printk(KERN_INFO "md: adding %s ...\n", |
| 5513 | bdevname(rdev->bdev,b)); |
| 5514 | list_move(&rdev->same_set, &candidates); |
| 5515 | } |
| 5516 | /* |
| 5517 | * now we have a set of devices, with all of them having |
| 5518 | * mostly sane superblocks. It's time to allocate the |
| 5519 | * mddev. |
| 5520 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5521 | if (part) { |
| 5522 | dev = MKDEV(mdp_major, |
| 5523 | rdev0->preferred_minor << MdpMinorShift); |
| 5524 | unit = MINOR(dev) >> MdpMinorShift; |
| 5525 | } else { |
| 5526 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5527 | unit = MINOR(dev); |
| 5528 | } |
| 5529 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5530 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5531 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5532 | break; |
| 5533 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | |
| 5535 | md_probe(dev, NULL, NULL); |
| 5536 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5537 | if (!mddev || !mddev->gendisk) { |
| 5538 | if (mddev) |
| 5539 | mddev_put(mddev); |
| 5540 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5541 | "md: cannot allocate memory for md drive.\n"); |
| 5542 | break; |
| 5543 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5544 | if (mddev_lock(mddev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5545 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5546 | mdname(mddev)); |
| 5547 | else if (mddev->raid_disks || mddev->major_version |
| 5548 | || !list_empty(&mddev->disks)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5549 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5550 | "md: %s already running, cannot run %s\n", |
| 5551 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5552 | mddev_unlock(mddev); |
| 5553 | } else { |
| 5554 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5555 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5556 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5557 | list_del_init(&rdev->same_set); |
| 5558 | if (bind_rdev_to_array(rdev, mddev)) |
| 5559 | export_rdev(rdev); |
| 5560 | } |
| 5561 | autorun_array(mddev); |
| 5562 | mddev_unlock(mddev); |
| 5563 | } |
| 5564 | /* on success, candidates will be empty, on error |
| 5565 | * it won't... |
| 5566 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5567 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5568 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5570 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5571 | mddev_put(mddev); |
| 5572 | } |
| 5573 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5574 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5575 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5577 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5578 | { |
| 5579 | mdu_version_t ver; |
| 5580 | |
| 5581 | ver.major = MD_MAJOR_VERSION; |
| 5582 | ver.minor = MD_MINOR_VERSION; |
| 5583 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5584 | |
| 5585 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5586 | return -EFAULT; |
| 5587 | |
| 5588 | return 0; |
| 5589 | } |
| 5590 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5591 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5592 | { |
| 5593 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5594 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5595 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5596 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5597 | nr = working = insync = failed = spare = 0; |
| 5598 | rcu_read_lock(); |
| 5599 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5601 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5602 | failed++; |
| 5603 | else { |
| 5604 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5605 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5606 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5607 | else |
| 5608 | spare++; |
| 5609 | } |
| 5610 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5611 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5612 | |
| 5613 | info.major_version = mddev->major_version; |
| 5614 | info.minor_version = mddev->minor_version; |
| 5615 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5616 | info.ctime = mddev->ctime; |
| 5617 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5618 | info.size = mddev->dev_sectors / 2; |
| 5619 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5620 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5621 | info.nr_disks = nr; |
| 5622 | info.raid_disks = mddev->raid_disks; |
| 5623 | info.md_minor = mddev->md_minor; |
| 5624 | info.not_persistent= !mddev->persistent; |
| 5625 | |
| 5626 | info.utime = mddev->utime; |
| 5627 | info.state = 0; |
| 5628 | if (mddev->in_sync) |
| 5629 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5630 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 5631 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5632 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5633 | info.working_disks = working; |
| 5634 | info.failed_disks = failed; |
| 5635 | info.spare_disks = spare; |
| 5636 | |
| 5637 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5638 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5639 | |
| 5640 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5641 | return -EFAULT; |
| 5642 | |
| 5643 | return 0; |
| 5644 | } |
| 5645 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5646 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5647 | { |
| 5648 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5649 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5650 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5651 | |
NeilBrown | 60559da | 2013-07-16 16:44:29 +1000 | [diff] [blame] | 5652 | file = kmalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5653 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5654 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5655 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5656 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5657 | spin_lock(&mddev->lock); |
| 5658 | /* bitmap disabled, zero the first byte and copy out */ |
| 5659 | if (!mddev->bitmap_info.file) |
| 5660 | file->pathname[0] = '\0'; |
| 5661 | else if ((ptr = d_path(&mddev->bitmap_info.file->f_path, |
| 5662 | file->pathname, sizeof(file->pathname))), |
| 5663 | IS_ERR(ptr)) |
| 5664 | err = PTR_ERR(ptr); |
| 5665 | else |
| 5666 | memmove(file->pathname, ptr, |
| 5667 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 5668 | spin_unlock(&mddev->lock); |
| 5669 | |
| 5670 | if (err == 0 && |
| 5671 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5672 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5673 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5674 | kfree(file); |
| 5675 | return err; |
| 5676 | } |
| 5677 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5678 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | { |
| 5680 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5681 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | |
| 5683 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5684 | return -EFAULT; |
| 5685 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5686 | rcu_read_lock(); |
| 5687 | rdev = find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | if (rdev) { |
| 5689 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5690 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5691 | info.raid_disk = rdev->raid_disk; |
| 5692 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5693 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5695 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5697 | info.state |= (1<<MD_DISK_SYNC); |
| 5698 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5699 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5700 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | } else { |
| 5702 | info.major = info.minor = 0; |
| 5703 | info.raid_disk = -1; |
| 5704 | info.state = (1<<MD_DISK_REMOVED); |
| 5705 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5706 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5707 | |
| 5708 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5709 | return -EFAULT; |
| 5710 | |
| 5711 | return 0; |
| 5712 | } |
| 5713 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5714 | 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] | 5715 | { |
| 5716 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5717 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5718 | dev_t dev = MKDEV(info->major,info->minor); |
| 5719 | |
| 5720 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5721 | return -EOVERFLOW; |
| 5722 | |
| 5723 | if (!mddev->raid_disks) { |
| 5724 | int err; |
| 5725 | /* expecting a device which has a superblock */ |
| 5726 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5727 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5728 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | "md: md_import_device returned %ld\n", |
| 5730 | PTR_ERR(rdev)); |
| 5731 | return PTR_ERR(rdev); |
| 5732 | } |
| 5733 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5734 | struct md_rdev *rdev0 |
| 5735 | = list_entry(mddev->disks.next, |
| 5736 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5737 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5739 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5740 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5741 | "md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5742 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | bdevname(rdev0->bdev,b2)); |
| 5744 | export_rdev(rdev); |
| 5745 | return -EINVAL; |
| 5746 | } |
| 5747 | } |
| 5748 | err = bind_rdev_to_array(rdev, mddev); |
| 5749 | if (err) |
| 5750 | export_rdev(rdev); |
| 5751 | return err; |
| 5752 | } |
| 5753 | |
| 5754 | /* |
| 5755 | * add_new_disk can be used once the array is assembled |
| 5756 | * to add "hot spares". They must already have a superblock |
| 5757 | * written |
| 5758 | */ |
| 5759 | if (mddev->pers) { |
| 5760 | int err; |
| 5761 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5762 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5763 | "%s: personality does not support diskops!\n", |
| 5764 | mdname(mddev)); |
| 5765 | return -EINVAL; |
| 5766 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5767 | if (mddev->persistent) |
| 5768 | rdev = md_import_device(dev, mddev->major_version, |
| 5769 | mddev->minor_version); |
| 5770 | else |
| 5771 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5772 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5773 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | "md: md_import_device returned %ld\n", |
| 5775 | PTR_ERR(rdev)); |
| 5776 | return PTR_ERR(rdev); |
| 5777 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5778 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5779 | if (!mddev->persistent) { |
| 5780 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5781 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5782 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5783 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 5784 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5785 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5786 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 5787 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5788 | } else |
| 5789 | super_types[mddev->major_version]. |
| 5790 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5791 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 5792 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5793 | /* This was a hot-add request, but events doesn't |
| 5794 | * match, so reject it. |
| 5795 | */ |
| 5796 | export_rdev(rdev); |
| 5797 | return -EINVAL; |
| 5798 | } |
| 5799 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5800 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5801 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5802 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5803 | else |
| 5804 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5806 | rdev->raid_disk = -1; |
| 5807 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5808 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5809 | /* If there is hot_add_disk but no hot_remove_disk |
| 5810 | * then added disks for geometry changes, |
| 5811 | * and should be added immediately. |
| 5812 | */ |
| 5813 | super_types[mddev->major_version]. |
| 5814 | validate_super(mddev, rdev); |
| 5815 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5816 | if (err) |
| 5817 | unbind_rdev_from_array(rdev); |
| 5818 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5819 | if (err) |
| 5820 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5821 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5822 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5823 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5824 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5825 | if (mddev->degraded) |
| 5826 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5827 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5828 | if (!err) |
| 5829 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5830 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | return err; |
| 5832 | } |
| 5833 | |
| 5834 | /* otherwise, add_new_disk is only allowed |
| 5835 | * for major_version==0 superblocks |
| 5836 | */ |
| 5837 | if (mddev->major_version != 0) { |
| 5838 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5839 | mdname(mddev)); |
| 5840 | return -EINVAL; |
| 5841 | } |
| 5842 | |
| 5843 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5844 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5845 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5846 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5847 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5848 | "md: error, md_import_device() returned %ld\n", |
| 5849 | PTR_ERR(rdev)); |
| 5850 | return PTR_ERR(rdev); |
| 5851 | } |
| 5852 | rdev->desc_nr = info->number; |
| 5853 | if (info->raid_disk < mddev->raid_disks) |
| 5854 | rdev->raid_disk = info->raid_disk; |
| 5855 | else |
| 5856 | rdev->raid_disk = -1; |
| 5857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5859 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5860 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5861 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5862 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5863 | set_bit(WriteMostly, &rdev->flags); |
| 5864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5865 | if (!mddev->persistent) { |
| 5866 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5867 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5868 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5869 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5870 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5871 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5872 | err = bind_rdev_to_array(rdev, mddev); |
| 5873 | if (err) { |
| 5874 | export_rdev(rdev); |
| 5875 | return err; |
| 5876 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | } |
| 5878 | |
| 5879 | return 0; |
| 5880 | } |
| 5881 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5882 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5883 | { |
| 5884 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5885 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5886 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5887 | rdev = find_rdev(mddev, dev); |
| 5888 | if (!rdev) |
| 5889 | return -ENXIO; |
| 5890 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 5891 | clear_bit(Blocked, &rdev->flags); |
| 5892 | remove_and_add_spares(mddev, rdev); |
| 5893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5894 | if (rdev->raid_disk >= 0) |
| 5895 | goto busy; |
| 5896 | |
| 5897 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5898 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5899 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | |
| 5901 | return 0; |
| 5902 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5903 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5905 | return -EBUSY; |
| 5906 | } |
| 5907 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5908 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | { |
| 5910 | char b[BDEVNAME_SIZE]; |
| 5911 | int err; |
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 (!mddev->pers) |
| 5915 | return -ENODEV; |
| 5916 | |
| 5917 | if (mddev->major_version != 0) { |
| 5918 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5919 | " version-0 superblocks.\n", |
| 5920 | mdname(mddev)); |
| 5921 | return -EINVAL; |
| 5922 | } |
| 5923 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5924 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5925 | "%s: personality does not support diskops!\n", |
| 5926 | mdname(mddev)); |
| 5927 | return -EINVAL; |
| 5928 | } |
| 5929 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5930 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5932 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | "md: error, md_import_device() returned %ld\n", |
| 5934 | PTR_ERR(rdev)); |
| 5935 | return -EINVAL; |
| 5936 | } |
| 5937 | |
| 5938 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5939 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5940 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5941 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5942 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5943 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5944 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5945 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5946 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5947 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5948 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5949 | err = -EINVAL; |
| 5950 | goto abort_export; |
| 5951 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5952 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5954 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5955 | err = bind_rdev_to_array(rdev, mddev); |
| 5956 | if (err) |
| 5957 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5958 | |
| 5959 | /* |
| 5960 | * The rest should better be atomic, we can have disk failures |
| 5961 | * noticed in interrupt contexts ... |
| 5962 | */ |
| 5963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5964 | rdev->raid_disk = -1; |
| 5965 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5966 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | |
| 5968 | /* |
| 5969 | * Kick recovery, maybe this spare has to be added to the |
| 5970 | * array immediately. |
| 5971 | */ |
| 5972 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5973 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5974 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5975 | return 0; |
| 5976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5977 | abort_export: |
| 5978 | export_rdev(rdev); |
| 5979 | return err; |
| 5980 | } |
| 5981 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5982 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5983 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 5984 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5985 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5986 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 5987 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5988 | return -EBUSY; |
| 5989 | if (mddev->recovery || mddev->sync_thread) |
| 5990 | return -EBUSY; |
| 5991 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5992 | } |
| 5993 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5994 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 5995 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5996 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5997 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5998 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 5999 | return -EEXIST; /* cannot add when bitmap is present */ |
| 6000 | f = fget(fd); |
| 6001 | |
| 6002 | if (f == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6003 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 6004 | mdname(mddev)); |
| 6005 | return -EBADF; |
| 6006 | } |
| 6007 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6008 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6009 | if (!S_ISREG(inode->i_mode)) { |
| 6010 | printk(KERN_ERR "%s: error: bitmap file must be a regular file\n", |
| 6011 | mdname(mddev)); |
| 6012 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6013 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6014 | printk(KERN_ERR "%s: error: bitmap file must open for write\n", |
| 6015 | mdname(mddev)); |
| 6016 | err = -EBADF; |
| 6017 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6018 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 6019 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6020 | err = -EBUSY; |
| 6021 | } |
| 6022 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6023 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6024 | return err; |
| 6025 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6026 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6027 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6028 | } else if (mddev->bitmap == NULL) |
| 6029 | return -ENOENT; /* cannot remove what isn't there */ |
| 6030 | err = 0; |
| 6031 | if (mddev->pers) { |
| 6032 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6033 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6034 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6035 | if (!err) |
| 6036 | err = bitmap_load(mddev); |
| 6037 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6038 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6039 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6040 | fd = -1; /* make sure to put the file */ |
| 6041 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6042 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6043 | } |
| 6044 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6045 | struct file *f = mddev->bitmap_info.file; |
| 6046 | if (f) { |
| 6047 | spin_lock(&mddev->lock); |
| 6048 | mddev->bitmap_info.file = NULL; |
| 6049 | spin_unlock(&mddev->lock); |
| 6050 | fput(f); |
| 6051 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6052 | } |
| 6053 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6054 | return err; |
| 6055 | } |
| 6056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | /* |
| 6058 | * set_array_info is used two different ways |
| 6059 | * The original usage is when creating a new array. |
| 6060 | * In this usage, raid_disks is > 0 and it together with |
| 6061 | * level, size, not_persistent,layout,chunksize determine the |
| 6062 | * shape of the array. |
| 6063 | * This will always create an array with a type-0.90.0 superblock. |
| 6064 | * The newer usage is when assembling an array. |
| 6065 | * In this case raid_disks will be 0, and the major_version field is |
| 6066 | * use to determine which style super-blocks are to be found on the devices. |
| 6067 | * The minor and patch _version numbers are also kept incase the |
| 6068 | * super_block handler wishes to interpret them. |
| 6069 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6070 | 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] | 6071 | { |
| 6072 | |
| 6073 | if (info->raid_disks == 0) { |
| 6074 | /* just setting version number for superblock loading */ |
| 6075 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6076 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6077 | super_types[info->major_version].name == NULL) { |
| 6078 | /* maybe try to auto-load a module? */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6079 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | "md: superblock version %d not known\n", |
| 6081 | info->major_version); |
| 6082 | return -EINVAL; |
| 6083 | } |
| 6084 | mddev->major_version = info->major_version; |
| 6085 | mddev->minor_version = info->minor_version; |
| 6086 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6087 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6088 | /* ensure mddev_put doesn't delete this now that there |
| 6089 | * is some minimal configuration. |
| 6090 | */ |
| 6091 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6092 | return 0; |
| 6093 | } |
| 6094 | mddev->major_version = MD_MAJOR_VERSION; |
| 6095 | mddev->minor_version = MD_MINOR_VERSION; |
| 6096 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 6097 | mddev->ctime = get_seconds(); |
| 6098 | |
| 6099 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6100 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6101 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6102 | mddev->raid_disks = info->raid_disks; |
| 6103 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6104 | * openned |
| 6105 | */ |
| 6106 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6107 | mddev->recovery_cp = MaxSector; |
| 6108 | else |
| 6109 | mddev->recovery_cp = 0; |
| 6110 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6111 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6112 | |
| 6113 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6114 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | |
| 6116 | mddev->max_disks = MD_SB_DISKS; |
| 6117 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6118 | if (mddev->persistent) |
| 6119 | mddev->flags = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6120 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6122 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6123 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6124 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6125 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6126 | mddev->reshape_position = MaxSector; |
| 6127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | /* |
| 6129 | * Generate a 128 bit UUID |
| 6130 | */ |
| 6131 | get_random_bytes(mddev->uuid, 16); |
| 6132 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6133 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6134 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6135 | mddev->new_layout = mddev->layout; |
| 6136 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6137 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | return 0; |
| 6140 | } |
| 6141 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6142 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6143 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6144 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6145 | |
| 6146 | if (mddev->external_size) |
| 6147 | return; |
| 6148 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6149 | mddev->array_sectors = array_sectors; |
| 6150 | } |
| 6151 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6152 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6153 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6154 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6155 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6156 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6157 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6158 | |
| 6159 | if (mddev->pers->resize == NULL) |
| 6160 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6161 | /* The "num_sectors" is the number of sectors of each device that |
| 6162 | * is used. This can only make sense for arrays with redundancy. |
| 6163 | * linear and raid0 always use whatever space is available. We can only |
| 6164 | * consider changing this number if no resync or reconstruction is |
| 6165 | * 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] | 6166 | * 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] | 6167 | * of each device. If num_sectors is zero, we find the largest size |
| 6168 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6169 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6170 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6171 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6172 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6173 | if (mddev->ro) |
| 6174 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6175 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6176 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6177 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6178 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6179 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6180 | num_sectors = avail; |
| 6181 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6182 | return -ENOSPC; |
| 6183 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6184 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6185 | if (!rv) |
| 6186 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6187 | return rv; |
| 6188 | } |
| 6189 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6190 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6191 | { |
| 6192 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6193 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6194 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6195 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6196 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6197 | if (mddev->ro) |
| 6198 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6199 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6200 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6201 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6202 | if (mddev->sync_thread || |
| 6203 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6204 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6205 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6206 | |
| 6207 | rdev_for_each(rdev, mddev) { |
| 6208 | if (mddev->raid_disks < raid_disks && |
| 6209 | rdev->data_offset < rdev->new_data_offset) |
| 6210 | return -EINVAL; |
| 6211 | if (mddev->raid_disks > raid_disks && |
| 6212 | rdev->data_offset > rdev->new_data_offset) |
| 6213 | return -EINVAL; |
| 6214 | } |
| 6215 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6216 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6217 | if (mddev->delta_disks < 0) |
| 6218 | mddev->reshape_backwards = 1; |
| 6219 | else if (mddev->delta_disks > 0) |
| 6220 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6221 | |
| 6222 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6223 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6224 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6225 | mddev->reshape_backwards = 0; |
| 6226 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6227 | return rv; |
| 6228 | } |
| 6229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6230 | /* |
| 6231 | * update_array_info is used to change the configuration of an |
| 6232 | * on-line array. |
| 6233 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6234 | * fields in the info are checked against the array. |
| 6235 | * Any differences that cannot be handled will cause an error. |
| 6236 | * Normally, only one change can be managed at a time. |
| 6237 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6238 | 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] | 6239 | { |
| 6240 | int rv = 0; |
| 6241 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6242 | int state = 0; |
| 6243 | |
| 6244 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6245 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6246 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6247 | |
| 6248 | if (mddev->major_version != info->major_version || |
| 6249 | mddev->minor_version != info->minor_version || |
| 6250 | /* mddev->patch_version != info->patch_version || */ |
| 6251 | mddev->ctime != info->ctime || |
| 6252 | mddev->level != info->level || |
| 6253 | /* mddev->layout != info->layout || */ |
| 6254 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6255 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6256 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6257 | ((state^info->state) & 0xfffffe00) |
| 6258 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6259 | return -EINVAL; |
| 6260 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6261 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6262 | cnt++; |
| 6263 | if (mddev->raid_disks != info->raid_disks) |
| 6264 | cnt++; |
| 6265 | if (mddev->layout != info->layout) |
| 6266 | cnt++; |
| 6267 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6268 | cnt++; |
| 6269 | if (cnt == 0) |
| 6270 | return 0; |
| 6271 | if (cnt > 1) |
| 6272 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6273 | |
| 6274 | if (mddev->layout != info->layout) { |
| 6275 | /* Change layout |
| 6276 | * we don't need to do anything at the md level, the |
| 6277 | * personality will take care of it all. |
| 6278 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6279 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6280 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6281 | else { |
| 6282 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6283 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6284 | if (rv) |
| 6285 | mddev->new_layout = mddev->layout; |
| 6286 | return rv; |
| 6287 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6288 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6289 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6290 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6291 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6292 | if (mddev->raid_disks != info->raid_disks) |
| 6293 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6294 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6295 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 6296 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6297 | return -EINVAL; |
| 6298 | if (mddev->recovery || mddev->sync_thread) |
| 6299 | return -EBUSY; |
| 6300 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6301 | /* add the bitmap */ |
| 6302 | if (mddev->bitmap) |
| 6303 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6304 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6305 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6306 | mddev->bitmap_info.offset = |
| 6307 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6308 | mddev->bitmap_info.space = |
| 6309 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6310 | mddev->pers->quiesce(mddev, 1); |
| 6311 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6312 | if (!rv) |
| 6313 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6314 | if (rv) |
| 6315 | bitmap_destroy(mddev); |
| 6316 | mddev->pers->quiesce(mddev, 0); |
| 6317 | } else { |
| 6318 | /* remove the bitmap */ |
| 6319 | if (!mddev->bitmap) |
| 6320 | return -ENOENT; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 6321 | if (mddev->bitmap->storage.file) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6322 | return -EINVAL; |
| 6323 | mddev->pers->quiesce(mddev, 1); |
| 6324 | bitmap_destroy(mddev); |
| 6325 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6326 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6327 | } |
| 6328 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6329 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6330 | return rv; |
| 6331 | } |
| 6332 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6333 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6334 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6335 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6336 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6337 | |
| 6338 | if (mddev->pers == NULL) |
| 6339 | return -ENODEV; |
| 6340 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6341 | rcu_read_lock(); |
| 6342 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6343 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6344 | err = -ENODEV; |
| 6345 | else { |
| 6346 | md_error(mddev, rdev); |
| 6347 | if (!test_bit(Faulty, &rdev->flags)) |
| 6348 | err = -EBUSY; |
| 6349 | } |
| 6350 | rcu_read_unlock(); |
| 6351 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6352 | } |
| 6353 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6354 | /* |
| 6355 | * We have a problem here : there is no easy way to give a CHS |
| 6356 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6357 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6358 | * dosfs just mad... ;-) |
| 6359 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6360 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6361 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6362 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6363 | |
| 6364 | geo->heads = 2; |
| 6365 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6366 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6367 | return 0; |
| 6368 | } |
| 6369 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6370 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 6371 | { |
| 6372 | switch (cmd) { |
| 6373 | case ADD_NEW_DISK: |
| 6374 | case BLKROSET: |
| 6375 | case GET_ARRAY_INFO: |
| 6376 | case GET_BITMAP_FILE: |
| 6377 | case GET_DISK_INFO: |
| 6378 | case HOT_ADD_DISK: |
| 6379 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6380 | case RAID_AUTORUN: |
| 6381 | case RAID_VERSION: |
| 6382 | case RESTART_ARRAY_RW: |
| 6383 | case RUN_ARRAY: |
| 6384 | case SET_ARRAY_INFO: |
| 6385 | case SET_BITMAP_FILE: |
| 6386 | case SET_DISK_FAULTY: |
| 6387 | case STOP_ARRAY: |
| 6388 | case STOP_ARRAY_RO: |
| 6389 | return true; |
| 6390 | default: |
| 6391 | return false; |
| 6392 | } |
| 6393 | } |
| 6394 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6395 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6396 | unsigned int cmd, unsigned long arg) |
| 6397 | { |
| 6398 | int err = 0; |
| 6399 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6400 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6401 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6402 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6403 | if (!md_ioctl_valid(cmd)) |
| 6404 | return -ENOTTY; |
| 6405 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6406 | switch (cmd) { |
| 6407 | case RAID_VERSION: |
| 6408 | case GET_ARRAY_INFO: |
| 6409 | case GET_DISK_INFO: |
| 6410 | break; |
| 6411 | default: |
| 6412 | if (!capable(CAP_SYS_ADMIN)) |
| 6413 | return -EACCES; |
| 6414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6415 | |
| 6416 | /* |
| 6417 | * Commands dealing with the RAID driver but not any |
| 6418 | * particular array: |
| 6419 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6420 | switch (cmd) { |
| 6421 | case RAID_VERSION: |
| 6422 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6423 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6425 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6426 | case RAID_AUTORUN: |
| 6427 | err = 0; |
| 6428 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6429 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6430 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6431 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | } |
| 6433 | |
| 6434 | /* |
| 6435 | * Commands creating/starting a new array: |
| 6436 | */ |
| 6437 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6438 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6439 | |
| 6440 | if (!mddev) { |
| 6441 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6442 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6443 | } |
| 6444 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6445 | /* Some actions do not requires the mutex */ |
| 6446 | switch (cmd) { |
| 6447 | case GET_ARRAY_INFO: |
| 6448 | if (!mddev->raid_disks && !mddev->external) |
| 6449 | err = -ENODEV; |
| 6450 | else |
| 6451 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6452 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6453 | |
| 6454 | case GET_DISK_INFO: |
| 6455 | if (!mddev->raid_disks && !mddev->external) |
| 6456 | err = -ENODEV; |
| 6457 | else |
| 6458 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6459 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6460 | |
| 6461 | case SET_DISK_FAULTY: |
| 6462 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6463 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6464 | |
| 6465 | case GET_BITMAP_FILE: |
| 6466 | err = get_bitmap_file(mddev, argp); |
| 6467 | goto out; |
| 6468 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6469 | } |
| 6470 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 6471 | if (cmd == ADD_NEW_DISK) |
| 6472 | /* need to ensure md_delayed_delete() has completed */ |
| 6473 | flush_workqueue(md_misc_wq); |
| 6474 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 6475 | if (cmd == HOT_REMOVE_DISK) |
| 6476 | /* need to ensure recovery thread has run */ |
| 6477 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 6478 | !test_bit(MD_RECOVERY_NEEDED, |
| 6479 | &mddev->flags), |
| 6480 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6481 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 6482 | /* Need to flush page cache, and ensure no-one else opens |
| 6483 | * and writes |
| 6484 | */ |
| 6485 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6486 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6487 | mutex_unlock(&mddev->open_mutex); |
| 6488 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6489 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6490 | } |
| 6491 | set_bit(MD_STILL_CLOSED, &mddev->flags); |
| 6492 | mutex_unlock(&mddev->open_mutex); |
| 6493 | sync_blockdev(bdev); |
| 6494 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | err = mddev_lock(mddev); |
| 6496 | if (err) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6497 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6498 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6499 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6500 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6501 | } |
| 6502 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6503 | if (cmd == SET_ARRAY_INFO) { |
| 6504 | mdu_array_info_t info; |
| 6505 | if (!arg) |
| 6506 | memset(&info, 0, sizeof(info)); |
| 6507 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6508 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6509 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6510 | } |
| 6511 | if (mddev->pers) { |
| 6512 | err = update_array_info(mddev, &info); |
| 6513 | if (err) { |
| 6514 | printk(KERN_WARNING "md: couldn't update" |
| 6515 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6516 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6517 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6518 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6519 | } |
| 6520 | if (!list_empty(&mddev->disks)) { |
| 6521 | printk(KERN_WARNING |
| 6522 | "md: array %s already has disks!\n", |
| 6523 | mdname(mddev)); |
| 6524 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6525 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6526 | } |
| 6527 | if (mddev->raid_disks) { |
| 6528 | printk(KERN_WARNING |
| 6529 | "md: array %s already initialised!\n", |
| 6530 | mdname(mddev)); |
| 6531 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6532 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6533 | } |
| 6534 | err = set_array_info(mddev, &info); |
| 6535 | if (err) { |
| 6536 | printk(KERN_WARNING "md: couldn't set" |
| 6537 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6538 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6539 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6540 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6541 | } |
| 6542 | |
| 6543 | /* |
| 6544 | * Commands querying/configuring an existing array: |
| 6545 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6546 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6547 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6548 | if ((!mddev->raid_disks && !mddev->external) |
| 6549 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6550 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6551 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6552 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6553 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6554 | } |
| 6555 | |
| 6556 | /* |
| 6557 | * Commands even a read-only array can execute: |
| 6558 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6559 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6560 | case RESTART_ARRAY_RW: |
| 6561 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6562 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6563 | |
| 6564 | case STOP_ARRAY: |
| 6565 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6566 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6567 | |
| 6568 | case STOP_ARRAY_RO: |
| 6569 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6570 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6571 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6572 | case HOT_REMOVE_DISK: |
| 6573 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6574 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6575 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6576 | case ADD_NEW_DISK: |
| 6577 | /* We can support ADD_NEW_DISK on read-only arrays |
| 6578 | * on if we are re-adding a preexisting device. |
| 6579 | * So require mddev->pers and MD_DISK_SYNC. |
| 6580 | */ |
| 6581 | if (mddev->pers) { |
| 6582 | mdu_disk_info_t info; |
| 6583 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6584 | err = -EFAULT; |
| 6585 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 6586 | /* Need to clear read-only for this */ |
| 6587 | break; |
| 6588 | else |
| 6589 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6590 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6591 | } |
| 6592 | break; |
| 6593 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6594 | case BLKROSET: |
| 6595 | if (get_user(ro, (int __user *)(arg))) { |
| 6596 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6597 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6598 | } |
| 6599 | err = -EINVAL; |
| 6600 | |
| 6601 | /* if the bdev is going readonly the value of mddev->ro |
| 6602 | * does not matter, no writes are coming |
| 6603 | */ |
| 6604 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6605 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6606 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6607 | /* are we are already prepared for writes? */ |
| 6608 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6609 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6610 | |
| 6611 | /* transitioning to readauto need only happen for |
| 6612 | * arrays that call md_write_start |
| 6613 | */ |
| 6614 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6615 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6616 | if (err == 0) { |
| 6617 | mddev->ro = 2; |
| 6618 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6619 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6620 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6621 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6622 | } |
| 6623 | |
| 6624 | /* |
| 6625 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6626 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6627 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 6628 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6629 | if (mddev->ro == 2) { |
| 6630 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6631 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6632 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6633 | /* mddev_unlock will wake thread */ |
| 6634 | /* If a device failed while we were read-only, we |
| 6635 | * need to make sure the metadata is updated now. |
| 6636 | */ |
| 6637 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
| 6638 | mddev_unlock(mddev); |
| 6639 | wait_event(mddev->sb_wait, |
| 6640 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && |
| 6641 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6642 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6643 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6644 | } else { |
| 6645 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6646 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6647 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | } |
| 6649 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6650 | switch (cmd) { |
| 6651 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6652 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6653 | mdu_disk_info_t info; |
| 6654 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6655 | err = -EFAULT; |
| 6656 | else |
| 6657 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6658 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6659 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6660 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6661 | case HOT_ADD_DISK: |
| 6662 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6663 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6664 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6665 | case RUN_ARRAY: |
| 6666 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6667 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6668 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6669 | case SET_BITMAP_FILE: |
| 6670 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6671 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6672 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6673 | default: |
| 6674 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6675 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6676 | } |
| 6677 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6678 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6679 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6680 | err != -EINVAL) |
| 6681 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6682 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6683 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6684 | return err; |
| 6685 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6686 | #ifdef CONFIG_COMPAT |
| 6687 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6688 | unsigned int cmd, unsigned long arg) |
| 6689 | { |
| 6690 | switch (cmd) { |
| 6691 | case HOT_REMOVE_DISK: |
| 6692 | case HOT_ADD_DISK: |
| 6693 | case SET_DISK_FAULTY: |
| 6694 | case SET_BITMAP_FILE: |
| 6695 | /* These take in integer arg, do not convert */ |
| 6696 | break; |
| 6697 | default: |
| 6698 | arg = (unsigned long)compat_ptr(arg); |
| 6699 | break; |
| 6700 | } |
| 6701 | |
| 6702 | return md_ioctl(bdev, mode, cmd, arg); |
| 6703 | } |
| 6704 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6705 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6706 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6707 | { |
| 6708 | /* |
| 6709 | * Succeed if we can lock the mddev, which confirms that |
| 6710 | * it isn't being stopped right now. |
| 6711 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6712 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6713 | int err; |
| 6714 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 6715 | if (!mddev) |
| 6716 | return -ENODEV; |
| 6717 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6718 | if (mddev->gendisk != bdev->bd_disk) { |
| 6719 | /* we are racing with mddev_put which is discarding this |
| 6720 | * bd_disk. |
| 6721 | */ |
| 6722 | mddev_put(mddev); |
| 6723 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6724 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6725 | /* Then retry the open from the top */ |
| 6726 | return -ERESTARTSYS; |
| 6727 | } |
| 6728 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6729 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6730 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6731 | goto out; |
| 6732 | |
| 6733 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6734 | atomic_inc(&mddev->openers); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6735 | clear_bit(MD_STILL_CLOSED, &mddev->flags); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6736 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6737 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6738 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6739 | out: |
| 6740 | return err; |
| 6741 | } |
| 6742 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 6743 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6744 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6745 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6746 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6747 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6748 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6749 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6750 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6751 | |
| 6752 | static int md_media_changed(struct gendisk *disk) |
| 6753 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6754 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6755 | |
| 6756 | return mddev->changed; |
| 6757 | } |
| 6758 | |
| 6759 | static int md_revalidate(struct gendisk *disk) |
| 6760 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6761 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6762 | |
| 6763 | mddev->changed = 0; |
| 6764 | return 0; |
| 6765 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6766 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6767 | { |
| 6768 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6769 | .open = md_open, |
| 6770 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6771 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6772 | #ifdef CONFIG_COMPAT |
| 6773 | .compat_ioctl = md_compat_ioctl, |
| 6774 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6775 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6776 | .media_changed = md_media_changed, |
| 6777 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6778 | }; |
| 6779 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6780 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6781 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6782 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6784 | /* |
| 6785 | * md_thread is a 'system-thread', it's priority should be very |
| 6786 | * high. We avoid resource deadlocks individually in each |
| 6787 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6788 | * the very same RT priority as kswapd, thus we will never get |
| 6789 | * into a priority inversion deadlock. |
| 6790 | * |
| 6791 | * we definitely have to have equal or higher priority than |
| 6792 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6793 | * many dirty RAID5 blocks. |
| 6794 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6795 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6796 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6797 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6798 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6799 | /* We need to wait INTERRUPTIBLE so that |
| 6800 | * we don't add to the load-average. |
| 6801 | * That means we need to be sure no signals are |
| 6802 | * pending |
| 6803 | */ |
| 6804 | if (signal_pending(current)) |
| 6805 | flush_signals(current); |
| 6806 | |
| 6807 | wait_event_interruptible_timeout |
| 6808 | (thread->wqueue, |
| 6809 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6810 | || kthread_should_stop(), |
| 6811 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6812 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6813 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6814 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6815 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6816 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6818 | return 0; |
| 6819 | } |
| 6820 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6821 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | { |
| 6823 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6824 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6825 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6826 | wake_up(&thread->wqueue); |
| 6827 | } |
| 6828 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 6829 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6830 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6831 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 6832 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6833 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6834 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6835 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6836 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6837 | if (!thread) |
| 6838 | return NULL; |
| 6839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6840 | init_waitqueue_head(&thread->wqueue); |
| 6841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6842 | thread->run = run; |
| 6843 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6844 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6845 | thread->tsk = kthread_run(md_thread, thread, |
| 6846 | "%s_%s", |
| 6847 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 6848 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6849 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6850 | kfree(thread); |
| 6851 | return NULL; |
| 6852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6853 | return thread; |
| 6854 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 6855 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6856 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6857 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6858 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6859 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6860 | if (!thread) |
| 6861 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6862 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6863 | /* Locking ensures that mddev_unlock does not wake_up a |
| 6864 | * non-existent thread |
| 6865 | */ |
| 6866 | spin_lock(&pers_lock); |
| 6867 | *threadp = NULL; |
| 6868 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6869 | |
| 6870 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6871 | kfree(thread); |
| 6872 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 6873 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6874 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6875 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6876 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6877 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6878 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6879 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6880 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6881 | return; |
| 6882 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6883 | if (mddev->degraded) |
| 6884 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6885 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6886 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6887 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6888 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6889 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6890 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6891 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 6893 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6894 | |
| 6895 | /* seq_file implementation /proc/mdstat */ |
| 6896 | |
| 6897 | static void status_unused(struct seq_file *seq) |
| 6898 | { |
| 6899 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6900 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6901 | |
| 6902 | seq_printf(seq, "unused devices: "); |
| 6903 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6904 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6905 | char b[BDEVNAME_SIZE]; |
| 6906 | i++; |
| 6907 | seq_printf(seq, "%s ", |
| 6908 | bdevname(rdev->bdev,b)); |
| 6909 | } |
| 6910 | if (!i) |
| 6911 | seq_printf(seq, "<none>"); |
| 6912 | |
| 6913 | seq_printf(seq, "\n"); |
| 6914 | } |
| 6915 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6916 | static void status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6917 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6918 | sector_t max_sectors, resync, res; |
| 6919 | unsigned long dt, db; |
| 6920 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6921 | int scale; |
| 6922 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6923 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 6924 | if (mddev->curr_resync <= 3) |
| 6925 | resync = 0; |
| 6926 | else |
| 6927 | resync = mddev->curr_resync |
| 6928 | - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6929 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 6930 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 6931 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6932 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6933 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6934 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6935 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 6936 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6937 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6938 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6939 | * u32, as those are the requirements for sector_div. |
| 6940 | * Thus 'scale' must be at least 10 |
| 6941 | */ |
| 6942 | scale = 10; |
| 6943 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6944 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6945 | scale++; |
| 6946 | } |
| 6947 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6948 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6949 | |
| 6950 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6951 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6952 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6953 | seq_printf(seq, "["); |
| 6954 | for (i = 0; i < x; i++) |
| 6955 | seq_printf(seq, "="); |
| 6956 | seq_printf(seq, ">"); |
| 6957 | for (i = 0; i < y; i++) |
| 6958 | seq_printf(seq, "."); |
| 6959 | seq_printf(seq, "] "); |
| 6960 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6961 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6962 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6963 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6964 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6965 | "check" : |
| 6966 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6967 | "resync" : "recovery"))), |
| 6968 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6969 | (unsigned long long) resync/2, |
| 6970 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6971 | |
| 6972 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6973 | * dt: time from mark until now |
| 6974 | * db: blocks written from mark until now |
| 6975 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6976 | * |
| 6977 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6978 | * So we divide before multiply in case it is 32bit and close |
| 6979 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6980 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6981 | * near the end of resync when the number of remaining sectors |
| 6982 | * is close to 'db'. |
| 6983 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6984 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6985 | */ |
| 6986 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6987 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6988 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6989 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6990 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6991 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6992 | sector_div(rt, db/32+1); |
| 6993 | rt *= dt; |
| 6994 | rt >>= 5; |
| 6995 | |
| 6996 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6997 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6998 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6999 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7000 | } |
| 7001 | |
| 7002 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 7003 | { |
| 7004 | struct list_head *tmp; |
| 7005 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7006 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7007 | |
| 7008 | if (l >= 0x10000) |
| 7009 | return NULL; |
| 7010 | if (!l--) |
| 7011 | /* header */ |
| 7012 | return (void*)1; |
| 7013 | |
| 7014 | spin_lock(&all_mddevs_lock); |
| 7015 | list_for_each(tmp,&all_mddevs) |
| 7016 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7017 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7018 | mddev_get(mddev); |
| 7019 | spin_unlock(&all_mddevs_lock); |
| 7020 | return mddev; |
| 7021 | } |
| 7022 | spin_unlock(&all_mddevs_lock); |
| 7023 | if (!l--) |
| 7024 | return (void*)2;/* tail */ |
| 7025 | return NULL; |
| 7026 | } |
| 7027 | |
| 7028 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 7029 | { |
| 7030 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7031 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7033 | ++*pos; |
| 7034 | if (v == (void*)2) |
| 7035 | return NULL; |
| 7036 | |
| 7037 | spin_lock(&all_mddevs_lock); |
| 7038 | if (v == (void*)1) |
| 7039 | tmp = all_mddevs.next; |
| 7040 | else |
| 7041 | tmp = mddev->all_mddevs.next; |
| 7042 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7043 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7044 | else { |
| 7045 | next_mddev = (void*)2; |
| 7046 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7047 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7048 | spin_unlock(&all_mddevs_lock); |
| 7049 | |
| 7050 | if (v != (void*)1) |
| 7051 | mddev_put(mddev); |
| 7052 | return next_mddev; |
| 7053 | |
| 7054 | } |
| 7055 | |
| 7056 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 7057 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7058 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7059 | |
| 7060 | if (mddev && v != (void*)1 && v != (void*)2) |
| 7061 | mddev_put(mddev); |
| 7062 | } |
| 7063 | |
| 7064 | static int md_seq_show(struct seq_file *seq, void *v) |
| 7065 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7066 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7067 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7068 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7069 | |
| 7070 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7071 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7072 | seq_printf(seq, "Personalities : "); |
| 7073 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7074 | list_for_each_entry(pers, &pers_list, list) |
| 7075 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7076 | |
| 7077 | spin_unlock(&pers_lock); |
| 7078 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7079 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7080 | return 0; |
| 7081 | } |
| 7082 | if (v == (void*)2) { |
| 7083 | status_unused(seq); |
| 7084 | return 0; |
| 7085 | } |
| 7086 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7087 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7088 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7089 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7090 | mddev->pers ? "" : "in"); |
| 7091 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7092 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7093 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7094 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7095 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7096 | seq_printf(seq, " %s", mddev->pers->name); |
| 7097 | } |
| 7098 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7099 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7100 | rcu_read_lock(); |
| 7101 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7102 | char b[BDEVNAME_SIZE]; |
| 7103 | seq_printf(seq, " %s[%d]", |
| 7104 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7105 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7106 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7107 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7108 | seq_printf(seq, "(F)"); |
| 7109 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7110 | } |
| 7111 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7112 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7113 | if (test_bit(Replacement, &rdev->flags)) |
| 7114 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7115 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7116 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7117 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7118 | |
| 7119 | if (!list_empty(&mddev->disks)) { |
| 7120 | if (mddev->pers) |
| 7121 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7122 | (unsigned long long) |
| 7123 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7124 | else |
| 7125 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7126 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7128 | if (mddev->persistent) { |
| 7129 | if (mddev->major_version != 0 || |
| 7130 | mddev->minor_version != 90) { |
| 7131 | seq_printf(seq," super %d.%d", |
| 7132 | mddev->major_version, |
| 7133 | mddev->minor_version); |
| 7134 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7135 | } else if (mddev->external) |
| 7136 | seq_printf(seq, " super external:%s", |
| 7137 | mddev->metadata_type); |
| 7138 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7139 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | |
| 7141 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7142 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7143 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7144 | if (mddev->pers->sync_request) { |
| 7145 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7146 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7147 | seq_printf(seq, "\n "); |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7148 | } else if (mddev->curr_resync >= 1) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7149 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 7150 | else if (mddev->recovery_cp < MaxSector) |
| 7151 | seq_printf(seq, "\tresync=PENDING\n "); |
| 7152 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7153 | } else |
| 7154 | seq_printf(seq, "\n "); |
| 7155 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7156 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7157 | |
| 7158 | seq_printf(seq, "\n"); |
| 7159 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7160 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7162 | return 0; |
| 7163 | } |
| 7164 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7165 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7166 | .start = md_seq_start, |
| 7167 | .next = md_seq_next, |
| 7168 | .stop = md_seq_stop, |
| 7169 | .show = md_seq_show, |
| 7170 | }; |
| 7171 | |
| 7172 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7173 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7174 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7175 | int error; |
| 7176 | |
| 7177 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7178 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7179 | return error; |
| 7180 | |
| 7181 | seq = file->private_data; |
| 7182 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7183 | return error; |
| 7184 | } |
| 7185 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7186 | static int md_unloading; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7187 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7188 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7189 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7190 | int mask; |
| 7191 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7192 | if (md_unloading) |
kbuild test robot | 7d7e64f | 2014-12-03 16:07:59 +1100 | [diff] [blame] | 7193 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7194 | poll_wait(filp, &md_event_waiters, wait); |
| 7195 | |
| 7196 | /* always allow read */ |
| 7197 | mask = POLLIN | POLLRDNORM; |
| 7198 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7199 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7200 | mask |= POLLERR | POLLPRI; |
| 7201 | return mask; |
| 7202 | } |
| 7203 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7204 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7205 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7206 | .open = md_seq_open, |
| 7207 | .read = seq_read, |
| 7208 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7209 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7210 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7211 | }; |
| 7212 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7213 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7214 | { |
Chao Yu | 50bd377 | 2014-09-25 15:28:34 +0800 | [diff] [blame] | 7215 | printk(KERN_INFO "md: %s personality registered for level %d\n", |
| 7216 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7217 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7218 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7219 | spin_unlock(&pers_lock); |
| 7220 | return 0; |
| 7221 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7222 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7223 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7224 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7225 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7226 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7227 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7228 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7229 | spin_unlock(&pers_lock); |
| 7230 | return 0; |
| 7231 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7232 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7233 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7234 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7235 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7236 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7237 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7238 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7239 | |
| 7240 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7241 | rcu_read_lock(); |
| 7242 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7243 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7244 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7245 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7246 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7247 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7248 | * as sync_io is counted when a request starts, and |
| 7249 | * disk_stats is counted when it completes. |
| 7250 | * So resync activity will cause curr_events to be smaller than |
| 7251 | * when there was no such activity. |
| 7252 | * non-sync IO will cause disk_stat to increase without |
| 7253 | * increasing sync_io so curr_events will (eventually) |
| 7254 | * be larger than it was before. Once it becomes |
| 7255 | * substantially larger, the test below will cause |
| 7256 | * the array to appear non-idle, and resync will slow |
| 7257 | * down. |
| 7258 | * If there is a lot of outstanding resync activity when |
| 7259 | * we set last_event to curr_events, then all that activity |
| 7260 | * completing might cause the array to appear non-idle |
| 7261 | * and resync will be slowed down even though there might |
| 7262 | * not have been non-resync activity. This will only |
| 7263 | * happen once though. 'last_events' will soon reflect |
| 7264 | * the state where there is little or no outstanding |
| 7265 | * resync requests, and further resync activity will |
| 7266 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7267 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7268 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7269 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7270 | rdev->last_events = curr_events; |
| 7271 | idle = 0; |
| 7272 | } |
| 7273 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7274 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7275 | return idle; |
| 7276 | } |
| 7277 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7278 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7279 | { |
| 7280 | /* another "blocks" (512byte) blocks have been synced */ |
| 7281 | atomic_sub(blocks, &mddev->recovery_active); |
| 7282 | wake_up(&mddev->recovery_wait); |
| 7283 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7284 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7285 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7286 | md_wakeup_thread(mddev->thread); |
| 7287 | // stop recovery, signal do_sync .... |
| 7288 | } |
| 7289 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7290 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7291 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7292 | /* md_write_start(mddev, bi) |
| 7293 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7294 | * in superblock) before writing, schedule a superblock update |
| 7295 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7296 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7297 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7298 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7299 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7300 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7301 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7302 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7303 | BUG_ON(mddev->ro == 1); |
| 7304 | if (mddev->ro == 2) { |
| 7305 | /* need to switch to read/write */ |
| 7306 | mddev->ro = 0; |
| 7307 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7308 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7309 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7310 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7311 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7312 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7313 | if (mddev->safemode == 1) |
| 7314 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7315 | if (mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7316 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7317 | if (mddev->in_sync) { |
| 7318 | mddev->in_sync = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7319 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7320 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7321 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7322 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7323 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7324 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7325 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7326 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7327 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7328 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7329 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7330 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7331 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7332 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7333 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7334 | { |
| 7335 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7336 | if (mddev->safemode == 2) |
| 7337 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7338 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7339 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7340 | } |
| 7341 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7342 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7343 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7344 | /* md_allow_write(mddev) |
| 7345 | * Calling this ensures that the array is marked 'active' so that writes |
| 7346 | * may proceed without blocking. It is important to call this before |
| 7347 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7348 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7349 | * |
| 7350 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7351 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7352 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7353 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7354 | { |
| 7355 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7356 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7357 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7358 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7359 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7360 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7361 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7362 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7363 | if (mddev->in_sync) { |
| 7364 | mddev->in_sync = 0; |
| 7365 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7366 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7367 | if (mddev->safemode_delay && |
| 7368 | mddev->safemode == 0) |
| 7369 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7370 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7371 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7372 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7373 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7374 | spin_unlock(&mddev->lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7375 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7376 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7377 | return -EAGAIN; |
| 7378 | else |
| 7379 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7380 | } |
| 7381 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7383 | #define SYNC_MARKS 10 |
| 7384 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7385 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7386 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7387 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7388 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7389 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7390 | unsigned int currspeed = 0, |
| 7391 | window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7392 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7393 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7394 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7395 | sector_t mark_cnt[SYNC_MARKS]; |
| 7396 | int last_mark,m; |
| 7397 | struct list_head *tmp; |
| 7398 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7399 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7400 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7401 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7402 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7403 | |
| 7404 | /* just incase thread restarts... */ |
| 7405 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7406 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7407 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 7408 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7409 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7411 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7412 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7413 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7414 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7415 | action = "check"; |
| 7416 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7417 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7418 | action = "repair"; |
| 7419 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7420 | desc = "resync"; |
| 7421 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7422 | desc = "reshape"; |
| 7423 | else |
| 7424 | desc = "recovery"; |
| 7425 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7426 | mddev->last_sync_action = action ?: desc; |
| 7427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7428 | /* we overload curr_resync somewhat here. |
| 7429 | * 0 == not engaged in resync at all |
| 7430 | * 2 == checking that there is no conflict with another sync |
| 7431 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7432 | * commense |
| 7433 | * other == active in resync - this many blocks |
| 7434 | * |
| 7435 | * Before starting a resync we must have set curr_resync to |
| 7436 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7437 | * less than ours. When we find one that is the same or higher |
| 7438 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7439 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7440 | * This will mean we have to start checking from the beginning again. |
| 7441 | * |
| 7442 | */ |
| 7443 | |
| 7444 | do { |
| 7445 | mddev->curr_resync = 2; |
| 7446 | |
| 7447 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7448 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7449 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7450 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7451 | if (mddev2 == mddev) |
| 7452 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7453 | if (!mddev->parallel_resync |
| 7454 | && mddev2->curr_resync |
| 7455 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7456 | DEFINE_WAIT(wq); |
| 7457 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7458 | /* arbitrarily yield */ |
| 7459 | mddev->curr_resync = 1; |
| 7460 | wake_up(&resync_wait); |
| 7461 | } |
| 7462 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7463 | /* no need to wait here, we can wait the next |
| 7464 | * time 'round when curr_resync == 2 |
| 7465 | */ |
| 7466 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7467 | /* We need to wait 'interruptible' so as not to |
| 7468 | * contribute to the load average, and not to |
| 7469 | * be caught by 'softlockup' |
| 7470 | */ |
| 7471 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7472 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7473 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7474 | printk(KERN_INFO "md: delaying %s of %s" |
| 7475 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7476 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7477 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7478 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7479 | if (signal_pending(current)) |
| 7480 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7481 | schedule(); |
| 7482 | finish_wait(&resync_wait, &wq); |
| 7483 | goto try_again; |
| 7484 | } |
| 7485 | finish_wait(&resync_wait, &wq); |
| 7486 | } |
| 7487 | } |
| 7488 | } while (mddev->curr_resync < 2); |
| 7489 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7490 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7491 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7492 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7493 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7494 | */ |
| 7495 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 7496 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7497 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7498 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7499 | j = mddev->resync_min; |
| 7500 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7501 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7502 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7503 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7504 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7505 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7506 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7507 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7508 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7509 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7510 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7511 | if (rdev->raid_disk >= 0 && |
| 7512 | !test_bit(Faulty, &rdev->flags) && |
| 7513 | !test_bit(In_sync, &rdev->flags) && |
| 7514 | rdev->recovery_offset < j) |
| 7515 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7516 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 7517 | |
| 7518 | /* If there is a bitmap, we need to make sure all |
| 7519 | * writes that started before we added a spare |
| 7520 | * complete before we start doing a recovery. |
| 7521 | * Otherwise the write might complete and (via |
| 7522 | * bitmap_endwrite) set a bit in the bitmap after the |
| 7523 | * recovery has checked that bit and skipped that |
| 7524 | * region. |
| 7525 | */ |
| 7526 | if (mddev->bitmap) { |
| 7527 | mddev->pers->quiesce(mddev, 1); |
| 7528 | mddev->pers->quiesce(mddev, 0); |
| 7529 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7530 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7531 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7532 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7533 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7534 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7535 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7536 | "(but not more than %d KB/sec) for %s.\n", |
| 7537 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7538 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7539 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7540 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7541 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7542 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7543 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7544 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7545 | } |
| 7546 | last_mark = 0; |
| 7547 | mddev->resync_mark = mark[last_mark]; |
| 7548 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7549 | |
| 7550 | /* |
| 7551 | * Tune reconstruction: |
| 7552 | */ |
| 7553 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450c | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7554 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7555 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7556 | |
| 7557 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7558 | last_check = 0; |
| 7559 | |
| 7560 | if (j>2) { |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7561 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7562 | "md: resuming %s of %s from checkpoint.\n", |
| 7563 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7564 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7565 | } else |
| 7566 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7567 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7568 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 7569 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7570 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7571 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7572 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7573 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7574 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7575 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7576 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7577 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7578 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7579 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7580 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7581 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7582 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7583 | (j - mddev->curr_resync_completed)*2 |
| 7584 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7585 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7586 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7587 | wait_event(mddev->recovery_wait, |
| 7588 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7589 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7590 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 7591 | j > mddev->recovery_cp) |
| 7592 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7593 | update_time = jiffies; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7594 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7595 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7596 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7597 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7598 | while (j >= mddev->resync_max && |
| 7599 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7600 | /* As this condition is controlled by user-space, |
| 7601 | * we can block indefinitely, so use '_interruptible' |
| 7602 | * to avoid triggering warnings. |
| 7603 | */ |
| 7604 | flush_signals(current); /* just in case */ |
| 7605 | wait_event_interruptible(mddev->recovery_wait, |
| 7606 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7607 | || test_bit(MD_RECOVERY_INTR, |
| 7608 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7609 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7610 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7611 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7612 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7613 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7614 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7615 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7616 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7617 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7618 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7619 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7620 | |
| 7621 | if (!skipped) { /* actual IO requested */ |
| 7622 | io_sectors += sectors; |
| 7623 | atomic_add(sectors, &mddev->recovery_active); |
| 7624 | } |
| 7625 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7626 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7627 | break; |
| 7628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7629 | j += sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7630 | if (j > 2) |
| 7631 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7632 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7633 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7634 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7635 | * visible in /proc/mdstat |
| 7636 | */ |
| 7637 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7638 | |
| 7639 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7640 | continue; |
| 7641 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7642 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7643 | repeat: |
| 7644 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7645 | /* step marks */ |
| 7646 | int next = (last_mark+1) % SYNC_MARKS; |
| 7647 | |
| 7648 | mddev->resync_mark = mark[next]; |
| 7649 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7650 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7651 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7652 | last_mark = next; |
| 7653 | } |
| 7654 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7655 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7656 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7657 | |
| 7658 | /* |
| 7659 | * this loop exits only if either when we are slower than |
| 7660 | * the 'hard' speed limit, or the system was IO-idle for |
| 7661 | * a jiffy. |
| 7662 | * the system might be non-idle CPU-wise, but we only care |
| 7663 | * about not overloading the IO subsystem. (things like an |
| 7664 | * e2fsck being done on the RAID array should execute fast) |
| 7665 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7666 | cond_resched(); |
| 7667 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7668 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 7669 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7670 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7671 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7672 | if (currspeed > speed_min(mddev)) { |
| 7673 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7674 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7675 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7676 | goto repeat; |
| 7677 | } |
| 7678 | } |
| 7679 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7680 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
| 7681 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 7682 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7683 | /* |
| 7684 | * this also signals 'finished resyncing' to md_stop |
| 7685 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7686 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7687 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7688 | |
| 7689 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7690 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7691 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7692 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7693 | mddev->curr_resync > 2) { |
| 7694 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7695 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7696 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7697 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7698 | "md: checkpointing %s of %s.\n", |
| 7699 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7700 | if (test_bit(MD_RECOVERY_ERROR, |
| 7701 | &mddev->recovery)) |
| 7702 | mddev->recovery_cp = |
| 7703 | mddev->curr_resync_completed; |
| 7704 | else |
| 7705 | mddev->recovery_cp = |
| 7706 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7707 | } |
| 7708 | } else |
| 7709 | mddev->recovery_cp = MaxSector; |
| 7710 | } else { |
| 7711 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7712 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7713 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7714 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7715 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7716 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7717 | !test_bit(Faulty, &rdev->flags) && |
| 7718 | !test_bit(In_sync, &rdev->flags) && |
| 7719 | rdev->recovery_offset < mddev->curr_resync) |
| 7720 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7721 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7722 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7723 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7724 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7725 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7726 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 7727 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7728 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7729 | /* We completed so min/max setting can be forgotten if used. */ |
| 7730 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7731 | mddev->resync_min = 0; |
| 7732 | mddev->resync_max = MaxSector; |
| 7733 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7734 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7735 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 7736 | spin_unlock(&mddev->lock); |
| 7737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7738 | wake_up(&resync_wait); |
| 7739 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7740 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7741 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7742 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7743 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7744 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7745 | static int remove_and_add_spares(struct mddev *mddev, |
| 7746 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7747 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7748 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7749 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7750 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7751 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7752 | rdev_for_each(rdev, mddev) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7753 | if ((this == NULL || rdev == this) && |
| 7754 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7755 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7756 | (test_bit(Faulty, &rdev->flags) || |
| 7757 | ! test_bit(In_sync, &rdev->flags)) && |
| 7758 | atomic_read(&rdev->nr_pending)==0) { |
| 7759 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7760 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7761 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7762 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7763 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7764 | } |
| 7765 | } |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 7766 | if (removed && mddev->kobj.sd) |
| 7767 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7768 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7769 | if (this) |
| 7770 | goto no_add; |
| 7771 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7772 | rdev_for_each(rdev, mddev) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7773 | if (rdev->raid_disk >= 0 && |
| 7774 | !test_bit(In_sync, &rdev->flags) && |
| 7775 | !test_bit(Faulty, &rdev->flags)) |
| 7776 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7777 | if (rdev->raid_disk >= 0) |
| 7778 | continue; |
| 7779 | if (test_bit(Faulty, &rdev->flags)) |
| 7780 | continue; |
| 7781 | if (mddev->ro && |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 7782 | ! (rdev->saved_raid_disk >= 0 && |
| 7783 | !test_bit(Bitmap_sync, &rdev->flags))) |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7784 | continue; |
| 7785 | |
NeilBrown | 7eb4188 | 2014-01-14 15:55:14 +1100 | [diff] [blame] | 7786 | if (rdev->saved_raid_disk < 0) |
| 7787 | rdev->recovery_offset = 0; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7788 | if (mddev->pers-> |
| 7789 | hot_add_disk(mddev, rdev) == 0) { |
| 7790 | if (sysfs_link_rdev(mddev, rdev)) |
| 7791 | /* failure here is OK */; |
| 7792 | spares++; |
| 7793 | md_new_event(mddev); |
| 7794 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7795 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7796 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7797 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 7798 | if (removed) |
| 7799 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7800 | return spares; |
| 7801 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7802 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7803 | static void md_start_sync(struct work_struct *ws) |
| 7804 | { |
| 7805 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
| 7806 | |
| 7807 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7808 | mddev, |
| 7809 | "resync"); |
| 7810 | if (!mddev->sync_thread) { |
| 7811 | printk(KERN_ERR "%s: could not start resync" |
| 7812 | " thread...\n", |
| 7813 | mdname(mddev)); |
| 7814 | /* leave the spares where they are, it shouldn't hurt */ |
| 7815 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7816 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7817 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7818 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7819 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7820 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7821 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7822 | &mddev->recovery)) |
| 7823 | if (mddev->sysfs_action) |
| 7824 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7825 | } else |
| 7826 | md_wakeup_thread(mddev->sync_thread); |
| 7827 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7828 | md_new_event(mddev); |
| 7829 | } |
| 7830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7831 | /* |
| 7832 | * This routine is regularly called by all per-raid-array threads to |
| 7833 | * deal with generic issues like resync and super-block update. |
| 7834 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7835 | * need this as they never do any recovery or update the superblock. |
| 7836 | * |
| 7837 | * It does not do any resync itself, but rather "forks" off other threads |
| 7838 | * to do that as needed. |
| 7839 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7840 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7841 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7842 | * and wakeups up this thread which will reap the thread and finish up. |
| 7843 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7844 | * |
| 7845 | * The overall approach is: |
| 7846 | * 1/ if the superblock needs updating, update it. |
| 7847 | * 2/ If a recovery thread is running, don't do anything else. |
| 7848 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7849 | * 4/ If there are any faulty devices, remove them. |
| 7850 | * 5/ If array is degraded, try to add spares devices |
| 7851 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7852 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7853 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7854 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7855 | if (mddev->suspended) |
| 7856 | return; |
| 7857 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7858 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7859 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7860 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7861 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7862 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7863 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7864 | mdname(mddev)); |
| 7865 | mddev->safemode = 2; |
| 7866 | } |
| 7867 | flush_signals(current); |
| 7868 | } |
| 7869 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7870 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7871 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7872 | if ( ! ( |
NeilBrown | 142d44c | 2013-11-28 10:34:18 +1100 | [diff] [blame] | 7873 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7874 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7875 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7876 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7877 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7878 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7879 | )) |
| 7880 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7881 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7882 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7883 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7884 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7885 | if (mddev->ro) { |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7886 | /* On a read-only array we can: |
| 7887 | * - remove failed devices |
| 7888 | * - add already-in_sync devices if the array itself |
| 7889 | * is in-sync. |
| 7890 | * As we only add devices that are already in-sync, |
| 7891 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7892 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7893 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 7894 | /* There is no thread, but we need to call |
| 7895 | * ->spare_active and clear saved_raid_disk |
| 7896 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 7897 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 7898 | md_reap_sync_thread(mddev); |
| 7899 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7900 | goto unlock; |
| 7901 | } |
| 7902 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7903 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7904 | int did_change = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7905 | spin_lock(&mddev->lock); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7906 | if (mddev->safemode && |
| 7907 | !atomic_read(&mddev->writes_pending) && |
| 7908 | !mddev->in_sync && |
| 7909 | mddev->recovery_cp == MaxSector) { |
| 7910 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7911 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7912 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7913 | } |
| 7914 | if (mddev->safemode == 1) |
| 7915 | mddev->safemode = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7916 | spin_unlock(&mddev->lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7917 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7918 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7919 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7920 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 7921 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7922 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7924 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7925 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7926 | /* resync/recovery still happening */ |
| 7927 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7928 | goto unlock; |
| 7929 | } |
| 7930 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 7931 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7932 | goto unlock; |
| 7933 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7934 | /* Set RUNNING before clearing NEEDED to avoid |
| 7935 | * any transients in the value of "sync_action". |
| 7936 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7937 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 7938 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7939 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 7940 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7941 | /* Clear some bits that don't mean anything, but |
| 7942 | * might be left set |
| 7943 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7944 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7945 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7946 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 7947 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 7948 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7949 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7950 | /* no recovery is running. |
| 7951 | * remove any failed drives, then |
| 7952 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7953 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7954 | * the personality to fail the re-add. |
| 7955 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7956 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7957 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7958 | if (mddev->pers->check_reshape == NULL || |
| 7959 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7960 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7961 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7962 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7963 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7964 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7965 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7966 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7967 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7968 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7969 | } else if (mddev->recovery_cp < MaxSector) { |
| 7970 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7971 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7972 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7973 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7974 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7976 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 7977 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7978 | /* We are adding a device or devices to an array |
| 7979 | * which has the bitmap stored on all devices. |
| 7980 | * So make sure all bitmap pages get written |
| 7981 | */ |
| 7982 | bitmap_write_all(mddev->bitmap); |
| 7983 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7984 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 7985 | queue_work(md_misc_wq, &mddev->del_work); |
| 7986 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7987 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7988 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7989 | if (!mddev->sync_thread) { |
| 7990 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7991 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7992 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7993 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7994 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7995 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7996 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7997 | unlock: |
| 7998 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7999 | mddev_unlock(mddev); |
| 8000 | } |
| 8001 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8002 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8003 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8004 | void md_reap_sync_thread(struct mddev *mddev) |
| 8005 | { |
| 8006 | struct md_rdev *rdev; |
| 8007 | |
| 8008 | /* resync has finished, collect result */ |
| 8009 | md_unregister_thread(&mddev->sync_thread); |
| 8010 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8011 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 8012 | /* success...*/ |
| 8013 | /* activate any spares */ |
| 8014 | if (mddev->pers->spare_active(mddev)) { |
| 8015 | sysfs_notify(&mddev->kobj, NULL, |
| 8016 | "degraded"); |
| 8017 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 8018 | } |
| 8019 | } |
| 8020 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8021 | mddev->pers->finish_reshape) |
| 8022 | mddev->pers->finish_reshape(mddev); |
| 8023 | |
| 8024 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8025 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8026 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8027 | if (!mddev->degraded) |
| 8028 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8029 | rdev->saved_raid_disk = -1; |
| 8030 | |
| 8031 | md_update_sb(mddev, 1); |
| 8032 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 8033 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8034 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8035 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8036 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8037 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8038 | /* flag recovery needed just to double check */ |
| 8039 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8040 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8041 | md_new_event(mddev); |
| 8042 | if (mddev->event_work.func) |
| 8043 | queue_work(md_misc_wq, &mddev->event_work); |
| 8044 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8045 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8046 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8047 | 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] | 8048 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8049 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8050 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8051 | !test_bit(Blocked, &rdev->flags) && |
| 8052 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8053 | msecs_to_jiffies(5000)); |
| 8054 | rdev_dec_pending(rdev, mddev); |
| 8055 | } |
| 8056 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 8057 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8058 | void md_finish_reshape(struct mddev *mddev) |
| 8059 | { |
| 8060 | /* called be personality module when reshape completes. */ |
| 8061 | struct md_rdev *rdev; |
| 8062 | |
| 8063 | rdev_for_each(rdev, mddev) { |
| 8064 | if (rdev->data_offset > rdev->new_data_offset) |
| 8065 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 8066 | else |
| 8067 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 8068 | rdev->data_offset = rdev->new_data_offset; |
| 8069 | } |
| 8070 | } |
| 8071 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8072 | |
| 8073 | /* Bad block management. |
| 8074 | * We can record which blocks on each device are 'bad' and so just |
| 8075 | * fail those blocks, or that stripe, rather than the whole device. |
| 8076 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 8077 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 8078 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 8079 | * A 'shift' can be set so that larger blocks are tracked and |
| 8080 | * consequently larger devices can be covered. |
| 8081 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 8082 | * |
| 8083 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 8084 | * might need to retry if it is very unlucky. |
| 8085 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 8086 | * so we use the write_seqlock_irq variant. |
| 8087 | * |
| 8088 | * When looking for a bad block we specify a range and want to |
| 8089 | * know if any block in the range is bad. So we binary-search |
| 8090 | * to the last range that starts at-or-before the given endpoint, |
| 8091 | * (or "before the sector after the target range") |
| 8092 | * then see if it ends after the given start. |
| 8093 | * We return |
| 8094 | * 0 if there are no known bad blocks in the range |
| 8095 | * 1 if there are known bad block which are all acknowledged |
| 8096 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 8097 | * plus the start/length of the first bad section we overlap. |
| 8098 | */ |
| 8099 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 8100 | sector_t *first_bad, int *bad_sectors) |
| 8101 | { |
| 8102 | int hi; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8103 | int lo; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8104 | u64 *p = bb->page; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8105 | int rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8106 | sector_t target = s + sectors; |
| 8107 | unsigned seq; |
| 8108 | |
| 8109 | if (bb->shift > 0) { |
| 8110 | /* round the start down, and the end up */ |
| 8111 | s >>= bb->shift; |
| 8112 | target += (1<<bb->shift) - 1; |
| 8113 | target >>= bb->shift; |
| 8114 | sectors = target - s; |
| 8115 | } |
| 8116 | /* 'target' is now the first block after the bad range */ |
| 8117 | |
| 8118 | retry: |
| 8119 | seq = read_seqbegin(&bb->lock); |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8120 | lo = 0; |
| 8121 | rv = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8122 | hi = bb->count; |
| 8123 | |
| 8124 | /* Binary search between lo and hi for 'target' |
| 8125 | * i.e. for the last range that starts before 'target' |
| 8126 | */ |
| 8127 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 8128 | * are known not to be the last range before target. |
| 8129 | * VARIANT: hi-lo is the number of possible |
| 8130 | * ranges, and decreases until it reaches 1 |
| 8131 | */ |
| 8132 | while (hi - lo > 1) { |
| 8133 | int mid = (lo + hi) / 2; |
| 8134 | sector_t a = BB_OFFSET(p[mid]); |
| 8135 | if (a < target) |
| 8136 | /* This could still be the one, earlier ranges |
| 8137 | * could not. */ |
| 8138 | lo = mid; |
| 8139 | else |
| 8140 | /* This and later ranges are definitely out. */ |
| 8141 | hi = mid; |
| 8142 | } |
| 8143 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 8144 | if (hi > lo) { |
| 8145 | /* need to check all range that end after 's' to see if |
| 8146 | * any are unacknowledged. |
| 8147 | */ |
| 8148 | while (lo >= 0 && |
| 8149 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8150 | if (BB_OFFSET(p[lo]) < target) { |
| 8151 | /* starts before the end, and finishes after |
| 8152 | * the start, so they must overlap |
| 8153 | */ |
| 8154 | if (rv != -1 && BB_ACK(p[lo])) |
| 8155 | rv = 1; |
| 8156 | else |
| 8157 | rv = -1; |
| 8158 | *first_bad = BB_OFFSET(p[lo]); |
| 8159 | *bad_sectors = BB_LEN(p[lo]); |
| 8160 | } |
| 8161 | lo--; |
| 8162 | } |
| 8163 | } |
| 8164 | |
| 8165 | if (read_seqretry(&bb->lock, seq)) |
| 8166 | goto retry; |
| 8167 | |
| 8168 | return rv; |
| 8169 | } |
| 8170 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 8171 | |
| 8172 | /* |
| 8173 | * Add a range of bad blocks to the table. |
| 8174 | * This might extend the table, or might contract it |
| 8175 | * if two adjacent ranges can be merged. |
| 8176 | * We binary-search to find the 'insertion' point, then |
| 8177 | * decide how best to handle it. |
| 8178 | */ |
| 8179 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 8180 | int acknowledged) |
| 8181 | { |
| 8182 | u64 *p; |
| 8183 | int lo, hi; |
| 8184 | int rv = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8185 | unsigned long flags; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8186 | |
| 8187 | if (bb->shift < 0) |
| 8188 | /* badblocks are disabled */ |
| 8189 | return 0; |
| 8190 | |
| 8191 | if (bb->shift) { |
| 8192 | /* round the start down, and the end up */ |
| 8193 | sector_t next = s + sectors; |
| 8194 | s >>= bb->shift; |
| 8195 | next += (1<<bb->shift) - 1; |
| 8196 | next >>= bb->shift; |
| 8197 | sectors = next - s; |
| 8198 | } |
| 8199 | |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8200 | write_seqlock_irqsave(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8201 | |
| 8202 | p = bb->page; |
| 8203 | lo = 0; |
| 8204 | hi = bb->count; |
| 8205 | /* Find the last range that starts at-or-before 's' */ |
| 8206 | while (hi - lo > 1) { |
| 8207 | int mid = (lo + hi) / 2; |
| 8208 | sector_t a = BB_OFFSET(p[mid]); |
| 8209 | if (a <= s) |
| 8210 | lo = mid; |
| 8211 | else |
| 8212 | hi = mid; |
| 8213 | } |
| 8214 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 8215 | hi = lo; |
| 8216 | |
| 8217 | if (hi > lo) { |
| 8218 | /* we found a range that might merge with the start |
| 8219 | * of our new range |
| 8220 | */ |
| 8221 | sector_t a = BB_OFFSET(p[lo]); |
| 8222 | sector_t e = a + BB_LEN(p[lo]); |
| 8223 | int ack = BB_ACK(p[lo]); |
| 8224 | if (e >= s) { |
| 8225 | /* Yes, we can merge with a previous range */ |
| 8226 | if (s == a && s + sectors >= e) |
| 8227 | /* new range covers old */ |
| 8228 | ack = acknowledged; |
| 8229 | else |
| 8230 | ack = ack && acknowledged; |
| 8231 | |
| 8232 | if (e < s + sectors) |
| 8233 | e = s + sectors; |
| 8234 | if (e - a <= BB_MAX_LEN) { |
| 8235 | p[lo] = BB_MAKE(a, e-a, ack); |
| 8236 | s = e; |
| 8237 | } else { |
| 8238 | /* does not all fit in one range, |
| 8239 | * make p[lo] maximal |
| 8240 | */ |
| 8241 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8242 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8243 | s = a + BB_MAX_LEN; |
| 8244 | } |
| 8245 | sectors = e - s; |
| 8246 | } |
| 8247 | } |
| 8248 | if (sectors && hi < bb->count) { |
| 8249 | /* 'hi' points to the first range that starts after 's'. |
| 8250 | * Maybe we can merge with the start of that range */ |
| 8251 | sector_t a = BB_OFFSET(p[hi]); |
| 8252 | sector_t e = a + BB_LEN(p[hi]); |
| 8253 | int ack = BB_ACK(p[hi]); |
| 8254 | if (a <= s + sectors) { |
| 8255 | /* merging is possible */ |
| 8256 | if (e <= s + sectors) { |
| 8257 | /* full overlap */ |
| 8258 | e = s + sectors; |
| 8259 | ack = acknowledged; |
| 8260 | } else |
| 8261 | ack = ack && acknowledged; |
| 8262 | |
| 8263 | a = s; |
| 8264 | if (e - a <= BB_MAX_LEN) { |
| 8265 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8266 | s = e; |
| 8267 | } else { |
| 8268 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8269 | s = a + BB_MAX_LEN; |
| 8270 | } |
| 8271 | sectors = e - s; |
| 8272 | lo = hi; |
| 8273 | hi++; |
| 8274 | } |
| 8275 | } |
| 8276 | if (sectors == 0 && hi < bb->count) { |
| 8277 | /* we might be able to combine lo and hi */ |
| 8278 | /* Note: 's' is at the end of 'lo' */ |
| 8279 | sector_t a = BB_OFFSET(p[hi]); |
| 8280 | int lolen = BB_LEN(p[lo]); |
| 8281 | int hilen = BB_LEN(p[hi]); |
| 8282 | int newlen = lolen + hilen - (s - a); |
| 8283 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8284 | /* yes, we can combine them */ |
| 8285 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8286 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8287 | memmove(p + hi, p + hi + 1, |
| 8288 | (bb->count - hi - 1) * 8); |
| 8289 | bb->count--; |
| 8290 | } |
| 8291 | } |
| 8292 | while (sectors) { |
| 8293 | /* didn't merge (it all). |
| 8294 | * Need to add a range just before 'hi' */ |
| 8295 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8296 | /* No room for more */ |
| 8297 | rv = 0; |
| 8298 | break; |
| 8299 | } else { |
| 8300 | int this_sectors = sectors; |
| 8301 | memmove(p + hi + 1, p + hi, |
| 8302 | (bb->count - hi) * 8); |
| 8303 | bb->count++; |
| 8304 | |
| 8305 | if (this_sectors > BB_MAX_LEN) |
| 8306 | this_sectors = BB_MAX_LEN; |
| 8307 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8308 | sectors -= this_sectors; |
| 8309 | s += this_sectors; |
| 8310 | } |
| 8311 | } |
| 8312 | |
| 8313 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8314 | if (!acknowledged) |
| 8315 | bb->unacked_exist = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8316 | write_sequnlock_irqrestore(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8317 | |
| 8318 | return rv; |
| 8319 | } |
| 8320 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8321 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8322 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8323 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8324 | int rv; |
| 8325 | if (is_new) |
| 8326 | s += rdev->new_data_offset; |
| 8327 | else |
| 8328 | s += rdev->data_offset; |
| 8329 | rv = md_set_badblocks(&rdev->badblocks, |
| 8330 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8331 | if (rv) { |
| 8332 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8333 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8334 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 8335 | md_wakeup_thread(rdev->mddev->thread); |
| 8336 | } |
| 8337 | return rv; |
| 8338 | } |
| 8339 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8340 | |
| 8341 | /* |
| 8342 | * Remove a range of bad blocks from the table. |
| 8343 | * This may involve extending the table if we spilt a region, |
| 8344 | * but it must not fail. So if the table becomes full, we just |
| 8345 | * drop the remove request. |
| 8346 | */ |
| 8347 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8348 | { |
| 8349 | u64 *p; |
| 8350 | int lo, hi; |
| 8351 | sector_t target = s + sectors; |
| 8352 | int rv = 0; |
| 8353 | |
| 8354 | if (bb->shift > 0) { |
| 8355 | /* When clearing we round the start up and the end down. |
| 8356 | * This should not matter as the shift should align with |
| 8357 | * the block size and no rounding should ever be needed. |
| 8358 | * However it is better the think a block is bad when it |
| 8359 | * isn't than to think a block is not bad when it is. |
| 8360 | */ |
| 8361 | s += (1<<bb->shift) - 1; |
| 8362 | s >>= bb->shift; |
| 8363 | target >>= bb->shift; |
| 8364 | sectors = target - s; |
| 8365 | } |
| 8366 | |
| 8367 | write_seqlock_irq(&bb->lock); |
| 8368 | |
| 8369 | p = bb->page; |
| 8370 | lo = 0; |
| 8371 | hi = bb->count; |
| 8372 | /* Find the last range that starts before 'target' */ |
| 8373 | while (hi - lo > 1) { |
| 8374 | int mid = (lo + hi) / 2; |
| 8375 | sector_t a = BB_OFFSET(p[mid]); |
| 8376 | if (a < target) |
| 8377 | lo = mid; |
| 8378 | else |
| 8379 | hi = mid; |
| 8380 | } |
| 8381 | if (hi > lo) { |
| 8382 | /* p[lo] is the last range that could overlap the |
| 8383 | * current range. Earlier ranges could also overlap, |
| 8384 | * but only this one can overlap the end of the range. |
| 8385 | */ |
| 8386 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8387 | /* Partial overlap, leave the tail of this range */ |
| 8388 | int ack = BB_ACK(p[lo]); |
| 8389 | sector_t a = BB_OFFSET(p[lo]); |
| 8390 | sector_t end = a + BB_LEN(p[lo]); |
| 8391 | |
| 8392 | if (a < s) { |
| 8393 | /* we need to split this range */ |
| 8394 | if (bb->count >= MD_MAX_BADBLOCKS) { |
NeilBrown | 8b32bf5 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 8395 | rv = -ENOSPC; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8396 | goto out; |
| 8397 | } |
| 8398 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8399 | bb->count++; |
| 8400 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8401 | lo++; |
| 8402 | } |
| 8403 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8404 | /* there is no longer an overlap */ |
| 8405 | hi = lo; |
| 8406 | lo--; |
| 8407 | } |
| 8408 | while (lo >= 0 && |
| 8409 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8410 | /* This range does overlap */ |
| 8411 | if (BB_OFFSET(p[lo]) < s) { |
| 8412 | /* Keep the early parts of this range. */ |
| 8413 | int ack = BB_ACK(p[lo]); |
| 8414 | sector_t start = BB_OFFSET(p[lo]); |
| 8415 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8416 | /* now low doesn't overlap, so.. */ |
| 8417 | break; |
| 8418 | } |
| 8419 | lo--; |
| 8420 | } |
| 8421 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8422 | * anything between needs to be discarded |
| 8423 | */ |
| 8424 | if (hi - lo > 1) { |
| 8425 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8426 | bb->count -= (hi - lo - 1); |
| 8427 | } |
| 8428 | } |
| 8429 | |
| 8430 | bb->changed = 1; |
| 8431 | out: |
| 8432 | write_sequnlock_irq(&bb->lock); |
| 8433 | return rv; |
| 8434 | } |
| 8435 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8436 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8437 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8438 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8439 | if (is_new) |
| 8440 | s += rdev->new_data_offset; |
| 8441 | else |
| 8442 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8443 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8444 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8445 | } |
| 8446 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8447 | |
| 8448 | /* |
| 8449 | * Acknowledge all bad blocks in a list. |
| 8450 | * This only succeeds if ->changed is clear. It is used by |
| 8451 | * in-kernel metadata updates |
| 8452 | */ |
| 8453 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8454 | { |
| 8455 | if (bb->page == NULL || bb->changed) |
| 8456 | /* no point even trying */ |
| 8457 | return; |
| 8458 | write_seqlock_irq(&bb->lock); |
| 8459 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8460 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8461 | u64 *p = bb->page; |
| 8462 | int i; |
| 8463 | for (i = 0; i < bb->count ; i++) { |
| 8464 | if (!BB_ACK(p[i])) { |
| 8465 | sector_t start = BB_OFFSET(p[i]); |
| 8466 | int len = BB_LEN(p[i]); |
| 8467 | p[i] = BB_MAKE(start, len, 1); |
| 8468 | } |
| 8469 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8470 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8471 | } |
| 8472 | write_sequnlock_irq(&bb->lock); |
| 8473 | } |
| 8474 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8475 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8476 | /* sysfs access to bad-blocks list. |
| 8477 | * We present two files. |
| 8478 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8479 | * are recorded as bad. The list is truncated to fit within |
| 8480 | * the one-page limit of sysfs. |
| 8481 | * Writing "sector length" to this file adds an acknowledged |
| 8482 | * bad block list. |
| 8483 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8484 | * been acknowledged. Writing to this file adds bad blocks |
| 8485 | * without acknowledging them. This is largely for testing. |
| 8486 | */ |
| 8487 | |
| 8488 | static ssize_t |
| 8489 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8490 | { |
| 8491 | size_t len; |
| 8492 | int i; |
| 8493 | u64 *p = bb->page; |
| 8494 | unsigned seq; |
| 8495 | |
| 8496 | if (bb->shift < 0) |
| 8497 | return 0; |
| 8498 | |
| 8499 | retry: |
| 8500 | seq = read_seqbegin(&bb->lock); |
| 8501 | |
| 8502 | len = 0; |
| 8503 | i = 0; |
| 8504 | |
| 8505 | while (len < PAGE_SIZE && i < bb->count) { |
| 8506 | sector_t s = BB_OFFSET(p[i]); |
| 8507 | unsigned int length = BB_LEN(p[i]); |
| 8508 | int ack = BB_ACK(p[i]); |
| 8509 | i++; |
| 8510 | |
| 8511 | if (unack && ack) |
| 8512 | continue; |
| 8513 | |
| 8514 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8515 | (unsigned long long)s << bb->shift, |
| 8516 | length << bb->shift); |
| 8517 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8518 | if (unack && len == 0) |
| 8519 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8520 | |
| 8521 | if (read_seqretry(&bb->lock, seq)) |
| 8522 | goto retry; |
| 8523 | |
| 8524 | return len; |
| 8525 | } |
| 8526 | |
| 8527 | #define DO_DEBUG 1 |
| 8528 | |
| 8529 | static ssize_t |
| 8530 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8531 | { |
| 8532 | unsigned long long sector; |
| 8533 | int length; |
| 8534 | char newline; |
| 8535 | #ifdef DO_DEBUG |
| 8536 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8537 | * Normally only a successful write may clear a badblock |
| 8538 | */ |
| 8539 | int clear = 0; |
| 8540 | if (page[0] == '-') { |
| 8541 | clear = 1; |
| 8542 | page++; |
| 8543 | } |
| 8544 | #endif /* DO_DEBUG */ |
| 8545 | |
| 8546 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8547 | case 3: |
| 8548 | if (newline != '\n') |
| 8549 | return -EINVAL; |
| 8550 | case 2: |
| 8551 | if (length <= 0) |
| 8552 | return -EINVAL; |
| 8553 | break; |
| 8554 | default: |
| 8555 | return -EINVAL; |
| 8556 | } |
| 8557 | |
| 8558 | #ifdef DO_DEBUG |
| 8559 | if (clear) { |
| 8560 | md_clear_badblocks(bb, sector, length); |
| 8561 | return len; |
| 8562 | } |
| 8563 | #endif /* DO_DEBUG */ |
| 8564 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8565 | return len; |
| 8566 | else |
| 8567 | return -ENOSPC; |
| 8568 | } |
| 8569 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8570 | static int md_notify_reboot(struct notifier_block *this, |
| 8571 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8572 | { |
| 8573 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8574 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8575 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8576 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8577 | for_each_mddev(mddev, tmp) { |
| 8578 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8579 | if (mddev->pers) |
| 8580 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 8581 | if (mddev->persistent) |
| 8582 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8583 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8584 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8585 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8586 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8587 | /* |
| 8588 | * certain more exotic SCSI devices are known to be |
| 8589 | * volatile wrt too early system reboots. While the |
| 8590 | * right place to handle this issue is the given |
| 8591 | * driver, we do want to have a safe RAID driver ... |
| 8592 | */ |
| 8593 | if (need_delay) |
| 8594 | mdelay(1000*1); |
| 8595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8596 | return NOTIFY_DONE; |
| 8597 | } |
| 8598 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8599 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8600 | .notifier_call = md_notify_reboot, |
| 8601 | .next = NULL, |
| 8602 | .priority = INT_MAX, /* before any real devices */ |
| 8603 | }; |
| 8604 | |
| 8605 | static void md_geninit(void) |
| 8606 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8607 | 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] | 8608 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8609 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8610 | } |
| 8611 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8612 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8613 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8614 | int ret = -ENOMEM; |
| 8615 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8616 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8617 | if (!md_wq) |
| 8618 | goto err_wq; |
| 8619 | |
| 8620 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8621 | if (!md_misc_wq) |
| 8622 | goto err_misc_wq; |
| 8623 | |
| 8624 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8625 | goto err_md; |
| 8626 | |
| 8627 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8628 | goto err_mdp; |
| 8629 | mdp_major = ret; |
| 8630 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8631 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8632 | md_probe, NULL, NULL); |
| 8633 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8634 | md_probe, NULL, NULL); |
| 8635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8636 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8637 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8638 | |
| 8639 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8640 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8641 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8642 | err_mdp: |
| 8643 | unregister_blkdev(MD_MAJOR, "md"); |
| 8644 | err_md: |
| 8645 | destroy_workqueue(md_misc_wq); |
| 8646 | err_misc_wq: |
| 8647 | destroy_workqueue(md_wq); |
| 8648 | err_wq: |
| 8649 | return ret; |
| 8650 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8651 | |
| 8652 | #ifndef MODULE |
| 8653 | |
| 8654 | /* |
| 8655 | * Searches all registered partitions for autorun RAID arrays |
| 8656 | * at boot time. |
| 8657 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8658 | |
| 8659 | static LIST_HEAD(all_detected_devices); |
| 8660 | struct detected_devices_node { |
| 8661 | struct list_head list; |
| 8662 | dev_t dev; |
| 8663 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8664 | |
| 8665 | void md_autodetect_dev(dev_t dev) |
| 8666 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8667 | struct detected_devices_node *node_detected_dev; |
| 8668 | |
| 8669 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8670 | if (node_detected_dev) { |
| 8671 | node_detected_dev->dev = dev; |
| 8672 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8673 | } else { |
| 8674 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8675 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8676 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8677 | } |
| 8678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8679 | static void autostart_arrays(int part) |
| 8680 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8681 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8682 | struct detected_devices_node *node_detected_dev; |
| 8683 | dev_t dev; |
| 8684 | int i_scanned, i_passed; |
| 8685 | |
| 8686 | i_scanned = 0; |
| 8687 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8688 | |
| 8689 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8690 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8691 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8692 | i_scanned++; |
| 8693 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8694 | struct detected_devices_node, list); |
| 8695 | list_del(&node_detected_dev->list); |
| 8696 | dev = node_detected_dev->dev; |
| 8697 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8698 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8699 | if (IS_ERR(rdev)) |
| 8700 | continue; |
| 8701 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8702 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8703 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8704 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8705 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8706 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8707 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8708 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8709 | |
| 8710 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8711 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8712 | |
| 8713 | autorun_devices(part); |
| 8714 | } |
| 8715 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8716 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8717 | |
| 8718 | static __exit void md_exit(void) |
| 8719 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8720 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8721 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8722 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8723 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8724 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8725 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8726 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8727 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8728 | unregister_blkdev(mdp_major, "mdp"); |
| 8729 | unregister_reboot_notifier(&md_notifier); |
| 8730 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8731 | |
| 8732 | /* We cannot unload the modules while some process is |
| 8733 | * waiting for us in select() or poll() - wake them up |
| 8734 | */ |
| 8735 | md_unloading = 1; |
| 8736 | while (waitqueue_active(&md_event_waiters)) { |
| 8737 | /* not safe to leave yet */ |
| 8738 | wake_up(&md_event_waiters); |
| 8739 | msleep(delay); |
| 8740 | delay += delay; |
| 8741 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8742 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8743 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8744 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8745 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8746 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8747 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8748 | destroy_workqueue(md_misc_wq); |
| 8749 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8750 | } |
| 8751 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8752 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8753 | module_exit(md_exit) |
| 8754 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8755 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8756 | { |
| 8757 | return sprintf(buffer, "%d", start_readonly); |
| 8758 | } |
| 8759 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8760 | { |
| 8761 | char *e; |
| 8762 | int num = simple_strtoul(val, &e, 10); |
| 8763 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8764 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8765 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8766 | } |
| 8767 | return -EINVAL; |
| 8768 | } |
| 8769 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8770 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8771 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8772 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8774 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8775 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8776 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8777 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |