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); |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 77 | * Default number of read corrections we'll attempt on an rdev |
| 78 | * before ejecting it from the array. We divide the read error |
| 79 | * count by 2 for every hour elapsed between read errors. |
| 80 | */ |
| 81 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 82 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 84 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 85 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 86 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 88 | * speed limit - in case reconstruction slows down your system despite |
| 89 | * idle IO detection. |
| 90 | * |
| 91 | * 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] | 92 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | */ |
| 94 | |
| 95 | static int sysctl_speed_limit_min = 1000; |
| 96 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 97 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 98 | { |
| 99 | return mddev->sync_speed_min ? |
| 100 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 101 | } |
| 102 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 103 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 104 | { |
| 105 | return mddev->sync_speed_max ? |
| 106 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 107 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | static struct ctl_table_header *raid_table_header; |
| 110 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 111 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | .procname = "speed_limit_min", |
| 114 | .data = &sysctl_speed_limit_min, |
| 115 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 116 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 117 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | }, |
| 119 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | .procname = "speed_limit_max", |
| 121 | .data = &sysctl_speed_limit_max, |
| 122 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 123 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 124 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 126 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 129 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | .procname = "raid", |
| 132 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 133 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | .child = raid_table, |
| 135 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 136 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 139 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | .procname = "dev", |
| 142 | .maxlen = 0, |
| 143 | .mode = 0555, |
| 144 | .child = raid_dir_table, |
| 145 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 146 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 149 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 151 | static int start_readonly; |
| 152 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 153 | /* bio_clone_mddev |
| 154 | * like bio_clone, but with a local bio set |
| 155 | */ |
| 156 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 157 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 158 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 159 | { |
| 160 | struct bio *b; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 161 | |
| 162 | if (!mddev || !mddev->bio_set) |
| 163 | return bio_alloc(gfp_mask, nr_iovecs); |
| 164 | |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 165 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 166 | if (!b) |
| 167 | return NULL; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 168 | return b; |
| 169 | } |
| 170 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 171 | |
| 172 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 173 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 174 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 175 | if (!mddev || !mddev->bio_set) |
| 176 | return bio_clone(bio, gfp_mask); |
| 177 | |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 178 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 179 | } |
| 180 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 183 | * We have a system wide 'event count' that is incremented |
| 184 | * on any 'interesting' event, and readers of /proc/mdstat |
| 185 | * can use 'poll' or 'select' to find out when the event |
| 186 | * count increases. |
| 187 | * |
| 188 | * Events are: |
| 189 | * start array, stop array, error, add device, remove device, |
| 190 | * start build, activate spare |
| 191 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 192 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 193 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 194 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 195 | { |
| 196 | atomic_inc(&md_event_count); |
| 197 | wake_up(&md_event_waiters); |
| 198 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 199 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 200 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 201 | /* Alternate version that can be called from interrupts |
| 202 | * when calling sysfs_notify isn't needed. |
| 203 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 204 | static void md_new_event_inintr(struct mddev *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 205 | { |
| 206 | atomic_inc(&md_event_count); |
| 207 | wake_up(&md_event_waiters); |
| 208 | } |
| 209 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 210 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | * Enables to iterate over all existing md arrays |
| 212 | * all_mddevs_lock protects this list. |
| 213 | */ |
| 214 | static LIST_HEAD(all_mddevs); |
| 215 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /* |
| 218 | * iterates through all used mddevs in the system. |
| 219 | * We take care to grab the all_mddevs_lock whenever navigating |
| 220 | * the list, and to always hold a refcount when unlocked. |
| 221 | * Any code which breaks out of this loop while own |
| 222 | * a reference to the current mddev and must mddev_put it. |
| 223 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 224 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | \ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 226 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 227 | _tmp = all_mddevs.next; \ |
| 228 | _mddev = NULL;}); \ |
| 229 | ({ if (_tmp != &all_mddevs) \ |
| 230 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 232 | if (_mddev) mddev_put(_mddev); \ |
| 233 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 234 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 236 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | ) |
| 238 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 239 | /* Rather than calling directly into the personality make_request function, |
| 240 | * IO requests come here first so that we can check if the device is |
| 241 | * being suspended pending a reconfiguration. |
| 242 | * We hold a refcount over the call to ->make_request. By the time that |
| 243 | * call has finished, the bio has been linked into some internal structure |
| 244 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 245 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 246 | static void md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 248 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 249 | struct mddev *mddev = q->queuedata; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 250 | unsigned int sectors; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 251 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 252 | if (mddev == NULL || mddev->pers == NULL |
| 253 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 254 | bio_io_error(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 255 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 256 | } |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 257 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
| 258 | bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS); |
| 259 | return; |
| 260 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 261 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 262 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 263 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 264 | DEFINE_WAIT(__wait); |
| 265 | for (;;) { |
| 266 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 267 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 268 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 269 | break; |
| 270 | rcu_read_unlock(); |
| 271 | schedule(); |
| 272 | rcu_read_lock(); |
| 273 | } |
| 274 | finish_wait(&mddev->sb_wait, &__wait); |
| 275 | } |
| 276 | atomic_inc(&mddev->active_io); |
| 277 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 278 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 279 | /* |
| 280 | * save the sectors now since our bio can |
| 281 | * go away inside make_request |
| 282 | */ |
| 283 | sectors = bio_sectors(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 284 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 285 | |
Gu Zheng | 18c0b22 | 2014-11-24 11:05:26 +0800 | [diff] [blame] | 286 | generic_start_io_acct(rw, sectors, &mddev->gendisk->part0); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 287 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 288 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 289 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 292 | /* mddev_suspend makes sure no new requests are submitted |
| 293 | * to the device, and that any requests that have been submitted |
| 294 | * are completely handled. |
| 295 | * Once ->stop is called and completes, the module will be completely |
| 296 | * unused. |
| 297 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 298 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 299 | { |
| 300 | BUG_ON(mddev->suspended); |
| 301 | mddev->suspended = 1; |
| 302 | synchronize_rcu(); |
| 303 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 304 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 305 | |
| 306 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 307 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 308 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 309 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 310 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 311 | { |
| 312 | mddev->suspended = 0; |
| 313 | wake_up(&mddev->sb_wait); |
| 314 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 315 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 316 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 317 | md_wakeup_thread(mddev->thread); |
| 318 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 319 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 320 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 321 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 322 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 323 | { |
| 324 | return mddev->suspended; |
| 325 | } |
| 326 | EXPORT_SYMBOL(mddev_congested); |
| 327 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 328 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 329 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 330 | */ |
| 331 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 332 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 333 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 334 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 335 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 336 | |
| 337 | rdev_dec_pending(rdev, mddev); |
| 338 | |
| 339 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 340 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 341 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 342 | } |
| 343 | bio_put(bio); |
| 344 | } |
| 345 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 346 | static void md_submit_flush_data(struct work_struct *ws); |
| 347 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 348 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 349 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 350 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 351 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 352 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 353 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 354 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 355 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 356 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 357 | if (rdev->raid_disk >= 0 && |
| 358 | !test_bit(Faulty, &rdev->flags)) { |
| 359 | /* Take two references, one is dropped |
| 360 | * when request finishes, one after |
| 361 | * we reclaim rcu_read_lock |
| 362 | */ |
| 363 | struct bio *bi; |
| 364 | atomic_inc(&rdev->nr_pending); |
| 365 | atomic_inc(&rdev->nr_pending); |
| 366 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 367 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 368 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 369 | bi->bi_private = rdev; |
| 370 | bi->bi_bdev = rdev->bdev; |
| 371 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 372 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 373 | rcu_read_lock(); |
| 374 | rdev_dec_pending(rdev, mddev); |
| 375 | } |
| 376 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 377 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 378 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 379 | } |
| 380 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 381 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 382 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 383 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 384 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 385 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 386 | if (bio->bi_iter.bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 387 | /* an empty barrier - all done */ |
| 388 | bio_endio(bio, 0); |
| 389 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 390 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 391 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 392 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 393 | |
| 394 | mddev->flush_bio = NULL; |
| 395 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 396 | } |
| 397 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 398 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 399 | { |
| 400 | spin_lock_irq(&mddev->write_lock); |
| 401 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 402 | !mddev->flush_bio, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 403 | mddev->write_lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 404 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 405 | spin_unlock_irq(&mddev->write_lock); |
| 406 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 407 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 408 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 409 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 410 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 411 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 412 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 413 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 414 | struct mddev *mddev = cb->data; |
| 415 | md_wakeup_thread(mddev->thread); |
| 416 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 417 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 418 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 419 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 420 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | { |
| 422 | atomic_inc(&mddev->active); |
| 423 | return mddev; |
| 424 | } |
| 425 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 426 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 427 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 428 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 430 | struct bio_set *bs = NULL; |
| 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 433 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 434 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 435 | mddev->ctime == 0 && !mddev->hold_active) { |
| 436 | /* Array is not configured at all, and not held active, |
| 437 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 438 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 439 | bs = mddev->bio_set; |
| 440 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 441 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 442 | /* We did a probe so need to clean up. Call |
| 443 | * queue_work inside the spinlock so that |
| 444 | * flush_workqueue() after mddev_find will |
| 445 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 446 | */ |
| 447 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 448 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 449 | } else |
| 450 | kfree(mddev); |
| 451 | } |
| 452 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 453 | if (bs) |
| 454 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 457 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 458 | { |
| 459 | mutex_init(&mddev->open_mutex); |
| 460 | mutex_init(&mddev->reconfig_mutex); |
| 461 | mutex_init(&mddev->bitmap_info.mutex); |
| 462 | INIT_LIST_HEAD(&mddev->disks); |
| 463 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 464 | init_timer(&mddev->safemode_timer); |
| 465 | atomic_set(&mddev->active, 1); |
| 466 | atomic_set(&mddev->openers, 0); |
| 467 | atomic_set(&mddev->active_io, 0); |
| 468 | spin_lock_init(&mddev->write_lock); |
| 469 | atomic_set(&mddev->flush_pending, 0); |
| 470 | init_waitqueue_head(&mddev->sb_wait); |
| 471 | init_waitqueue_head(&mddev->recovery_wait); |
| 472 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 473 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 474 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 475 | mddev->resync_min = 0; |
| 476 | mddev->resync_max = MaxSector; |
| 477 | mddev->level = LEVEL_NONE; |
| 478 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 479 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 480 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 481 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 483 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 485 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 486 | unit &= ~((1<<MdpMinorShift)-1); |
| 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | retry: |
| 489 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 491 | if (unit) { |
| 492 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 493 | if (mddev->unit == unit) { |
| 494 | mddev_get(mddev); |
| 495 | spin_unlock(&all_mddevs_lock); |
| 496 | kfree(new); |
| 497 | return mddev; |
| 498 | } |
| 499 | |
| 500 | if (new) { |
| 501 | list_add(&new->all_mddevs, &all_mddevs); |
| 502 | spin_unlock(&all_mddevs_lock); |
| 503 | new->hold_active = UNTIL_IOCTL; |
| 504 | return new; |
| 505 | } |
| 506 | } else if (new) { |
| 507 | /* find an unused unit number */ |
| 508 | static int next_minor = 512; |
| 509 | int start = next_minor; |
| 510 | int is_free = 0; |
| 511 | int dev = 0; |
| 512 | while (!is_free) { |
| 513 | dev = MKDEV(MD_MAJOR, next_minor); |
| 514 | next_minor++; |
| 515 | if (next_minor > MINORMASK) |
| 516 | next_minor = 0; |
| 517 | if (next_minor == start) { |
| 518 | /* Oh dear, all in use. */ |
| 519 | spin_unlock(&all_mddevs_lock); |
| 520 | kfree(new); |
| 521 | return NULL; |
| 522 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 523 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 524 | is_free = 1; |
| 525 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 526 | if (mddev->unit == dev) { |
| 527 | is_free = 0; |
| 528 | break; |
| 529 | } |
| 530 | } |
| 531 | new->unit = dev; |
| 532 | new->md_minor = MINOR(dev); |
| 533 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | list_add(&new->all_mddevs, &all_mddevs); |
| 535 | spin_unlock(&all_mddevs_lock); |
| 536 | return new; |
| 537 | } |
| 538 | spin_unlock(&all_mddevs_lock); |
| 539 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 540 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | if (!new) |
| 542 | return NULL; |
| 543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | new->unit = unit; |
| 545 | if (MAJOR(unit) == MD_MAJOR) |
| 546 | new->md_minor = MINOR(unit); |
| 547 | else |
| 548 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 549 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 550 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | goto retry; |
| 553 | } |
| 554 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 555 | static inline int __must_check mddev_lock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 557 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
| 559 | |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 560 | /* Sometimes we need to take the lock in a situation where |
| 561 | * failure due to interrupts is not acceptable. |
| 562 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 563 | static inline void mddev_lock_nointr(struct mddev *mddev) |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 564 | { |
| 565 | mutex_lock(&mddev->reconfig_mutex); |
| 566 | } |
| 567 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 568 | static inline int mddev_is_locked(struct mddev *mddev) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 569 | { |
| 570 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 571 | } |
| 572 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 573 | static inline int mddev_trylock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 575 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
| 577 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 578 | static struct attribute_group md_redundancy_group; |
| 579 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 580 | static void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 582 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 583 | /* These cannot be removed under reconfig_mutex as |
| 584 | * an access to the files will try to take reconfig_mutex |
| 585 | * while holding the file unremovable, which leads to |
| 586 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 587 | * So hold set sysfs_active while the remove in happeing, |
| 588 | * and anything else which might set ->to_remove or my |
| 589 | * otherwise change the sysfs namespace will fail with |
| 590 | * -EBUSY if sysfs_active is still set. |
| 591 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 592 | * test it under the same mutex to ensure its correct value |
| 593 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 594 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 595 | struct attribute_group *to_remove = mddev->to_remove; |
| 596 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 597 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 598 | mutex_unlock(&mddev->reconfig_mutex); |
| 599 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 600 | if (mddev->kobj.sd) { |
| 601 | if (to_remove != &md_redundancy_group) |
| 602 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 603 | if (mddev->pers == NULL || |
| 604 | mddev->pers->sync_request == NULL) { |
| 605 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 606 | if (mddev->sysfs_action) |
| 607 | sysfs_put(mddev->sysfs_action); |
| 608 | mddev->sysfs_action = NULL; |
| 609 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 610 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 611 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 612 | } else |
| 613 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 615 | /* As we've dropped the mutex we need a spinlock to |
| 616 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 617 | */ |
| 618 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 619 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 620 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | } |
| 622 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 623 | static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr) |
| 624 | { |
| 625 | struct md_rdev *rdev; |
| 626 | |
| 627 | rdev_for_each_rcu(rdev, mddev) |
| 628 | if (rdev->desc_nr == nr) |
| 629 | return rdev; |
| 630 | |
| 631 | return NULL; |
| 632 | } |
| 633 | |
| 634 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 636 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 638 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | if (rdev->bdev->bd_dev == dev) |
| 640 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 641 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | return NULL; |
| 643 | } |
| 644 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 645 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 646 | { |
| 647 | struct md_rdev *rdev; |
| 648 | |
| 649 | rdev_for_each_rcu(rdev, mddev) |
| 650 | if (rdev->bdev->bd_dev == dev) |
| 651 | return rdev; |
| 652 | |
| 653 | return NULL; |
| 654 | } |
| 655 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 656 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 657 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 658 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 659 | list_for_each_entry(pers, &pers_list, list) { |
| 660 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 661 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 662 | if (strcmp(pers->name, clevel)==0) |
| 663 | return pers; |
| 664 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 665 | return NULL; |
| 666 | } |
| 667 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 668 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 669 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 671 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 672 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } |
| 674 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 675 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 678 | if (!rdev->sb_page) { |
| 679 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 680 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 686 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | { |
| 688 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 689 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | rdev->sb_loaded = 0; |
| 691 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 692 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 693 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 695 | if (rdev->bb_page) { |
| 696 | put_page(rdev->bb_page); |
| 697 | rdev->bb_page = NULL; |
| 698 | } |
NeilBrown | 4fa2f32 | 2012-05-22 13:55:01 +1000 | [diff] [blame] | 699 | kfree(rdev->badblocks.page); |
| 700 | rdev->badblocks.page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 702 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 704 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 705 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 706 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 707 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 708 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 709 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 710 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 711 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 712 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 713 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 714 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 715 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 716 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 717 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 718 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 719 | } |
| 720 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 721 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 722 | sector_t sector, int size, struct page *page) |
| 723 | { |
| 724 | /* write first size bytes of page to sector of rdev |
| 725 | * Increment mddev->pending_writes before returning |
| 726 | * and decrement it on completion, waking up sb_wait |
| 727 | * if zero is reached. |
| 728 | * If an error occurred, call md_error |
| 729 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 730 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 731 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 732 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 733 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 734 | bio_add_page(bio, page, size, 0); |
| 735 | bio->bi_private = rdev; |
| 736 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 737 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 738 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 739 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 740 | } |
| 741 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 742 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 743 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 744 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 745 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 746 | } |
| 747 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 748 | 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] | 749 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 751 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | int ret; |
| 753 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 754 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 755 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 756 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 757 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 758 | else if (rdev->mddev->reshape_position != MaxSector && |
| 759 | (rdev->mddev->reshape_backwards == |
| 760 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 761 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 762 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 763 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | bio_add_page(bio, page, size, 0); |
Kent Overstreet | c170bbb | 2013-11-24 16:32:22 -0700 | [diff] [blame] | 765 | submit_bio_wait(rw, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
| 767 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 768 | bio_put(bio); |
| 769 | return ret; |
| 770 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 771 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 773 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | { |
| 775 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | if (rdev->sb_loaded) |
| 778 | return 0; |
| 779 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 780 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | goto fail; |
| 782 | rdev->sb_loaded = 1; |
| 783 | return 0; |
| 784 | |
| 785 | fail: |
| 786 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 787 | bdevname(rdev->bdev,b)); |
| 788 | return -EINVAL; |
| 789 | } |
| 790 | |
| 791 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 792 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 793 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 794 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 795 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 796 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 800 | { |
| 801 | int ret; |
| 802 | mdp_super_t *tmp1, *tmp2; |
| 803 | |
| 804 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 805 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 806 | |
| 807 | if (!tmp1 || !tmp2) { |
| 808 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 809 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | goto abort; |
| 811 | } |
| 812 | |
| 813 | *tmp1 = *sb1; |
| 814 | *tmp2 = *sb2; |
| 815 | |
| 816 | /* |
| 817 | * nr_disks is not constant |
| 818 | */ |
| 819 | tmp1->nr_disks = 0; |
| 820 | tmp2->nr_disks = 0; |
| 821 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 822 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 824 | kfree(tmp1); |
| 825 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | return ret; |
| 827 | } |
| 828 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 829 | static u32 md_csum_fold(u32 csum) |
| 830 | { |
| 831 | csum = (csum & 0xffff) + (csum >> 16); |
| 832 | return (csum & 0xffff) + (csum >> 16); |
| 833 | } |
| 834 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 835 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 837 | u64 newcsum = 0; |
| 838 | u32 *sb32 = (u32*)sb; |
| 839 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | unsigned int disk_csum, csum; |
| 841 | |
| 842 | disk_csum = sb->sb_csum; |
| 843 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 844 | |
| 845 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 846 | newcsum += sb32[i]; |
| 847 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 848 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 849 | #ifdef CONFIG_ALPHA |
| 850 | /* This used to use csum_partial, which was wrong for several |
| 851 | * reasons including that different results are returned on |
| 852 | * different architectures. It isn't critical that we get exactly |
| 853 | * the same return value as before (we always csum_fold before |
| 854 | * testing, and that removes any differences). However as we |
| 855 | * know that csum_partial always returned a 16bit value on |
| 856 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 857 | */ |
| 858 | sb->sb_csum = md_csum_fold(disk_csum); |
| 859 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 861 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | return csum; |
| 863 | } |
| 864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | /* |
| 866 | * Handle superblock details. |
| 867 | * We want to be able to handle multiple superblock formats |
| 868 | * so we have a common interface to them all, and an array of |
| 869 | * different handlers. |
| 870 | * We rely on user-space to write the initial superblock, and support |
| 871 | * reading and updating of superblocks. |
| 872 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 873 | * 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] | 874 | * loads and validates a superblock on dev. |
| 875 | * if refdev != NULL, compare superblocks on both devices |
| 876 | * Return: |
| 877 | * 0 - dev has a superblock that is compatible with refdev |
| 878 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 879 | * so dev should be used as the refdev in future |
| 880 | * -EINVAL superblock incompatible or invalid |
| 881 | * -othererror e.g. -EIO |
| 882 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 883 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | * Verify that dev is acceptable into mddev. |
| 885 | * The first time, mddev->raid_disks will be 0, and data from |
| 886 | * dev should be merged in. Subsequent calls check that dev |
| 887 | * is new enough. Return 0 or -EINVAL |
| 888 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 889 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | * Update the superblock for rdev with data in mddev |
| 891 | * This does not write to disc. |
| 892 | * |
| 893 | */ |
| 894 | |
| 895 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 896 | char *name; |
| 897 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 898 | int (*load_super)(struct md_rdev *rdev, |
| 899 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 900 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 901 | int (*validate_super)(struct mddev *mddev, |
| 902 | struct md_rdev *rdev); |
| 903 | void (*sync_super)(struct mddev *mddev, |
| 904 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 905 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 906 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 907 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 908 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | }; |
| 910 | |
| 911 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 912 | * Check that the given mddev has no bitmap. |
| 913 | * |
| 914 | * This function is called from the run method of all personalities that do not |
| 915 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 916 | * has a bitmap. Otherwise, it returns 0. |
| 917 | * |
| 918 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 919 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 920 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 921 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 922 | return 0; |
| 923 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 924 | mdname(mddev), mddev->pers->name); |
| 925 | return 1; |
| 926 | } |
| 927 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 928 | |
| 929 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 930 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 932 | 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] | 933 | { |
| 934 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 935 | mdp_super_t *sb; |
| 936 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
| 938 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 939 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | * it's at the end of the disk. |
| 941 | * |
| 942 | * It also happens to be a multiple of 4Kb. |
| 943 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 944 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 946 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | if (ret) return ret; |
| 948 | |
| 949 | ret = -EINVAL; |
| 950 | |
| 951 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 952 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
| 954 | if (sb->md_magic != MD_SB_MAGIC) { |
| 955 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 956 | b); |
| 957 | goto abort; |
| 958 | } |
| 959 | |
| 960 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 961 | sb->minor_version < 90 || |
| 962 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 964 | sb->major_version, sb->minor_version, |
| 965 | b); |
| 966 | goto abort; |
| 967 | } |
| 968 | |
| 969 | if (sb->raid_disks <= 0) |
| 970 | goto abort; |
| 971 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 972 | 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] | 973 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 974 | b); |
| 975 | goto abort; |
| 976 | } |
| 977 | |
| 978 | rdev->preferred_minor = sb->md_minor; |
| 979 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 980 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 981 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 982 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
| 984 | if (sb->level == LEVEL_MULTIPATH) |
| 985 | rdev->desc_nr = -1; |
| 986 | else |
| 987 | rdev->desc_nr = sb->this_disk.number; |
| 988 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 989 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 991 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 993 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | if (!uuid_equal(refsb, sb)) { |
| 995 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 996 | b, bdevname(refdev->bdev,b2)); |
| 997 | goto abort; |
| 998 | } |
| 999 | if (!sb_equal(refsb, sb)) { |
| 1000 | printk(KERN_WARNING "md: %s has same UUID" |
| 1001 | " but different superblock to %s\n", |
| 1002 | b, bdevname(refdev->bdev, b2)); |
| 1003 | goto abort; |
| 1004 | } |
| 1005 | ev1 = md_event(sb); |
| 1006 | ev2 = md_event(refsb); |
| 1007 | if (ev1 > ev2) |
| 1008 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1009 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | ret = 0; |
| 1011 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1012 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1013 | /* Limit to 4TB as metadata cannot record more than that. |
| 1014 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1015 | * record this size) |
| 1016 | */ |
| 1017 | if (rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1018 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1020 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1021 | /* "this cannot possibly happen" ... */ |
| 1022 | ret = -EINVAL; |
| 1023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | abort: |
| 1025 | return ret; |
| 1026 | } |
| 1027 | |
| 1028 | /* |
| 1029 | * validate_super for 0.90.0 |
| 1030 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1031 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | { |
| 1033 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1034 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1035 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1037 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1038 | clear_bit(Faulty, &rdev->flags); |
| 1039 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1040 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1041 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1042 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | if (mddev->raid_disks == 0) { |
| 1044 | mddev->major_version = 0; |
| 1045 | mddev->minor_version = sb->minor_version; |
| 1046 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1047 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1048 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | mddev->ctime = sb->ctime; |
| 1050 | mddev->utime = sb->utime; |
| 1051 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1052 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | mddev->layout = sb->layout; |
| 1054 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1055 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1056 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1057 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1058 | mddev->bitmap_info.space = 0; |
| 1059 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1060 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1061 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1062 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1064 | if (mddev->minor_version >= 91) { |
| 1065 | mddev->reshape_position = sb->reshape_position; |
| 1066 | mddev->delta_disks = sb->delta_disks; |
| 1067 | mddev->new_level = sb->new_level; |
| 1068 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1069 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1070 | if (mddev->delta_disks < 0) |
| 1071 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1072 | } else { |
| 1073 | mddev->reshape_position = MaxSector; |
| 1074 | mddev->delta_disks = 0; |
| 1075 | mddev->new_level = mddev->level; |
| 1076 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1077 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1081 | mddev->recovery_cp = MaxSector; |
| 1082 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1083 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | sb->events_lo == sb->cp_events_lo) { |
| 1085 | mddev->recovery_cp = sb->recovery_cp; |
| 1086 | } else |
| 1087 | mddev->recovery_cp = 0; |
| 1088 | } |
| 1089 | |
| 1090 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1091 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1092 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1093 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1094 | |
| 1095 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1096 | |
| 1097 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1098 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1099 | mddev->bitmap_info.offset = |
| 1100 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1101 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1102 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1103 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1104 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1105 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1106 | /* Insist on good event counter while assembling, except |
| 1107 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1109 | if (sb->disks[rdev->desc_nr].state & ( |
| 1110 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1111 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1112 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1113 | } else if (mddev->bitmap) { |
| 1114 | /* if adding to array with a bitmap, then we can accept an |
| 1115 | * older device ... but not too old. |
| 1116 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1117 | if (ev1 < mddev->bitmap->events_cleared) |
| 1118 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1119 | if (ev1 < mddev->events) |
| 1120 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1121 | } else { |
| 1122 | if (ev1 < mddev->events) |
| 1123 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1124 | return 0; |
| 1125 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | desc = sb->disks + rdev->desc_nr; |
| 1129 | |
| 1130 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1131 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1132 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1133 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1134 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1136 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1137 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1138 | /* active but not in sync implies recovery up to |
| 1139 | * reshape position. We don't know exactly where |
| 1140 | * that is, so set to zero for now */ |
| 1141 | if (mddev->minor_version >= 91) { |
| 1142 | rdev->recovery_offset = 0; |
| 1143 | rdev->raid_disk = desc->raid_disk; |
| 1144 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1146 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1147 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1148 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1149 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | /* |
| 1154 | * sync_super for 0.90.0 |
| 1155 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1156 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | { |
| 1158 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1159 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | /* make rdev->sb match mddev data.. |
| 1163 | * |
| 1164 | * 1/ zero out disks |
| 1165 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1166 | * 3/ any empty disks < next_spare become removed |
| 1167 | * |
| 1168 | * disks[0] gets initialised to REMOVED because |
| 1169 | * we cannot be sure from other fields if it has |
| 1170 | * been initialised or not. |
| 1171 | */ |
| 1172 | int i; |
| 1173 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1174 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1175 | rdev->sb_size = MD_SB_BYTES; |
| 1176 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1177 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | |
| 1179 | memset(sb, 0, sizeof(*sb)); |
| 1180 | |
| 1181 | sb->md_magic = MD_SB_MAGIC; |
| 1182 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | sb->patch_version = mddev->patch_version; |
| 1184 | sb->gvalid_words = 0; /* ignored */ |
| 1185 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1186 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1187 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1188 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1189 | |
| 1190 | sb->ctime = mddev->ctime; |
| 1191 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1192 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | sb->raid_disks = mddev->raid_disks; |
| 1194 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1195 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | sb->utime = mddev->utime; |
| 1197 | sb->state = 0; |
| 1198 | sb->events_hi = (mddev->events>>32); |
| 1199 | sb->events_lo = (u32)mddev->events; |
| 1200 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1201 | if (mddev->reshape_position == MaxSector) |
| 1202 | sb->minor_version = 90; |
| 1203 | else { |
| 1204 | sb->minor_version = 91; |
| 1205 | sb->reshape_position = mddev->reshape_position; |
| 1206 | sb->new_level = mddev->new_level; |
| 1207 | sb->delta_disks = mddev->delta_disks; |
| 1208 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1209 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1210 | } |
| 1211 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | if (mddev->in_sync) |
| 1213 | { |
| 1214 | sb->recovery_cp = mddev->recovery_cp; |
| 1215 | sb->cp_events_hi = (mddev->events>>32); |
| 1216 | sb->cp_events_lo = (u32)mddev->events; |
| 1217 | if (mddev->recovery_cp == MaxSector) |
| 1218 | sb->state = (1<< MD_SB_CLEAN); |
| 1219 | } else |
| 1220 | sb->recovery_cp = 0; |
| 1221 | |
| 1222 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1223 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1225 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1226 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1229 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1231 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1232 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1233 | |
| 1234 | if (rdev2->raid_disk >= 0 && |
| 1235 | sb->minor_version >= 91) |
| 1236 | /* we have nowhere to store the recovery_offset, |
| 1237 | * but if it is not below the reshape_position, |
| 1238 | * we can piggy-back on that. |
| 1239 | */ |
| 1240 | is_active = 1; |
| 1241 | if (rdev2->raid_disk < 0 || |
| 1242 | test_bit(Faulty, &rdev2->flags)) |
| 1243 | is_active = 0; |
| 1244 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1245 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1247 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1248 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | d = &sb->disks[rdev2->desc_nr]; |
| 1250 | nr_disks++; |
| 1251 | d->number = rdev2->desc_nr; |
| 1252 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1253 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1254 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | d->raid_disk = rdev2->raid_disk; |
| 1256 | else |
| 1257 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1258 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1260 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1262 | if (test_bit(In_sync, &rdev2->flags)) |
| 1263 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | active++; |
| 1265 | working++; |
| 1266 | } else { |
| 1267 | d->state = 0; |
| 1268 | spare++; |
| 1269 | working++; |
| 1270 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1271 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1272 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1275 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1276 | mdp_disk_t *d = &sb->disks[i]; |
| 1277 | if (d->state == 0 && d->number == 0) { |
| 1278 | d->number = i; |
| 1279 | d->raid_disk = i; |
| 1280 | d->state = (1<<MD_DISK_REMOVED); |
| 1281 | d->state |= (1<<MD_DISK_FAULTY); |
| 1282 | failed++; |
| 1283 | } |
| 1284 | } |
| 1285 | sb->nr_disks = nr_disks; |
| 1286 | sb->active_disks = active; |
| 1287 | sb->working_disks = working; |
| 1288 | sb->failed_disks = failed; |
| 1289 | sb->spare_disks = spare; |
| 1290 | |
| 1291 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1292 | sb->sb_csum = calc_sb_csum(sb); |
| 1293 | } |
| 1294 | |
| 1295 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1296 | * rdev_size_change for 0.90.0 |
| 1297 | */ |
| 1298 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1299 | 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] | 1300 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1301 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1302 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1303 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1304 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1305 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1306 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1307 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1308 | /* Limit to 4TB as metadata cannot record more than that. |
| 1309 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1310 | */ |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1311 | if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1312 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1313 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1314 | rdev->sb_page); |
| 1315 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1316 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1317 | } |
| 1318 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1319 | static int |
| 1320 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1321 | { |
| 1322 | /* non-zero offset changes not possible with v0.90 */ |
| 1323 | return new_offset == 0; |
| 1324 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1325 | |
| 1326 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | * version 1 superblock |
| 1328 | */ |
| 1329 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1330 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1332 | __le32 disk_csum; |
| 1333 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | unsigned long long newcsum; |
| 1335 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1336 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
| 1338 | disk_csum = sb->sb_csum; |
| 1339 | sb->sb_csum = 0; |
| 1340 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1341 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | newcsum += le32_to_cpu(*isuper++); |
| 1343 | |
| 1344 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1345 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | |
| 1347 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1348 | sb->sb_csum = disk_csum; |
| 1349 | return cpu_to_le32(csum); |
| 1350 | } |
| 1351 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1352 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1353 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1354 | 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] | 1355 | { |
| 1356 | struct mdp_superblock_1 *sb; |
| 1357 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1358 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1359 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1361 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
| 1363 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1364 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | * It is always aligned to a 4K boundary and |
| 1366 | * depeding on minor_version, it can be: |
| 1367 | * 0: At least 8K, but less than 12K, from end of device |
| 1368 | * 1: At start of device |
| 1369 | * 2: 4K from start of device. |
| 1370 | */ |
| 1371 | switch(minor_version) { |
| 1372 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1373 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1374 | sb_start -= 8*2; |
| 1375 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | break; |
| 1377 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1378 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | break; |
| 1380 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1381 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | break; |
| 1383 | default: |
| 1384 | return -EINVAL; |
| 1385 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1386 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1388 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1389 | * and it is safe to read 4k, so we do that |
| 1390 | */ |
| 1391 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | if (ret) return ret; |
| 1393 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1394 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
| 1396 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1397 | sb->major_version != cpu_to_le32(1) || |
| 1398 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1399 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1400 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | return -EINVAL; |
| 1402 | |
| 1403 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1404 | printk("md: invalid superblock checksum on %s\n", |
| 1405 | bdevname(rdev->bdev,b)); |
| 1406 | return -EINVAL; |
| 1407 | } |
| 1408 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1409 | printk("md: data_size too small on %s\n", |
| 1410 | bdevname(rdev->bdev,b)); |
| 1411 | return -EINVAL; |
| 1412 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1413 | if (sb->pad0 || |
| 1414 | sb->pad3[0] || |
| 1415 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1416 | /* Some padding is non-zero, might be a new feature */ |
| 1417 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | rdev->preferred_minor = 0xffff; |
| 1420 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1421 | rdev->new_data_offset = rdev->data_offset; |
| 1422 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1423 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1424 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1425 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1427 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1428 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1429 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1430 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1431 | |
| 1432 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1433 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1434 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1435 | if (minor_version |
| 1436 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1437 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1438 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1439 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1440 | rdev->desc_nr = -1; |
| 1441 | else |
| 1442 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1443 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1444 | if (!rdev->bb_page) { |
| 1445 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1446 | if (!rdev->bb_page) |
| 1447 | return -ENOMEM; |
| 1448 | } |
| 1449 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1450 | rdev->badblocks.count == 0) { |
| 1451 | /* need to load the bad block list. |
| 1452 | * Currently we limit it to one page. |
| 1453 | */ |
| 1454 | s32 offset; |
| 1455 | sector_t bb_sector; |
| 1456 | u64 *bbp; |
| 1457 | int i; |
| 1458 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1459 | if (sectors > (PAGE_SIZE / 512)) |
| 1460 | return -EINVAL; |
| 1461 | offset = le32_to_cpu(sb->bblog_offset); |
| 1462 | if (offset == 0) |
| 1463 | return -EINVAL; |
| 1464 | bb_sector = (long long)offset; |
| 1465 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1466 | rdev->bb_page, READ, true)) |
| 1467 | return -EIO; |
| 1468 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1469 | rdev->badblocks.shift = sb->bblog_shift; |
| 1470 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1471 | u64 bb = le64_to_cpu(*bbp); |
| 1472 | int count = bb & (0x3ff); |
| 1473 | u64 sector = bb >> 10; |
| 1474 | sector <<= sb->bblog_shift; |
| 1475 | count <<= sb->bblog_shift; |
| 1476 | if (bb + 1 == 0) |
| 1477 | break; |
| 1478 | if (md_set_badblocks(&rdev->badblocks, |
| 1479 | sector, count, 1) == 0) |
| 1480 | return -EINVAL; |
| 1481 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1482 | } else if (sb->bblog_offset != 0) |
| 1483 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1484 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1485 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1486 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1487 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1489 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | |
| 1491 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1492 | sb->level != refsb->level || |
| 1493 | sb->layout != refsb->layout || |
| 1494 | sb->chunksize != refsb->chunksize) { |
| 1495 | printk(KERN_WARNING "md: %s has strangely different" |
| 1496 | " superblock to %s\n", |
| 1497 | bdevname(rdev->bdev,b), |
| 1498 | bdevname(refdev->bdev,b2)); |
| 1499 | return -EINVAL; |
| 1500 | } |
| 1501 | ev1 = le64_to_cpu(sb->events); |
| 1502 | ev2 = le64_to_cpu(refsb->events); |
| 1503 | |
| 1504 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1505 | ret = 1; |
| 1506 | else |
| 1507 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1509 | if (minor_version) { |
| 1510 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1511 | sectors -= rdev->data_offset; |
| 1512 | } else |
| 1513 | sectors = rdev->sb_start; |
| 1514 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1516 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1517 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1520 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1522 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1523 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1525 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1526 | clear_bit(Faulty, &rdev->flags); |
| 1527 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1528 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1529 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | if (mddev->raid_disks == 0) { |
| 1532 | mddev->major_version = 1; |
| 1533 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1534 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1535 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1537 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1538 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1539 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | mddev->layout = le32_to_cpu(sb->layout); |
| 1541 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1542 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1543 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1544 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1545 | mddev->bitmap_info.space = 0; |
| 1546 | /* Default location for bitmap is 1K after superblock |
| 1547 | * using 3K - total of 4K |
| 1548 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1549 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1550 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1551 | mddev->reshape_backwards = 0; |
| 1552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1554 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1555 | |
| 1556 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1557 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1558 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1559 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1560 | mddev->bitmap_info.offset = |
| 1561 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1562 | /* Metadata doesn't record how much space is available. |
| 1563 | * For 1.0, we assume we can use up to the superblock |
| 1564 | * if before, else to 4K beyond superblock. |
| 1565 | * For others, assume no change is possible. |
| 1566 | */ |
| 1567 | if (mddev->minor_version > 0) |
| 1568 | mddev->bitmap_info.space = 0; |
| 1569 | else if (mddev->bitmap_info.offset > 0) |
| 1570 | mddev->bitmap_info.space = |
| 1571 | 8 - mddev->bitmap_info.offset; |
| 1572 | else |
| 1573 | mddev->bitmap_info.space = |
| 1574 | -mddev->bitmap_info.offset; |
| 1575 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1576 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1577 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1578 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1579 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1580 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1581 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1582 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1583 | if (mddev->delta_disks < 0 || |
| 1584 | (mddev->delta_disks == 0 && |
| 1585 | (le32_to_cpu(sb->feature_map) |
| 1586 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1587 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1588 | } else { |
| 1589 | mddev->reshape_position = MaxSector; |
| 1590 | mddev->delta_disks = 0; |
| 1591 | mddev->new_level = mddev->level; |
| 1592 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1593 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1594 | } |
| 1595 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1596 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1597 | /* Insist of good event counter while assembling, except for |
| 1598 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1600 | if (rdev->desc_nr >= 0 && |
| 1601 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1602 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1603 | if (ev1 < mddev->events) |
| 1604 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1605 | } else if (mddev->bitmap) { |
| 1606 | /* If adding to array with a bitmap, then we can accept an |
| 1607 | * older device, but not too old. |
| 1608 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1609 | if (ev1 < mddev->bitmap->events_cleared) |
| 1610 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1611 | if (ev1 < mddev->events) |
| 1612 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1613 | } else { |
| 1614 | if (ev1 < mddev->events) |
| 1615 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1616 | return 0; |
| 1617 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1619 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1620 | if (rdev->desc_nr < 0 || |
| 1621 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1622 | role = 0xffff; |
| 1623 | rdev->desc_nr = -1; |
| 1624 | } else |
| 1625 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | switch(role) { |
| 1627 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | break; |
| 1629 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1630 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | break; |
| 1632 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1633 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1634 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1635 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1636 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1637 | if (!(le32_to_cpu(sb->feature_map) & |
| 1638 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1639 | rdev->saved_raid_disk = -1; |
| 1640 | } else |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1641 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | rdev->raid_disk = role; |
| 1643 | break; |
| 1644 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1645 | if (sb->devflags & WriteMostly1) |
| 1646 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1647 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1648 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1649 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1650 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1651 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | return 0; |
| 1653 | } |
| 1654 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1655 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | { |
| 1657 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1658 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | int max_dev, i; |
| 1660 | /* make rdev->sb match mddev and rdev data. */ |
| 1661 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1662 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | |
| 1664 | sb->feature_map = 0; |
| 1665 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1666 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1668 | |
| 1669 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1670 | sb->events = cpu_to_le64(mddev->events); |
| 1671 | if (mddev->in_sync) |
| 1672 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1673 | else |
| 1674 | sb->resync_offset = cpu_to_le64(0); |
| 1675 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1676 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1677 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1678 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1679 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1680 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1681 | sb->level = cpu_to_le32(mddev->level); |
| 1682 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1683 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1684 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1685 | sb->devflags |= WriteMostly1; |
| 1686 | else |
| 1687 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1688 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1689 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1690 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1691 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1692 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1693 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1694 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1695 | |
| 1696 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1697 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1698 | sb->feature_map |= |
| 1699 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1700 | sb->recovery_offset = |
| 1701 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1702 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 1703 | sb->feature_map |= |
| 1704 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1705 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1706 | if (test_bit(Replacement, &rdev->flags)) |
| 1707 | sb->feature_map |= |
| 1708 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1709 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1710 | if (mddev->reshape_position != MaxSector) { |
| 1711 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1712 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1713 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1714 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1715 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1716 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1717 | if (mddev->delta_disks == 0 && |
| 1718 | mddev->reshape_backwards) |
| 1719 | sb->feature_map |
| 1720 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1721 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1722 | sb->feature_map |
| 1723 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1724 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1725 | - rdev->data_offset)); |
| 1726 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1727 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1728 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1729 | if (rdev->badblocks.count == 0) |
| 1730 | /* Nothing to do for bad blocks*/ ; |
| 1731 | else if (sb->bblog_offset == 0) |
| 1732 | /* Cannot record bad blocks on this device */ |
| 1733 | md_error(mddev, rdev); |
| 1734 | else { |
| 1735 | struct badblocks *bb = &rdev->badblocks; |
| 1736 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1737 | u64 *p = bb->page; |
| 1738 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1739 | if (bb->changed) { |
| 1740 | unsigned seq; |
| 1741 | |
| 1742 | retry: |
| 1743 | seq = read_seqbegin(&bb->lock); |
| 1744 | |
| 1745 | memset(bbp, 0xff, PAGE_SIZE); |
| 1746 | |
| 1747 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1748 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1749 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1750 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1751 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1752 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1753 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1754 | if (read_seqretry(&bb->lock, seq)) |
| 1755 | goto retry; |
| 1756 | |
| 1757 | bb->sector = (rdev->sb_start + |
| 1758 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1759 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1760 | } |
| 1761 | } |
| 1762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1764 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | if (rdev2->desc_nr+1 > max_dev) |
| 1766 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1767 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1768 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1769 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1770 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1771 | rdev->sb_size = max_dev * 2 + 256; |
| 1772 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1773 | if (rdev->sb_size & bmask) |
| 1774 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1775 | } else |
| 1776 | max_dev = le32_to_cpu(sb->max_dev); |
| 1777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | for (i=0; i<max_dev;i++) |
| 1779 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1780 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1781 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1783 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1785 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1787 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1788 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | else |
| 1790 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1791 | } |
| 1792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1794 | } |
| 1795 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1796 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1797 | 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] | 1798 | { |
| 1799 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1800 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1801 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1802 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1803 | if (rdev->data_offset != rdev->new_data_offset) |
| 1804 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1805 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1806 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1807 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1808 | max_sectors -= rdev->data_offset; |
| 1809 | if (!num_sectors || num_sectors > max_sectors) |
| 1810 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1811 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1812 | /* minor version 0 with bitmap we can't move */ |
| 1813 | return 0; |
| 1814 | } else { |
| 1815 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1816 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1817 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1818 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1819 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1820 | if (!num_sectors || num_sectors > max_sectors) |
| 1821 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1822 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1823 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1824 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1825 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1826 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1827 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1828 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1829 | rdev->sb_page); |
| 1830 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1831 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1832 | |
| 1833 | } |
| 1834 | |
| 1835 | static int |
| 1836 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1837 | unsigned long long new_offset) |
| 1838 | { |
| 1839 | /* All necessary checks on new >= old have been done */ |
| 1840 | struct bitmap *bitmap; |
| 1841 | if (new_offset >= rdev->data_offset) |
| 1842 | return 1; |
| 1843 | |
| 1844 | /* with 1.0 metadata, there is no metadata to tread on |
| 1845 | * so we can always move back */ |
| 1846 | if (rdev->mddev->minor_version == 0) |
| 1847 | return 1; |
| 1848 | |
| 1849 | /* otherwise we must be sure not to step on |
| 1850 | * any metadata, so stay: |
| 1851 | * 36K beyond start of superblock |
| 1852 | * beyond end of badblocks |
| 1853 | * beyond write-intent bitmap |
| 1854 | */ |
| 1855 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1856 | return 0; |
| 1857 | bitmap = rdev->mddev->bitmap; |
| 1858 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1859 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1860 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1861 | return 0; |
| 1862 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1863 | return 0; |
| 1864 | |
| 1865 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1866 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1868 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | [0] = { |
| 1870 | .name = "0.90.0", |
| 1871 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1872 | .load_super = super_90_load, |
| 1873 | .validate_super = super_90_validate, |
| 1874 | .sync_super = super_90_sync, |
| 1875 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1876 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | }, |
| 1878 | [1] = { |
| 1879 | .name = "md-1", |
| 1880 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1881 | .load_super = super_1_load, |
| 1882 | .validate_super = super_1_validate, |
| 1883 | .sync_super = super_1_sync, |
| 1884 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1885 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | }, |
| 1887 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1889 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1890 | { |
| 1891 | if (mddev->sync_super) { |
| 1892 | mddev->sync_super(mddev, rdev); |
| 1893 | return; |
| 1894 | } |
| 1895 | |
| 1896 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1897 | |
| 1898 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1899 | } |
| 1900 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1901 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1903 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1905 | rcu_read_lock(); |
| 1906 | rdev_for_each_rcu(rdev, mddev1) |
| 1907 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1908 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1909 | rdev2->bdev->bd_contains) { |
| 1910 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1911 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1912 | } |
| 1913 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | return 0; |
| 1915 | } |
| 1916 | |
| 1917 | static LIST_HEAD(pending_raid_disks); |
| 1918 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1919 | /* |
| 1920 | * Try to register data integrity profile for an mddev |
| 1921 | * |
| 1922 | * This is called when an array is started and after a disk has been kicked |
| 1923 | * from the array. It only succeeds if all working and active component devices |
| 1924 | * are integrity capable with matching profiles. |
| 1925 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1926 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1927 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1928 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1929 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1930 | if (list_empty(&mddev->disks)) |
| 1931 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1932 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1933 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1934 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1935 | /* skip spares and non-functional disks */ |
| 1936 | if (test_bit(Faulty, &rdev->flags)) |
| 1937 | continue; |
| 1938 | if (rdev->raid_disk < 0) |
| 1939 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1940 | if (!reference) { |
| 1941 | /* Use the first rdev as the reference */ |
| 1942 | reference = rdev; |
| 1943 | continue; |
| 1944 | } |
| 1945 | /* does this rdev's profile match the reference profile? */ |
| 1946 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1947 | rdev->bdev->bd_disk) < 0) |
| 1948 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1949 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1950 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1951 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1952 | /* |
| 1953 | * All component devices are integrity capable and have matching |
| 1954 | * profiles, register the common profile for the md device. |
| 1955 | */ |
| 1956 | if (blk_integrity_register(mddev->gendisk, |
| 1957 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1958 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1959 | mdname(mddev)); |
| 1960 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1961 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1962 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 1963 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 1964 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 1965 | mdname(mddev)); |
| 1966 | return -EINVAL; |
| 1967 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1968 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1969 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1970 | EXPORT_SYMBOL(md_integrity_register); |
| 1971 | |
| 1972 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1973 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1974 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 1975 | struct blk_integrity *bi_rdev; |
| 1976 | struct blk_integrity *bi_mddev; |
| 1977 | |
| 1978 | if (!mddev->gendisk) |
| 1979 | return; |
| 1980 | |
| 1981 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1982 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1983 | |
| 1984 | if (!bi_mddev) /* nothing to do */ |
| 1985 | return; |
| 1986 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1987 | return; |
| 1988 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1989 | rdev->bdev->bd_disk) >= 0) |
| 1990 | return; |
| 1991 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1992 | blk_integrity_unregister(mddev->gendisk); |
| 1993 | } |
| 1994 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1995 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1996 | 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] | 1997 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1998 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1999 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2000 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2001 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2003 | /* prevent duplicates */ |
| 2004 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2005 | return -EEXIST; |
| 2006 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2007 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2008 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2009 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2010 | if (mddev->pers) { |
| 2011 | /* Cannot change size, so fail |
| 2012 | * If mddev->level <= 0, then we don't care |
| 2013 | * about aligning sizes (e.g. linear) |
| 2014 | */ |
| 2015 | if (mddev->level > 0) |
| 2016 | return -ENOSPC; |
| 2017 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2018 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2019 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | |
| 2021 | /* Verify rdev->desc_nr is unique. |
| 2022 | * If it is -1, assign a free number, else |
| 2023 | * check number is not in use |
| 2024 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2025 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | if (rdev->desc_nr < 0) { |
| 2027 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2028 | if (mddev->pers) |
| 2029 | choice = mddev->raid_disks; |
| 2030 | while (find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | choice++; |
| 2032 | rdev->desc_nr = choice; |
| 2033 | } else { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2034 | if (find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
| 2035 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2037 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2039 | rcu_read_unlock(); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2040 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2041 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2042 | mdname(mddev), mddev->max_disks); |
| 2043 | return -EBUSY; |
| 2044 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2045 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2046 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2047 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2050 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2051 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2052 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2053 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2054 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2055 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2056 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2057 | /* failure here is OK */; |
| 2058 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2059 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2060 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2061 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2062 | |
| 2063 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2064 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2067 | |
| 2068 | fail: |
| 2069 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2070 | b, mdname(mddev)); |
| 2071 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | } |
| 2073 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2074 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2075 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2076 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2077 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2078 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2081 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | { |
| 2083 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2084 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2085 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2086 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2088 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2089 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2090 | sysfs_put(rdev->sysfs_state); |
| 2091 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2092 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2093 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2094 | * writing to 'remove' to "dev/state". We also need |
| 2095 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2096 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2097 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2098 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2099 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2100 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | /* |
| 2104 | * prevent the device from being mounted, repartitioned or |
| 2105 | * otherwise reused by a RAID array (or any other kernel |
| 2106 | * subsystem), by bd_claiming the device. |
| 2107 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2108 | 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] | 2109 | { |
| 2110 | int err = 0; |
| 2111 | struct block_device *bdev; |
| 2112 | char b[BDEVNAME_SIZE]; |
| 2113 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2114 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2115 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | if (IS_ERR(bdev)) { |
| 2117 | printk(KERN_ERR "md: could not open %s.\n", |
| 2118 | __bdevname(dev, b)); |
| 2119 | return PTR_ERR(bdev); |
| 2120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | rdev->bdev = bdev; |
| 2122 | return err; |
| 2123 | } |
| 2124 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2125 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | { |
| 2127 | struct block_device *bdev = rdev->bdev; |
| 2128 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2129 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | void md_autodetect_dev(dev_t dev); |
| 2133 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2134 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | { |
| 2136 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2139 | bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2140 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2142 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2143 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | #endif |
| 2145 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2146 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2149 | static void kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | { |
| 2151 | unbind_rdev_from_array(rdev); |
| 2152 | export_rdev(rdev); |
| 2153 | } |
| 2154 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2155 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2157 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2159 | while (!list_empty(&mddev->disks)) { |
| 2160 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2161 | same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | kick_rdev_from_array(rdev); |
| 2163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | mddev->raid_disks = 0; |
| 2165 | mddev->major_version = 0; |
| 2166 | } |
| 2167 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2168 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2170 | /* Update each superblock (in-memory image), but |
| 2171 | * if we are allowed to, skip spares which already |
| 2172 | * have the right event counter, or have one earlier |
| 2173 | * (which would mean they aren't being marked as dirty |
| 2174 | * with the rest of the array) |
| 2175 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2176 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2177 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2178 | if (rdev->sb_events == mddev->events || |
| 2179 | (nospares && |
| 2180 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2181 | rdev->sb_events+1 == mddev->events)) { |
| 2182 | /* Don't update this superblock */ |
| 2183 | rdev->sb_loaded = 2; |
| 2184 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2185 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2186 | rdev->sb_loaded = 1; |
| 2187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | } |
| 2189 | } |
| 2190 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2191 | static void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2193 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2194 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2195 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2196 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2198 | if (mddev->ro) { |
| 2199 | if (force_change) |
| 2200 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2201 | return; |
| 2202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2204 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2205 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2206 | if (rdev->raid_disk >= 0 && |
| 2207 | mddev->delta_disks >= 0 && |
| 2208 | !test_bit(In_sync, &rdev->flags) && |
| 2209 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2210 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2211 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2212 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2213 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2214 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2215 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2216 | if (!mddev->external) { |
NeilBrown | d97a41dc | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2217 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2218 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2219 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2220 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2221 | md_ack_all_badblocks(&rdev->badblocks); |
| 2222 | md_error(mddev, rdev); |
| 2223 | } |
| 2224 | clear_bit(Blocked, &rdev->flags); |
| 2225 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2226 | wake_up(&rdev->blocked_wait); |
| 2227 | } |
| 2228 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2229 | wake_up(&mddev->sb_wait); |
| 2230 | return; |
| 2231 | } |
| 2232 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2233 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2234 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2235 | mddev->utime = get_seconds(); |
| 2236 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2237 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2238 | force_change = 1; |
| 2239 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2240 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2241 | * though if events isn't the right even/odd, we will have to do |
| 2242 | * spares after all |
| 2243 | */ |
| 2244 | nospares = 1; |
| 2245 | if (force_change) |
| 2246 | nospares = 0; |
| 2247 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2248 | /* If the array is degraded, then skipping spares is both |
| 2249 | * dangerous and fairly pointless. |
| 2250 | * Dangerous because a device that was removed from the array |
| 2251 | * might have a event_count that still looks up-to-date, |
| 2252 | * so it can be re-added without a resync. |
| 2253 | * Pointless because if there are any spares to skip, |
| 2254 | * then a recovery will happen and soon that array won't |
| 2255 | * be degraded any more and the spare can go back to sleep then. |
| 2256 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2257 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2258 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2259 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2260 | |
| 2261 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2262 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2263 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2264 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2265 | && mddev->can_decrease_events |
| 2266 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2267 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2268 | mddev->can_decrease_events = 0; |
| 2269 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2270 | /* otherwise we have to go forward and ... */ |
| 2271 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2272 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2275 | /* |
| 2276 | * This 64-bit counter should never wrap. |
| 2277 | * Either we are in around ~1 trillion A.C., assuming |
| 2278 | * 1 reboot per second, or we have a bug... |
| 2279 | */ |
| 2280 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2281 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2282 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2283 | if (rdev->badblocks.changed) |
| 2284 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2285 | if (test_bit(Faulty, &rdev->flags)) |
| 2286 | set_bit(FaultRecorded, &rdev->flags); |
| 2287 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2288 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2289 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2290 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2292 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2293 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2295 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2296 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2298 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2299 | if (rdev->sb_loaded != 1) |
| 2300 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2302 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2303 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2304 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2305 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2306 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2307 | bdevname(rdev->bdev, b), |
| 2308 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2309 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2310 | if (rdev->badblocks.size) { |
| 2311 | md_super_write(mddev, rdev, |
| 2312 | rdev->badblocks.sector, |
| 2313 | rdev->badblocks.size << 9, |
| 2314 | rdev->bb_page); |
| 2315 | rdev->badblocks.size = 0; |
| 2316 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2317 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2318 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2319 | pr_debug("md: %s (skipping faulty)\n", |
| 2320 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2321 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2322 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | /* only need to write one superblock... */ |
| 2324 | break; |
| 2325 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2326 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2327 | /* 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] | 2328 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2329 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2330 | if (mddev->in_sync != sync_req || |
| 2331 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2332 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2333 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2334 | goto repeat; |
| 2335 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2336 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2337 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2338 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2339 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2340 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2341 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2342 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2343 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2344 | clear_bit(Blocked, &rdev->flags); |
| 2345 | |
| 2346 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2347 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2348 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2349 | wake_up(&rdev->blocked_wait); |
| 2350 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | } |
| 2352 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2353 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2354 | * We want to accept with case. For this we use cmd_match. |
| 2355 | */ |
| 2356 | static int cmd_match(const char *cmd, const char *str) |
| 2357 | { |
| 2358 | /* See if cmd, written into a sysfs file, matches |
| 2359 | * str. They must either be the same, or cmd can |
| 2360 | * have a trailing newline |
| 2361 | */ |
| 2362 | while (*cmd && *str && *cmd == *str) { |
| 2363 | cmd++; |
| 2364 | str++; |
| 2365 | } |
| 2366 | if (*cmd == '\n') |
| 2367 | cmd++; |
| 2368 | if (*str || *cmd) |
| 2369 | return 0; |
| 2370 | return 1; |
| 2371 | } |
| 2372 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2373 | struct rdev_sysfs_entry { |
| 2374 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2375 | ssize_t (*show)(struct md_rdev *, char *); |
| 2376 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2377 | }; |
| 2378 | |
| 2379 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2380 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2381 | { |
| 2382 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2383 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2384 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2385 | if (test_bit(Faulty, &rdev->flags) || |
| 2386 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2387 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2388 | sep = ","; |
| 2389 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2390 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2391 | len += sprintf(page+len, "%sin_sync",sep); |
| 2392 | sep = ","; |
| 2393 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2394 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2395 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2396 | sep = ","; |
| 2397 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2398 | if (test_bit(Blocked, &rdev->flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2399 | (rdev->badblocks.unacked_exist |
| 2400 | && !test_bit(Faulty, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2401 | len += sprintf(page+len, "%sblocked", sep); |
| 2402 | sep = ","; |
| 2403 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2404 | if (!test_bit(Faulty, &rdev->flags) && |
| 2405 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2406 | len += sprintf(page+len, "%sspare", sep); |
| 2407 | sep = ","; |
| 2408 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2409 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 2410 | len += sprintf(page+len, "%swrite_error", sep); |
| 2411 | sep = ","; |
| 2412 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2413 | if (test_bit(WantReplacement, &rdev->flags)) { |
| 2414 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2415 | sep = ","; |
| 2416 | } |
| 2417 | if (test_bit(Replacement, &rdev->flags)) { |
| 2418 | len += sprintf(page+len, "%sreplacement", sep); |
| 2419 | sep = ","; |
| 2420 | } |
| 2421 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2422 | return len+sprintf(page+len, "\n"); |
| 2423 | } |
| 2424 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2425 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2426 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2427 | { |
| 2428 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2429 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2430 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2431 | * writemostly - sets write_mostly |
| 2432 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2433 | * blocked - sets the Blocked flags |
| 2434 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2435 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2436 | * -insync - clear Insync for a device with a slot assigned, |
| 2437 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2438 | * write_error - sets WriteErrorSeen |
| 2439 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2440 | */ |
| 2441 | int err = -EINVAL; |
| 2442 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2443 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2444 | if (test_bit(Faulty, &rdev->flags)) |
| 2445 | err = 0; |
| 2446 | else |
| 2447 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2448 | } else if (cmd_match(buf, "remove")) { |
| 2449 | if (rdev->raid_disk >= 0) |
| 2450 | err = -EBUSY; |
| 2451 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2452 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2453 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2454 | if (mddev->pers) |
| 2455 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2456 | md_new_event(mddev); |
| 2457 | err = 0; |
| 2458 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2459 | } else if (cmd_match(buf, "writemostly")) { |
| 2460 | set_bit(WriteMostly, &rdev->flags); |
| 2461 | err = 0; |
| 2462 | } else if (cmd_match(buf, "-writemostly")) { |
| 2463 | clear_bit(WriteMostly, &rdev->flags); |
| 2464 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2465 | } else if (cmd_match(buf, "blocked")) { |
| 2466 | set_bit(Blocked, &rdev->flags); |
| 2467 | err = 0; |
| 2468 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2469 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2470 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2471 | /* metadata handler doesn't understand badblocks, |
| 2472 | * so we need to fail the device |
| 2473 | */ |
| 2474 | md_error(rdev->mddev, rdev); |
| 2475 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2476 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2477 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2478 | wake_up(&rdev->blocked_wait); |
| 2479 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2480 | md_wakeup_thread(rdev->mddev->thread); |
| 2481 | |
| 2482 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2483 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2484 | set_bit(In_sync, &rdev->flags); |
| 2485 | err = 0; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2486 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 2487 | if (rdev->mddev->pers == NULL) { |
| 2488 | clear_bit(In_sync, &rdev->flags); |
| 2489 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2490 | rdev->raid_disk = -1; |
| 2491 | err = 0; |
| 2492 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2493 | } else if (cmd_match(buf, "write_error")) { |
| 2494 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2495 | err = 0; |
| 2496 | } else if (cmd_match(buf, "-write_error")) { |
| 2497 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2498 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2499 | } else if (cmd_match(buf, "want_replacement")) { |
| 2500 | /* Any non-spare device that is not a replacement can |
| 2501 | * become want_replacement at any time, but we then need to |
| 2502 | * check if recovery is needed. |
| 2503 | */ |
| 2504 | if (rdev->raid_disk >= 0 && |
| 2505 | !test_bit(Replacement, &rdev->flags)) |
| 2506 | set_bit(WantReplacement, &rdev->flags); |
| 2507 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2508 | md_wakeup_thread(rdev->mddev->thread); |
| 2509 | err = 0; |
| 2510 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2511 | /* Clearing 'want_replacement' is always allowed. |
| 2512 | * Once replacements starts it is too late though. |
| 2513 | */ |
| 2514 | err = 0; |
| 2515 | clear_bit(WantReplacement, &rdev->flags); |
| 2516 | } else if (cmd_match(buf, "replacement")) { |
| 2517 | /* Can only set a device as a replacement when array has not |
| 2518 | * yet been started. Once running, replacement is automatic |
| 2519 | * from spares, or by assigning 'slot'. |
| 2520 | */ |
| 2521 | if (rdev->mddev->pers) |
| 2522 | err = -EBUSY; |
| 2523 | else { |
| 2524 | set_bit(Replacement, &rdev->flags); |
| 2525 | err = 0; |
| 2526 | } |
| 2527 | } else if (cmd_match(buf, "-replacement")) { |
| 2528 | /* Similarly, can only clear Replacement before start */ |
| 2529 | if (rdev->mddev->pers) |
| 2530 | err = -EBUSY; |
| 2531 | else { |
| 2532 | clear_bit(Replacement, &rdev->flags); |
| 2533 | err = 0; |
| 2534 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2535 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2536 | if (!err) |
| 2537 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2538 | return err ? err : len; |
| 2539 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2540 | static struct rdev_sysfs_entry rdev_state = |
| 2541 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2542 | |
| 2543 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2544 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2545 | { |
| 2546 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2547 | } |
| 2548 | |
| 2549 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2550 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2551 | { |
| 2552 | char *e; |
| 2553 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2554 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2555 | atomic_set(&rdev->corrected_errors, n); |
| 2556 | return len; |
| 2557 | } |
| 2558 | return -EINVAL; |
| 2559 | } |
| 2560 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2561 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2562 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2563 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2564 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2565 | { |
| 2566 | if (rdev->raid_disk < 0) |
| 2567 | return sprintf(page, "none\n"); |
| 2568 | else |
| 2569 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2570 | } |
| 2571 | |
| 2572 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2573 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2574 | { |
| 2575 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2576 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2577 | int slot = simple_strtoul(buf, &e, 10); |
| 2578 | if (strncmp(buf, "none", 4)==0) |
| 2579 | slot = -1; |
| 2580 | else if (e==buf || (*e && *e!= '\n')) |
| 2581 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2582 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2583 | /* Setting 'slot' on an active array requires also |
| 2584 | * updating the 'rd%d' link, and communicating |
| 2585 | * with the personality with ->hot_*_disk. |
| 2586 | * For now we only support removing |
| 2587 | * failed/spare devices. This normally happens automatically, |
| 2588 | * but not when the metadata is externally managed. |
| 2589 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2590 | if (rdev->raid_disk == -1) |
| 2591 | return -EEXIST; |
| 2592 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2593 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2594 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2595 | clear_bit(Blocked, &rdev->flags); |
| 2596 | remove_and_add_spares(rdev->mddev, rdev); |
| 2597 | if (rdev->raid_disk >= 0) |
| 2598 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2599 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2600 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2601 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2602 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2603 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2604 | */ |
| 2605 | |
| 2606 | if (rdev->raid_disk != -1) |
| 2607 | return -EBUSY; |
| 2608 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2609 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2610 | return -EBUSY; |
| 2611 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2612 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2613 | return -EINVAL; |
| 2614 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2615 | if (slot >= rdev->mddev->raid_disks && |
| 2616 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2617 | return -ENOSPC; |
| 2618 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2619 | rdev->raid_disk = slot; |
| 2620 | if (test_bit(In_sync, &rdev->flags)) |
| 2621 | rdev->saved_raid_disk = slot; |
| 2622 | else |
| 2623 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2624 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 2625 | clear_bit(Bitmap_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2626 | err = rdev->mddev->pers-> |
| 2627 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2628 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2629 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2630 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2631 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2632 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2633 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2634 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2635 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2636 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2637 | if (slot >= rdev->mddev->raid_disks && |
| 2638 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2639 | return -ENOSPC; |
| 2640 | rdev->raid_disk = slot; |
| 2641 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2642 | clear_bit(Faulty, &rdev->flags); |
| 2643 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2644 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2645 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2646 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2647 | return len; |
| 2648 | } |
| 2649 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2650 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2651 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2652 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2653 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2654 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2655 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2656 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2660 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2661 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2662 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2663 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2664 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2665 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2666 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2667 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2668 | /* Must set offset before size, so overlap checks |
| 2669 | * can be sane */ |
| 2670 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2671 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2672 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2673 | return len; |
| 2674 | } |
| 2675 | |
| 2676 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2677 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2678 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2679 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2680 | { |
| 2681 | return sprintf(page, "%llu\n", |
| 2682 | (unsigned long long)rdev->new_data_offset); |
| 2683 | } |
| 2684 | |
| 2685 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2686 | const char *buf, size_t len) |
| 2687 | { |
| 2688 | unsigned long long new_offset; |
| 2689 | struct mddev *mddev = rdev->mddev; |
| 2690 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2691 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2692 | return -EINVAL; |
| 2693 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 2694 | if (mddev->sync_thread || |
| 2695 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2696 | return -EBUSY; |
| 2697 | if (new_offset == rdev->data_offset) |
| 2698 | /* reset is always permitted */ |
| 2699 | ; |
| 2700 | else if (new_offset > rdev->data_offset) { |
| 2701 | /* must not push array size beyond rdev_sectors */ |
| 2702 | if (new_offset - rdev->data_offset |
| 2703 | + mddev->dev_sectors > rdev->sectors) |
| 2704 | return -E2BIG; |
| 2705 | } |
| 2706 | /* Metadata worries about other space details. */ |
| 2707 | |
| 2708 | /* decreasing the offset is inconsistent with a backwards |
| 2709 | * reshape. |
| 2710 | */ |
| 2711 | if (new_offset < rdev->data_offset && |
| 2712 | mddev->reshape_backwards) |
| 2713 | return -EINVAL; |
| 2714 | /* Increasing offset is inconsistent with forwards |
| 2715 | * reshape. reshape_direction should be set to |
| 2716 | * 'backwards' first. |
| 2717 | */ |
| 2718 | if (new_offset > rdev->data_offset && |
| 2719 | !mddev->reshape_backwards) |
| 2720 | return -EINVAL; |
| 2721 | |
| 2722 | if (mddev->pers && mddev->persistent && |
| 2723 | !super_types[mddev->major_version] |
| 2724 | .allow_new_offset(rdev, new_offset)) |
| 2725 | return -E2BIG; |
| 2726 | rdev->new_data_offset = new_offset; |
| 2727 | if (new_offset > rdev->data_offset) |
| 2728 | mddev->reshape_backwards = 1; |
| 2729 | else if (new_offset < rdev->data_offset) |
| 2730 | mddev->reshape_backwards = 0; |
| 2731 | |
| 2732 | return len; |
| 2733 | } |
| 2734 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2735 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2736 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2737 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2738 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2739 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2740 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2741 | } |
| 2742 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2743 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2744 | { |
| 2745 | /* check if two start/length pairs overlap */ |
| 2746 | if (s1+l1 <= s2) |
| 2747 | return 0; |
| 2748 | if (s2+l2 <= s1) |
| 2749 | return 0; |
| 2750 | return 1; |
| 2751 | } |
| 2752 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2753 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2754 | { |
| 2755 | unsigned long long blocks; |
| 2756 | sector_t new; |
| 2757 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2758 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2759 | return -EINVAL; |
| 2760 | |
| 2761 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2762 | return -EINVAL; /* sector conversion overflow */ |
| 2763 | |
| 2764 | new = blocks * 2; |
| 2765 | if (new != blocks * 2) |
| 2766 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2767 | |
| 2768 | *sectors = new; |
| 2769 | return 0; |
| 2770 | } |
| 2771 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2772 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2773 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2774 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2775 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2776 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2777 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2778 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2779 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2780 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2781 | if (rdev->data_offset != rdev->new_data_offset) |
| 2782 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2783 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2784 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2785 | sectors = super_types[my_mddev->major_version]. |
| 2786 | rdev_size_change(rdev, sectors); |
| 2787 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2788 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2789 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2790 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2791 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 2792 | if (!my_mddev->pers->resize) |
| 2793 | /* Cannot change size for RAID0 or Linear etc */ |
| 2794 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2795 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2796 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2797 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2798 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2799 | rdev->sectors = sectors; |
| 2800 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2801 | /* Need to check that all other rdevs with the same |
| 2802 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 2803 | * the rdev lists safely. |
| 2804 | * This check does not provide a hard guarantee, it |
| 2805 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2806 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2807 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2808 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2809 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2810 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2811 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2812 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2813 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2814 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2815 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2816 | if (rdev->bdev == rdev2->bdev && |
| 2817 | rdev != rdev2 && |
| 2818 | overlaps(rdev->data_offset, rdev->sectors, |
| 2819 | rdev2->data_offset, |
| 2820 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2821 | overlap = 1; |
| 2822 | break; |
| 2823 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2824 | if (overlap) { |
| 2825 | mddev_put(mddev); |
| 2826 | break; |
| 2827 | } |
| 2828 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2829 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2830 | if (overlap) { |
| 2831 | /* Someone else could have slipped in a size |
| 2832 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2833 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2834 | * safe, and trust userspace not to race with |
| 2835 | * itself |
| 2836 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2837 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2838 | return -EBUSY; |
| 2839 | } |
| 2840 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2841 | return len; |
| 2842 | } |
| 2843 | |
| 2844 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2845 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2846 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2847 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2848 | { |
| 2849 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2850 | |
| 2851 | if (test_bit(In_sync, &rdev->flags) || |
| 2852 | recovery_start == MaxSector) |
| 2853 | return sprintf(page, "none\n"); |
| 2854 | |
| 2855 | return sprintf(page, "%llu\n", recovery_start); |
| 2856 | } |
| 2857 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2858 | 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] | 2859 | { |
| 2860 | unsigned long long recovery_start; |
| 2861 | |
| 2862 | if (cmd_match(buf, "none")) |
| 2863 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2864 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2865 | return -EINVAL; |
| 2866 | |
| 2867 | if (rdev->mddev->pers && |
| 2868 | rdev->raid_disk >= 0) |
| 2869 | return -EBUSY; |
| 2870 | |
| 2871 | rdev->recovery_offset = recovery_start; |
| 2872 | if (recovery_start == MaxSector) |
| 2873 | set_bit(In_sync, &rdev->flags); |
| 2874 | else |
| 2875 | clear_bit(In_sync, &rdev->flags); |
| 2876 | return len; |
| 2877 | } |
| 2878 | |
| 2879 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2880 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2881 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2882 | static ssize_t |
| 2883 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 2884 | static ssize_t |
| 2885 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 2886 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2887 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2888 | { |
| 2889 | return badblocks_show(&rdev->badblocks, page, 0); |
| 2890 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2891 | 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] | 2892 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2893 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 2894 | /* Maybe that ack was all we needed */ |
| 2895 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 2896 | wake_up(&rdev->blocked_wait); |
| 2897 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2898 | } |
| 2899 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 2900 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 2901 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2902 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2903 | { |
| 2904 | return badblocks_show(&rdev->badblocks, page, 1); |
| 2905 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2906 | 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] | 2907 | { |
| 2908 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 2909 | } |
| 2910 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 2911 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 2912 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2913 | static struct attribute *rdev_default_attrs[] = { |
| 2914 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2915 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2916 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2917 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2918 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2919 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2920 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2921 | &rdev_bad_blocks.attr, |
| 2922 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2923 | NULL, |
| 2924 | }; |
| 2925 | static ssize_t |
| 2926 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2927 | { |
| 2928 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2929 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2930 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2931 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2932 | |
| 2933 | if (!entry->show) |
| 2934 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2935 | |
| 2936 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2937 | if (!rv) { |
| 2938 | if (rdev->mddev == NULL) |
| 2939 | rv = -EBUSY; |
| 2940 | else |
| 2941 | rv = entry->show(rdev, page); |
| 2942 | mddev_unlock(mddev); |
| 2943 | } |
| 2944 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | static ssize_t |
| 2948 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2949 | const char *page, size_t length) |
| 2950 | { |
| 2951 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2952 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2953 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2954 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2955 | |
| 2956 | if (!entry->store) |
| 2957 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2958 | if (!capable(CAP_SYS_ADMIN)) |
| 2959 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2960 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2961 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2962 | if (rdev->mddev == NULL) |
| 2963 | rv = -EBUSY; |
| 2964 | else |
| 2965 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2966 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2967 | } |
| 2968 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | static void rdev_free(struct kobject *ko) |
| 2972 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2973 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2974 | kfree(rdev); |
| 2975 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2976 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2977 | .show = rdev_attr_show, |
| 2978 | .store = rdev_attr_store, |
| 2979 | }; |
| 2980 | static struct kobj_type rdev_ktype = { |
| 2981 | .release = rdev_free, |
| 2982 | .sysfs_ops = &rdev_sysfs_ops, |
| 2983 | .default_attrs = rdev_default_attrs, |
| 2984 | }; |
| 2985 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2986 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2987 | { |
| 2988 | rdev->desc_nr = -1; |
| 2989 | rdev->saved_raid_disk = -1; |
| 2990 | rdev->raid_disk = -1; |
| 2991 | rdev->flags = 0; |
| 2992 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2993 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2994 | rdev->sb_events = 0; |
| 2995 | rdev->last_read_error.tv_sec = 0; |
| 2996 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2997 | rdev->sb_loaded = 0; |
| 2998 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2999 | atomic_set(&rdev->nr_pending, 0); |
| 3000 | atomic_set(&rdev->read_errors, 0); |
| 3001 | atomic_set(&rdev->corrected_errors, 0); |
| 3002 | |
| 3003 | INIT_LIST_HEAD(&rdev->same_set); |
| 3004 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3005 | |
| 3006 | /* Add space to store bad block list. |
| 3007 | * This reserves the space even on arrays where it cannot |
| 3008 | * be used - I wonder if that matters |
| 3009 | */ |
| 3010 | rdev->badblocks.count = 0; |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 3011 | rdev->badblocks.shift = -1; /* disabled until explicitly enabled */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3012 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3013 | seqlock_init(&rdev->badblocks.lock); |
| 3014 | if (rdev->badblocks.page == NULL) |
| 3015 | return -ENOMEM; |
| 3016 | |
| 3017 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3018 | } |
| 3019 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | /* |
| 3021 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3022 | * |
| 3023 | * mark the device faulty if: |
| 3024 | * |
| 3025 | * - the device is nonexistent (zero size) |
| 3026 | * - the device has no valid superblock |
| 3027 | * |
| 3028 | * a faulty rdev _never_ has rdev->sb set. |
| 3029 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3030 | 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] | 3031 | { |
| 3032 | char b[BDEVNAME_SIZE]; |
| 3033 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3034 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | sector_t size; |
| 3036 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3037 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | if (!rdev) { |
| 3039 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3040 | return ERR_PTR(-ENOMEM); |
| 3041 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3043 | err = md_rdev_init(rdev); |
| 3044 | if (err) |
| 3045 | goto abort_free; |
| 3046 | err = alloc_disk_sb(rdev); |
| 3047 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | goto abort_free; |
| 3049 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3050 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | if (err) |
| 3052 | goto abort_free; |
| 3053 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3054 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3055 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3056 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | if (!size) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3058 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3060 | bdevname(rdev->bdev,b)); |
| 3061 | err = -EINVAL; |
| 3062 | goto abort_free; |
| 3063 | } |
| 3064 | |
| 3065 | if (super_format >= 0) { |
| 3066 | err = super_types[super_format]. |
| 3067 | load_super(rdev, NULL, super_minor); |
| 3068 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3069 | printk(KERN_WARNING |
| 3070 | "md: %s does not have a valid v%d.%d " |
| 3071 | "superblock, not importing!\n", |
| 3072 | bdevname(rdev->bdev,b), |
| 3073 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | goto abort_free; |
| 3075 | } |
| 3076 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3077 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | "md: could not read %s's sb, not importing!\n", |
| 3079 | bdevname(rdev->bdev,b)); |
| 3080 | goto abort_free; |
| 3081 | } |
| 3082 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3083 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | return rdev; |
| 3085 | |
| 3086 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3087 | if (rdev->bdev) |
| 3088 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3089 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | kfree(rdev); |
| 3091 | return ERR_PTR(err); |
| 3092 | } |
| 3093 | |
| 3094 | /* |
| 3095 | * Check a full RAID array for plausibility |
| 3096 | */ |
| 3097 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3098 | static void analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3099 | { |
| 3100 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3101 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3102 | char b[BDEVNAME_SIZE]; |
| 3103 | |
| 3104 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3105 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | switch (super_types[mddev->major_version]. |
| 3107 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3108 | case 1: |
| 3109 | freshest = rdev; |
| 3110 | break; |
| 3111 | case 0: |
| 3112 | break; |
| 3113 | default: |
| 3114 | printk( KERN_ERR \ |
| 3115 | "md: fatal superblock inconsistency in %s" |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3116 | " -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | bdevname(rdev->bdev,b)); |
| 3118 | kick_rdev_from_array(rdev); |
| 3119 | } |
| 3120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | super_types[mddev->major_version]. |
| 3122 | validate_super(mddev, freshest); |
| 3123 | |
| 3124 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3125 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3126 | if (mddev->max_disks && |
| 3127 | (rdev->desc_nr >= mddev->max_disks || |
| 3128 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3129 | printk(KERN_WARNING |
| 3130 | "md: %s: %s: only %d devices permitted\n", |
| 3131 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3132 | mddev->max_disks); |
| 3133 | kick_rdev_from_array(rdev); |
| 3134 | continue; |
| 3135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3136 | if (rdev != freshest) |
| 3137 | if (super_types[mddev->major_version]. |
| 3138 | validate_super(mddev, rdev)) { |
| 3139 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3140 | " from array!\n", |
| 3141 | bdevname(rdev->bdev,b)); |
| 3142 | kick_rdev_from_array(rdev); |
| 3143 | continue; |
| 3144 | } |
| 3145 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3146 | rdev->desc_nr = i++; |
| 3147 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3148 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3149 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3150 | rdev->raid_disk = -1; |
| 3151 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | } |
| 3153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | } |
| 3155 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3156 | /* Read a fixed-point number. |
| 3157 | * Numbers in sysfs attributes should be in "standard" units where |
| 3158 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3159 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3160 | * milliseconds or jiffies. |
| 3161 | * This function takes a decimal number with a possible fractional |
| 3162 | * component, and produces an integer which is the result of |
| 3163 | * multiplying that number by 10^'scale'. |
| 3164 | * all without any floating-point arithmetic. |
| 3165 | */ |
| 3166 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3167 | { |
| 3168 | unsigned long result = 0; |
| 3169 | long decimals = -1; |
| 3170 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3171 | if (*cp == '.') |
| 3172 | decimals = 0; |
| 3173 | else if (decimals < scale) { |
| 3174 | unsigned int value; |
| 3175 | value = *cp - '0'; |
| 3176 | result = result * 10 + value; |
| 3177 | if (decimals >= 0) |
| 3178 | decimals++; |
| 3179 | } |
| 3180 | cp++; |
| 3181 | } |
| 3182 | if (*cp == '\n') |
| 3183 | cp++; |
| 3184 | if (*cp) |
| 3185 | return -EINVAL; |
| 3186 | if (decimals < 0) |
| 3187 | decimals = 0; |
| 3188 | while (decimals < scale) { |
| 3189 | result *= 10; |
| 3190 | decimals ++; |
| 3191 | } |
| 3192 | *res = result; |
| 3193 | return 0; |
| 3194 | } |
| 3195 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3196 | static void md_safemode_timeout(unsigned long data); |
| 3197 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3198 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3199 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3200 | { |
| 3201 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3202 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3203 | } |
| 3204 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3205 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3206 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3207 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3208 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3209 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3210 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3211 | if (msec == 0) |
| 3212 | mddev->safemode_delay = 0; |
| 3213 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3214 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3215 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3216 | if (mddev->safemode_delay == 0) |
| 3217 | mddev->safemode_delay = 1; |
NeilBrown | 275c51c | 2013-08-08 09:20:32 +1000 | [diff] [blame] | 3218 | if (mddev->safemode_delay < old_delay || old_delay == 0) |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3219 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3220 | } |
| 3221 | return len; |
| 3222 | } |
| 3223 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3224 | __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] | 3225 | |
| 3226 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3227 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3228 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3229 | struct md_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3230 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3231 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3232 | else if (mddev->clevel[0]) |
| 3233 | return sprintf(page, "%s\n", mddev->clevel); |
| 3234 | else if (mddev->level != LEVEL_NONE) |
| 3235 | return sprintf(page, "%d\n", mddev->level); |
| 3236 | else |
| 3237 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3238 | } |
| 3239 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3240 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3241 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3242 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3243 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3244 | ssize_t rv = len; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3245 | struct md_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3246 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3247 | void *priv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3248 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3249 | |
| 3250 | if (mddev->pers == NULL) { |
| 3251 | if (len == 0) |
| 3252 | return 0; |
| 3253 | if (len >= sizeof(mddev->clevel)) |
| 3254 | return -ENOSPC; |
| 3255 | strncpy(mddev->clevel, buf, len); |
| 3256 | if (mddev->clevel[len-1] == '\n') |
| 3257 | len--; |
| 3258 | mddev->clevel[len] = 0; |
| 3259 | mddev->level = LEVEL_NONE; |
| 3260 | return rv; |
| 3261 | } |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3262 | if (mddev->ro) |
| 3263 | return -EROFS; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3264 | |
| 3265 | /* request to change the personality. Need to ensure: |
| 3266 | * - array is not engaged in resync/recovery/reshape |
| 3267 | * - old personality can be suspended |
| 3268 | * - new personality will access other array. |
| 3269 | */ |
| 3270 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3271 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3272 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3273 | mddev->reshape_position != MaxSector || |
| 3274 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3275 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3276 | |
| 3277 | if (!mddev->pers->quiesce) { |
| 3278 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3279 | mdname(mddev), mddev->pers->name); |
| 3280 | return -EINVAL; |
| 3281 | } |
| 3282 | |
| 3283 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3284 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3285 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3286 | strncpy(clevel, buf, len); |
| 3287 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3288 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3289 | clevel[len] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3290 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3291 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3292 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3293 | if (request_module("md-%s", clevel) != 0) |
| 3294 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3295 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3296 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3297 | if (!pers || !try_module_get(pers->owner)) { |
| 3298 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3299 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3300 | return -EINVAL; |
| 3301 | } |
| 3302 | spin_unlock(&pers_lock); |
| 3303 | |
| 3304 | if (pers == mddev->pers) { |
| 3305 | /* Nothing to do! */ |
| 3306 | module_put(pers->owner); |
| 3307 | return rv; |
| 3308 | } |
| 3309 | if (!pers->takeover) { |
| 3310 | module_put(pers->owner); |
| 3311 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3312 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3313 | return -EINVAL; |
| 3314 | } |
| 3315 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3316 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3317 | rdev->new_raid_disk = rdev->raid_disk; |
| 3318 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3319 | /* ->takeover must set new_* and/or delta_disks |
| 3320 | * if it succeeds, and may set them when it fails. |
| 3321 | */ |
| 3322 | priv = pers->takeover(mddev); |
| 3323 | if (IS_ERR(priv)) { |
| 3324 | mddev->new_level = mddev->level; |
| 3325 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3326 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3327 | mddev->raid_disks -= mddev->delta_disks; |
| 3328 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3329 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3330 | module_put(pers->owner); |
| 3331 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3332 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3333 | return PTR_ERR(priv); |
| 3334 | } |
| 3335 | |
| 3336 | /* Looks like we have a winner */ |
| 3337 | mddev_suspend(mddev); |
| 3338 | mddev->pers->stop(mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3339 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3340 | if (mddev->pers->sync_request == NULL && |
| 3341 | pers->sync_request != NULL) { |
| 3342 | /* need to add the md_redundancy_group */ |
| 3343 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3344 | printk(KERN_WARNING |
| 3345 | "md: cannot register extra attributes for %s\n", |
| 3346 | mdname(mddev)); |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 3347 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3348 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3349 | if (mddev->pers->sync_request != NULL && |
| 3350 | pers->sync_request == NULL) { |
| 3351 | /* need to remove the md_redundancy_group */ |
| 3352 | if (mddev->to_remove == NULL) |
| 3353 | mddev->to_remove = &md_redundancy_group; |
| 3354 | } |
| 3355 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3356 | if (mddev->pers->sync_request == NULL && |
| 3357 | mddev->external) { |
| 3358 | /* We are converting from a no-redundancy array |
| 3359 | * to a redundancy array and metadata is managed |
| 3360 | * externally so we need to be sure that writes |
| 3361 | * won't block due to a need to transition |
| 3362 | * clean->dirty |
| 3363 | * until external management is started. |
| 3364 | */ |
| 3365 | mddev->in_sync = 0; |
| 3366 | mddev->safemode_delay = 0; |
| 3367 | mddev->safemode = 0; |
| 3368 | } |
| 3369 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3370 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3371 | if (rdev->raid_disk < 0) |
| 3372 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3373 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3374 | rdev->new_raid_disk = -1; |
| 3375 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3376 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3377 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3378 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3379 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3380 | if (rdev->raid_disk < 0) |
| 3381 | continue; |
| 3382 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3383 | continue; |
| 3384 | rdev->raid_disk = rdev->new_raid_disk; |
| 3385 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3386 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3387 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3388 | if (sysfs_link_rdev(mddev, rdev)) |
| 3389 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3390 | " for %s after level change\n", |
| 3391 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3392 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3393 | } |
| 3394 | |
| 3395 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3396 | mddev->pers = pers; |
| 3397 | mddev->private = priv; |
| 3398 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3399 | mddev->level = mddev->new_level; |
| 3400 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3401 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3402 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3403 | mddev->reshape_backwards = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3404 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3405 | if (mddev->pers->sync_request == NULL) { |
| 3406 | /* this is now an array without redundancy, so |
| 3407 | * it must always be in_sync |
| 3408 | */ |
| 3409 | mddev->in_sync = 1; |
| 3410 | del_timer_sync(&mddev->safemode_timer); |
| 3411 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 3412 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3413 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3414 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3415 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 3416 | if (!mddev->thread) |
| 3417 | md_update_sb(mddev, 1); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3418 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3419 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3420 | return rv; |
| 3421 | } |
| 3422 | |
| 3423 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3424 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3425 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3426 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3427 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3428 | { |
| 3429 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3430 | if (mddev->reshape_position != MaxSector && |
| 3431 | mddev->layout != mddev->new_layout) |
| 3432 | return sprintf(page, "%d (%d)\n", |
| 3433 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3434 | return sprintf(page, "%d\n", mddev->layout); |
| 3435 | } |
| 3436 | |
| 3437 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3438 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3439 | { |
| 3440 | char *e; |
| 3441 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3442 | |
| 3443 | if (!*buf || (*e && *e != '\n')) |
| 3444 | return -EINVAL; |
| 3445 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3446 | if (mddev->pers) { |
| 3447 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3448 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3449 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3450 | if (mddev->ro) |
| 3451 | return -EROFS; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3452 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3453 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3454 | if (err) { |
| 3455 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3456 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3457 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3458 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3459 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3460 | if (mddev->reshape_position == MaxSector) |
| 3461 | mddev->layout = n; |
| 3462 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3463 | return len; |
| 3464 | } |
| 3465 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3466 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3467 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3468 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3469 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3470 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3471 | if (mddev->raid_disks == 0) |
| 3472 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3473 | if (mddev->reshape_position != MaxSector && |
| 3474 | mddev->delta_disks != 0) |
| 3475 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3476 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3477 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3478 | } |
| 3479 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3480 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3481 | |
| 3482 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3483 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3484 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3485 | char *e; |
| 3486 | int rv = 0; |
| 3487 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3488 | |
| 3489 | if (!*buf || (*e && *e != '\n')) |
| 3490 | return -EINVAL; |
| 3491 | |
| 3492 | if (mddev->pers) |
| 3493 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3494 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3495 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3496 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3497 | |
| 3498 | rdev_for_each(rdev, mddev) { |
| 3499 | if (olddisks < n && |
| 3500 | rdev->data_offset < rdev->new_data_offset) |
| 3501 | return -EINVAL; |
| 3502 | if (olddisks > n && |
| 3503 | rdev->data_offset > rdev->new_data_offset) |
| 3504 | return -EINVAL; |
| 3505 | } |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3506 | mddev->delta_disks = n - olddisks; |
| 3507 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3508 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3509 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3510 | mddev->raid_disks = n; |
| 3511 | return rv ? rv : len; |
| 3512 | } |
| 3513 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3514 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3515 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3516 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3517 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3518 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3519 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3520 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3521 | return sprintf(page, "%d (%d)\n", |
| 3522 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3523 | mddev->chunk_sectors << 9); |
| 3524 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3528 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3529 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3530 | char *e; |
| 3531 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3532 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3533 | if (!*buf || (*e && *e != '\n')) |
| 3534 | return -EINVAL; |
| 3535 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3536 | if (mddev->pers) { |
| 3537 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3538 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3539 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3540 | if (mddev->ro) |
| 3541 | return -EROFS; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3542 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3543 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3544 | if (err) { |
| 3545 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3546 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3547 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3548 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3549 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3550 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3551 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3552 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3553 | return len; |
| 3554 | } |
| 3555 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3556 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3557 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3558 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3559 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3560 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3561 | if (mddev->recovery_cp == MaxSector) |
| 3562 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3563 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3564 | } |
| 3565 | |
| 3566 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3567 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3568 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3569 | char *e; |
| 3570 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3571 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3572 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3573 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3574 | if (cmd_match(buf, "none")) |
| 3575 | n = MaxSector; |
| 3576 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3577 | return -EINVAL; |
| 3578 | |
| 3579 | mddev->recovery_cp = n; |
NeilBrown | db07d85 | 2012-10-11 14:22:17 +1100 | [diff] [blame] | 3580 | if (mddev->pers) |
| 3581 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3582 | return len; |
| 3583 | } |
| 3584 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3585 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3586 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3587 | /* |
| 3588 | * The array state can be: |
| 3589 | * |
| 3590 | * clear |
| 3591 | * No devices, no size, no level |
| 3592 | * Equivalent to STOP_ARRAY ioctl |
| 3593 | * inactive |
| 3594 | * May have some settings, but array is not active |
| 3595 | * all IO results in error |
| 3596 | * When written, doesn't tear down array, but just stops it |
| 3597 | * suspended (not supported yet) |
| 3598 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3599 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3600 | * readonly |
| 3601 | * no resync can happen. no superblocks get written. |
| 3602 | * write requests fail |
| 3603 | * read-auto |
| 3604 | * like readonly, but behaves like 'clean' on a write request. |
| 3605 | * |
| 3606 | * clean - no pending writes, but otherwise active. |
| 3607 | * When written to inactive array, starts without resync |
| 3608 | * If a write request arrives then |
| 3609 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3610 | * if not known, block and switch to write-pending |
| 3611 | * If written to an active array that has pending writes, then fails. |
| 3612 | * active |
| 3613 | * fully active: IO and resync can be happening. |
| 3614 | * When written to inactive array, starts with resync |
| 3615 | * |
| 3616 | * write-pending |
| 3617 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3618 | * |
| 3619 | * active-idle |
| 3620 | * like active, but no writes have been seen for a while (100msec). |
| 3621 | * |
| 3622 | */ |
| 3623 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3624 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3625 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3626 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3627 | "write-pending", "active-idle", NULL }; |
| 3628 | |
| 3629 | static int match_word(const char *word, char **list) |
| 3630 | { |
| 3631 | int n; |
| 3632 | for (n=0; list[n]; n++) |
| 3633 | if (cmd_match(word, list[n])) |
| 3634 | break; |
| 3635 | return n; |
| 3636 | } |
| 3637 | |
| 3638 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3639 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3640 | { |
| 3641 | enum array_state st = inactive; |
| 3642 | |
| 3643 | if (mddev->pers) |
| 3644 | switch(mddev->ro) { |
| 3645 | case 1: |
| 3646 | st = readonly; |
| 3647 | break; |
| 3648 | case 2: |
| 3649 | st = read_auto; |
| 3650 | break; |
| 3651 | case 0: |
| 3652 | if (mddev->in_sync) |
| 3653 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3654 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3655 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3656 | else if (mddev->safemode) |
| 3657 | st = active_idle; |
| 3658 | else |
| 3659 | st = active; |
| 3660 | } |
| 3661 | else { |
| 3662 | if (list_empty(&mddev->disks) && |
| 3663 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3664 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3665 | st = clear; |
| 3666 | else |
| 3667 | st = inactive; |
| 3668 | } |
| 3669 | return sprintf(page, "%s\n", array_states[st]); |
| 3670 | } |
| 3671 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3672 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 3673 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 3674 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3675 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3676 | |
| 3677 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3678 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3679 | { |
| 3680 | int err = -EINVAL; |
| 3681 | enum array_state st = match_word(buf, array_states); |
| 3682 | switch(st) { |
| 3683 | case bad_word: |
| 3684 | break; |
| 3685 | case clear: |
| 3686 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3687 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3688 | break; |
| 3689 | case inactive: |
| 3690 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3691 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3692 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3693 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3694 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3695 | break; |
| 3696 | case suspended: |
| 3697 | break; /* not supported yet */ |
| 3698 | case readonly: |
| 3699 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3700 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3701 | else { |
| 3702 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3703 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3704 | err = do_md_run(mddev); |
| 3705 | } |
| 3706 | break; |
| 3707 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3708 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3709 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3710 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3711 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3712 | err = restart_array(mddev); |
| 3713 | if (err == 0) { |
| 3714 | mddev->ro = 2; |
| 3715 | set_disk_ro(mddev->gendisk, 0); |
| 3716 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3717 | } else { |
| 3718 | mddev->ro = 2; |
| 3719 | err = do_md_run(mddev); |
| 3720 | } |
| 3721 | break; |
| 3722 | case clean: |
| 3723 | if (mddev->pers) { |
| 3724 | restart_array(mddev); |
| 3725 | spin_lock_irq(&mddev->write_lock); |
| 3726 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3727 | if (mddev->in_sync == 0) { |
| 3728 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3729 | if (mddev->safemode == 1) |
| 3730 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3731 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3732 | } |
| 3733 | err = 0; |
| 3734 | } else |
| 3735 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3736 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3737 | } else |
| 3738 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3739 | break; |
| 3740 | case active: |
| 3741 | if (mddev->pers) { |
| 3742 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3743 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3744 | wake_up(&mddev->sb_wait); |
| 3745 | err = 0; |
| 3746 | } else { |
| 3747 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3748 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3749 | err = do_md_run(mddev); |
| 3750 | } |
| 3751 | break; |
| 3752 | case write_pending: |
| 3753 | case active_idle: |
| 3754 | /* these cannot be set */ |
| 3755 | break; |
| 3756 | } |
| 3757 | if (err) |
| 3758 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3759 | else { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 3760 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3761 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3762 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3763 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3764 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3765 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3766 | static struct md_sysfs_entry md_array_state = |
| 3767 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3768 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3769 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3770 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3771 | return sprintf(page, "%d\n", |
| 3772 | atomic_read(&mddev->max_corr_read_errors)); |
| 3773 | } |
| 3774 | |
| 3775 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3776 | 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] | 3777 | { |
| 3778 | char *e; |
| 3779 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3780 | |
| 3781 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3782 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3783 | return len; |
| 3784 | } |
| 3785 | return -EINVAL; |
| 3786 | } |
| 3787 | |
| 3788 | static struct md_sysfs_entry max_corr_read_errors = |
| 3789 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3790 | max_corrected_read_errors_store); |
| 3791 | |
| 3792 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3793 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3794 | { |
| 3795 | return -EINVAL; |
| 3796 | } |
| 3797 | |
| 3798 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3799 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3800 | { |
| 3801 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3802 | /* The new device is added to the array. |
| 3803 | * If the array has a persistent superblock, we read the |
| 3804 | * superblock to initialise info and check validity. |
| 3805 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3806 | * which mainly checks size. |
| 3807 | */ |
| 3808 | char *e; |
| 3809 | int major = simple_strtoul(buf, &e, 10); |
| 3810 | int minor; |
| 3811 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3812 | struct md_rdev *rdev; |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3813 | int err; |
| 3814 | |
| 3815 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3816 | return -EINVAL; |
| 3817 | minor = simple_strtoul(e+1, &e, 10); |
| 3818 | if (*e && *e != '\n') |
| 3819 | return -EINVAL; |
| 3820 | dev = MKDEV(major, minor); |
| 3821 | if (major != MAJOR(dev) || |
| 3822 | minor != MINOR(dev)) |
| 3823 | return -EOVERFLOW; |
| 3824 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3825 | if (mddev->persistent) { |
| 3826 | rdev = md_import_device(dev, mddev->major_version, |
| 3827 | mddev->minor_version); |
| 3828 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3829 | struct md_rdev *rdev0 |
| 3830 | = list_entry(mddev->disks.next, |
| 3831 | struct md_rdev, same_set); |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3832 | err = super_types[mddev->major_version] |
| 3833 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3834 | if (err < 0) |
| 3835 | goto out; |
| 3836 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3837 | } else if (mddev->external) |
| 3838 | rdev = md_import_device(dev, -2, -1); |
| 3839 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3840 | rdev = md_import_device(dev, -1, -1); |
| 3841 | |
| 3842 | if (IS_ERR(rdev)) |
| 3843 | return PTR_ERR(rdev); |
| 3844 | err = bind_rdev_to_array(rdev, mddev); |
| 3845 | out: |
| 3846 | if (err) |
| 3847 | export_rdev(rdev); |
| 3848 | return err ? err : len; |
| 3849 | } |
| 3850 | |
| 3851 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3852 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3853 | |
| 3854 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3855 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3856 | { |
| 3857 | char *end; |
| 3858 | unsigned long chunk, end_chunk; |
| 3859 | |
| 3860 | if (!mddev->bitmap) |
| 3861 | goto out; |
| 3862 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3863 | while (*buf) { |
| 3864 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3865 | if (buf == end) break; |
| 3866 | if (*end == '-') { /* range */ |
| 3867 | buf = end + 1; |
| 3868 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3869 | if (buf == end) break; |
| 3870 | } |
| 3871 | if (*end && !isspace(*end)) break; |
| 3872 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3873 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3874 | } |
| 3875 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3876 | out: |
| 3877 | return len; |
| 3878 | } |
| 3879 | |
| 3880 | static struct md_sysfs_entry md_bitmap = |
| 3881 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3882 | |
| 3883 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3884 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3885 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3886 | return sprintf(page, "%llu\n", |
| 3887 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3888 | } |
| 3889 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3890 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3891 | |
| 3892 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3893 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3894 | { |
| 3895 | /* If array is inactive, we can reduce the component size, but |
| 3896 | * not increase it (except from 0). |
| 3897 | * If array is active, we can try an on-line resize |
| 3898 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3899 | sector_t sectors; |
| 3900 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3901 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3902 | if (err < 0) |
| 3903 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3904 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3905 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3906 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3907 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3908 | if (mddev->dev_sectors == 0 || |
| 3909 | mddev->dev_sectors > sectors) |
| 3910 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3911 | else |
| 3912 | err = -ENOSPC; |
| 3913 | } |
| 3914 | return err ? err : len; |
| 3915 | } |
| 3916 | |
| 3917 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3918 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3919 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 3920 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3921 | * This is one of |
| 3922 | * 'none' for arrays with no metadata (good luck...) |
| 3923 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3924 | * or N.M for internally known formats |
| 3925 | */ |
| 3926 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3927 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3928 | { |
| 3929 | if (mddev->persistent) |
| 3930 | return sprintf(page, "%d.%d\n", |
| 3931 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3932 | else if (mddev->external) |
| 3933 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3934 | else |
| 3935 | return sprintf(page, "none\n"); |
| 3936 | } |
| 3937 | |
| 3938 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3939 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3940 | { |
| 3941 | int major, minor; |
| 3942 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3943 | /* Changing the details of 'external' metadata is |
| 3944 | * always permitted. Otherwise there must be |
| 3945 | * no devices attached to the array. |
| 3946 | */ |
| 3947 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3948 | ; |
| 3949 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3950 | return -EBUSY; |
| 3951 | |
| 3952 | if (cmd_match(buf, "none")) { |
| 3953 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3954 | mddev->external = 0; |
| 3955 | mddev->major_version = 0; |
| 3956 | mddev->minor_version = 90; |
| 3957 | return len; |
| 3958 | } |
| 3959 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3960 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3961 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3962 | namelen = sizeof(mddev->metadata_type)-1; |
| 3963 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3964 | mddev->metadata_type[namelen] = 0; |
| 3965 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3966 | mddev->metadata_type[--namelen] = 0; |
| 3967 | mddev->persistent = 0; |
| 3968 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3969 | mddev->major_version = 0; |
| 3970 | mddev->minor_version = 90; |
| 3971 | return len; |
| 3972 | } |
| 3973 | major = simple_strtoul(buf, &e, 10); |
| 3974 | if (e==buf || *e != '.') |
| 3975 | return -EINVAL; |
| 3976 | buf = e+1; |
| 3977 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3978 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3979 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3980 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3981 | return -ENOENT; |
| 3982 | mddev->major_version = major; |
| 3983 | mddev->minor_version = minor; |
| 3984 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3985 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3986 | return len; |
| 3987 | } |
| 3988 | |
| 3989 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3990 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3991 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3992 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3993 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3994 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3995 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3996 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3997 | type = "frozen"; |
| 3998 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3999 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4000 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4001 | type = "reshape"; |
| 4002 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4003 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 4004 | type = "resync"; |
| 4005 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 4006 | type = "check"; |
| 4007 | else |
| 4008 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4009 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4010 | type = "recover"; |
| 4011 | } |
| 4012 | return sprintf(page, "%s\n", type); |
| 4013 | } |
| 4014 | |
| 4015 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4016 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4017 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4018 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4019 | return -EINVAL; |
| 4020 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4021 | if (cmd_match(page, "frozen")) |
| 4022 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4023 | else |
| 4024 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4025 | |
| 4026 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 4027 | flush_workqueue(md_misc_wq); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4028 | if (mddev->sync_thread) { |
| 4029 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 4030 | md_reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4031 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4032 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4033 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4034 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4035 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4036 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4037 | else if (cmd_match(page, "recover")) { |
| 4038 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4039 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4040 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4041 | int err; |
| 4042 | if (mddev->pers->start_reshape == NULL) |
| 4043 | return -EINVAL; |
| 4044 | err = mddev->pers->start_reshape(mddev); |
| 4045 | if (err) |
| 4046 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4047 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4048 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4049 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4050 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4051 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4052 | return -EINVAL; |
| 4053 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4054 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4055 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4056 | if (mddev->ro == 2) { |
| 4057 | /* A write to sync_action is enough to justify |
| 4058 | * canceling read-auto mode |
| 4059 | */ |
| 4060 | mddev->ro = 0; |
| 4061 | md_wakeup_thread(mddev->sync_thread); |
| 4062 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4063 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4064 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4065 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4066 | return len; |
| 4067 | } |
| 4068 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4069 | static struct md_sysfs_entry md_scan_mode = |
| 4070 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
| 4071 | |
| 4072 | static ssize_t |
| 4073 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4074 | { |
| 4075 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4076 | } |
| 4077 | |
| 4078 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4079 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4080 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4081 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4082 | { |
| 4083 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4084 | (unsigned long long) |
| 4085 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4086 | } |
| 4087 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4088 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4089 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4090 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4091 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4092 | { |
| 4093 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4094 | mddev->sync_speed_min ? "local": "system"); |
| 4095 | } |
| 4096 | |
| 4097 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4098 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4099 | { |
| 4100 | int min; |
| 4101 | char *e; |
| 4102 | if (strncmp(buf, "system", 6)==0) { |
| 4103 | mddev->sync_speed_min = 0; |
| 4104 | return len; |
| 4105 | } |
| 4106 | min = simple_strtoul(buf, &e, 10); |
| 4107 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4108 | return -EINVAL; |
| 4109 | mddev->sync_speed_min = min; |
| 4110 | return len; |
| 4111 | } |
| 4112 | |
| 4113 | static struct md_sysfs_entry md_sync_min = |
| 4114 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4115 | |
| 4116 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4117 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4118 | { |
| 4119 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4120 | mddev->sync_speed_max ? "local": "system"); |
| 4121 | } |
| 4122 | |
| 4123 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4124 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4125 | { |
| 4126 | int max; |
| 4127 | char *e; |
| 4128 | if (strncmp(buf, "system", 6)==0) { |
| 4129 | mddev->sync_speed_max = 0; |
| 4130 | return len; |
| 4131 | } |
| 4132 | max = simple_strtoul(buf, &e, 10); |
| 4133 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4134 | return -EINVAL; |
| 4135 | mddev->sync_speed_max = max; |
| 4136 | return len; |
| 4137 | } |
| 4138 | |
| 4139 | static struct md_sysfs_entry md_sync_max = |
| 4140 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4141 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4142 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4143 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4144 | { |
| 4145 | return sprintf(page, "%d\n", mddev->degraded); |
| 4146 | } |
| 4147 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4148 | |
| 4149 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4150 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4151 | { |
| 4152 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4153 | } |
| 4154 | |
| 4155 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4156 | 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] | 4157 | { |
| 4158 | long n; |
| 4159 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4160 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4161 | return -EINVAL; |
| 4162 | |
| 4163 | if (n != 0 && n != 1) |
| 4164 | return -EINVAL; |
| 4165 | |
| 4166 | mddev->parallel_resync = n; |
| 4167 | |
| 4168 | if (mddev->sync_thread) |
| 4169 | wake_up(&resync_wait); |
| 4170 | |
| 4171 | return len; |
| 4172 | } |
| 4173 | |
| 4174 | /* force parallel resync, even with shared block devices */ |
| 4175 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4176 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4177 | sync_force_parallel_show, sync_force_parallel_store); |
| 4178 | |
| 4179 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4180 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4181 | { |
| 4182 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4183 | if (mddev->curr_resync == 0) |
| 4184 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4185 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4186 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4187 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4188 | db = resync - mddev->resync_mark_cnt; |
| 4189 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4190 | } |
| 4191 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4192 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4193 | |
| 4194 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4195 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4196 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4197 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4198 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4199 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4200 | return sprintf(page, "none\n"); |
| 4201 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4202 | if (mddev->curr_resync == 1 || |
| 4203 | mddev->curr_resync == 2) |
| 4204 | return sprintf(page, "delayed\n"); |
| 4205 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4206 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4207 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4208 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4209 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4210 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4211 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4212 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4213 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4214 | } |
| 4215 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4216 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4217 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4218 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4219 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4220 | { |
| 4221 | return sprintf(page, "%llu\n", |
| 4222 | (unsigned long long)mddev->resync_min); |
| 4223 | } |
| 4224 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4225 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4226 | { |
| 4227 | unsigned long long min; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4228 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4229 | return -EINVAL; |
| 4230 | if (min > mddev->resync_max) |
| 4231 | return -EINVAL; |
| 4232 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4233 | return -EBUSY; |
| 4234 | |
| 4235 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4236 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4237 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4238 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4239 | return -EINVAL; |
| 4240 | } |
| 4241 | mddev->resync_min = min; |
| 4242 | |
| 4243 | return len; |
| 4244 | } |
| 4245 | |
| 4246 | static struct md_sysfs_entry md_min_sync = |
| 4247 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4248 | |
| 4249 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4250 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4251 | { |
| 4252 | if (mddev->resync_max == MaxSector) |
| 4253 | return sprintf(page, "max\n"); |
| 4254 | else |
| 4255 | return sprintf(page, "%llu\n", |
| 4256 | (unsigned long long)mddev->resync_max); |
| 4257 | } |
| 4258 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4259 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4260 | { |
| 4261 | if (strncmp(buf, "max", 3) == 0) |
| 4262 | mddev->resync_max = MaxSector; |
| 4263 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4264 | unsigned long long max; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4265 | if (kstrtoull(buf, 10, &max)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4266 | return -EINVAL; |
| 4267 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4268 | return -EINVAL; |
| 4269 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4270 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4271 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4272 | return -EBUSY; |
| 4273 | |
| 4274 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4275 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4276 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4277 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4278 | return -EINVAL; |
| 4279 | } |
| 4280 | mddev->resync_max = max; |
| 4281 | } |
| 4282 | wake_up(&mddev->recovery_wait); |
| 4283 | return len; |
| 4284 | } |
| 4285 | |
| 4286 | static struct md_sysfs_entry md_max_sync = |
| 4287 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4288 | |
| 4289 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4290 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4291 | { |
| 4292 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4293 | } |
| 4294 | |
| 4295 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4296 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4297 | { |
| 4298 | char *e; |
| 4299 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4300 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4301 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4302 | if (mddev->pers == NULL || |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4303 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4304 | return -EINVAL; |
| 4305 | if (buf == e || (*e && *e != '\n')) |
| 4306 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4307 | |
| 4308 | mddev->suspend_lo = new; |
| 4309 | if (new >= old) |
| 4310 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4311 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4312 | else { |
| 4313 | /* Expanding suspended region - need to wait */ |
| 4314 | mddev->pers->quiesce(mddev, 1); |
| 4315 | mddev->pers->quiesce(mddev, 0); |
| 4316 | } |
| 4317 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4318 | } |
| 4319 | static struct md_sysfs_entry md_suspend_lo = |
| 4320 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4321 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4322 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4323 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4324 | { |
| 4325 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4326 | } |
| 4327 | |
| 4328 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4329 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4330 | { |
| 4331 | char *e; |
| 4332 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4333 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4334 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4335 | if (mddev->pers == NULL || |
| 4336 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4337 | return -EINVAL; |
| 4338 | if (buf == e || (*e && *e != '\n')) |
| 4339 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4340 | |
| 4341 | mddev->suspend_hi = new; |
| 4342 | if (new <= old) |
| 4343 | /* Shrinking suspended region */ |
| 4344 | mddev->pers->quiesce(mddev, 2); |
| 4345 | else { |
| 4346 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4347 | mddev->pers->quiesce(mddev, 1); |
| 4348 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4349 | } |
| 4350 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4351 | } |
| 4352 | static struct md_sysfs_entry md_suspend_hi = |
| 4353 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4354 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4355 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4356 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4357 | { |
| 4358 | if (mddev->reshape_position != MaxSector) |
| 4359 | return sprintf(page, "%llu\n", |
| 4360 | (unsigned long long)mddev->reshape_position); |
| 4361 | strcpy(page, "none\n"); |
| 4362 | return 5; |
| 4363 | } |
| 4364 | |
| 4365 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4366 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4367 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4368 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4369 | char *e; |
| 4370 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4371 | if (mddev->pers) |
| 4372 | return -EBUSY; |
| 4373 | if (buf == e || (*e && *e != '\n')) |
| 4374 | return -EINVAL; |
| 4375 | mddev->reshape_position = new; |
| 4376 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4377 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4378 | mddev->new_level = mddev->level; |
| 4379 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4380 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4381 | rdev_for_each(rdev, mddev) |
| 4382 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4383 | return len; |
| 4384 | } |
| 4385 | |
| 4386 | static struct md_sysfs_entry md_reshape_position = |
| 4387 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4388 | reshape_position_store); |
| 4389 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4390 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4391 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4392 | { |
| 4393 | return sprintf(page, "%s\n", |
| 4394 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4395 | } |
| 4396 | |
| 4397 | static ssize_t |
| 4398 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4399 | { |
| 4400 | int backwards = 0; |
| 4401 | if (cmd_match(buf, "forwards")) |
| 4402 | backwards = 0; |
| 4403 | else if (cmd_match(buf, "backwards")) |
| 4404 | backwards = 1; |
| 4405 | else |
| 4406 | return -EINVAL; |
| 4407 | if (mddev->reshape_backwards == backwards) |
| 4408 | return len; |
| 4409 | |
| 4410 | /* check if we are allowed to change */ |
| 4411 | if (mddev->delta_disks) |
| 4412 | return -EBUSY; |
| 4413 | |
| 4414 | if (mddev->persistent && |
| 4415 | mddev->major_version == 0) |
| 4416 | return -EINVAL; |
| 4417 | |
| 4418 | mddev->reshape_backwards = backwards; |
| 4419 | return len; |
| 4420 | } |
| 4421 | |
| 4422 | static struct md_sysfs_entry md_reshape_direction = |
| 4423 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4424 | reshape_direction_store); |
| 4425 | |
| 4426 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4427 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4428 | { |
| 4429 | if (mddev->external_size) |
| 4430 | return sprintf(page, "%llu\n", |
| 4431 | (unsigned long long)mddev->array_sectors/2); |
| 4432 | else |
| 4433 | return sprintf(page, "default\n"); |
| 4434 | } |
| 4435 | |
| 4436 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4437 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4438 | { |
| 4439 | sector_t sectors; |
| 4440 | |
| 4441 | if (strncmp(buf, "default", 7) == 0) { |
| 4442 | if (mddev->pers) |
| 4443 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4444 | else |
| 4445 | sectors = mddev->array_sectors; |
| 4446 | |
| 4447 | mddev->external_size = 0; |
| 4448 | } else { |
| 4449 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4450 | return -EINVAL; |
| 4451 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4452 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4453 | |
| 4454 | mddev->external_size = 1; |
| 4455 | } |
| 4456 | |
| 4457 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4458 | if (mddev->pers) { |
| 4459 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4460 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4461 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4462 | return len; |
| 4463 | } |
| 4464 | |
| 4465 | static struct md_sysfs_entry md_array_size = |
| 4466 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4467 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4468 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4469 | static struct attribute *md_default_attrs[] = { |
| 4470 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4471 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4472 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4473 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4474 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4475 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4476 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4477 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4478 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4479 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4480 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4481 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4482 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4483 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4484 | NULL, |
| 4485 | }; |
| 4486 | |
| 4487 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4488 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4489 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4490 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4491 | &md_sync_min.attr, |
| 4492 | &md_sync_max.attr, |
| 4493 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4494 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4495 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4496 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4497 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4498 | &md_suspend_lo.attr, |
| 4499 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4500 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4501 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4502 | NULL, |
| 4503 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4504 | static struct attribute_group md_redundancy_group = { |
| 4505 | .name = NULL, |
| 4506 | .attrs = md_redundancy_attrs, |
| 4507 | }; |
| 4508 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4509 | static ssize_t |
| 4510 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4511 | { |
| 4512 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4513 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4514 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4515 | |
| 4516 | if (!entry->show) |
| 4517 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4518 | spin_lock(&all_mddevs_lock); |
| 4519 | if (list_empty(&mddev->all_mddevs)) { |
| 4520 | spin_unlock(&all_mddevs_lock); |
| 4521 | return -EBUSY; |
| 4522 | } |
| 4523 | mddev_get(mddev); |
| 4524 | spin_unlock(&all_mddevs_lock); |
| 4525 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4526 | rv = mddev_lock(mddev); |
| 4527 | if (!rv) { |
| 4528 | rv = entry->show(mddev, page); |
| 4529 | mddev_unlock(mddev); |
| 4530 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4531 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4532 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | static ssize_t |
| 4536 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4537 | const char *page, size_t length) |
| 4538 | { |
| 4539 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4540 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4541 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4542 | |
| 4543 | if (!entry->store) |
| 4544 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4545 | if (!capable(CAP_SYS_ADMIN)) |
| 4546 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4547 | spin_lock(&all_mddevs_lock); |
| 4548 | if (list_empty(&mddev->all_mddevs)) { |
| 4549 | spin_unlock(&all_mddevs_lock); |
| 4550 | return -EBUSY; |
| 4551 | } |
| 4552 | mddev_get(mddev); |
| 4553 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 4554 | if (entry->store == new_dev_store) |
| 4555 | flush_workqueue(md_misc_wq); |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4556 | rv = mddev_lock(mddev); |
| 4557 | if (!rv) { |
| 4558 | rv = entry->store(mddev, page, length); |
| 4559 | mddev_unlock(mddev); |
| 4560 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4561 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4562 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | static void md_free(struct kobject *ko) |
| 4566 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4567 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4568 | |
| 4569 | if (mddev->sysfs_state) |
| 4570 | sysfs_put(mddev->sysfs_state); |
| 4571 | |
| 4572 | if (mddev->gendisk) { |
| 4573 | del_gendisk(mddev->gendisk); |
| 4574 | put_disk(mddev->gendisk); |
| 4575 | } |
| 4576 | if (mddev->queue) |
| 4577 | blk_cleanup_queue(mddev->queue); |
| 4578 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4579 | kfree(mddev); |
| 4580 | } |
| 4581 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4582 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4583 | .show = md_attr_show, |
| 4584 | .store = md_attr_store, |
| 4585 | }; |
| 4586 | static struct kobj_type md_ktype = { |
| 4587 | .release = md_free, |
| 4588 | .sysfs_ops = &md_sysfs_ops, |
| 4589 | .default_attrs = md_default_attrs, |
| 4590 | }; |
| 4591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | int mdp_major = 0; |
| 4593 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4594 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4595 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4596 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4597 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4598 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4599 | kobject_del(&mddev->kobj); |
| 4600 | kobject_put(&mddev->kobj); |
| 4601 | } |
| 4602 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4603 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4605 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4606 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4608 | int partitioned; |
| 4609 | int shift; |
| 4610 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4611 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | |
| 4613 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4614 | return -ENODEV; |
| 4615 | |
| 4616 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4617 | shift = partitioned ? MdpMinorShift : 0; |
| 4618 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4619 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4620 | /* wait for any previous instance of this device to be |
| 4621 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4622 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4623 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4624 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4625 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4626 | error = -EEXIST; |
| 4627 | if (mddev->gendisk) |
| 4628 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4629 | |
| 4630 | if (name) { |
| 4631 | /* Need to ensure that 'name' is not a duplicate. |
| 4632 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4633 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4634 | spin_lock(&all_mddevs_lock); |
| 4635 | |
| 4636 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4637 | if (mddev2->gendisk && |
| 4638 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4639 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4640 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4641 | } |
| 4642 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4644 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4645 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4646 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4647 | if (!mddev->queue) |
| 4648 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4649 | mddev->queue->queuedata = mddev; |
| 4650 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4651 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 4652 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | disk = alloc_disk(1 << shift); |
| 4655 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4656 | blk_cleanup_queue(mddev->queue); |
| 4657 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4658 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4659 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4660 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4662 | if (name) |
| 4663 | strcpy(disk->disk_name, name); |
| 4664 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4666 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4668 | disk->fops = &md_fops; |
| 4669 | disk->private_data = mddev; |
| 4670 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4671 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4672 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4673 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4674 | * remove it now. |
| 4675 | */ |
| 4676 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4677 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4678 | /* As soon as we call add_disk(), another thread could get |
| 4679 | * through to md_open, so make sure it doesn't get too far |
| 4680 | */ |
| 4681 | mutex_lock(&mddev->open_mutex); |
| 4682 | add_disk(disk); |
| 4683 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4684 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4685 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4686 | if (error) { |
| 4687 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4688 | * __must_check, we must do something with the result |
| 4689 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4690 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4691 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4692 | error = 0; |
| 4693 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4694 | if (mddev->kobj.sd && |
| 4695 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4696 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4697 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4698 | abort: |
| 4699 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4700 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4701 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4702 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4703 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4704 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4705 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4706 | } |
| 4707 | |
| 4708 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4709 | { |
| 4710 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | return NULL; |
| 4712 | } |
| 4713 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4714 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4715 | { |
| 4716 | /* val must be "md_*" where * is not all digits. |
| 4717 | * We allocate an array with a large free minor number, and |
| 4718 | * set the name to val. val must not already be an active name. |
| 4719 | */ |
| 4720 | int len = strlen(val); |
| 4721 | char buf[DISK_NAME_LEN]; |
| 4722 | |
| 4723 | while (len && val[len-1] == '\n') |
| 4724 | len--; |
| 4725 | if (len >= DISK_NAME_LEN) |
| 4726 | return -E2BIG; |
| 4727 | strlcpy(buf, val, len+1); |
| 4728 | if (strncmp(buf, "md_", 3) != 0) |
| 4729 | return -EINVAL; |
| 4730 | return md_alloc(0, buf); |
| 4731 | } |
| 4732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4733 | static void md_safemode_timeout(unsigned long data) |
| 4734 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4735 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4737 | if (!atomic_read(&mddev->writes_pending)) { |
| 4738 | mddev->safemode = 1; |
| 4739 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4740 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4741 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | md_wakeup_thread(mddev->thread); |
| 4743 | } |
| 4744 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4745 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4747 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4748 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4749 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4750 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4751 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4752 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4753 | if (list_empty(&mddev->disks)) |
| 4754 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4755 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | |
| 4757 | if (mddev->pers) |
| 4758 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4759 | /* Cannot run until previous stop completes properly */ |
| 4760 | if (mddev->sysfs_active) |
| 4761 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4763 | /* |
| 4764 | * Analyze all RAID superblock(s) |
| 4765 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4766 | if (!mddev->raid_disks) { |
| 4767 | if (!mddev->persistent) |
| 4768 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4769 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4770 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4772 | if (mddev->level != LEVEL_NONE) |
| 4773 | request_module("md-level-%d", mddev->level); |
| 4774 | else if (mddev->clevel[0]) |
| 4775 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4776 | |
| 4777 | /* |
| 4778 | * Drop all container device buffers, from now on |
| 4779 | * the only valid external interface is through the md |
| 4780 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4781 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4782 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4783 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4784 | continue; |
| 4785 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4786 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4787 | |
| 4788 | /* perform some consistency tests on the device. |
| 4789 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4790 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4791 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4792 | if (rdev->meta_bdev) { |
| 4793 | /* Nothing to check */; |
| 4794 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4795 | if (mddev->dev_sectors && |
| 4796 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4797 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4798 | printk("md: %s: data overlaps metadata\n", |
| 4799 | mdname(mddev)); |
| 4800 | return -EINVAL; |
| 4801 | } |
| 4802 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4803 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4804 | > rdev->data_offset) { |
| 4805 | printk("md: %s: metadata overlaps data\n", |
| 4806 | mdname(mddev)); |
| 4807 | return -EINVAL; |
| 4808 | } |
| 4809 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4810 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | } |
| 4812 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4813 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 4814 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4816 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4817 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4818 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4820 | if (mddev->level != LEVEL_NONE) |
| 4821 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4822 | mddev->level); |
| 4823 | else |
| 4824 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4825 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4826 | return -EINVAL; |
| 4827 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4828 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4829 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4830 | if (mddev->level != pers->level) { |
| 4831 | mddev->level = pers->level; |
| 4832 | mddev->new_level = pers->level; |
| 4833 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4834 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4836 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4837 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4838 | /* This personality cannot handle reshaping... */ |
| 4839 | mddev->pers = NULL; |
| 4840 | module_put(pers->owner); |
| 4841 | return -EINVAL; |
| 4842 | } |
| 4843 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4844 | if (pers->sync_request) { |
| 4845 | /* Warn if this is a potentially silly |
| 4846 | * configuration. |
| 4847 | */ |
| 4848 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4849 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4850 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4851 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4852 | rdev_for_each(rdev, mddev) |
| 4853 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4854 | if (rdev < rdev2 && |
| 4855 | rdev->bdev->bd_contains == |
| 4856 | rdev2->bdev->bd_contains) { |
| 4857 | printk(KERN_WARNING |
| 4858 | "%s: WARNING: %s appears to be" |
| 4859 | " on the same physical disk as" |
| 4860 | " %s.\n", |
| 4861 | mdname(mddev), |
| 4862 | bdevname(rdev->bdev,b), |
| 4863 | bdevname(rdev2->bdev,b2)); |
| 4864 | warned = 1; |
| 4865 | } |
| 4866 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4867 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4868 | if (warned) |
| 4869 | printk(KERN_WARNING |
| 4870 | "True protection against single-disk" |
| 4871 | " failure might be compromised.\n"); |
| 4872 | } |
| 4873 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4874 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4875 | /* may be over-ridden by personality */ |
| 4876 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4877 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4878 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4880 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4881 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4882 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4883 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4884 | if (err) |
| 4885 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4886 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4887 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4888 | " but 'external_size' not in effect?\n", __func__); |
| 4889 | printk(KERN_ERR |
| 4890 | "md: invalid array_size %llu > default size %llu\n", |
| 4891 | (unsigned long long)mddev->array_sectors / 2, |
| 4892 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4893 | err = -EINVAL; |
| 4894 | mddev->pers->stop(mddev); |
| 4895 | } |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 4896 | if (err == 0 && mddev->pers->sync_request && |
| 4897 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4898 | err = bitmap_create(mddev); |
| 4899 | if (err) { |
| 4900 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4901 | mdname(mddev), err); |
| 4902 | mddev->pers->stop(mddev); |
| 4903 | } |
| 4904 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4905 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | module_put(mddev->pers->owner); |
| 4907 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4908 | bitmap_destroy(mddev); |
| 4909 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4910 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4911 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4912 | if (mddev->kobj.sd && |
| 4913 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4914 | printk(KERN_WARNING |
| 4915 | "md: cannot register extra attributes for %s\n", |
| 4916 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4917 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4918 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4919 | mddev->ro = 0; |
| 4920 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4921 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4922 | atomic_set(&mddev->max_corr_read_errors, |
| 4923 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | mddev->safemode = 0; |
| 4925 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4926 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4927 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4928 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 4929 | smp_wmb(); |
| 4930 | mddev->ready = 1; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4931 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4932 | if (rdev->raid_disk >= 0) |
| 4933 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4934 | /* failure here is OK */; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4937 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 4938 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4939 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4941 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4942 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4943 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4944 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4945 | return 0; |
| 4946 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4947 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4949 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4950 | { |
| 4951 | int err; |
| 4952 | |
| 4953 | err = md_run(mddev); |
| 4954 | if (err) |
| 4955 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4956 | err = bitmap_load(mddev); |
| 4957 | if (err) { |
| 4958 | bitmap_destroy(mddev); |
| 4959 | goto out; |
| 4960 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 4961 | |
| 4962 | md_wakeup_thread(mddev->thread); |
| 4963 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 4964 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4965 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4966 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 4967 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4968 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4969 | out: |
| 4970 | return err; |
| 4971 | } |
| 4972 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4973 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 | { |
| 4975 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4977 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4978 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4979 | return -ENXIO; |
| 4980 | if (!mddev->pers) |
| 4981 | return -EINVAL; |
| 4982 | if (!mddev->ro) |
| 4983 | return -EBUSY; |
| 4984 | mddev->safemode = 0; |
| 4985 | mddev->ro = 0; |
| 4986 | set_disk_ro(disk, 0); |
| 4987 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4988 | mdname(mddev)); |
| 4989 | /* Kick recovery or resync if necessary */ |
| 4990 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4991 | md_wakeup_thread(mddev->thread); |
| 4992 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4993 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4994 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | } |
| 4996 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4997 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4998 | { |
| 4999 | mddev->array_sectors = 0; |
| 5000 | mddev->external_size = 0; |
| 5001 | mddev->dev_sectors = 0; |
| 5002 | mddev->raid_disks = 0; |
| 5003 | mddev->recovery_cp = 0; |
| 5004 | mddev->resync_min = 0; |
| 5005 | mddev->resync_max = MaxSector; |
| 5006 | mddev->reshape_position = MaxSector; |
| 5007 | mddev->external = 0; |
| 5008 | mddev->persistent = 0; |
| 5009 | mddev->level = LEVEL_NONE; |
| 5010 | mddev->clevel[0] = 0; |
| 5011 | mddev->flags = 0; |
| 5012 | mddev->ro = 0; |
| 5013 | mddev->metadata_type[0] = 0; |
| 5014 | mddev->chunk_sectors = 0; |
| 5015 | mddev->ctime = mddev->utime = 0; |
| 5016 | mddev->layout = 0; |
| 5017 | mddev->max_disks = 0; |
| 5018 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5019 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5020 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5021 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5022 | mddev->new_level = LEVEL_NONE; |
| 5023 | mddev->new_layout = 0; |
| 5024 | mddev->new_chunk_sectors = 0; |
| 5025 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5026 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5027 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5028 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5029 | mddev->recovery = 0; |
| 5030 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5031 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5032 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5033 | mddev->safemode = 0; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5034 | mddev->merge_check_needed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5035 | mddev->bitmap_info.offset = 0; |
| 5036 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5037 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5038 | mddev->bitmap_info.chunksize = 0; |
| 5039 | mddev->bitmap_info.daemon_sleep = 0; |
| 5040 | mddev->bitmap_info.max_write_behind = 0; |
| 5041 | } |
| 5042 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5043 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5044 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 5045 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5046 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5047 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5048 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5049 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5050 | } |
| 5051 | |
| 5052 | del_timer_sync(&mddev->safemode_timer); |
| 5053 | |
| 5054 | bitmap_flush(mddev); |
| 5055 | md_super_wait(mddev); |
| 5056 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5057 | if (mddev->ro == 0 && |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5058 | (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5059 | /* mark array as shutdown cleanly */ |
| 5060 | mddev->in_sync = 1; |
| 5061 | md_update_sb(mddev, 1); |
| 5062 | } |
| 5063 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5064 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5065 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5066 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 5067 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5068 | __md_stop_writes(mddev); |
| 5069 | mddev_unlock(mddev); |
| 5070 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5071 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5072 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5073 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5074 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5075 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5076 | mddev->pers->stop(mddev); |
| 5077 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 5078 | mddev->to_remove = &md_redundancy_group; |
| 5079 | module_put(mddev->pers->owner); |
| 5080 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5081 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5082 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5083 | |
| 5084 | void md_stop(struct mddev *mddev) |
| 5085 | { |
| 5086 | /* stop the array and free an attached data structures. |
| 5087 | * This is called from dm-raid |
| 5088 | */ |
| 5089 | __md_stop(mddev); |
| 5090 | bitmap_destroy(mddev); |
| 5091 | if (mddev->bio_set) |
| 5092 | bioset_free(mddev->bio_set); |
| 5093 | } |
| 5094 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5095 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5096 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5097 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5098 | { |
| 5099 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5100 | int did_freeze = 0; |
| 5101 | |
| 5102 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5103 | did_freeze = 1; |
| 5104 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5105 | md_wakeup_thread(mddev->thread); |
| 5106 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5107 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5108 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5109 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5110 | /* Thread might be blocked waiting for metadata update |
| 5111 | * which will now never happen */ |
| 5112 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5113 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5114 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5115 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 5116 | &mddev->recovery)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5117 | mddev_lock_nointr(mddev); |
| 5118 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5119 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5120 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5121 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5122 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5123 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5124 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5125 | if (did_freeze) { |
| 5126 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5127 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5128 | md_wakeup_thread(mddev->thread); |
| 5129 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5130 | err = -EBUSY; |
| 5131 | goto out; |
| 5132 | } |
| 5133 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5134 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5135 | |
| 5136 | err = -ENXIO; |
| 5137 | if (mddev->ro==1) |
| 5138 | goto out; |
| 5139 | mddev->ro = 1; |
| 5140 | set_disk_ro(mddev->gendisk, 1); |
| 5141 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5142 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5143 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5144 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5145 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5146 | } |
| 5147 | out: |
| 5148 | mutex_unlock(&mddev->open_mutex); |
| 5149 | return err; |
| 5150 | } |
| 5151 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5152 | /* mode: |
| 5153 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5154 | * 2 - stop but do not disassemble array |
| 5155 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5156 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5157 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5158 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5160 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5161 | int did_freeze = 0; |
| 5162 | |
| 5163 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5164 | did_freeze = 1; |
| 5165 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5166 | md_wakeup_thread(mddev->thread); |
| 5167 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5168 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5169 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5170 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5171 | /* Thread might be blocked waiting for metadata update |
| 5172 | * which will now never happen */ |
| 5173 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5174 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5175 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5176 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 5177 | !test_bit(MD_RECOVERY_RUNNING, |
| 5178 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5179 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5181 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5182 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5183 | mddev->sysfs_active || |
| 5184 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5185 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5186 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5187 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5188 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5189 | if (did_freeze) { |
| 5190 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5191 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5192 | md_wakeup_thread(mddev->thread); |
| 5193 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 5194 | return -EBUSY; |
| 5195 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5196 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5197 | if (mddev->ro) |
| 5198 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5199 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5200 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5201 | __md_stop(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5202 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5203 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5204 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5205 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5206 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5207 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5208 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5209 | if (rdev->raid_disk >= 0) |
| 5210 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5211 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5212 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5213 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5214 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5215 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5216 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5217 | if (mddev->ro) |
| 5218 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5219 | } else |
| 5220 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5221 | /* |
| 5222 | * Free resources if final stop |
| 5223 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5224 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5225 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5226 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5227 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5228 | if (mddev->bitmap_info.file) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5229 | fput(mddev->bitmap_info.file); |
| 5230 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5231 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5232 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5234 | export_array(mddev); |
| 5235 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5236 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5237 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5238 | if (mddev->hold_active == UNTIL_STOP) |
| 5239 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5240 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5241 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5242 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5243 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5244 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5245 | } |
| 5246 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5247 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5248 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5250 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | int err; |
| 5252 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5253 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5254 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | |
| 5256 | printk(KERN_INFO "md: running: "); |
| 5257 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5258 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | char b[BDEVNAME_SIZE]; |
| 5260 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5261 | } |
| 5262 | printk("\n"); |
| 5263 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5264 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | if (err) { |
| 5266 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5267 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | } |
| 5269 | } |
| 5270 | |
| 5271 | /* |
| 5272 | * lets try to run arrays based on all disks that have arrived |
| 5273 | * until now. (those are in pending_raid_disks) |
| 5274 | * |
| 5275 | * the method: pick the first pending disk, collect all disks with |
| 5276 | * the same UUID, remove all from the pending list and put them into |
| 5277 | * the 'same_array' list. Then order this list based on superblock |
| 5278 | * update time (freshest comes first), kick out 'old' disks and |
| 5279 | * compare superblocks. If everything's fine then run it. |
| 5280 | * |
| 5281 | * If "unit" is allocated, then bump its reference count |
| 5282 | */ |
| 5283 | static void autorun_devices(int part) |
| 5284 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5285 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5286 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | char b[BDEVNAME_SIZE]; |
| 5288 | |
| 5289 | printk(KERN_INFO "md: autorun ...\n"); |
| 5290 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5291 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5293 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5295 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5296 | |
| 5297 | printk(KERN_INFO "md: considering %s ...\n", |
| 5298 | bdevname(rdev0->bdev,b)); |
| 5299 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5300 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5301 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5302 | printk(KERN_INFO "md: adding %s ...\n", |
| 5303 | bdevname(rdev->bdev,b)); |
| 5304 | list_move(&rdev->same_set, &candidates); |
| 5305 | } |
| 5306 | /* |
| 5307 | * now we have a set of devices, with all of them having |
| 5308 | * mostly sane superblocks. It's time to allocate the |
| 5309 | * mddev. |
| 5310 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5311 | if (part) { |
| 5312 | dev = MKDEV(mdp_major, |
| 5313 | rdev0->preferred_minor << MdpMinorShift); |
| 5314 | unit = MINOR(dev) >> MdpMinorShift; |
| 5315 | } else { |
| 5316 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5317 | unit = MINOR(dev); |
| 5318 | } |
| 5319 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5320 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5321 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5322 | break; |
| 5323 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | |
| 5325 | md_probe(dev, NULL, NULL); |
| 5326 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5327 | if (!mddev || !mddev->gendisk) { |
| 5328 | if (mddev) |
| 5329 | mddev_put(mddev); |
| 5330 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5331 | "md: cannot allocate memory for md drive.\n"); |
| 5332 | break; |
| 5333 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5334 | if (mddev_lock(mddev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5335 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5336 | mdname(mddev)); |
| 5337 | else if (mddev->raid_disks || mddev->major_version |
| 5338 | || !list_empty(&mddev->disks)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5339 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5340 | "md: %s already running, cannot run %s\n", |
| 5341 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5342 | mddev_unlock(mddev); |
| 5343 | } else { |
| 5344 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5345 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5346 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5347 | list_del_init(&rdev->same_set); |
| 5348 | if (bind_rdev_to_array(rdev, mddev)) |
| 5349 | export_rdev(rdev); |
| 5350 | } |
| 5351 | autorun_array(mddev); |
| 5352 | mddev_unlock(mddev); |
| 5353 | } |
| 5354 | /* on success, candidates will be empty, on error |
| 5355 | * it won't... |
| 5356 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5357 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5358 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5359 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5360 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5361 | mddev_put(mddev); |
| 5362 | } |
| 5363 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5364 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5365 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5366 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5367 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5368 | { |
| 5369 | mdu_version_t ver; |
| 5370 | |
| 5371 | ver.major = MD_MAJOR_VERSION; |
| 5372 | ver.minor = MD_MINOR_VERSION; |
| 5373 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5374 | |
| 5375 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5376 | return -EFAULT; |
| 5377 | |
| 5378 | return 0; |
| 5379 | } |
| 5380 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5381 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | { |
| 5383 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5384 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5385 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5386 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5387 | nr = working = insync = failed = spare = 0; |
| 5388 | rcu_read_lock(); |
| 5389 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5390 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5391 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5392 | failed++; |
| 5393 | else { |
| 5394 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5395 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5396 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | else |
| 5398 | spare++; |
| 5399 | } |
| 5400 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5401 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | |
| 5403 | info.major_version = mddev->major_version; |
| 5404 | info.minor_version = mddev->minor_version; |
| 5405 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5406 | info.ctime = mddev->ctime; |
| 5407 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5408 | info.size = mddev->dev_sectors / 2; |
| 5409 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5410 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | info.nr_disks = nr; |
| 5412 | info.raid_disks = mddev->raid_disks; |
| 5413 | info.md_minor = mddev->md_minor; |
| 5414 | info.not_persistent= !mddev->persistent; |
| 5415 | |
| 5416 | info.utime = mddev->utime; |
| 5417 | info.state = 0; |
| 5418 | if (mddev->in_sync) |
| 5419 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5420 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 5421 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5422 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | info.working_disks = working; |
| 5424 | info.failed_disks = failed; |
| 5425 | info.spare_disks = spare; |
| 5426 | |
| 5427 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5428 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5429 | |
| 5430 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5431 | return -EFAULT; |
| 5432 | |
| 5433 | return 0; |
| 5434 | } |
| 5435 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5436 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5437 | { |
| 5438 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5439 | char *ptr, *buf = NULL; |
| 5440 | int err = -ENOMEM; |
| 5441 | |
NeilBrown | 60559da | 2013-07-16 16:44:29 +1000 | [diff] [blame] | 5442 | file = kmalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5443 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5444 | if (!file) |
| 5445 | goto out; |
| 5446 | |
| 5447 | /* bitmap disabled, zero the first byte and copy out */ |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 5448 | if (!mddev->bitmap || !mddev->bitmap->storage.file) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5449 | file->pathname[0] = '\0'; |
| 5450 | goto copy_out; |
| 5451 | } |
| 5452 | |
| 5453 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5454 | if (!buf) |
| 5455 | goto out; |
| 5456 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 5457 | ptr = d_path(&mddev->bitmap->storage.file->f_path, |
| 5458 | buf, sizeof(file->pathname)); |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5459 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5460 | goto out; |
| 5461 | |
| 5462 | strcpy(file->pathname, ptr); |
| 5463 | |
| 5464 | copy_out: |
| 5465 | err = 0; |
| 5466 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5467 | err = -EFAULT; |
| 5468 | out: |
| 5469 | kfree(buf); |
| 5470 | kfree(file); |
| 5471 | return err; |
| 5472 | } |
| 5473 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5474 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | { |
| 5476 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5477 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | |
| 5479 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5480 | return -EFAULT; |
| 5481 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5482 | rcu_read_lock(); |
| 5483 | rdev = find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5484 | if (rdev) { |
| 5485 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5486 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5487 | info.raid_disk = rdev->raid_disk; |
| 5488 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5489 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5490 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5491 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5493 | info.state |= (1<<MD_DISK_SYNC); |
| 5494 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5495 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5496 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | } else { |
| 5498 | info.major = info.minor = 0; |
| 5499 | info.raid_disk = -1; |
| 5500 | info.state = (1<<MD_DISK_REMOVED); |
| 5501 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5502 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | |
| 5504 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5505 | return -EFAULT; |
| 5506 | |
| 5507 | return 0; |
| 5508 | } |
| 5509 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5510 | 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] | 5511 | { |
| 5512 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5513 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | dev_t dev = MKDEV(info->major,info->minor); |
| 5515 | |
| 5516 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5517 | return -EOVERFLOW; |
| 5518 | |
| 5519 | if (!mddev->raid_disks) { |
| 5520 | int err; |
| 5521 | /* expecting a device which has a superblock */ |
| 5522 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5523 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5524 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5525 | "md: md_import_device returned %ld\n", |
| 5526 | PTR_ERR(rdev)); |
| 5527 | return PTR_ERR(rdev); |
| 5528 | } |
| 5529 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5530 | struct md_rdev *rdev0 |
| 5531 | = list_entry(mddev->disks.next, |
| 5532 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5533 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5535 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5536 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5537 | "md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5538 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5539 | bdevname(rdev0->bdev,b2)); |
| 5540 | export_rdev(rdev); |
| 5541 | return -EINVAL; |
| 5542 | } |
| 5543 | } |
| 5544 | err = bind_rdev_to_array(rdev, mddev); |
| 5545 | if (err) |
| 5546 | export_rdev(rdev); |
| 5547 | return err; |
| 5548 | } |
| 5549 | |
| 5550 | /* |
| 5551 | * add_new_disk can be used once the array is assembled |
| 5552 | * to add "hot spares". They must already have a superblock |
| 5553 | * written |
| 5554 | */ |
| 5555 | if (mddev->pers) { |
| 5556 | int err; |
| 5557 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5558 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | "%s: personality does not support diskops!\n", |
| 5560 | mdname(mddev)); |
| 5561 | return -EINVAL; |
| 5562 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5563 | if (mddev->persistent) |
| 5564 | rdev = md_import_device(dev, mddev->major_version, |
| 5565 | mddev->minor_version); |
| 5566 | else |
| 5567 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5569 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5570 | "md: md_import_device returned %ld\n", |
| 5571 | PTR_ERR(rdev)); |
| 5572 | return PTR_ERR(rdev); |
| 5573 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5574 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5575 | if (!mddev->persistent) { |
| 5576 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5577 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5578 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5579 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 5580 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5581 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5582 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 5583 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5584 | } else |
| 5585 | super_types[mddev->major_version]. |
| 5586 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5587 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 5588 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5589 | /* This was a hot-add request, but events doesn't |
| 5590 | * match, so reject it. |
| 5591 | */ |
| 5592 | export_rdev(rdev); |
| 5593 | return -EINVAL; |
| 5594 | } |
| 5595 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5596 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5597 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5598 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5599 | else |
| 5600 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5602 | rdev->raid_disk = -1; |
| 5603 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5604 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5605 | /* If there is hot_add_disk but no hot_remove_disk |
| 5606 | * then added disks for geometry changes, |
| 5607 | * and should be added immediately. |
| 5608 | */ |
| 5609 | super_types[mddev->major_version]. |
| 5610 | validate_super(mddev, rdev); |
| 5611 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5612 | if (err) |
| 5613 | unbind_rdev_from_array(rdev); |
| 5614 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5615 | if (err) |
| 5616 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5617 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5618 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5619 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5620 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5621 | if (mddev->degraded) |
| 5622 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5623 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5624 | if (!err) |
| 5625 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5626 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5627 | return err; |
| 5628 | } |
| 5629 | |
| 5630 | /* otherwise, add_new_disk is only allowed |
| 5631 | * for major_version==0 superblocks |
| 5632 | */ |
| 5633 | if (mddev->major_version != 0) { |
| 5634 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5635 | mdname(mddev)); |
| 5636 | return -EINVAL; |
| 5637 | } |
| 5638 | |
| 5639 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5640 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5641 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5643 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5644 | "md: error, md_import_device() returned %ld\n", |
| 5645 | PTR_ERR(rdev)); |
| 5646 | return PTR_ERR(rdev); |
| 5647 | } |
| 5648 | rdev->desc_nr = info->number; |
| 5649 | if (info->raid_disk < mddev->raid_disks) |
| 5650 | rdev->raid_disk = info->raid_disk; |
| 5651 | else |
| 5652 | rdev->raid_disk = -1; |
| 5653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5655 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5656 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5657 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5658 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5659 | set_bit(WriteMostly, &rdev->flags); |
| 5660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5661 | if (!mddev->persistent) { |
| 5662 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5663 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5664 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5665 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5666 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5667 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5668 | err = bind_rdev_to_array(rdev, mddev); |
| 5669 | if (err) { |
| 5670 | export_rdev(rdev); |
| 5671 | return err; |
| 5672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5673 | } |
| 5674 | |
| 5675 | return 0; |
| 5676 | } |
| 5677 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5678 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | { |
| 5680 | char b[BDEVNAME_SIZE]; |
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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5683 | rdev = find_rdev(mddev, dev); |
| 5684 | if (!rdev) |
| 5685 | return -ENXIO; |
| 5686 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 5687 | clear_bit(Blocked, &rdev->flags); |
| 5688 | remove_and_add_spares(mddev, rdev); |
| 5689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5690 | if (rdev->raid_disk >= 0) |
| 5691 | goto busy; |
| 5692 | |
| 5693 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5694 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5695 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | |
| 5697 | return 0; |
| 5698 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5699 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5700 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5701 | return -EBUSY; |
| 5702 | } |
| 5703 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5704 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | { |
| 5706 | char b[BDEVNAME_SIZE]; |
| 5707 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5708 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | |
| 5710 | if (!mddev->pers) |
| 5711 | return -ENODEV; |
| 5712 | |
| 5713 | if (mddev->major_version != 0) { |
| 5714 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5715 | " version-0 superblocks.\n", |
| 5716 | mdname(mddev)); |
| 5717 | return -EINVAL; |
| 5718 | } |
| 5719 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5720 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5721 | "%s: personality does not support diskops!\n", |
| 5722 | mdname(mddev)); |
| 5723 | return -EINVAL; |
| 5724 | } |
| 5725 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5726 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: error, md_import_device() returned %ld\n", |
| 5730 | PTR_ERR(rdev)); |
| 5731 | return -EINVAL; |
| 5732 | } |
| 5733 | |
| 5734 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5735 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5737 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5739 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5741 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5742 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5744 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5745 | err = -EINVAL; |
| 5746 | goto abort_export; |
| 5747 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5748 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5749 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5750 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5751 | err = bind_rdev_to_array(rdev, mddev); |
| 5752 | if (err) |
| 5753 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5754 | |
| 5755 | /* |
| 5756 | * The rest should better be atomic, we can have disk failures |
| 5757 | * noticed in interrupt contexts ... |
| 5758 | */ |
| 5759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | rdev->raid_disk = -1; |
| 5761 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5762 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5763 | |
| 5764 | /* |
| 5765 | * Kick recovery, maybe this spare has to be added to the |
| 5766 | * array immediately. |
| 5767 | */ |
| 5768 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5769 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5770 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5771 | return 0; |
| 5772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5773 | abort_export: |
| 5774 | export_rdev(rdev); |
| 5775 | return err; |
| 5776 | } |
| 5777 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5778 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5779 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 5780 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5781 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5782 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 5783 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5784 | return -EBUSY; |
| 5785 | if (mddev->recovery || mddev->sync_thread) |
| 5786 | return -EBUSY; |
| 5787 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5788 | } |
| 5789 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5790 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 5791 | struct inode *inode; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5792 | if (mddev->bitmap) |
| 5793 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5794 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5795 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5796 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5797 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5798 | mdname(mddev)); |
| 5799 | return -EBADF; |
| 5800 | } |
| 5801 | |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 5802 | inode = mddev->bitmap_info.file->f_mapping->host; |
| 5803 | if (!S_ISREG(inode->i_mode)) { |
| 5804 | printk(KERN_ERR "%s: error: bitmap file must be a regular file\n", |
| 5805 | mdname(mddev)); |
| 5806 | err = -EBADF; |
| 5807 | } else if (!(mddev->bitmap_info.file->f_mode & FMODE_WRITE)) { |
| 5808 | printk(KERN_ERR "%s: error: bitmap file must open for write\n", |
| 5809 | mdname(mddev)); |
| 5810 | err = -EBADF; |
| 5811 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5812 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5813 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 5814 | err = -EBUSY; |
| 5815 | } |
| 5816 | if (err) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5817 | fput(mddev->bitmap_info.file); |
| 5818 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5819 | return err; |
| 5820 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5821 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5822 | } else if (mddev->bitmap == NULL) |
| 5823 | return -ENOENT; /* cannot remove what isn't there */ |
| 5824 | err = 0; |
| 5825 | if (mddev->pers) { |
| 5826 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5827 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5828 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5829 | if (!err) |
| 5830 | err = bitmap_load(mddev); |
| 5831 | } |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5832 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5833 | bitmap_destroy(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5834 | fd = -1; /* make sure to put the file */ |
| 5835 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5836 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5837 | } |
| 5838 | if (fd < 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 5839 | if (mddev->bitmap_info.file) |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5840 | fput(mddev->bitmap_info.file); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5841 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5842 | } |
| 5843 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5844 | return err; |
| 5845 | } |
| 5846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5847 | /* |
| 5848 | * set_array_info is used two different ways |
| 5849 | * The original usage is when creating a new array. |
| 5850 | * In this usage, raid_disks is > 0 and it together with |
| 5851 | * level, size, not_persistent,layout,chunksize determine the |
| 5852 | * shape of the array. |
| 5853 | * This will always create an array with a type-0.90.0 superblock. |
| 5854 | * The newer usage is when assembling an array. |
| 5855 | * In this case raid_disks will be 0, and the major_version field is |
| 5856 | * use to determine which style super-blocks are to be found on the devices. |
| 5857 | * The minor and patch _version numbers are also kept incase the |
| 5858 | * super_block handler wishes to interpret them. |
| 5859 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5860 | 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] | 5861 | { |
| 5862 | |
| 5863 | if (info->raid_disks == 0) { |
| 5864 | /* just setting version number for superblock loading */ |
| 5865 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5866 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | super_types[info->major_version].name == NULL) { |
| 5868 | /* maybe try to auto-load a module? */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5869 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5870 | "md: superblock version %d not known\n", |
| 5871 | info->major_version); |
| 5872 | return -EINVAL; |
| 5873 | } |
| 5874 | mddev->major_version = info->major_version; |
| 5875 | mddev->minor_version = info->minor_version; |
| 5876 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5877 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5878 | /* ensure mddev_put doesn't delete this now that there |
| 5879 | * is some minimal configuration. |
| 5880 | */ |
| 5881 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5882 | return 0; |
| 5883 | } |
| 5884 | mddev->major_version = MD_MAJOR_VERSION; |
| 5885 | mddev->minor_version = MD_MINOR_VERSION; |
| 5886 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5887 | mddev->ctime = get_seconds(); |
| 5888 | |
| 5889 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5890 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5891 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5892 | mddev->raid_disks = info->raid_disks; |
| 5893 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5894 | * openned |
| 5895 | */ |
| 5896 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5897 | mddev->recovery_cp = MaxSector; |
| 5898 | else |
| 5899 | mddev->recovery_cp = 0; |
| 5900 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5901 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5902 | |
| 5903 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5904 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5905 | |
| 5906 | mddev->max_disks = MD_SB_DISKS; |
| 5907 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5908 | if (mddev->persistent) |
| 5909 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5910 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5911 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5912 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5913 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5914 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5915 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5916 | mddev->reshape_position = MaxSector; |
| 5917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | /* |
| 5919 | * Generate a 128 bit UUID |
| 5920 | */ |
| 5921 | get_random_bytes(mddev->uuid, 16); |
| 5922 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5923 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5924 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5925 | mddev->new_layout = mddev->layout; |
| 5926 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5927 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | return 0; |
| 5930 | } |
| 5931 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5932 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5933 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5934 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5935 | |
| 5936 | if (mddev->external_size) |
| 5937 | return; |
| 5938 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5939 | mddev->array_sectors = array_sectors; |
| 5940 | } |
| 5941 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5942 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5943 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5944 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5945 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5946 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5947 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5948 | |
| 5949 | if (mddev->pers->resize == NULL) |
| 5950 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5951 | /* The "num_sectors" is the number of sectors of each device that |
| 5952 | * is used. This can only make sense for arrays with redundancy. |
| 5953 | * linear and raid0 always use whatever space is available. We can only |
| 5954 | * consider changing this number if no resync or reconstruction is |
| 5955 | * 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] | 5956 | * 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] | 5957 | * of each device. If num_sectors is zero, we find the largest size |
| 5958 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5959 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5960 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 5961 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5962 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 5963 | if (mddev->ro) |
| 5964 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5965 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5966 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5967 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5968 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5969 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5970 | num_sectors = avail; |
| 5971 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5972 | return -ENOSPC; |
| 5973 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5974 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5975 | if (!rv) |
| 5976 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5977 | return rv; |
| 5978 | } |
| 5979 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5980 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5981 | { |
| 5982 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5983 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5984 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5985 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5986 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 5987 | if (mddev->ro) |
| 5988 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5989 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5990 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5991 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5992 | if (mddev->sync_thread || |
| 5993 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 5994 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5995 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5996 | |
| 5997 | rdev_for_each(rdev, mddev) { |
| 5998 | if (mddev->raid_disks < raid_disks && |
| 5999 | rdev->data_offset < rdev->new_data_offset) |
| 6000 | return -EINVAL; |
| 6001 | if (mddev->raid_disks > raid_disks && |
| 6002 | rdev->data_offset > rdev->new_data_offset) |
| 6003 | return -EINVAL; |
| 6004 | } |
| 6005 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6006 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6007 | if (mddev->delta_disks < 0) |
| 6008 | mddev->reshape_backwards = 1; |
| 6009 | else if (mddev->delta_disks > 0) |
| 6010 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6011 | |
| 6012 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6013 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6014 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6015 | mddev->reshape_backwards = 0; |
| 6016 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6017 | return rv; |
| 6018 | } |
| 6019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | /* |
| 6021 | * update_array_info is used to change the configuration of an |
| 6022 | * on-line array. |
| 6023 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6024 | * fields in the info are checked against the array. |
| 6025 | * Any differences that cannot be handled will cause an error. |
| 6026 | * Normally, only one change can be managed at a time. |
| 6027 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6028 | 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] | 6029 | { |
| 6030 | int rv = 0; |
| 6031 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6032 | int state = 0; |
| 6033 | |
| 6034 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6035 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6036 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6037 | |
| 6038 | if (mddev->major_version != info->major_version || |
| 6039 | mddev->minor_version != info->minor_version || |
| 6040 | /* mddev->patch_version != info->patch_version || */ |
| 6041 | mddev->ctime != info->ctime || |
| 6042 | mddev->level != info->level || |
| 6043 | /* mddev->layout != info->layout || */ |
| 6044 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6045 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6046 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6047 | ((state^info->state) & 0xfffffe00) |
| 6048 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6049 | return -EINVAL; |
| 6050 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6051 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6052 | cnt++; |
| 6053 | if (mddev->raid_disks != info->raid_disks) |
| 6054 | cnt++; |
| 6055 | if (mddev->layout != info->layout) |
| 6056 | cnt++; |
| 6057 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6058 | cnt++; |
| 6059 | if (cnt == 0) |
| 6060 | return 0; |
| 6061 | if (cnt > 1) |
| 6062 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | |
| 6064 | if (mddev->layout != info->layout) { |
| 6065 | /* Change layout |
| 6066 | * we don't need to do anything at the md level, the |
| 6067 | * personality will take care of it all. |
| 6068 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6069 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6071 | else { |
| 6072 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6073 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6074 | if (rv) |
| 6075 | mddev->new_layout = mddev->layout; |
| 6076 | return rv; |
| 6077 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6079 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6080 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6081 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6082 | if (mddev->raid_disks != info->raid_disks) |
| 6083 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6085 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 6086 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6087 | return -EINVAL; |
| 6088 | if (mddev->recovery || mddev->sync_thread) |
| 6089 | return -EBUSY; |
| 6090 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6091 | /* add the bitmap */ |
| 6092 | if (mddev->bitmap) |
| 6093 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6094 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6095 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6096 | mddev->bitmap_info.offset = |
| 6097 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6098 | mddev->bitmap_info.space = |
| 6099 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6100 | mddev->pers->quiesce(mddev, 1); |
| 6101 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6102 | if (!rv) |
| 6103 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6104 | if (rv) |
| 6105 | bitmap_destroy(mddev); |
| 6106 | mddev->pers->quiesce(mddev, 0); |
| 6107 | } else { |
| 6108 | /* remove the bitmap */ |
| 6109 | if (!mddev->bitmap) |
| 6110 | return -ENOENT; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 6111 | if (mddev->bitmap->storage.file) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6112 | return -EINVAL; |
| 6113 | mddev->pers->quiesce(mddev, 1); |
| 6114 | bitmap_destroy(mddev); |
| 6115 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6116 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6117 | } |
| 6118 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6119 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | return rv; |
| 6121 | } |
| 6122 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6123 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6124 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6125 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6126 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6127 | |
| 6128 | if (mddev->pers == NULL) |
| 6129 | return -ENODEV; |
| 6130 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6131 | rcu_read_lock(); |
| 6132 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6133 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6134 | err = -ENODEV; |
| 6135 | else { |
| 6136 | md_error(mddev, rdev); |
| 6137 | if (!test_bit(Faulty, &rdev->flags)) |
| 6138 | err = -EBUSY; |
| 6139 | } |
| 6140 | rcu_read_unlock(); |
| 6141 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | } |
| 6143 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6144 | /* |
| 6145 | * We have a problem here : there is no easy way to give a CHS |
| 6146 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6147 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6148 | * dosfs just mad... ;-) |
| 6149 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6150 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6151 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6152 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6153 | |
| 6154 | geo->heads = 2; |
| 6155 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6156 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6157 | return 0; |
| 6158 | } |
| 6159 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6160 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 6161 | { |
| 6162 | switch (cmd) { |
| 6163 | case ADD_NEW_DISK: |
| 6164 | case BLKROSET: |
| 6165 | case GET_ARRAY_INFO: |
| 6166 | case GET_BITMAP_FILE: |
| 6167 | case GET_DISK_INFO: |
| 6168 | case HOT_ADD_DISK: |
| 6169 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6170 | case RAID_AUTORUN: |
| 6171 | case RAID_VERSION: |
| 6172 | case RESTART_ARRAY_RW: |
| 6173 | case RUN_ARRAY: |
| 6174 | case SET_ARRAY_INFO: |
| 6175 | case SET_BITMAP_FILE: |
| 6176 | case SET_DISK_FAULTY: |
| 6177 | case STOP_ARRAY: |
| 6178 | case STOP_ARRAY_RO: |
| 6179 | return true; |
| 6180 | default: |
| 6181 | return false; |
| 6182 | } |
| 6183 | } |
| 6184 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6185 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6186 | unsigned int cmd, unsigned long arg) |
| 6187 | { |
| 6188 | int err = 0; |
| 6189 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6190 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6191 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6192 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6193 | if (!md_ioctl_valid(cmd)) |
| 6194 | return -ENOTTY; |
| 6195 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6196 | switch (cmd) { |
| 6197 | case RAID_VERSION: |
| 6198 | case GET_ARRAY_INFO: |
| 6199 | case GET_DISK_INFO: |
| 6200 | break; |
| 6201 | default: |
| 6202 | if (!capable(CAP_SYS_ADMIN)) |
| 6203 | return -EACCES; |
| 6204 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6205 | |
| 6206 | /* |
| 6207 | * Commands dealing with the RAID driver but not any |
| 6208 | * particular array: |
| 6209 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6210 | switch (cmd) { |
| 6211 | case RAID_VERSION: |
| 6212 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6213 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6215 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6216 | case RAID_AUTORUN: |
| 6217 | err = 0; |
| 6218 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6219 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6220 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6221 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | /* |
| 6225 | * Commands creating/starting a new array: |
| 6226 | */ |
| 6227 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6228 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6229 | |
| 6230 | if (!mddev) { |
| 6231 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6232 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6233 | } |
| 6234 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6235 | /* Some actions do not requires the mutex */ |
| 6236 | switch (cmd) { |
| 6237 | case GET_ARRAY_INFO: |
| 6238 | if (!mddev->raid_disks && !mddev->external) |
| 6239 | err = -ENODEV; |
| 6240 | else |
| 6241 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6242 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6243 | |
| 6244 | case GET_DISK_INFO: |
| 6245 | if (!mddev->raid_disks && !mddev->external) |
| 6246 | err = -ENODEV; |
| 6247 | else |
| 6248 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6249 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6250 | |
| 6251 | case SET_DISK_FAULTY: |
| 6252 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6253 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6254 | } |
| 6255 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 6256 | if (cmd == ADD_NEW_DISK) |
| 6257 | /* need to ensure md_delayed_delete() has completed */ |
| 6258 | flush_workqueue(md_misc_wq); |
| 6259 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 6260 | if (cmd == HOT_REMOVE_DISK) |
| 6261 | /* need to ensure recovery thread has run */ |
| 6262 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 6263 | !test_bit(MD_RECOVERY_NEEDED, |
| 6264 | &mddev->flags), |
| 6265 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6266 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 6267 | /* Need to flush page cache, and ensure no-one else opens |
| 6268 | * and writes |
| 6269 | */ |
| 6270 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6271 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6272 | mutex_unlock(&mddev->open_mutex); |
| 6273 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6274 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6275 | } |
| 6276 | set_bit(MD_STILL_CLOSED, &mddev->flags); |
| 6277 | mutex_unlock(&mddev->open_mutex); |
| 6278 | sync_blockdev(bdev); |
| 6279 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6280 | err = mddev_lock(mddev); |
| 6281 | if (err) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6282 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6283 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6284 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6285 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6286 | } |
| 6287 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6288 | if (cmd == SET_ARRAY_INFO) { |
| 6289 | mdu_array_info_t info; |
| 6290 | if (!arg) |
| 6291 | memset(&info, 0, sizeof(info)); |
| 6292 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6293 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6294 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6295 | } |
| 6296 | if (mddev->pers) { |
| 6297 | err = update_array_info(mddev, &info); |
| 6298 | if (err) { |
| 6299 | printk(KERN_WARNING "md: couldn't update" |
| 6300 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6301 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6302 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6303 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6304 | } |
| 6305 | if (!list_empty(&mddev->disks)) { |
| 6306 | printk(KERN_WARNING |
| 6307 | "md: array %s already has disks!\n", |
| 6308 | mdname(mddev)); |
| 6309 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6310 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6311 | } |
| 6312 | if (mddev->raid_disks) { |
| 6313 | printk(KERN_WARNING |
| 6314 | "md: array %s already initialised!\n", |
| 6315 | mdname(mddev)); |
| 6316 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6317 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6318 | } |
| 6319 | err = set_array_info(mddev, &info); |
| 6320 | if (err) { |
| 6321 | printk(KERN_WARNING "md: couldn't set" |
| 6322 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6323 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6324 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6325 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6326 | } |
| 6327 | |
| 6328 | /* |
| 6329 | * Commands querying/configuring an existing array: |
| 6330 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6331 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6332 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6333 | if ((!mddev->raid_disks && !mddev->external) |
| 6334 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6335 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6336 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6337 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6338 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6339 | } |
| 6340 | |
| 6341 | /* |
| 6342 | * Commands even a read-only array can execute: |
| 6343 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6344 | switch (cmd) { |
| 6345 | case GET_BITMAP_FILE: |
| 6346 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6347 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6348 | |
| 6349 | case RESTART_ARRAY_RW: |
| 6350 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6351 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6352 | |
| 6353 | case STOP_ARRAY: |
| 6354 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6355 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6356 | |
| 6357 | case STOP_ARRAY_RO: |
| 6358 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6359 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6360 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6361 | case HOT_REMOVE_DISK: |
| 6362 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6363 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6364 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6365 | case ADD_NEW_DISK: |
| 6366 | /* We can support ADD_NEW_DISK on read-only arrays |
| 6367 | * on if we are re-adding a preexisting device. |
| 6368 | * So require mddev->pers and MD_DISK_SYNC. |
| 6369 | */ |
| 6370 | if (mddev->pers) { |
| 6371 | mdu_disk_info_t info; |
| 6372 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6373 | err = -EFAULT; |
| 6374 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 6375 | /* Need to clear read-only for this */ |
| 6376 | break; |
| 6377 | else |
| 6378 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6379 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6380 | } |
| 6381 | break; |
| 6382 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6383 | case BLKROSET: |
| 6384 | if (get_user(ro, (int __user *)(arg))) { |
| 6385 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6386 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6387 | } |
| 6388 | err = -EINVAL; |
| 6389 | |
| 6390 | /* if the bdev is going readonly the value of mddev->ro |
| 6391 | * does not matter, no writes are coming |
| 6392 | */ |
| 6393 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6394 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6395 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6396 | /* are we are already prepared for writes? */ |
| 6397 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6398 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6399 | |
| 6400 | /* transitioning to readauto need only happen for |
| 6401 | * arrays that call md_write_start |
| 6402 | */ |
| 6403 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6404 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6405 | if (err == 0) { |
| 6406 | mddev->ro = 2; |
| 6407 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6408 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6409 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6410 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6411 | } |
| 6412 | |
| 6413 | /* |
| 6414 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6415 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6416 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 6417 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6418 | if (mddev->ro == 2) { |
| 6419 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6420 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6421 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6422 | /* mddev_unlock will wake thread */ |
| 6423 | /* If a device failed while we were read-only, we |
| 6424 | * need to make sure the metadata is updated now. |
| 6425 | */ |
| 6426 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
| 6427 | mddev_unlock(mddev); |
| 6428 | wait_event(mddev->sb_wait, |
| 6429 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && |
| 6430 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6431 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6432 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6433 | } else { |
| 6434 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6435 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6437 | } |
| 6438 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6439 | switch (cmd) { |
| 6440 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6441 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6442 | mdu_disk_info_t info; |
| 6443 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6444 | err = -EFAULT; |
| 6445 | else |
| 6446 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6447 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6448 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6449 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6450 | case HOT_ADD_DISK: |
| 6451 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6452 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6454 | case RUN_ARRAY: |
| 6455 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6456 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6457 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6458 | case SET_BITMAP_FILE: |
| 6459 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6460 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6461 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6462 | default: |
| 6463 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6464 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6465 | } |
| 6466 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6467 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6468 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6469 | err != -EINVAL) |
| 6470 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6471 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6472 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6473 | return err; |
| 6474 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6475 | #ifdef CONFIG_COMPAT |
| 6476 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6477 | unsigned int cmd, unsigned long arg) |
| 6478 | { |
| 6479 | switch (cmd) { |
| 6480 | case HOT_REMOVE_DISK: |
| 6481 | case HOT_ADD_DISK: |
| 6482 | case SET_DISK_FAULTY: |
| 6483 | case SET_BITMAP_FILE: |
| 6484 | /* These take in integer arg, do not convert */ |
| 6485 | break; |
| 6486 | default: |
| 6487 | arg = (unsigned long)compat_ptr(arg); |
| 6488 | break; |
| 6489 | } |
| 6490 | |
| 6491 | return md_ioctl(bdev, mode, cmd, arg); |
| 6492 | } |
| 6493 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6494 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6495 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6496 | { |
| 6497 | /* |
| 6498 | * Succeed if we can lock the mddev, which confirms that |
| 6499 | * it isn't being stopped right now. |
| 6500 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6501 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6502 | int err; |
| 6503 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 6504 | if (!mddev) |
| 6505 | return -ENODEV; |
| 6506 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6507 | if (mddev->gendisk != bdev->bd_disk) { |
| 6508 | /* we are racing with mddev_put which is discarding this |
| 6509 | * bd_disk. |
| 6510 | */ |
| 6511 | mddev_put(mddev); |
| 6512 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6513 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6514 | /* Then retry the open from the top */ |
| 6515 | return -ERESTARTSYS; |
| 6516 | } |
| 6517 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6518 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6519 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6520 | goto out; |
| 6521 | |
| 6522 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6523 | atomic_inc(&mddev->openers); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6524 | clear_bit(MD_STILL_CLOSED, &mddev->flags); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6525 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6526 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6527 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6528 | out: |
| 6529 | return err; |
| 6530 | } |
| 6531 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 6532 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6533 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6534 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6536 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6537 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6538 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6540 | |
| 6541 | static int md_media_changed(struct gendisk *disk) |
| 6542 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6543 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6544 | |
| 6545 | return mddev->changed; |
| 6546 | } |
| 6547 | |
| 6548 | static int md_revalidate(struct gendisk *disk) |
| 6549 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6550 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6551 | |
| 6552 | mddev->changed = 0; |
| 6553 | return 0; |
| 6554 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6555 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6556 | { |
| 6557 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6558 | .open = md_open, |
| 6559 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6560 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6561 | #ifdef CONFIG_COMPAT |
| 6562 | .compat_ioctl = md_compat_ioctl, |
| 6563 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6564 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6565 | .media_changed = md_media_changed, |
| 6566 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6567 | }; |
| 6568 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6569 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6570 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6571 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6573 | /* |
| 6574 | * md_thread is a 'system-thread', it's priority should be very |
| 6575 | * high. We avoid resource deadlocks individually in each |
| 6576 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6577 | * the very same RT priority as kswapd, thus we will never get |
| 6578 | * into a priority inversion deadlock. |
| 6579 | * |
| 6580 | * we definitely have to have equal or higher priority than |
| 6581 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6582 | * many dirty RAID5 blocks. |
| 6583 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6584 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6585 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6586 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6587 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6588 | /* We need to wait INTERRUPTIBLE so that |
| 6589 | * we don't add to the load-average. |
| 6590 | * That means we need to be sure no signals are |
| 6591 | * pending |
| 6592 | */ |
| 6593 | if (signal_pending(current)) |
| 6594 | flush_signals(current); |
| 6595 | |
| 6596 | wait_event_interruptible_timeout |
| 6597 | (thread->wqueue, |
| 6598 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6599 | || kthread_should_stop(), |
| 6600 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6601 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6602 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6603 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6604 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6605 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6607 | return 0; |
| 6608 | } |
| 6609 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6610 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6611 | { |
| 6612 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6613 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6614 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6615 | wake_up(&thread->wqueue); |
| 6616 | } |
| 6617 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 6618 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6619 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6620 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 6621 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6622 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6623 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6624 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6625 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6626 | if (!thread) |
| 6627 | return NULL; |
| 6628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6629 | init_waitqueue_head(&thread->wqueue); |
| 6630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6631 | thread->run = run; |
| 6632 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6633 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6634 | thread->tsk = kthread_run(md_thread, thread, |
| 6635 | "%s_%s", |
| 6636 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 6637 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6638 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6639 | kfree(thread); |
| 6640 | return NULL; |
| 6641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6642 | return thread; |
| 6643 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 6644 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6645 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6646 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6647 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6648 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6649 | if (!thread) |
| 6650 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6651 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6652 | /* Locking ensures that mddev_unlock does not wake_up a |
| 6653 | * non-existent thread |
| 6654 | */ |
| 6655 | spin_lock(&pers_lock); |
| 6656 | *threadp = NULL; |
| 6657 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6658 | |
| 6659 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6660 | kfree(thread); |
| 6661 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 6662 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6663 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6664 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6665 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6666 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6667 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6668 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6669 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6670 | return; |
| 6671 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6672 | if (mddev->degraded) |
| 6673 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6674 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6675 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6676 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6677 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6678 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6679 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6680 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6681 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 6682 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6683 | |
| 6684 | /* seq_file implementation /proc/mdstat */ |
| 6685 | |
| 6686 | static void status_unused(struct seq_file *seq) |
| 6687 | { |
| 6688 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6689 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6690 | |
| 6691 | seq_printf(seq, "unused devices: "); |
| 6692 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6693 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6694 | char b[BDEVNAME_SIZE]; |
| 6695 | i++; |
| 6696 | seq_printf(seq, "%s ", |
| 6697 | bdevname(rdev->bdev,b)); |
| 6698 | } |
| 6699 | if (!i) |
| 6700 | seq_printf(seq, "<none>"); |
| 6701 | |
| 6702 | seq_printf(seq, "\n"); |
| 6703 | } |
| 6704 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6705 | static void status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6706 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6707 | sector_t max_sectors, resync, res; |
| 6708 | unsigned long dt, db; |
| 6709 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6710 | int scale; |
| 6711 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6712 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 6713 | if (mddev->curr_resync <= 3) |
| 6714 | resync = 0; |
| 6715 | else |
| 6716 | resync = mddev->curr_resync |
| 6717 | - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6718 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 6719 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 6720 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6721 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6722 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6723 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6724 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 6725 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6726 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6727 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6728 | * u32, as those are the requirements for sector_div. |
| 6729 | * Thus 'scale' must be at least 10 |
| 6730 | */ |
| 6731 | scale = 10; |
| 6732 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6733 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6734 | scale++; |
| 6735 | } |
| 6736 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6737 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6738 | |
| 6739 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6740 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6741 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6742 | seq_printf(seq, "["); |
| 6743 | for (i = 0; i < x; i++) |
| 6744 | seq_printf(seq, "="); |
| 6745 | seq_printf(seq, ">"); |
| 6746 | for (i = 0; i < y; i++) |
| 6747 | seq_printf(seq, "."); |
| 6748 | seq_printf(seq, "] "); |
| 6749 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6750 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6751 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6752 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6753 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6754 | "check" : |
| 6755 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6756 | "resync" : "recovery"))), |
| 6757 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6758 | (unsigned long long) resync/2, |
| 6759 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6760 | |
| 6761 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | * dt: time from mark until now |
| 6763 | * db: blocks written from mark until now |
| 6764 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6765 | * |
| 6766 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6767 | * So we divide before multiply in case it is 32bit and close |
| 6768 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6769 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6770 | * near the end of resync when the number of remaining sectors |
| 6771 | * is close to 'db'. |
| 6772 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6773 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6774 | */ |
| 6775 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6776 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6777 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6778 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6779 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6780 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6781 | sector_div(rt, db/32+1); |
| 6782 | rt *= dt; |
| 6783 | rt >>= 5; |
| 6784 | |
| 6785 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6786 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6787 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6788 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6789 | } |
| 6790 | |
| 6791 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6792 | { |
| 6793 | struct list_head *tmp; |
| 6794 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6795 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6796 | |
| 6797 | if (l >= 0x10000) |
| 6798 | return NULL; |
| 6799 | if (!l--) |
| 6800 | /* header */ |
| 6801 | return (void*)1; |
| 6802 | |
| 6803 | spin_lock(&all_mddevs_lock); |
| 6804 | list_for_each(tmp,&all_mddevs) |
| 6805 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6806 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6807 | mddev_get(mddev); |
| 6808 | spin_unlock(&all_mddevs_lock); |
| 6809 | return mddev; |
| 6810 | } |
| 6811 | spin_unlock(&all_mddevs_lock); |
| 6812 | if (!l--) |
| 6813 | return (void*)2;/* tail */ |
| 6814 | return NULL; |
| 6815 | } |
| 6816 | |
| 6817 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6818 | { |
| 6819 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6820 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | ++*pos; |
| 6823 | if (v == (void*)2) |
| 6824 | return NULL; |
| 6825 | |
| 6826 | spin_lock(&all_mddevs_lock); |
| 6827 | if (v == (void*)1) |
| 6828 | tmp = all_mddevs.next; |
| 6829 | else |
| 6830 | tmp = mddev->all_mddevs.next; |
| 6831 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6832 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6833 | else { |
| 6834 | next_mddev = (void*)2; |
| 6835 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6836 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6837 | spin_unlock(&all_mddevs_lock); |
| 6838 | |
| 6839 | if (v != (void*)1) |
| 6840 | mddev_put(mddev); |
| 6841 | return next_mddev; |
| 6842 | |
| 6843 | } |
| 6844 | |
| 6845 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6846 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6847 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6848 | |
| 6849 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6850 | mddev_put(mddev); |
| 6851 | } |
| 6852 | |
| 6853 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6854 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6855 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6856 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6857 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6858 | |
| 6859 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6860 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6861 | seq_printf(seq, "Personalities : "); |
| 6862 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6863 | list_for_each_entry(pers, &pers_list, list) |
| 6864 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6865 | |
| 6866 | spin_unlock(&pers_lock); |
| 6867 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6868 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6869 | return 0; |
| 6870 | } |
| 6871 | if (v == (void*)2) { |
| 6872 | status_unused(seq); |
| 6873 | return 0; |
| 6874 | } |
| 6875 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6876 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6877 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6879 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6880 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6881 | mddev->pers ? "" : "in"); |
| 6882 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6883 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6884 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6885 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6886 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6887 | seq_printf(seq, " %s", mddev->pers->name); |
| 6888 | } |
| 6889 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6890 | sectors = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6891 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | char b[BDEVNAME_SIZE]; |
| 6893 | seq_printf(seq, " %s[%d]", |
| 6894 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6895 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6896 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6897 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6898 | seq_printf(seq, "(F)"); |
| 6899 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 6900 | } |
| 6901 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6902 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 6903 | if (test_bit(Replacement, &rdev->flags)) |
| 6904 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6905 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6906 | } |
| 6907 | |
| 6908 | if (!list_empty(&mddev->disks)) { |
| 6909 | if (mddev->pers) |
| 6910 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6911 | (unsigned long long) |
| 6912 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6913 | else |
| 6914 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6915 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6916 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6917 | if (mddev->persistent) { |
| 6918 | if (mddev->major_version != 0 || |
| 6919 | mddev->minor_version != 90) { |
| 6920 | seq_printf(seq," super %d.%d", |
| 6921 | mddev->major_version, |
| 6922 | mddev->minor_version); |
| 6923 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6924 | } else if (mddev->external) |
| 6925 | seq_printf(seq, " super external:%s", |
| 6926 | mddev->metadata_type); |
| 6927 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6928 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6929 | |
| 6930 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6931 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6932 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6933 | if (mddev->pers->sync_request) { |
| 6934 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6935 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6936 | seq_printf(seq, "\n "); |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 6937 | } else if (mddev->curr_resync >= 1) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6938 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6939 | else if (mddev->recovery_cp < MaxSector) |
| 6940 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6941 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6942 | } else |
| 6943 | seq_printf(seq, "\n "); |
| 6944 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 6945 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6946 | |
| 6947 | seq_printf(seq, "\n"); |
| 6948 | } |
| 6949 | mddev_unlock(mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6951 | return 0; |
| 6952 | } |
| 6953 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6954 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6955 | .start = md_seq_start, |
| 6956 | .next = md_seq_next, |
| 6957 | .stop = md_seq_stop, |
| 6958 | .show = md_seq_show, |
| 6959 | }; |
| 6960 | |
| 6961 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6962 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6963 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6964 | int error; |
| 6965 | |
| 6966 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6967 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6968 | return error; |
| 6969 | |
| 6970 | seq = file->private_data; |
| 6971 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6972 | return error; |
| 6973 | } |
| 6974 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 6975 | static int md_unloading; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6976 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6977 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6978 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6979 | int mask; |
| 6980 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 6981 | if (md_unloading) |
kbuild test robot | 7d7e64f | 2014-12-03 16:07:59 +1100 | [diff] [blame] | 6982 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6983 | poll_wait(filp, &md_event_waiters, wait); |
| 6984 | |
| 6985 | /* always allow read */ |
| 6986 | mask = POLLIN | POLLRDNORM; |
| 6987 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 6988 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6989 | mask |= POLLERR | POLLPRI; |
| 6990 | return mask; |
| 6991 | } |
| 6992 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6993 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6994 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6995 | .open = md_seq_open, |
| 6996 | .read = seq_read, |
| 6997 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6998 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6999 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7000 | }; |
| 7001 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7002 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7003 | { |
Chao Yu | 50bd377 | 2014-09-25 15:28:34 +0800 | [diff] [blame] | 7004 | printk(KERN_INFO "md: %s personality registered for level %d\n", |
| 7005 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7006 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7007 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7008 | spin_unlock(&pers_lock); |
| 7009 | return 0; |
| 7010 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7011 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7012 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7013 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7014 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7015 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7016 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7017 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7018 | spin_unlock(&pers_lock); |
| 7019 | return 0; |
| 7020 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7021 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7022 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7023 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7024 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7025 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7026 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7027 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7028 | |
| 7029 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7030 | rcu_read_lock(); |
| 7031 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7032 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7033 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7034 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7035 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7036 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7037 | * as sync_io is counted when a request starts, and |
| 7038 | * disk_stats is counted when it completes. |
| 7039 | * So resync activity will cause curr_events to be smaller than |
| 7040 | * when there was no such activity. |
| 7041 | * non-sync IO will cause disk_stat to increase without |
| 7042 | * increasing sync_io so curr_events will (eventually) |
| 7043 | * be larger than it was before. Once it becomes |
| 7044 | * substantially larger, the test below will cause |
| 7045 | * the array to appear non-idle, and resync will slow |
| 7046 | * down. |
| 7047 | * If there is a lot of outstanding resync activity when |
| 7048 | * we set last_event to curr_events, then all that activity |
| 7049 | * completing might cause the array to appear non-idle |
| 7050 | * and resync will be slowed down even though there might |
| 7051 | * not have been non-resync activity. This will only |
| 7052 | * happen once though. 'last_events' will soon reflect |
| 7053 | * the state where there is little or no outstanding |
| 7054 | * resync requests, and further resync activity will |
| 7055 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7056 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7057 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7058 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7059 | rdev->last_events = curr_events; |
| 7060 | idle = 0; |
| 7061 | } |
| 7062 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7063 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7064 | return idle; |
| 7065 | } |
| 7066 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7067 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7068 | { |
| 7069 | /* another "blocks" (512byte) blocks have been synced */ |
| 7070 | atomic_sub(blocks, &mddev->recovery_active); |
| 7071 | wake_up(&mddev->recovery_wait); |
| 7072 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7073 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7074 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7075 | md_wakeup_thread(mddev->thread); |
| 7076 | // stop recovery, signal do_sync .... |
| 7077 | } |
| 7078 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7079 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7080 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7081 | /* md_write_start(mddev, bi) |
| 7082 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7083 | * in superblock) before writing, schedule a superblock update |
| 7084 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7085 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7086 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7087 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7088 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7089 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7090 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7091 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7092 | BUG_ON(mddev->ro == 1); |
| 7093 | if (mddev->ro == 2) { |
| 7094 | /* need to switch to read/write */ |
| 7095 | mddev->ro = 0; |
| 7096 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7097 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7098 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7099 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7100 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7101 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7102 | if (mddev->safemode == 1) |
| 7103 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7104 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7105 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7106 | if (mddev->in_sync) { |
| 7107 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7108 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7109 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7110 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7111 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7112 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7113 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7114 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7115 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7116 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7117 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7118 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7119 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7120 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7121 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7122 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7123 | { |
| 7124 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7125 | if (mddev->safemode == 2) |
| 7126 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7127 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7128 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7129 | } |
| 7130 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7131 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7132 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7133 | /* md_allow_write(mddev) |
| 7134 | * Calling this ensures that the array is marked 'active' so that writes |
| 7135 | * may proceed without blocking. It is important to call this before |
| 7136 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7137 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7138 | * |
| 7139 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7140 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7141 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7142 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7143 | { |
| 7144 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7145 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7146 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7147 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7148 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7149 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7150 | |
| 7151 | spin_lock_irq(&mddev->write_lock); |
| 7152 | if (mddev->in_sync) { |
| 7153 | mddev->in_sync = 0; |
| 7154 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7155 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7156 | if (mddev->safemode_delay && |
| 7157 | mddev->safemode == 0) |
| 7158 | mddev->safemode = 1; |
| 7159 | spin_unlock_irq(&mddev->write_lock); |
| 7160 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7161 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7162 | } else |
| 7163 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7164 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7165 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7166 | return -EAGAIN; |
| 7167 | else |
| 7168 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7169 | } |
| 7170 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7172 | #define SYNC_MARKS 10 |
| 7173 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7174 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7175 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7176 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7177 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7178 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7179 | unsigned int currspeed = 0, |
| 7180 | window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7181 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7182 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7183 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7184 | sector_t mark_cnt[SYNC_MARKS]; |
| 7185 | int last_mark,m; |
| 7186 | struct list_head *tmp; |
| 7187 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7188 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7189 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7190 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7191 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7192 | |
| 7193 | /* just incase thread restarts... */ |
| 7194 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7195 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7196 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 7197 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7198 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7200 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7201 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7202 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7203 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7204 | action = "check"; |
| 7205 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7206 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7207 | action = "repair"; |
| 7208 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7209 | desc = "resync"; |
| 7210 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7211 | desc = "reshape"; |
| 7212 | else |
| 7213 | desc = "recovery"; |
| 7214 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7215 | mddev->last_sync_action = action ?: desc; |
| 7216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7217 | /* we overload curr_resync somewhat here. |
| 7218 | * 0 == not engaged in resync at all |
| 7219 | * 2 == checking that there is no conflict with another sync |
| 7220 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7221 | * commense |
| 7222 | * other == active in resync - this many blocks |
| 7223 | * |
| 7224 | * Before starting a resync we must have set curr_resync to |
| 7225 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7226 | * less than ours. When we find one that is the same or higher |
| 7227 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7228 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7229 | * This will mean we have to start checking from the beginning again. |
| 7230 | * |
| 7231 | */ |
| 7232 | |
| 7233 | do { |
| 7234 | mddev->curr_resync = 2; |
| 7235 | |
| 7236 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7237 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7238 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7239 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7240 | if (mddev2 == mddev) |
| 7241 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7242 | if (!mddev->parallel_resync |
| 7243 | && mddev2->curr_resync |
| 7244 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7245 | DEFINE_WAIT(wq); |
| 7246 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7247 | /* arbitrarily yield */ |
| 7248 | mddev->curr_resync = 1; |
| 7249 | wake_up(&resync_wait); |
| 7250 | } |
| 7251 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7252 | /* no need to wait here, we can wait the next |
| 7253 | * time 'round when curr_resync == 2 |
| 7254 | */ |
| 7255 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7256 | /* We need to wait 'interruptible' so as not to |
| 7257 | * contribute to the load average, and not to |
| 7258 | * be caught by 'softlockup' |
| 7259 | */ |
| 7260 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7261 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7262 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7263 | printk(KERN_INFO "md: delaying %s of %s" |
| 7264 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7265 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7266 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7267 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7268 | if (signal_pending(current)) |
| 7269 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7270 | schedule(); |
| 7271 | finish_wait(&resync_wait, &wq); |
| 7272 | goto try_again; |
| 7273 | } |
| 7274 | finish_wait(&resync_wait, &wq); |
| 7275 | } |
| 7276 | } |
| 7277 | } while (mddev->curr_resync < 2); |
| 7278 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7279 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7280 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7281 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7282 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7283 | */ |
| 7284 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 7285 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7286 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7287 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7288 | j = mddev->resync_min; |
| 7289 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7290 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7291 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7292 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7293 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7294 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7295 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7296 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7297 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7298 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7299 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7300 | if (rdev->raid_disk >= 0 && |
| 7301 | !test_bit(Faulty, &rdev->flags) && |
| 7302 | !test_bit(In_sync, &rdev->flags) && |
| 7303 | rdev->recovery_offset < j) |
| 7304 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7305 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 7306 | |
| 7307 | /* If there is a bitmap, we need to make sure all |
| 7308 | * writes that started before we added a spare |
| 7309 | * complete before we start doing a recovery. |
| 7310 | * Otherwise the write might complete and (via |
| 7311 | * bitmap_endwrite) set a bit in the bitmap after the |
| 7312 | * recovery has checked that bit and skipped that |
| 7313 | * region. |
| 7314 | */ |
| 7315 | if (mddev->bitmap) { |
| 7316 | mddev->pers->quiesce(mddev, 1); |
| 7317 | mddev->pers->quiesce(mddev, 0); |
| 7318 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7320 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7321 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7322 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7323 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7324 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7325 | "(but not more than %d KB/sec) for %s.\n", |
| 7326 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7327 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7328 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7329 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7330 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7331 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7332 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7333 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7334 | } |
| 7335 | last_mark = 0; |
| 7336 | mddev->resync_mark = mark[last_mark]; |
| 7337 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7338 | |
| 7339 | /* |
| 7340 | * Tune reconstruction: |
| 7341 | */ |
| 7342 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7343 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7344 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7345 | |
| 7346 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7347 | last_check = 0; |
| 7348 | |
| 7349 | if (j>2) { |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7350 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7351 | "md: resuming %s of %s from checkpoint.\n", |
| 7352 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7353 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7354 | } else |
| 7355 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7356 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7357 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 7358 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7359 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7360 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7361 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7362 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7363 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7364 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7365 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7366 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7367 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7368 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7369 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7370 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7371 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7372 | (j - mddev->curr_resync_completed)*2 |
| 7373 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7374 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7375 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7376 | wait_event(mddev->recovery_wait, |
| 7377 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7378 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7379 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 7380 | j > mddev->recovery_cp) |
| 7381 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7382 | update_time = jiffies; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7383 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7384 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7385 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7386 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7387 | while (j >= mddev->resync_max && |
| 7388 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7389 | /* As this condition is controlled by user-space, |
| 7390 | * we can block indefinitely, so use '_interruptible' |
| 7391 | * to avoid triggering warnings. |
| 7392 | */ |
| 7393 | flush_signals(current); /* just in case */ |
| 7394 | wait_event_interruptible(mddev->recovery_wait, |
| 7395 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7396 | || test_bit(MD_RECOVERY_INTR, |
| 7397 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7398 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7399 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7400 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7401 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7402 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7403 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7404 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7405 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7406 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7407 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7408 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7409 | |
| 7410 | if (!skipped) { /* actual IO requested */ |
| 7411 | io_sectors += sectors; |
| 7412 | atomic_add(sectors, &mddev->recovery_active); |
| 7413 | } |
| 7414 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7415 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7416 | break; |
| 7417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7418 | j += sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7419 | if (j > 2) |
| 7420 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7421 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7422 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7423 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7424 | * visible in /proc/mdstat |
| 7425 | */ |
| 7426 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7427 | |
| 7428 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7429 | continue; |
| 7430 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7431 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7432 | repeat: |
| 7433 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7434 | /* step marks */ |
| 7435 | int next = (last_mark+1) % SYNC_MARKS; |
| 7436 | |
| 7437 | mddev->resync_mark = mark[next]; |
| 7438 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7439 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7440 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7441 | last_mark = next; |
| 7442 | } |
| 7443 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7444 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7445 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7446 | |
| 7447 | /* |
| 7448 | * this loop exits only if either when we are slower than |
| 7449 | * the 'hard' speed limit, or the system was IO-idle for |
| 7450 | * a jiffy. |
| 7451 | * the system might be non-idle CPU-wise, but we only care |
| 7452 | * about not overloading the IO subsystem. (things like an |
| 7453 | * e2fsck being done on the RAID array should execute fast) |
| 7454 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7455 | cond_resched(); |
| 7456 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7457 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 7458 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7459 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7460 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7461 | if (currspeed > speed_min(mddev)) { |
| 7462 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7463 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7464 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7465 | goto repeat; |
| 7466 | } |
| 7467 | } |
| 7468 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7469 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
| 7470 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 7471 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7472 | /* |
| 7473 | * this also signals 'finished resyncing' to md_stop |
| 7474 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7475 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7476 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7477 | |
| 7478 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7479 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7480 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7481 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7482 | mddev->curr_resync > 2) { |
| 7483 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7484 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7485 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7486 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7487 | "md: checkpointing %s of %s.\n", |
| 7488 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7489 | if (test_bit(MD_RECOVERY_ERROR, |
| 7490 | &mddev->recovery)) |
| 7491 | mddev->recovery_cp = |
| 7492 | mddev->curr_resync_completed; |
| 7493 | else |
| 7494 | mddev->recovery_cp = |
| 7495 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7496 | } |
| 7497 | } else |
| 7498 | mddev->recovery_cp = MaxSector; |
| 7499 | } else { |
| 7500 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7501 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7502 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7503 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7504 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7505 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7506 | !test_bit(Faulty, &rdev->flags) && |
| 7507 | !test_bit(In_sync, &rdev->flags) && |
| 7508 | rdev->recovery_offset < mddev->curr_resync) |
| 7509 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7510 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7512 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7513 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7514 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7515 | |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7516 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7517 | /* We completed so min/max setting can be forgotten if used. */ |
| 7518 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7519 | mddev->resync_min = 0; |
| 7520 | mddev->resync_max = MaxSector; |
| 7521 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7522 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7523 | mddev->curr_resync = 0; |
| 7524 | wake_up(&resync_wait); |
| 7525 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7526 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7527 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7528 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7529 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7530 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7531 | static int remove_and_add_spares(struct mddev *mddev, |
| 7532 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7533 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7534 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7535 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7536 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7537 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7538 | rdev_for_each(rdev, mddev) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7539 | if ((this == NULL || rdev == this) && |
| 7540 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7541 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7542 | (test_bit(Faulty, &rdev->flags) || |
| 7543 | ! test_bit(In_sync, &rdev->flags)) && |
| 7544 | atomic_read(&rdev->nr_pending)==0) { |
| 7545 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7546 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7547 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7548 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7549 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7550 | } |
| 7551 | } |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 7552 | if (removed && mddev->kobj.sd) |
| 7553 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7554 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7555 | if (this) |
| 7556 | goto no_add; |
| 7557 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7558 | rdev_for_each(rdev, mddev) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7559 | if (rdev->raid_disk >= 0 && |
| 7560 | !test_bit(In_sync, &rdev->flags) && |
| 7561 | !test_bit(Faulty, &rdev->flags)) |
| 7562 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7563 | if (rdev->raid_disk >= 0) |
| 7564 | continue; |
| 7565 | if (test_bit(Faulty, &rdev->flags)) |
| 7566 | continue; |
| 7567 | if (mddev->ro && |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 7568 | ! (rdev->saved_raid_disk >= 0 && |
| 7569 | !test_bit(Bitmap_sync, &rdev->flags))) |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7570 | continue; |
| 7571 | |
NeilBrown | 7eb4188 | 2014-01-14 15:55:14 +1100 | [diff] [blame] | 7572 | if (rdev->saved_raid_disk < 0) |
| 7573 | rdev->recovery_offset = 0; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7574 | if (mddev->pers-> |
| 7575 | hot_add_disk(mddev, rdev) == 0) { |
| 7576 | if (sysfs_link_rdev(mddev, rdev)) |
| 7577 | /* failure here is OK */; |
| 7578 | spares++; |
| 7579 | md_new_event(mddev); |
| 7580 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7581 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7582 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7583 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 7584 | if (removed) |
| 7585 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7586 | return spares; |
| 7587 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7588 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7589 | static void md_start_sync(struct work_struct *ws) |
| 7590 | { |
| 7591 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
| 7592 | |
| 7593 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7594 | mddev, |
| 7595 | "resync"); |
| 7596 | if (!mddev->sync_thread) { |
| 7597 | printk(KERN_ERR "%s: could not start resync" |
| 7598 | " thread...\n", |
| 7599 | mdname(mddev)); |
| 7600 | /* leave the spares where they are, it shouldn't hurt */ |
| 7601 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7602 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7603 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7604 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7605 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7606 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7607 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7608 | &mddev->recovery)) |
| 7609 | if (mddev->sysfs_action) |
| 7610 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7611 | } else |
| 7612 | md_wakeup_thread(mddev->sync_thread); |
| 7613 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7614 | md_new_event(mddev); |
| 7615 | } |
| 7616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7617 | /* |
| 7618 | * This routine is regularly called by all per-raid-array threads to |
| 7619 | * deal with generic issues like resync and super-block update. |
| 7620 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7621 | * need this as they never do any recovery or update the superblock. |
| 7622 | * |
| 7623 | * It does not do any resync itself, but rather "forks" off other threads |
| 7624 | * to do that as needed. |
| 7625 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7626 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7627 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7628 | * and wakeups up this thread which will reap the thread and finish up. |
| 7629 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7630 | * |
| 7631 | * The overall approach is: |
| 7632 | * 1/ if the superblock needs updating, update it. |
| 7633 | * 2/ If a recovery thread is running, don't do anything else. |
| 7634 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7635 | * 4/ If there are any faulty devices, remove them. |
| 7636 | * 5/ If array is degraded, try to add spares devices |
| 7637 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7638 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7639 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7640 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7641 | if (mddev->suspended) |
| 7642 | return; |
| 7643 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7644 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7645 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7646 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7647 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7648 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7649 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7650 | mdname(mddev)); |
| 7651 | mddev->safemode = 2; |
| 7652 | } |
| 7653 | flush_signals(current); |
| 7654 | } |
| 7655 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7656 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7657 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7658 | if ( ! ( |
NeilBrown | 142d44c | 2013-11-28 10:34:18 +1100 | [diff] [blame] | 7659 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7660 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7661 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7662 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7663 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7664 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7665 | )) |
| 7666 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7667 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7668 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7669 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7670 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7671 | if (mddev->ro) { |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7672 | /* On a read-only array we can: |
| 7673 | * - remove failed devices |
| 7674 | * - add already-in_sync devices if the array itself |
| 7675 | * is in-sync. |
| 7676 | * As we only add devices that are already in-sync, |
| 7677 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7678 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7679 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 7680 | /* There is no thread, but we need to call |
| 7681 | * ->spare_active and clear saved_raid_disk |
| 7682 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 7683 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 7684 | md_reap_sync_thread(mddev); |
| 7685 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7686 | goto unlock; |
| 7687 | } |
| 7688 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7689 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7690 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7691 | spin_lock_irq(&mddev->write_lock); |
| 7692 | if (mddev->safemode && |
| 7693 | !atomic_read(&mddev->writes_pending) && |
| 7694 | !mddev->in_sync && |
| 7695 | mddev->recovery_cp == MaxSector) { |
| 7696 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7697 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7698 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7699 | } |
| 7700 | if (mddev->safemode == 1) |
| 7701 | mddev->safemode = 0; |
| 7702 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7703 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7704 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7705 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7706 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 7707 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7708 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7710 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7711 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7712 | /* resync/recovery still happening */ |
| 7713 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7714 | goto unlock; |
| 7715 | } |
| 7716 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 7717 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7718 | goto unlock; |
| 7719 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7720 | /* Set RUNNING before clearing NEEDED to avoid |
| 7721 | * any transients in the value of "sync_action". |
| 7722 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7723 | mddev->curr_resync_completed = 0; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7724 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7725 | /* Clear some bits that don't mean anything, but |
| 7726 | * might be left set |
| 7727 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7728 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7729 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7730 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 7731 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 7732 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7733 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7734 | /* no recovery is running. |
| 7735 | * remove any failed drives, then |
| 7736 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7737 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7738 | * the personality to fail the re-add. |
| 7739 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7740 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7741 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7742 | if (mddev->pers->check_reshape == NULL || |
| 7743 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7744 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7745 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7746 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7747 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7748 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7749 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7750 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7751 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7752 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7753 | } else if (mddev->recovery_cp < MaxSector) { |
| 7754 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7755 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7756 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7757 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7758 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7760 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 7761 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7762 | /* We are adding a device or devices to an array |
| 7763 | * which has the bitmap stored on all devices. |
| 7764 | * So make sure all bitmap pages get written |
| 7765 | */ |
| 7766 | bitmap_write_all(mddev->bitmap); |
| 7767 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7768 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 7769 | queue_work(md_misc_wq, &mddev->del_work); |
| 7770 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7771 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7772 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7773 | if (!mddev->sync_thread) { |
| 7774 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7775 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7776 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7777 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7778 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7779 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7780 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 7781 | unlock: |
| 7782 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7783 | mddev_unlock(mddev); |
| 7784 | } |
| 7785 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7786 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7787 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 7788 | void md_reap_sync_thread(struct mddev *mddev) |
| 7789 | { |
| 7790 | struct md_rdev *rdev; |
| 7791 | |
| 7792 | /* resync has finished, collect result */ |
| 7793 | md_unregister_thread(&mddev->sync_thread); |
| 7794 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7795 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7796 | /* success...*/ |
| 7797 | /* activate any spares */ |
| 7798 | if (mddev->pers->spare_active(mddev)) { |
| 7799 | sysfs_notify(&mddev->kobj, NULL, |
| 7800 | "degraded"); |
| 7801 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 7802 | } |
| 7803 | } |
| 7804 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7805 | mddev->pers->finish_reshape) |
| 7806 | mddev->pers->finish_reshape(mddev); |
| 7807 | |
| 7808 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 7809 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 7810 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 7811 | if (!mddev->degraded) |
| 7812 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 7813 | rdev->saved_raid_disk = -1; |
| 7814 | |
| 7815 | md_update_sb(mddev, 1); |
| 7816 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7817 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7818 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7819 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7820 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7821 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 7822 | /* flag recovery needed just to double check */ |
| 7823 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7824 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7825 | md_new_event(mddev); |
| 7826 | if (mddev->event_work.func) |
| 7827 | queue_work(md_misc_wq, &mddev->event_work); |
| 7828 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7829 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 7830 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7831 | 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] | 7832 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7833 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7834 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7835 | !test_bit(Blocked, &rdev->flags) && |
| 7836 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7837 | msecs_to_jiffies(5000)); |
| 7838 | rdev_dec_pending(rdev, mddev); |
| 7839 | } |
| 7840 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7841 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7842 | void md_finish_reshape(struct mddev *mddev) |
| 7843 | { |
| 7844 | /* called be personality module when reshape completes. */ |
| 7845 | struct md_rdev *rdev; |
| 7846 | |
| 7847 | rdev_for_each(rdev, mddev) { |
| 7848 | if (rdev->data_offset > rdev->new_data_offset) |
| 7849 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 7850 | else |
| 7851 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 7852 | rdev->data_offset = rdev->new_data_offset; |
| 7853 | } |
| 7854 | } |
| 7855 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7856 | |
| 7857 | /* Bad block management. |
| 7858 | * We can record which blocks on each device are 'bad' and so just |
| 7859 | * fail those blocks, or that stripe, rather than the whole device. |
| 7860 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 7861 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 7862 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 7863 | * A 'shift' can be set so that larger blocks are tracked and |
| 7864 | * consequently larger devices can be covered. |
| 7865 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 7866 | * |
| 7867 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 7868 | * might need to retry if it is very unlucky. |
| 7869 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 7870 | * so we use the write_seqlock_irq variant. |
| 7871 | * |
| 7872 | * When looking for a bad block we specify a range and want to |
| 7873 | * know if any block in the range is bad. So we binary-search |
| 7874 | * to the last range that starts at-or-before the given endpoint, |
| 7875 | * (or "before the sector after the target range") |
| 7876 | * then see if it ends after the given start. |
| 7877 | * We return |
| 7878 | * 0 if there are no known bad blocks in the range |
| 7879 | * 1 if there are known bad block which are all acknowledged |
| 7880 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 7881 | * plus the start/length of the first bad section we overlap. |
| 7882 | */ |
| 7883 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 7884 | sector_t *first_bad, int *bad_sectors) |
| 7885 | { |
| 7886 | int hi; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 7887 | int lo; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7888 | u64 *p = bb->page; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 7889 | int rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7890 | sector_t target = s + sectors; |
| 7891 | unsigned seq; |
| 7892 | |
| 7893 | if (bb->shift > 0) { |
| 7894 | /* round the start down, and the end up */ |
| 7895 | s >>= bb->shift; |
| 7896 | target += (1<<bb->shift) - 1; |
| 7897 | target >>= bb->shift; |
| 7898 | sectors = target - s; |
| 7899 | } |
| 7900 | /* 'target' is now the first block after the bad range */ |
| 7901 | |
| 7902 | retry: |
| 7903 | seq = read_seqbegin(&bb->lock); |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 7904 | lo = 0; |
| 7905 | rv = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7906 | hi = bb->count; |
| 7907 | |
| 7908 | /* Binary search between lo and hi for 'target' |
| 7909 | * i.e. for the last range that starts before 'target' |
| 7910 | */ |
| 7911 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 7912 | * are known not to be the last range before target. |
| 7913 | * VARIANT: hi-lo is the number of possible |
| 7914 | * ranges, and decreases until it reaches 1 |
| 7915 | */ |
| 7916 | while (hi - lo > 1) { |
| 7917 | int mid = (lo + hi) / 2; |
| 7918 | sector_t a = BB_OFFSET(p[mid]); |
| 7919 | if (a < target) |
| 7920 | /* This could still be the one, earlier ranges |
| 7921 | * could not. */ |
| 7922 | lo = mid; |
| 7923 | else |
| 7924 | /* This and later ranges are definitely out. */ |
| 7925 | hi = mid; |
| 7926 | } |
| 7927 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 7928 | if (hi > lo) { |
| 7929 | /* need to check all range that end after 's' to see if |
| 7930 | * any are unacknowledged. |
| 7931 | */ |
| 7932 | while (lo >= 0 && |
| 7933 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 7934 | if (BB_OFFSET(p[lo]) < target) { |
| 7935 | /* starts before the end, and finishes after |
| 7936 | * the start, so they must overlap |
| 7937 | */ |
| 7938 | if (rv != -1 && BB_ACK(p[lo])) |
| 7939 | rv = 1; |
| 7940 | else |
| 7941 | rv = -1; |
| 7942 | *first_bad = BB_OFFSET(p[lo]); |
| 7943 | *bad_sectors = BB_LEN(p[lo]); |
| 7944 | } |
| 7945 | lo--; |
| 7946 | } |
| 7947 | } |
| 7948 | |
| 7949 | if (read_seqretry(&bb->lock, seq)) |
| 7950 | goto retry; |
| 7951 | |
| 7952 | return rv; |
| 7953 | } |
| 7954 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 7955 | |
| 7956 | /* |
| 7957 | * Add a range of bad blocks to the table. |
| 7958 | * This might extend the table, or might contract it |
| 7959 | * if two adjacent ranges can be merged. |
| 7960 | * We binary-search to find the 'insertion' point, then |
| 7961 | * decide how best to handle it. |
| 7962 | */ |
| 7963 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 7964 | int acknowledged) |
| 7965 | { |
| 7966 | u64 *p; |
| 7967 | int lo, hi; |
| 7968 | int rv = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 7969 | unsigned long flags; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7970 | |
| 7971 | if (bb->shift < 0) |
| 7972 | /* badblocks are disabled */ |
| 7973 | return 0; |
| 7974 | |
| 7975 | if (bb->shift) { |
| 7976 | /* round the start down, and the end up */ |
| 7977 | sector_t next = s + sectors; |
| 7978 | s >>= bb->shift; |
| 7979 | next += (1<<bb->shift) - 1; |
| 7980 | next >>= bb->shift; |
| 7981 | sectors = next - s; |
| 7982 | } |
| 7983 | |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 7984 | write_seqlock_irqsave(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 7985 | |
| 7986 | p = bb->page; |
| 7987 | lo = 0; |
| 7988 | hi = bb->count; |
| 7989 | /* Find the last range that starts at-or-before 's' */ |
| 7990 | while (hi - lo > 1) { |
| 7991 | int mid = (lo + hi) / 2; |
| 7992 | sector_t a = BB_OFFSET(p[mid]); |
| 7993 | if (a <= s) |
| 7994 | lo = mid; |
| 7995 | else |
| 7996 | hi = mid; |
| 7997 | } |
| 7998 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 7999 | hi = lo; |
| 8000 | |
| 8001 | if (hi > lo) { |
| 8002 | /* we found a range that might merge with the start |
| 8003 | * of our new range |
| 8004 | */ |
| 8005 | sector_t a = BB_OFFSET(p[lo]); |
| 8006 | sector_t e = a + BB_LEN(p[lo]); |
| 8007 | int ack = BB_ACK(p[lo]); |
| 8008 | if (e >= s) { |
| 8009 | /* Yes, we can merge with a previous range */ |
| 8010 | if (s == a && s + sectors >= e) |
| 8011 | /* new range covers old */ |
| 8012 | ack = acknowledged; |
| 8013 | else |
| 8014 | ack = ack && acknowledged; |
| 8015 | |
| 8016 | if (e < s + sectors) |
| 8017 | e = s + sectors; |
| 8018 | if (e - a <= BB_MAX_LEN) { |
| 8019 | p[lo] = BB_MAKE(a, e-a, ack); |
| 8020 | s = e; |
| 8021 | } else { |
| 8022 | /* does not all fit in one range, |
| 8023 | * make p[lo] maximal |
| 8024 | */ |
| 8025 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8026 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8027 | s = a + BB_MAX_LEN; |
| 8028 | } |
| 8029 | sectors = e - s; |
| 8030 | } |
| 8031 | } |
| 8032 | if (sectors && hi < bb->count) { |
| 8033 | /* 'hi' points to the first range that starts after 's'. |
| 8034 | * Maybe we can merge with the start of that range */ |
| 8035 | sector_t a = BB_OFFSET(p[hi]); |
| 8036 | sector_t e = a + BB_LEN(p[hi]); |
| 8037 | int ack = BB_ACK(p[hi]); |
| 8038 | if (a <= s + sectors) { |
| 8039 | /* merging is possible */ |
| 8040 | if (e <= s + sectors) { |
| 8041 | /* full overlap */ |
| 8042 | e = s + sectors; |
| 8043 | ack = acknowledged; |
| 8044 | } else |
| 8045 | ack = ack && acknowledged; |
| 8046 | |
| 8047 | a = s; |
| 8048 | if (e - a <= BB_MAX_LEN) { |
| 8049 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8050 | s = e; |
| 8051 | } else { |
| 8052 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8053 | s = a + BB_MAX_LEN; |
| 8054 | } |
| 8055 | sectors = e - s; |
| 8056 | lo = hi; |
| 8057 | hi++; |
| 8058 | } |
| 8059 | } |
| 8060 | if (sectors == 0 && hi < bb->count) { |
| 8061 | /* we might be able to combine lo and hi */ |
| 8062 | /* Note: 's' is at the end of 'lo' */ |
| 8063 | sector_t a = BB_OFFSET(p[hi]); |
| 8064 | int lolen = BB_LEN(p[lo]); |
| 8065 | int hilen = BB_LEN(p[hi]); |
| 8066 | int newlen = lolen + hilen - (s - a); |
| 8067 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8068 | /* yes, we can combine them */ |
| 8069 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8070 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8071 | memmove(p + hi, p + hi + 1, |
| 8072 | (bb->count - hi - 1) * 8); |
| 8073 | bb->count--; |
| 8074 | } |
| 8075 | } |
| 8076 | while (sectors) { |
| 8077 | /* didn't merge (it all). |
| 8078 | * Need to add a range just before 'hi' */ |
| 8079 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8080 | /* No room for more */ |
| 8081 | rv = 0; |
| 8082 | break; |
| 8083 | } else { |
| 8084 | int this_sectors = sectors; |
| 8085 | memmove(p + hi + 1, p + hi, |
| 8086 | (bb->count - hi) * 8); |
| 8087 | bb->count++; |
| 8088 | |
| 8089 | if (this_sectors > BB_MAX_LEN) |
| 8090 | this_sectors = BB_MAX_LEN; |
| 8091 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8092 | sectors -= this_sectors; |
| 8093 | s += this_sectors; |
| 8094 | } |
| 8095 | } |
| 8096 | |
| 8097 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8098 | if (!acknowledged) |
| 8099 | bb->unacked_exist = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8100 | write_sequnlock_irqrestore(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8101 | |
| 8102 | return rv; |
| 8103 | } |
| 8104 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8105 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8106 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8107 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8108 | int rv; |
| 8109 | if (is_new) |
| 8110 | s += rdev->new_data_offset; |
| 8111 | else |
| 8112 | s += rdev->data_offset; |
| 8113 | rv = md_set_badblocks(&rdev->badblocks, |
| 8114 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8115 | if (rv) { |
| 8116 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8117 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8118 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 8119 | md_wakeup_thread(rdev->mddev->thread); |
| 8120 | } |
| 8121 | return rv; |
| 8122 | } |
| 8123 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8124 | |
| 8125 | /* |
| 8126 | * Remove a range of bad blocks from the table. |
| 8127 | * This may involve extending the table if we spilt a region, |
| 8128 | * but it must not fail. So if the table becomes full, we just |
| 8129 | * drop the remove request. |
| 8130 | */ |
| 8131 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8132 | { |
| 8133 | u64 *p; |
| 8134 | int lo, hi; |
| 8135 | sector_t target = s + sectors; |
| 8136 | int rv = 0; |
| 8137 | |
| 8138 | if (bb->shift > 0) { |
| 8139 | /* When clearing we round the start up and the end down. |
| 8140 | * This should not matter as the shift should align with |
| 8141 | * the block size and no rounding should ever be needed. |
| 8142 | * However it is better the think a block is bad when it |
| 8143 | * isn't than to think a block is not bad when it is. |
| 8144 | */ |
| 8145 | s += (1<<bb->shift) - 1; |
| 8146 | s >>= bb->shift; |
| 8147 | target >>= bb->shift; |
| 8148 | sectors = target - s; |
| 8149 | } |
| 8150 | |
| 8151 | write_seqlock_irq(&bb->lock); |
| 8152 | |
| 8153 | p = bb->page; |
| 8154 | lo = 0; |
| 8155 | hi = bb->count; |
| 8156 | /* Find the last range that starts before 'target' */ |
| 8157 | while (hi - lo > 1) { |
| 8158 | int mid = (lo + hi) / 2; |
| 8159 | sector_t a = BB_OFFSET(p[mid]); |
| 8160 | if (a < target) |
| 8161 | lo = mid; |
| 8162 | else |
| 8163 | hi = mid; |
| 8164 | } |
| 8165 | if (hi > lo) { |
| 8166 | /* p[lo] is the last range that could overlap the |
| 8167 | * current range. Earlier ranges could also overlap, |
| 8168 | * but only this one can overlap the end of the range. |
| 8169 | */ |
| 8170 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8171 | /* Partial overlap, leave the tail of this range */ |
| 8172 | int ack = BB_ACK(p[lo]); |
| 8173 | sector_t a = BB_OFFSET(p[lo]); |
| 8174 | sector_t end = a + BB_LEN(p[lo]); |
| 8175 | |
| 8176 | if (a < s) { |
| 8177 | /* we need to split this range */ |
| 8178 | if (bb->count >= MD_MAX_BADBLOCKS) { |
NeilBrown | 8b32bf5 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 8179 | rv = -ENOSPC; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8180 | goto out; |
| 8181 | } |
| 8182 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8183 | bb->count++; |
| 8184 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8185 | lo++; |
| 8186 | } |
| 8187 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8188 | /* there is no longer an overlap */ |
| 8189 | hi = lo; |
| 8190 | lo--; |
| 8191 | } |
| 8192 | while (lo >= 0 && |
| 8193 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8194 | /* This range does overlap */ |
| 8195 | if (BB_OFFSET(p[lo]) < s) { |
| 8196 | /* Keep the early parts of this range. */ |
| 8197 | int ack = BB_ACK(p[lo]); |
| 8198 | sector_t start = BB_OFFSET(p[lo]); |
| 8199 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8200 | /* now low doesn't overlap, so.. */ |
| 8201 | break; |
| 8202 | } |
| 8203 | lo--; |
| 8204 | } |
| 8205 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8206 | * anything between needs to be discarded |
| 8207 | */ |
| 8208 | if (hi - lo > 1) { |
| 8209 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8210 | bb->count -= (hi - lo - 1); |
| 8211 | } |
| 8212 | } |
| 8213 | |
| 8214 | bb->changed = 1; |
| 8215 | out: |
| 8216 | write_sequnlock_irq(&bb->lock); |
| 8217 | return rv; |
| 8218 | } |
| 8219 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8220 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8221 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8222 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8223 | if (is_new) |
| 8224 | s += rdev->new_data_offset; |
| 8225 | else |
| 8226 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8227 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8228 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8229 | } |
| 8230 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8231 | |
| 8232 | /* |
| 8233 | * Acknowledge all bad blocks in a list. |
| 8234 | * This only succeeds if ->changed is clear. It is used by |
| 8235 | * in-kernel metadata updates |
| 8236 | */ |
| 8237 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8238 | { |
| 8239 | if (bb->page == NULL || bb->changed) |
| 8240 | /* no point even trying */ |
| 8241 | return; |
| 8242 | write_seqlock_irq(&bb->lock); |
| 8243 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8244 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8245 | u64 *p = bb->page; |
| 8246 | int i; |
| 8247 | for (i = 0; i < bb->count ; i++) { |
| 8248 | if (!BB_ACK(p[i])) { |
| 8249 | sector_t start = BB_OFFSET(p[i]); |
| 8250 | int len = BB_LEN(p[i]); |
| 8251 | p[i] = BB_MAKE(start, len, 1); |
| 8252 | } |
| 8253 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8254 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8255 | } |
| 8256 | write_sequnlock_irq(&bb->lock); |
| 8257 | } |
| 8258 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8259 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8260 | /* sysfs access to bad-blocks list. |
| 8261 | * We present two files. |
| 8262 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8263 | * are recorded as bad. The list is truncated to fit within |
| 8264 | * the one-page limit of sysfs. |
| 8265 | * Writing "sector length" to this file adds an acknowledged |
| 8266 | * bad block list. |
| 8267 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8268 | * been acknowledged. Writing to this file adds bad blocks |
| 8269 | * without acknowledging them. This is largely for testing. |
| 8270 | */ |
| 8271 | |
| 8272 | static ssize_t |
| 8273 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8274 | { |
| 8275 | size_t len; |
| 8276 | int i; |
| 8277 | u64 *p = bb->page; |
| 8278 | unsigned seq; |
| 8279 | |
| 8280 | if (bb->shift < 0) |
| 8281 | return 0; |
| 8282 | |
| 8283 | retry: |
| 8284 | seq = read_seqbegin(&bb->lock); |
| 8285 | |
| 8286 | len = 0; |
| 8287 | i = 0; |
| 8288 | |
| 8289 | while (len < PAGE_SIZE && i < bb->count) { |
| 8290 | sector_t s = BB_OFFSET(p[i]); |
| 8291 | unsigned int length = BB_LEN(p[i]); |
| 8292 | int ack = BB_ACK(p[i]); |
| 8293 | i++; |
| 8294 | |
| 8295 | if (unack && ack) |
| 8296 | continue; |
| 8297 | |
| 8298 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8299 | (unsigned long long)s << bb->shift, |
| 8300 | length << bb->shift); |
| 8301 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8302 | if (unack && len == 0) |
| 8303 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8304 | |
| 8305 | if (read_seqretry(&bb->lock, seq)) |
| 8306 | goto retry; |
| 8307 | |
| 8308 | return len; |
| 8309 | } |
| 8310 | |
| 8311 | #define DO_DEBUG 1 |
| 8312 | |
| 8313 | static ssize_t |
| 8314 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8315 | { |
| 8316 | unsigned long long sector; |
| 8317 | int length; |
| 8318 | char newline; |
| 8319 | #ifdef DO_DEBUG |
| 8320 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8321 | * Normally only a successful write may clear a badblock |
| 8322 | */ |
| 8323 | int clear = 0; |
| 8324 | if (page[0] == '-') { |
| 8325 | clear = 1; |
| 8326 | page++; |
| 8327 | } |
| 8328 | #endif /* DO_DEBUG */ |
| 8329 | |
| 8330 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8331 | case 3: |
| 8332 | if (newline != '\n') |
| 8333 | return -EINVAL; |
| 8334 | case 2: |
| 8335 | if (length <= 0) |
| 8336 | return -EINVAL; |
| 8337 | break; |
| 8338 | default: |
| 8339 | return -EINVAL; |
| 8340 | } |
| 8341 | |
| 8342 | #ifdef DO_DEBUG |
| 8343 | if (clear) { |
| 8344 | md_clear_badblocks(bb, sector, length); |
| 8345 | return len; |
| 8346 | } |
| 8347 | #endif /* DO_DEBUG */ |
| 8348 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8349 | return len; |
| 8350 | else |
| 8351 | return -ENOSPC; |
| 8352 | } |
| 8353 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8354 | static int md_notify_reboot(struct notifier_block *this, |
| 8355 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8356 | { |
| 8357 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8358 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8359 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8360 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8361 | for_each_mddev(mddev, tmp) { |
| 8362 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8363 | if (mddev->pers) |
| 8364 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 8365 | if (mddev->persistent) |
| 8366 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8367 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8368 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8369 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8370 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8371 | /* |
| 8372 | * certain more exotic SCSI devices are known to be |
| 8373 | * volatile wrt too early system reboots. While the |
| 8374 | * right place to handle this issue is the given |
| 8375 | * driver, we do want to have a safe RAID driver ... |
| 8376 | */ |
| 8377 | if (need_delay) |
| 8378 | mdelay(1000*1); |
| 8379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8380 | return NOTIFY_DONE; |
| 8381 | } |
| 8382 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8383 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8384 | .notifier_call = md_notify_reboot, |
| 8385 | .next = NULL, |
| 8386 | .priority = INT_MAX, /* before any real devices */ |
| 8387 | }; |
| 8388 | |
| 8389 | static void md_geninit(void) |
| 8390 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8391 | 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] | 8392 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8393 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8394 | } |
| 8395 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8396 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8397 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8398 | int ret = -ENOMEM; |
| 8399 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8400 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8401 | if (!md_wq) |
| 8402 | goto err_wq; |
| 8403 | |
| 8404 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8405 | if (!md_misc_wq) |
| 8406 | goto err_misc_wq; |
| 8407 | |
| 8408 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8409 | goto err_md; |
| 8410 | |
| 8411 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8412 | goto err_mdp; |
| 8413 | mdp_major = ret; |
| 8414 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8415 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8416 | md_probe, NULL, NULL); |
| 8417 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8418 | md_probe, NULL, NULL); |
| 8419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8420 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8421 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8422 | |
| 8423 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8424 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8425 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8426 | err_mdp: |
| 8427 | unregister_blkdev(MD_MAJOR, "md"); |
| 8428 | err_md: |
| 8429 | destroy_workqueue(md_misc_wq); |
| 8430 | err_misc_wq: |
| 8431 | destroy_workqueue(md_wq); |
| 8432 | err_wq: |
| 8433 | return ret; |
| 8434 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8435 | |
| 8436 | #ifndef MODULE |
| 8437 | |
| 8438 | /* |
| 8439 | * Searches all registered partitions for autorun RAID arrays |
| 8440 | * at boot time. |
| 8441 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8442 | |
| 8443 | static LIST_HEAD(all_detected_devices); |
| 8444 | struct detected_devices_node { |
| 8445 | struct list_head list; |
| 8446 | dev_t dev; |
| 8447 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8448 | |
| 8449 | void md_autodetect_dev(dev_t dev) |
| 8450 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8451 | struct detected_devices_node *node_detected_dev; |
| 8452 | |
| 8453 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8454 | if (node_detected_dev) { |
| 8455 | node_detected_dev->dev = dev; |
| 8456 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8457 | } else { |
| 8458 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8459 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8460 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8461 | } |
| 8462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8463 | static void autostart_arrays(int part) |
| 8464 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8465 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8466 | struct detected_devices_node *node_detected_dev; |
| 8467 | dev_t dev; |
| 8468 | int i_scanned, i_passed; |
| 8469 | |
| 8470 | i_scanned = 0; |
| 8471 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8472 | |
| 8473 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8474 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8475 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8476 | i_scanned++; |
| 8477 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8478 | struct detected_devices_node, list); |
| 8479 | list_del(&node_detected_dev->list); |
| 8480 | dev = node_detected_dev->dev; |
| 8481 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8482 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8483 | if (IS_ERR(rdev)) |
| 8484 | continue; |
| 8485 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8486 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8487 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8488 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8489 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8490 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8491 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8492 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8493 | |
| 8494 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8495 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8496 | |
| 8497 | autorun_devices(part); |
| 8498 | } |
| 8499 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8500 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8501 | |
| 8502 | static __exit void md_exit(void) |
| 8503 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8504 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8505 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8506 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8507 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 8508 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8509 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8510 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8511 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8512 | unregister_blkdev(mdp_major, "mdp"); |
| 8513 | unregister_reboot_notifier(&md_notifier); |
| 8514 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8515 | |
| 8516 | /* We cannot unload the modules while some process is |
| 8517 | * waiting for us in select() or poll() - wake them up |
| 8518 | */ |
| 8519 | md_unloading = 1; |
| 8520 | while (waitqueue_active(&md_event_waiters)) { |
| 8521 | /* not safe to leave yet */ |
| 8522 | wake_up(&md_event_waiters); |
| 8523 | msleep(delay); |
| 8524 | delay += delay; |
| 8525 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8526 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8527 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8528 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8529 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8530 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8531 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8532 | destroy_workqueue(md_misc_wq); |
| 8533 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8534 | } |
| 8535 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8536 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8537 | module_exit(md_exit) |
| 8538 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8539 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8540 | { |
| 8541 | return sprintf(buffer, "%d", start_readonly); |
| 8542 | } |
| 8543 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8544 | { |
| 8545 | char *e; |
| 8546 | int num = simple_strtoul(val, &e, 10); |
| 8547 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8548 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8549 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8550 | } |
| 8551 | return -EINVAL; |
| 8552 | } |
| 8553 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8554 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8555 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8556 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8558 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8559 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8560 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8561 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |