Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
| 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
| 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 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 69 | static void md_print_devices(void); |
| 70 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 71 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 72 | static struct workqueue_struct *md_wq; |
| 73 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 74 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 75 | static int remove_and_add_spares(struct mddev *mddev, |
| 76 | struct md_rdev *this); |
| 77 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 78 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 81 | * Default number of read corrections we'll attempt on an rdev |
| 82 | * before ejecting it from the array. We divide the read error |
| 83 | * count by 2 for every hour elapsed between read errors. |
| 84 | */ |
| 85 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 86 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 88 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 89 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 90 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 92 | * speed limit - in case reconstruction slows down your system despite |
| 93 | * idle IO detection. |
| 94 | * |
| 95 | * 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] | 96 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | */ |
| 98 | |
| 99 | static int sysctl_speed_limit_min = 1000; |
| 100 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 101 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 102 | { |
| 103 | return mddev->sync_speed_min ? |
| 104 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 105 | } |
| 106 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 107 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 108 | { |
| 109 | return mddev->sync_speed_max ? |
| 110 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | static struct ctl_table_header *raid_table_header; |
| 114 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 115 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | .procname = "speed_limit_min", |
| 118 | .data = &sysctl_speed_limit_min, |
| 119 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 120 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 121 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | }, |
| 123 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | .procname = "speed_limit_max", |
| 125 | .data = &sysctl_speed_limit_max, |
| 126 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 127 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 128 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 130 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 133 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | .procname = "raid", |
| 136 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 137 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | .child = raid_table, |
| 139 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 140 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 143 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | .procname = "dev", |
| 146 | .maxlen = 0, |
| 147 | .mode = 0555, |
| 148 | .child = raid_dir_table, |
| 149 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 150 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 153 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 155 | static int start_readonly; |
| 156 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 157 | /* bio_clone_mddev |
| 158 | * like bio_clone, but with a local bio set |
| 159 | */ |
| 160 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 161 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 162 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 163 | { |
| 164 | struct bio *b; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 165 | |
| 166 | if (!mddev || !mddev->bio_set) |
| 167 | return bio_alloc(gfp_mask, nr_iovecs); |
| 168 | |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 169 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 170 | if (!b) |
| 171 | return NULL; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 172 | return b; |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 175 | |
| 176 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 177 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 178 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 179 | if (!mddev || !mddev->bio_set) |
| 180 | return bio_clone(bio, gfp_mask); |
| 181 | |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 182 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 183 | } |
| 184 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 187 | * We have a system wide 'event count' that is incremented |
| 188 | * on any 'interesting' event, and readers of /proc/mdstat |
| 189 | * can use 'poll' or 'select' to find out when the event |
| 190 | * count increases. |
| 191 | * |
| 192 | * Events are: |
| 193 | * start array, stop array, error, add device, remove device, |
| 194 | * start build, activate spare |
| 195 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 196 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 197 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 198 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 199 | { |
| 200 | atomic_inc(&md_event_count); |
| 201 | wake_up(&md_event_waiters); |
| 202 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 203 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 204 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 205 | /* Alternate version that can be called from interrupts |
| 206 | * when calling sysfs_notify isn't needed. |
| 207 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 208 | static void md_new_event_inintr(struct mddev *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 209 | { |
| 210 | atomic_inc(&md_event_count); |
| 211 | wake_up(&md_event_waiters); |
| 212 | } |
| 213 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 214 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | * Enables to iterate over all existing md arrays |
| 216 | * all_mddevs_lock protects this list. |
| 217 | */ |
| 218 | static LIST_HEAD(all_mddevs); |
| 219 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 220 | |
| 221 | |
| 222 | /* |
| 223 | * iterates through all used mddevs in the system. |
| 224 | * We take care to grab the all_mddevs_lock whenever navigating |
| 225 | * the list, and to always hold a refcount when unlocked. |
| 226 | * Any code which breaks out of this loop while own |
| 227 | * a reference to the current mddev and must mddev_put it. |
| 228 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 229 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | \ |
| 231 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 232 | _tmp = all_mddevs.next; \ |
| 233 | _mddev = NULL;}); \ |
| 234 | ({ if (_tmp != &all_mddevs) \ |
| 235 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 237 | if (_mddev) mddev_put(_mddev); \ |
| 238 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 239 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 241 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | ) |
| 243 | |
| 244 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 245 | /* Rather than calling directly into the personality make_request function, |
| 246 | * IO requests come here first so that we can check if the device is |
| 247 | * being suspended pending a reconfiguration. |
| 248 | * We hold a refcount over the call to ->make_request. By the time that |
| 249 | * call has finished, the bio has been linked into some internal structure |
| 250 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 251 | */ |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 252 | static void md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 254 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 255 | struct mddev *mddev = q->queuedata; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 256 | int cpu; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 257 | unsigned int sectors; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 258 | |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 259 | if (mddev == NULL || mddev->pers == NULL |
| 260 | || !mddev->ready) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 261 | bio_io_error(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 262 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 263 | } |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 264 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
| 265 | bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS); |
| 266 | return; |
| 267 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 268 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 269 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 270 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 271 | DEFINE_WAIT(__wait); |
| 272 | for (;;) { |
| 273 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 274 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 275 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 276 | break; |
| 277 | rcu_read_unlock(); |
| 278 | schedule(); |
| 279 | rcu_read_lock(); |
| 280 | } |
| 281 | finish_wait(&mddev->sb_wait, &__wait); |
| 282 | } |
| 283 | atomic_inc(&mddev->active_io); |
| 284 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 285 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 286 | /* |
| 287 | * save the sectors now since our bio can |
| 288 | * go away inside make_request |
| 289 | */ |
| 290 | sectors = bio_sectors(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 291 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 292 | |
| 293 | cpu = part_stat_lock(); |
| 294 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 295 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 296 | part_stat_unlock(); |
| 297 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 298 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 299 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 302 | /* mddev_suspend makes sure no new requests are submitted |
| 303 | * to the device, and that any requests that have been submitted |
| 304 | * are completely handled. |
| 305 | * Once ->stop is called and completes, the module will be completely |
| 306 | * unused. |
| 307 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 308 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 309 | { |
| 310 | BUG_ON(mddev->suspended); |
| 311 | mddev->suspended = 1; |
| 312 | synchronize_rcu(); |
| 313 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 314 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 315 | |
| 316 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 317 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 318 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 319 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 320 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 321 | { |
| 322 | mddev->suspended = 0; |
| 323 | wake_up(&mddev->sb_wait); |
| 324 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 325 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 326 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 327 | md_wakeup_thread(mddev->thread); |
| 328 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 329 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 330 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 331 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 332 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 333 | { |
| 334 | return mddev->suspended; |
| 335 | } |
| 336 | EXPORT_SYMBOL(mddev_congested); |
| 337 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 338 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 339 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 340 | */ |
| 341 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 342 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 343 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 344 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 345 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 346 | |
| 347 | rdev_dec_pending(rdev, mddev); |
| 348 | |
| 349 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 350 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 351 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 352 | } |
| 353 | bio_put(bio); |
| 354 | } |
| 355 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 356 | static void md_submit_flush_data(struct work_struct *ws); |
| 357 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 358 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 359 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 360 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 361 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 362 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 363 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 364 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 365 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 366 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 367 | if (rdev->raid_disk >= 0 && |
| 368 | !test_bit(Faulty, &rdev->flags)) { |
| 369 | /* Take two references, one is dropped |
| 370 | * when request finishes, one after |
| 371 | * we reclaim rcu_read_lock |
| 372 | */ |
| 373 | struct bio *bi; |
| 374 | atomic_inc(&rdev->nr_pending); |
| 375 | atomic_inc(&rdev->nr_pending); |
| 376 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 377 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 378 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 379 | bi->bi_private = rdev; |
| 380 | bi->bi_bdev = rdev->bdev; |
| 381 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 382 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 383 | rcu_read_lock(); |
| 384 | rdev_dec_pending(rdev, mddev); |
| 385 | } |
| 386 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 387 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 388 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 389 | } |
| 390 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 391 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 392 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 393 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 394 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 395 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 396 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 397 | /* an empty barrier - all done */ |
| 398 | bio_endio(bio, 0); |
| 399 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 400 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 401 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 402 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 403 | |
| 404 | mddev->flush_bio = NULL; |
| 405 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 406 | } |
| 407 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 408 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 409 | { |
| 410 | spin_lock_irq(&mddev->write_lock); |
| 411 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 412 | !mddev->flush_bio, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 413 | mddev->write_lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 414 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 415 | spin_unlock_irq(&mddev->write_lock); |
| 416 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 417 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 418 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 419 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 420 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 421 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 422 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 423 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 424 | struct mddev *mddev = cb->data; |
| 425 | md_wakeup_thread(mddev->thread); |
| 426 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 427 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 428 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 429 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 430 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
| 432 | atomic_inc(&mddev->active); |
| 433 | return mddev; |
| 434 | } |
| 435 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 436 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 437 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 438 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 440 | struct bio_set *bs = NULL; |
| 441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 443 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 444 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 445 | mddev->ctime == 0 && !mddev->hold_active) { |
| 446 | /* Array is not configured at all, and not held active, |
| 447 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 448 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 449 | bs = mddev->bio_set; |
| 450 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 451 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 452 | /* We did a probe so need to clean up. Call |
| 453 | * queue_work inside the spinlock so that |
| 454 | * flush_workqueue() after mddev_find will |
| 455 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 456 | */ |
| 457 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 458 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 459 | } else |
| 460 | kfree(mddev); |
| 461 | } |
| 462 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 463 | if (bs) |
| 464 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
| 466 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 467 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 468 | { |
| 469 | mutex_init(&mddev->open_mutex); |
| 470 | mutex_init(&mddev->reconfig_mutex); |
| 471 | mutex_init(&mddev->bitmap_info.mutex); |
| 472 | INIT_LIST_HEAD(&mddev->disks); |
| 473 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 474 | init_timer(&mddev->safemode_timer); |
| 475 | atomic_set(&mddev->active, 1); |
| 476 | atomic_set(&mddev->openers, 0); |
| 477 | atomic_set(&mddev->active_io, 0); |
| 478 | spin_lock_init(&mddev->write_lock); |
| 479 | atomic_set(&mddev->flush_pending, 0); |
| 480 | init_waitqueue_head(&mddev->sb_wait); |
| 481 | init_waitqueue_head(&mddev->recovery_wait); |
| 482 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 483 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 484 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 485 | mddev->resync_min = 0; |
| 486 | mddev->resync_max = MaxSector; |
| 487 | mddev->level = LEVEL_NONE; |
| 488 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 489 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 490 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 491 | static struct mddev * mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 493 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 495 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 496 | unit &= ~((1<<MdpMinorShift)-1); |
| 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | retry: |
| 499 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 501 | if (unit) { |
| 502 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 503 | if (mddev->unit == unit) { |
| 504 | mddev_get(mddev); |
| 505 | spin_unlock(&all_mddevs_lock); |
| 506 | kfree(new); |
| 507 | return mddev; |
| 508 | } |
| 509 | |
| 510 | if (new) { |
| 511 | list_add(&new->all_mddevs, &all_mddevs); |
| 512 | spin_unlock(&all_mddevs_lock); |
| 513 | new->hold_active = UNTIL_IOCTL; |
| 514 | return new; |
| 515 | } |
| 516 | } else if (new) { |
| 517 | /* find an unused unit number */ |
| 518 | static int next_minor = 512; |
| 519 | int start = next_minor; |
| 520 | int is_free = 0; |
| 521 | int dev = 0; |
| 522 | while (!is_free) { |
| 523 | dev = MKDEV(MD_MAJOR, next_minor); |
| 524 | next_minor++; |
| 525 | if (next_minor > MINORMASK) |
| 526 | next_minor = 0; |
| 527 | if (next_minor == start) { |
| 528 | /* Oh dear, all in use. */ |
| 529 | spin_unlock(&all_mddevs_lock); |
| 530 | kfree(new); |
| 531 | return NULL; |
| 532 | } |
| 533 | |
| 534 | is_free = 1; |
| 535 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 536 | if (mddev->unit == dev) { |
| 537 | is_free = 0; |
| 538 | break; |
| 539 | } |
| 540 | } |
| 541 | new->unit = dev; |
| 542 | new->md_minor = MINOR(dev); |
| 543 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | list_add(&new->all_mddevs, &all_mddevs); |
| 545 | spin_unlock(&all_mddevs_lock); |
| 546 | return new; |
| 547 | } |
| 548 | spin_unlock(&all_mddevs_lock); |
| 549 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 550 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | if (!new) |
| 552 | return NULL; |
| 553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | new->unit = unit; |
| 555 | if (MAJOR(unit) == MD_MAJOR) |
| 556 | new->md_minor = MINOR(unit); |
| 557 | else |
| 558 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 559 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 560 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | goto retry; |
| 563 | } |
| 564 | |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 565 | static inline int __must_check mddev_lock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 567 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } |
| 569 | |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 570 | /* Sometimes we need to take the lock in a situation where |
| 571 | * failure due to interrupts is not acceptable. |
| 572 | */ |
| 573 | static inline void mddev_lock_nointr(struct mddev * mddev) |
| 574 | { |
| 575 | mutex_lock(&mddev->reconfig_mutex); |
| 576 | } |
| 577 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 578 | static inline int mddev_is_locked(struct mddev *mddev) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 579 | { |
| 580 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 581 | } |
| 582 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 583 | static inline int mddev_trylock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 585 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |
| 587 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 588 | static struct attribute_group md_redundancy_group; |
| 589 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 590 | static void mddev_unlock(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 592 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 593 | /* These cannot be removed under reconfig_mutex as |
| 594 | * an access to the files will try to take reconfig_mutex |
| 595 | * while holding the file unremovable, which leads to |
| 596 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 597 | * So hold set sysfs_active while the remove in happeing, |
| 598 | * and anything else which might set ->to_remove or my |
| 599 | * otherwise change the sysfs namespace will fail with |
| 600 | * -EBUSY if sysfs_active is still set. |
| 601 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 602 | * test it under the same mutex to ensure its correct value |
| 603 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 604 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 605 | struct attribute_group *to_remove = mddev->to_remove; |
| 606 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 607 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 608 | mutex_unlock(&mddev->reconfig_mutex); |
| 609 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 610 | if (mddev->kobj.sd) { |
| 611 | if (to_remove != &md_redundancy_group) |
| 612 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 613 | if (mddev->pers == NULL || |
| 614 | mddev->pers->sync_request == NULL) { |
| 615 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 616 | if (mddev->sysfs_action) |
| 617 | sysfs_put(mddev->sysfs_action); |
| 618 | mddev->sysfs_action = NULL; |
| 619 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 620 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 621 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 622 | } else |
| 623 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 625 | /* As we've dropped the mutex we need a spinlock to |
| 626 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 627 | */ |
| 628 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 629 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 630 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
| 632 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 633 | static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 635 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 637 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | if (rdev->desc_nr == nr) |
| 639 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | return NULL; |
| 642 | } |
| 643 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 644 | static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr) |
| 645 | { |
| 646 | struct md_rdev *rdev; |
| 647 | |
| 648 | rdev_for_each_rcu(rdev, mddev) |
| 649 | if (rdev->desc_nr == nr) |
| 650 | return rdev; |
| 651 | |
| 652 | return NULL; |
| 653 | } |
| 654 | |
| 655 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 657 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 659 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | if (rdev->bdev->bd_dev == dev) |
| 661 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | return NULL; |
| 664 | } |
| 665 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 666 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 667 | { |
| 668 | struct md_rdev *rdev; |
| 669 | |
| 670 | rdev_for_each_rcu(rdev, mddev) |
| 671 | if (rdev->bdev->bd_dev == dev) |
| 672 | return rdev; |
| 673 | |
| 674 | return NULL; |
| 675 | } |
| 676 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 677 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 678 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 679 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 680 | list_for_each_entry(pers, &pers_list, list) { |
| 681 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 682 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 683 | if (strcmp(pers->name, clevel)==0) |
| 684 | return pers; |
| 685 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 686 | return NULL; |
| 687 | } |
| 688 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 689 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 690 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 692 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 693 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | } |
| 695 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 696 | static int alloc_disk_sb(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | { |
| 698 | if (rdev->sb_page) |
| 699 | MD_BUG(); |
| 700 | |
| 701 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 702 | if (!rdev->sb_page) { |
| 703 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 704 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 710 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | { |
| 712 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 713 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | rdev->sb_loaded = 0; |
| 715 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 716 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 717 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 719 | if (rdev->bb_page) { |
| 720 | put_page(rdev->bb_page); |
| 721 | rdev->bb_page = NULL; |
| 722 | } |
NeilBrown | 4fa2f32 | 2012-05-22 13:55:01 +1000 | [diff] [blame] | 723 | kfree(rdev->badblocks.page); |
| 724 | rdev->badblocks.page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 726 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 728 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 729 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 730 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 731 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 732 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 733 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 734 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 735 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 736 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 737 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 738 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 739 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 740 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 741 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 742 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 743 | } |
| 744 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 745 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 746 | sector_t sector, int size, struct page *page) |
| 747 | { |
| 748 | /* write first size bytes of page to sector of rdev |
| 749 | * Increment mddev->pending_writes before returning |
| 750 | * and decrement it on completion, waking up sb_wait |
| 751 | * if zero is reached. |
| 752 | * If an error occurred, call md_error |
| 753 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 754 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 755 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 756 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 757 | bio->bi_sector = sector; |
| 758 | bio_add_page(bio, page, size, 0); |
| 759 | bio->bi_private = rdev; |
| 760 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 761 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 762 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 763 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 764 | } |
| 765 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 766 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 767 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 768 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 769 | DEFINE_WAIT(wq); |
| 770 | for(;;) { |
| 771 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 772 | if (atomic_read(&mddev->pending_writes)==0) |
| 773 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 774 | schedule(); |
| 775 | } |
| 776 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 777 | } |
| 778 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 779 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | } |
| 783 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 784 | 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] | 785 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 787 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | struct completion event; |
| 789 | int ret; |
| 790 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 791 | rw |= REQ_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 793 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 794 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 795 | if (metadata_op) |
| 796 | bio->bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 797 | else if (rdev->mddev->reshape_position != MaxSector && |
| 798 | (rdev->mddev->reshape_backwards == |
| 799 | (sector >= rdev->mddev->reshape_position))) |
| 800 | bio->bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 801 | else |
| 802 | bio->bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | bio_add_page(bio, page, size, 0); |
| 804 | init_completion(&event); |
| 805 | bio->bi_private = &event; |
| 806 | bio->bi_end_io = bi_complete; |
| 807 | submit_bio(rw, bio); |
| 808 | wait_for_completion(&event); |
| 809 | |
| 810 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 811 | bio_put(bio); |
| 812 | return ret; |
| 813 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 814 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 816 | static int read_disk_sb(struct md_rdev * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | { |
| 818 | char b[BDEVNAME_SIZE]; |
| 819 | if (!rdev->sb_page) { |
| 820 | MD_BUG(); |
| 821 | return -EINVAL; |
| 822 | } |
| 823 | if (rdev->sb_loaded) |
| 824 | return 0; |
| 825 | |
| 826 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 827 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | goto fail; |
| 829 | rdev->sb_loaded = 1; |
| 830 | return 0; |
| 831 | |
| 832 | fail: |
| 833 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 834 | bdevname(rdev->bdev,b)); |
| 835 | return -EINVAL; |
| 836 | } |
| 837 | |
| 838 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 839 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 840 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 841 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 842 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 843 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 847 | { |
| 848 | int ret; |
| 849 | mdp_super_t *tmp1, *tmp2; |
| 850 | |
| 851 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 852 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 853 | |
| 854 | if (!tmp1 || !tmp2) { |
| 855 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 856 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | goto abort; |
| 858 | } |
| 859 | |
| 860 | *tmp1 = *sb1; |
| 861 | *tmp2 = *sb2; |
| 862 | |
| 863 | /* |
| 864 | * nr_disks is not constant |
| 865 | */ |
| 866 | tmp1->nr_disks = 0; |
| 867 | tmp2->nr_disks = 0; |
| 868 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 869 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 871 | kfree(tmp1); |
| 872 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | return ret; |
| 874 | } |
| 875 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 876 | |
| 877 | static u32 md_csum_fold(u32 csum) |
| 878 | { |
| 879 | csum = (csum & 0xffff) + (csum >> 16); |
| 880 | return (csum & 0xffff) + (csum >> 16); |
| 881 | } |
| 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 884 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 885 | u64 newcsum = 0; |
| 886 | u32 *sb32 = (u32*)sb; |
| 887 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | unsigned int disk_csum, csum; |
| 889 | |
| 890 | disk_csum = sb->sb_csum; |
| 891 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 892 | |
| 893 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 894 | newcsum += sb32[i]; |
| 895 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 896 | |
| 897 | |
| 898 | #ifdef CONFIG_ALPHA |
| 899 | /* This used to use csum_partial, which was wrong for several |
| 900 | * reasons including that different results are returned on |
| 901 | * different architectures. It isn't critical that we get exactly |
| 902 | * the same return value as before (we always csum_fold before |
| 903 | * testing, and that removes any differences). However as we |
| 904 | * know that csum_partial always returned a 16bit value on |
| 905 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 906 | */ |
| 907 | sb->sb_csum = md_csum_fold(disk_csum); |
| 908 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 910 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | return csum; |
| 912 | } |
| 913 | |
| 914 | |
| 915 | /* |
| 916 | * Handle superblock details. |
| 917 | * We want to be able to handle multiple superblock formats |
| 918 | * so we have a common interface to them all, and an array of |
| 919 | * different handlers. |
| 920 | * We rely on user-space to write the initial superblock, and support |
| 921 | * reading and updating of superblocks. |
| 922 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 923 | * 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] | 924 | * loads and validates a superblock on dev. |
| 925 | * if refdev != NULL, compare superblocks on both devices |
| 926 | * Return: |
| 927 | * 0 - dev has a superblock that is compatible with refdev |
| 928 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 929 | * so dev should be used as the refdev in future |
| 930 | * -EINVAL superblock incompatible or invalid |
| 931 | * -othererror e.g. -EIO |
| 932 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 933 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | * Verify that dev is acceptable into mddev. |
| 935 | * The first time, mddev->raid_disks will be 0, and data from |
| 936 | * dev should be merged in. Subsequent calls check that dev |
| 937 | * is new enough. Return 0 or -EINVAL |
| 938 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 939 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | * Update the superblock for rdev with data in mddev |
| 941 | * This does not write to disc. |
| 942 | * |
| 943 | */ |
| 944 | |
| 945 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 946 | char *name; |
| 947 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 948 | int (*load_super)(struct md_rdev *rdev, |
| 949 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 950 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 951 | int (*validate_super)(struct mddev *mddev, |
| 952 | struct md_rdev *rdev); |
| 953 | void (*sync_super)(struct mddev *mddev, |
| 954 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 955 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 956 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 957 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 958 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | }; |
| 960 | |
| 961 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 962 | * Check that the given mddev has no bitmap. |
| 963 | * |
| 964 | * This function is called from the run method of all personalities that do not |
| 965 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 966 | * has a bitmap. Otherwise, it returns 0. |
| 967 | * |
| 968 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 969 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 970 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 971 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 972 | return 0; |
| 973 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 974 | mdname(mddev), mddev->pers->name); |
| 975 | return 1; |
| 976 | } |
| 977 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 978 | |
| 979 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | * load_super for 0.90.0 |
| 981 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 982 | 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] | 983 | { |
| 984 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 985 | mdp_super_t *sb; |
| 986 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | |
| 988 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 989 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | * it's at the end of the disk. |
| 991 | * |
| 992 | * It also happens to be a multiple of 4Kb. |
| 993 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 994 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 996 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | if (ret) return ret; |
| 998 | |
| 999 | ret = -EINVAL; |
| 1000 | |
| 1001 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1002 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | |
| 1004 | if (sb->md_magic != MD_SB_MAGIC) { |
| 1005 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 1006 | b); |
| 1007 | goto abort; |
| 1008 | } |
| 1009 | |
| 1010 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1011 | sb->minor_version < 90 || |
| 1012 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 1014 | sb->major_version, sb->minor_version, |
| 1015 | b); |
| 1016 | goto abort; |
| 1017 | } |
| 1018 | |
| 1019 | if (sb->raid_disks <= 0) |
| 1020 | goto abort; |
| 1021 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1022 | 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] | 1023 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1024 | b); |
| 1025 | goto abort; |
| 1026 | } |
| 1027 | |
| 1028 | rdev->preferred_minor = sb->md_minor; |
| 1029 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1030 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1031 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1032 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
| 1034 | if (sb->level == LEVEL_MULTIPATH) |
| 1035 | rdev->desc_nr = -1; |
| 1036 | else |
| 1037 | rdev->desc_nr = sb->this_disk.number; |
| 1038 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1039 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1041 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1043 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | if (!uuid_equal(refsb, sb)) { |
| 1045 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1046 | b, bdevname(refdev->bdev,b2)); |
| 1047 | goto abort; |
| 1048 | } |
| 1049 | if (!sb_equal(refsb, sb)) { |
| 1050 | printk(KERN_WARNING "md: %s has same UUID" |
| 1051 | " but different superblock to %s\n", |
| 1052 | b, bdevname(refdev->bdev, b2)); |
| 1053 | goto abort; |
| 1054 | } |
| 1055 | ev1 = md_event(sb); |
| 1056 | ev2 = md_event(refsb); |
| 1057 | if (ev1 > ev2) |
| 1058 | ret = 1; |
| 1059 | else |
| 1060 | ret = 0; |
| 1061 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1062 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1063 | /* Limit to 4TB as metadata cannot record more than that. |
| 1064 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1065 | * record this size) |
| 1066 | */ |
| 1067 | if (rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1068 | rdev->sectors = (2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1070 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1071 | /* "this cannot possibly happen" ... */ |
| 1072 | ret = -EINVAL; |
| 1073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | abort: |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * validate_super for 0.90.0 |
| 1080 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1081 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | { |
| 1083 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1084 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1085 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1087 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1088 | clear_bit(Faulty, &rdev->flags); |
| 1089 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1090 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1091 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | if (mddev->raid_disks == 0) { |
| 1094 | mddev->major_version = 0; |
| 1095 | mddev->minor_version = sb->minor_version; |
| 1096 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1097 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1098 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | mddev->ctime = sb->ctime; |
| 1100 | mddev->utime = sb->utime; |
| 1101 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1102 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | mddev->layout = sb->layout; |
| 1104 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1105 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1106 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1107 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1108 | mddev->bitmap_info.space = 0; |
| 1109 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1110 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1111 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1112 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1114 | if (mddev->minor_version >= 91) { |
| 1115 | mddev->reshape_position = sb->reshape_position; |
| 1116 | mddev->delta_disks = sb->delta_disks; |
| 1117 | mddev->new_level = sb->new_level; |
| 1118 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1119 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1120 | if (mddev->delta_disks < 0) |
| 1121 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1122 | } else { |
| 1123 | mddev->reshape_position = MaxSector; |
| 1124 | mddev->delta_disks = 0; |
| 1125 | mddev->new_level = mddev->level; |
| 1126 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1127 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1131 | mddev->recovery_cp = MaxSector; |
| 1132 | else { |
| 1133 | if (sb->events_hi == sb->cp_events_hi && |
| 1134 | sb->events_lo == sb->cp_events_lo) { |
| 1135 | mddev->recovery_cp = sb->recovery_cp; |
| 1136 | } else |
| 1137 | mddev->recovery_cp = 0; |
| 1138 | } |
| 1139 | |
| 1140 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1141 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1142 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1143 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1144 | |
| 1145 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1146 | |
| 1147 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1148 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1149 | mddev->bitmap_info.offset = |
| 1150 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1151 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1152 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1153 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1154 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1155 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1156 | /* Insist on good event counter while assembling, except |
| 1157 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1159 | if (sb->disks[rdev->desc_nr].state & ( |
| 1160 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1161 | if (ev1 < mddev->events) |
| 1162 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1163 | } else if (mddev->bitmap) { |
| 1164 | /* if adding to array with a bitmap, then we can accept an |
| 1165 | * older device ... but not too old. |
| 1166 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1167 | if (ev1 < mddev->bitmap->events_cleared) |
| 1168 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1169 | if (ev1 < mddev->events) |
| 1170 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1171 | } else { |
| 1172 | if (ev1 < mddev->events) |
| 1173 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1174 | return 0; |
| 1175 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | desc = sb->disks + rdev->desc_nr; |
| 1179 | |
| 1180 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1181 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1182 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1183 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1184 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame^] | 1186 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1187 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1188 | /* active but not in sync implies recovery up to |
| 1189 | * reshape position. We don't know exactly where |
| 1190 | * that is, so set to zero for now */ |
| 1191 | if (mddev->minor_version >= 91) { |
| 1192 | rdev->recovery_offset = 0; |
| 1193 | rdev->raid_disk = desc->raid_disk; |
| 1194 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1196 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1197 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1198 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1199 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | /* |
| 1204 | * sync_super for 0.90.0 |
| 1205 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1206 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | { |
| 1208 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1209 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | |
| 1213 | /* make rdev->sb match mddev data.. |
| 1214 | * |
| 1215 | * 1/ zero out disks |
| 1216 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1217 | * 3/ any empty disks < next_spare become removed |
| 1218 | * |
| 1219 | * disks[0] gets initialised to REMOVED because |
| 1220 | * we cannot be sure from other fields if it has |
| 1221 | * been initialised or not. |
| 1222 | */ |
| 1223 | int i; |
| 1224 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1225 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1226 | rdev->sb_size = MD_SB_BYTES; |
| 1227 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1228 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | |
| 1230 | memset(sb, 0, sizeof(*sb)); |
| 1231 | |
| 1232 | sb->md_magic = MD_SB_MAGIC; |
| 1233 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | sb->patch_version = mddev->patch_version; |
| 1235 | sb->gvalid_words = 0; /* ignored */ |
| 1236 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1237 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1238 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1239 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1240 | |
| 1241 | sb->ctime = mddev->ctime; |
| 1242 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1243 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | sb->raid_disks = mddev->raid_disks; |
| 1245 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1246 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | sb->utime = mddev->utime; |
| 1248 | sb->state = 0; |
| 1249 | sb->events_hi = (mddev->events>>32); |
| 1250 | sb->events_lo = (u32)mddev->events; |
| 1251 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1252 | if (mddev->reshape_position == MaxSector) |
| 1253 | sb->minor_version = 90; |
| 1254 | else { |
| 1255 | sb->minor_version = 91; |
| 1256 | sb->reshape_position = mddev->reshape_position; |
| 1257 | sb->new_level = mddev->new_level; |
| 1258 | sb->delta_disks = mddev->delta_disks; |
| 1259 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1260 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1261 | } |
| 1262 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | if (mddev->in_sync) |
| 1264 | { |
| 1265 | sb->recovery_cp = mddev->recovery_cp; |
| 1266 | sb->cp_events_hi = (mddev->events>>32); |
| 1267 | sb->cp_events_lo = (u32)mddev->events; |
| 1268 | if (mddev->recovery_cp == MaxSector) |
| 1269 | sb->state = (1<< MD_SB_CLEAN); |
| 1270 | } else |
| 1271 | sb->recovery_cp = 0; |
| 1272 | |
| 1273 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1274 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1276 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1277 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1280 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1282 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1283 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1284 | |
| 1285 | if (rdev2->raid_disk >= 0 && |
| 1286 | sb->minor_version >= 91) |
| 1287 | /* we have nowhere to store the recovery_offset, |
| 1288 | * but if it is not below the reshape_position, |
| 1289 | * we can piggy-back on that. |
| 1290 | */ |
| 1291 | is_active = 1; |
| 1292 | if (rdev2->raid_disk < 0 || |
| 1293 | test_bit(Faulty, &rdev2->flags)) |
| 1294 | is_active = 0; |
| 1295 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1296 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1298 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1299 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | d = &sb->disks[rdev2->desc_nr]; |
| 1301 | nr_disks++; |
| 1302 | d->number = rdev2->desc_nr; |
| 1303 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1304 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1305 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | d->raid_disk = rdev2->raid_disk; |
| 1307 | else |
| 1308 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1309 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1311 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1313 | if (test_bit(In_sync, &rdev2->flags)) |
| 1314 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | active++; |
| 1316 | working++; |
| 1317 | } else { |
| 1318 | d->state = 0; |
| 1319 | spare++; |
| 1320 | working++; |
| 1321 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1322 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1323 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1326 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1327 | mdp_disk_t *d = &sb->disks[i]; |
| 1328 | if (d->state == 0 && d->number == 0) { |
| 1329 | d->number = i; |
| 1330 | d->raid_disk = i; |
| 1331 | d->state = (1<<MD_DISK_REMOVED); |
| 1332 | d->state |= (1<<MD_DISK_FAULTY); |
| 1333 | failed++; |
| 1334 | } |
| 1335 | } |
| 1336 | sb->nr_disks = nr_disks; |
| 1337 | sb->active_disks = active; |
| 1338 | sb->working_disks = working; |
| 1339 | sb->failed_disks = failed; |
| 1340 | sb->spare_disks = spare; |
| 1341 | |
| 1342 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1343 | sb->sb_csum = calc_sb_csum(sb); |
| 1344 | } |
| 1345 | |
| 1346 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1347 | * rdev_size_change for 0.90.0 |
| 1348 | */ |
| 1349 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1350 | 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] | 1351 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1352 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1353 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1354 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1355 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1356 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1357 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1358 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1359 | /* Limit to 4TB as metadata cannot record more than that. |
| 1360 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1361 | */ |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1362 | if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1363 | num_sectors = (2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1364 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1365 | rdev->sb_page); |
| 1366 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1367 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1368 | } |
| 1369 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1370 | static int |
| 1371 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1372 | { |
| 1373 | /* non-zero offset changes not possible with v0.90 */ |
| 1374 | return new_offset == 0; |
| 1375 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1376 | |
| 1377 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | * version 1 superblock |
| 1379 | */ |
| 1380 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1381 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1383 | __le32 disk_csum; |
| 1384 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | unsigned long long newcsum; |
| 1386 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1387 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | |
| 1389 | disk_csum = sb->sb_csum; |
| 1390 | sb->sb_csum = 0; |
| 1391 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1392 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | newcsum += le32_to_cpu(*isuper++); |
| 1394 | |
| 1395 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1396 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | |
| 1398 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1399 | sb->sb_csum = disk_csum; |
| 1400 | return cpu_to_le32(csum); |
| 1401 | } |
| 1402 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1403 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1404 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1405 | 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] | 1406 | { |
| 1407 | struct mdp_superblock_1 *sb; |
| 1408 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1409 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1410 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1412 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | |
| 1414 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1415 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | * It is always aligned to a 4K boundary and |
| 1417 | * depeding on minor_version, it can be: |
| 1418 | * 0: At least 8K, but less than 12K, from end of device |
| 1419 | * 1: At start of device |
| 1420 | * 2: 4K from start of device. |
| 1421 | */ |
| 1422 | switch(minor_version) { |
| 1423 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1424 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1425 | sb_start -= 8*2; |
| 1426 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | break; |
| 1428 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1429 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | break; |
| 1431 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1432 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | break; |
| 1434 | default: |
| 1435 | return -EINVAL; |
| 1436 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1437 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1439 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1440 | * and it is safe to read 4k, so we do that |
| 1441 | */ |
| 1442 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | if (ret) return ret; |
| 1444 | |
| 1445 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1446 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | |
| 1448 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1449 | sb->major_version != cpu_to_le32(1) || |
| 1450 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1451 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1452 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | return -EINVAL; |
| 1454 | |
| 1455 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1456 | printk("md: invalid superblock checksum on %s\n", |
| 1457 | bdevname(rdev->bdev,b)); |
| 1458 | return -EINVAL; |
| 1459 | } |
| 1460 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1461 | printk("md: data_size too small on %s\n", |
| 1462 | bdevname(rdev->bdev,b)); |
| 1463 | return -EINVAL; |
| 1464 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1465 | if (sb->pad0 || |
| 1466 | sb->pad3[0] || |
| 1467 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1468 | /* Some padding is non-zero, might be a new feature */ |
| 1469 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | rdev->preferred_minor = 0xffff; |
| 1472 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1473 | rdev->new_data_offset = rdev->data_offset; |
| 1474 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1475 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1476 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1477 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1479 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1480 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1481 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1482 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1483 | |
| 1484 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1485 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1486 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1487 | if (minor_version |
| 1488 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1489 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1490 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1491 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1492 | rdev->desc_nr = -1; |
| 1493 | else |
| 1494 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1495 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1496 | if (!rdev->bb_page) { |
| 1497 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1498 | if (!rdev->bb_page) |
| 1499 | return -ENOMEM; |
| 1500 | } |
| 1501 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1502 | rdev->badblocks.count == 0) { |
| 1503 | /* need to load the bad block list. |
| 1504 | * Currently we limit it to one page. |
| 1505 | */ |
| 1506 | s32 offset; |
| 1507 | sector_t bb_sector; |
| 1508 | u64 *bbp; |
| 1509 | int i; |
| 1510 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1511 | if (sectors > (PAGE_SIZE / 512)) |
| 1512 | return -EINVAL; |
| 1513 | offset = le32_to_cpu(sb->bblog_offset); |
| 1514 | if (offset == 0) |
| 1515 | return -EINVAL; |
| 1516 | bb_sector = (long long)offset; |
| 1517 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1518 | rdev->bb_page, READ, true)) |
| 1519 | return -EIO; |
| 1520 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1521 | rdev->badblocks.shift = sb->bblog_shift; |
| 1522 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1523 | u64 bb = le64_to_cpu(*bbp); |
| 1524 | int count = bb & (0x3ff); |
| 1525 | u64 sector = bb >> 10; |
| 1526 | sector <<= sb->bblog_shift; |
| 1527 | count <<= sb->bblog_shift; |
| 1528 | if (bb + 1 == 0) |
| 1529 | break; |
| 1530 | if (md_set_badblocks(&rdev->badblocks, |
| 1531 | sector, count, 1) == 0) |
| 1532 | return -EINVAL; |
| 1533 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1534 | } else if (sb->bblog_offset != 0) |
| 1535 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1536 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1537 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1538 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1539 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1541 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | |
| 1543 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1544 | sb->level != refsb->level || |
| 1545 | sb->layout != refsb->layout || |
| 1546 | sb->chunksize != refsb->chunksize) { |
| 1547 | printk(KERN_WARNING "md: %s has strangely different" |
| 1548 | " superblock to %s\n", |
| 1549 | bdevname(rdev->bdev,b), |
| 1550 | bdevname(refdev->bdev,b2)); |
| 1551 | return -EINVAL; |
| 1552 | } |
| 1553 | ev1 = le64_to_cpu(sb->events); |
| 1554 | ev2 = le64_to_cpu(refsb->events); |
| 1555 | |
| 1556 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1557 | ret = 1; |
| 1558 | else |
| 1559 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1561 | if (minor_version) { |
| 1562 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1563 | sectors -= rdev->data_offset; |
| 1564 | } else |
| 1565 | sectors = rdev->sb_start; |
| 1566 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1568 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1569 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1572 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1574 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1575 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1577 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1578 | clear_bit(Faulty, &rdev->flags); |
| 1579 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1580 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1581 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | if (mddev->raid_disks == 0) { |
| 1584 | mddev->major_version = 1; |
| 1585 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1586 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1587 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1589 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1590 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1591 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | mddev->layout = le32_to_cpu(sb->layout); |
| 1593 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1594 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1595 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1596 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1597 | mddev->bitmap_info.space = 0; |
| 1598 | /* Default location for bitmap is 1K after superblock |
| 1599 | * using 3K - total of 4K |
| 1600 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1601 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1602 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1603 | mddev->reshape_backwards = 0; |
| 1604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1606 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1607 | |
| 1608 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1609 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1610 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1611 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1612 | mddev->bitmap_info.offset = |
| 1613 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1614 | /* Metadata doesn't record how much space is available. |
| 1615 | * For 1.0, we assume we can use up to the superblock |
| 1616 | * if before, else to 4K beyond superblock. |
| 1617 | * For others, assume no change is possible. |
| 1618 | */ |
| 1619 | if (mddev->minor_version > 0) |
| 1620 | mddev->bitmap_info.space = 0; |
| 1621 | else if (mddev->bitmap_info.offset > 0) |
| 1622 | mddev->bitmap_info.space = |
| 1623 | 8 - mddev->bitmap_info.offset; |
| 1624 | else |
| 1625 | mddev->bitmap_info.space = |
| 1626 | -mddev->bitmap_info.offset; |
| 1627 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1628 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1629 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1630 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1631 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1632 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1633 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1634 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1635 | if (mddev->delta_disks < 0 || |
| 1636 | (mddev->delta_disks == 0 && |
| 1637 | (le32_to_cpu(sb->feature_map) |
| 1638 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1639 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1640 | } else { |
| 1641 | mddev->reshape_position = MaxSector; |
| 1642 | mddev->delta_disks = 0; |
| 1643 | mddev->new_level = mddev->level; |
| 1644 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1645 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1646 | } |
| 1647 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1648 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1649 | /* Insist of good event counter while assembling, except for |
| 1650 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1652 | if (rdev->desc_nr >= 0 && |
| 1653 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1654 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1655 | if (ev1 < mddev->events) |
| 1656 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1657 | } else if (mddev->bitmap) { |
| 1658 | /* If adding to array with a bitmap, then we can accept an |
| 1659 | * older device, but not too old. |
| 1660 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1661 | if (ev1 < mddev->bitmap->events_cleared) |
| 1662 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1663 | if (ev1 < mddev->events) |
| 1664 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1665 | } else { |
| 1666 | if (ev1 < mddev->events) |
| 1667 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1668 | return 0; |
| 1669 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1671 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1672 | if (rdev->desc_nr < 0 || |
| 1673 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1674 | role = 0xffff; |
| 1675 | rdev->desc_nr = -1; |
| 1676 | } else |
| 1677 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | switch(role) { |
| 1679 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | break; |
| 1681 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1682 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | break; |
| 1684 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame^] | 1685 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1686 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame^] | 1687 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1688 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame^] | 1689 | if (!(le32_to_cpu(sb->feature_map) & |
| 1690 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1691 | rdev->saved_raid_disk = -1; |
| 1692 | } else |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1693 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | rdev->raid_disk = role; |
| 1695 | break; |
| 1696 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1697 | if (sb->devflags & WriteMostly1) |
| 1698 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1699 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1700 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1701 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1702 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | return 0; |
| 1705 | } |
| 1706 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1707 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | { |
| 1709 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1710 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | int max_dev, i; |
| 1712 | /* make rdev->sb match mddev and rdev data. */ |
| 1713 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1714 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | |
| 1716 | sb->feature_map = 0; |
| 1717 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1718 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1720 | |
| 1721 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1722 | sb->events = cpu_to_le64(mddev->events); |
| 1723 | if (mddev->in_sync) |
| 1724 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1725 | else |
| 1726 | sb->resync_offset = cpu_to_le64(0); |
| 1727 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1728 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1729 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1730 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1731 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1732 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1733 | sb->level = cpu_to_le32(mddev->level); |
| 1734 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1735 | |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1736 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1737 | sb->devflags |= WriteMostly1; |
| 1738 | else |
| 1739 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1740 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1741 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b21 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1742 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1743 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1744 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1745 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1746 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1747 | |
| 1748 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1749 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1750 | sb->feature_map |= |
| 1751 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1752 | sb->recovery_offset = |
| 1753 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame^] | 1754 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 1755 | sb->feature_map |= |
| 1756 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1757 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1758 | if (test_bit(Replacement, &rdev->flags)) |
| 1759 | sb->feature_map |= |
| 1760 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1761 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1762 | if (mddev->reshape_position != MaxSector) { |
| 1763 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1764 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1765 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1766 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1767 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1768 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1769 | if (mddev->delta_disks == 0 && |
| 1770 | mddev->reshape_backwards) |
| 1771 | sb->feature_map |
| 1772 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1773 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1774 | sb->feature_map |
| 1775 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1776 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1777 | - rdev->data_offset)); |
| 1778 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1779 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1780 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1781 | if (rdev->badblocks.count == 0) |
| 1782 | /* Nothing to do for bad blocks*/ ; |
| 1783 | else if (sb->bblog_offset == 0) |
| 1784 | /* Cannot record bad blocks on this device */ |
| 1785 | md_error(mddev, rdev); |
| 1786 | else { |
| 1787 | struct badblocks *bb = &rdev->badblocks; |
| 1788 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1789 | u64 *p = bb->page; |
| 1790 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1791 | if (bb->changed) { |
| 1792 | unsigned seq; |
| 1793 | |
| 1794 | retry: |
| 1795 | seq = read_seqbegin(&bb->lock); |
| 1796 | |
| 1797 | memset(bbp, 0xff, PAGE_SIZE); |
| 1798 | |
| 1799 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1800 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1801 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1802 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1803 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1804 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1805 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1806 | if (read_seqretry(&bb->lock, seq)) |
| 1807 | goto retry; |
| 1808 | |
| 1809 | bb->sector = (rdev->sb_start + |
| 1810 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1811 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1812 | } |
| 1813 | } |
| 1814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1816 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | if (rdev2->desc_nr+1 > max_dev) |
| 1818 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1819 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1820 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1821 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1822 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1823 | rdev->sb_size = max_dev * 2 + 256; |
| 1824 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1825 | if (rdev->sb_size & bmask) |
| 1826 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1827 | } else |
| 1828 | max_dev = le32_to_cpu(sb->max_dev); |
| 1829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | for (i=0; i<max_dev;i++) |
| 1831 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1832 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1833 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1835 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1837 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1839 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1840 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | else |
| 1842 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1843 | } |
| 1844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1846 | } |
| 1847 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1848 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1849 | 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] | 1850 | { |
| 1851 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1852 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1853 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1854 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1855 | if (rdev->data_offset != rdev->new_data_offset) |
| 1856 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1857 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1858 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1859 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1860 | max_sectors -= rdev->data_offset; |
| 1861 | if (!num_sectors || num_sectors > max_sectors) |
| 1862 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1863 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1864 | /* minor version 0 with bitmap we can't move */ |
| 1865 | return 0; |
| 1866 | } else { |
| 1867 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1868 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1869 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1870 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1871 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1872 | if (!num_sectors || num_sectors > max_sectors) |
| 1873 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1874 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1875 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1876 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1877 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1878 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1879 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1880 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1881 | rdev->sb_page); |
| 1882 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1883 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1884 | |
| 1885 | } |
| 1886 | |
| 1887 | static int |
| 1888 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1889 | unsigned long long new_offset) |
| 1890 | { |
| 1891 | /* All necessary checks on new >= old have been done */ |
| 1892 | struct bitmap *bitmap; |
| 1893 | if (new_offset >= rdev->data_offset) |
| 1894 | return 1; |
| 1895 | |
| 1896 | /* with 1.0 metadata, there is no metadata to tread on |
| 1897 | * so we can always move back */ |
| 1898 | if (rdev->mddev->minor_version == 0) |
| 1899 | return 1; |
| 1900 | |
| 1901 | /* otherwise we must be sure not to step on |
| 1902 | * any metadata, so stay: |
| 1903 | * 36K beyond start of superblock |
| 1904 | * beyond end of badblocks |
| 1905 | * beyond write-intent bitmap |
| 1906 | */ |
| 1907 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1908 | return 0; |
| 1909 | bitmap = rdev->mddev->bitmap; |
| 1910 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1911 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1912 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1913 | return 0; |
| 1914 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1915 | return 0; |
| 1916 | |
| 1917 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1918 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1920 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | [0] = { |
| 1922 | .name = "0.90.0", |
| 1923 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1924 | .load_super = super_90_load, |
| 1925 | .validate_super = super_90_validate, |
| 1926 | .sync_super = super_90_sync, |
| 1927 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1928 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | }, |
| 1930 | [1] = { |
| 1931 | .name = "md-1", |
| 1932 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1933 | .load_super = super_1_load, |
| 1934 | .validate_super = super_1_validate, |
| 1935 | .sync_super = super_1_sync, |
| 1936 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1937 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | }, |
| 1939 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1941 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1942 | { |
| 1943 | if (mddev->sync_super) { |
| 1944 | mddev->sync_super(mddev, rdev); |
| 1945 | return; |
| 1946 | } |
| 1947 | |
| 1948 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1949 | |
| 1950 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1951 | } |
| 1952 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1953 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1955 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1957 | rcu_read_lock(); |
| 1958 | rdev_for_each_rcu(rdev, mddev1) |
| 1959 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1960 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1961 | rdev2->bdev->bd_contains) { |
| 1962 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1963 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1964 | } |
| 1965 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | return 0; |
| 1967 | } |
| 1968 | |
| 1969 | static LIST_HEAD(pending_raid_disks); |
| 1970 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1971 | /* |
| 1972 | * Try to register data integrity profile for an mddev |
| 1973 | * |
| 1974 | * This is called when an array is started and after a disk has been kicked |
| 1975 | * from the array. It only succeeds if all working and active component devices |
| 1976 | * are integrity capable with matching profiles. |
| 1977 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1978 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1979 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1980 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1981 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1982 | if (list_empty(&mddev->disks)) |
| 1983 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1984 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1985 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1986 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1987 | /* skip spares and non-functional disks */ |
| 1988 | if (test_bit(Faulty, &rdev->flags)) |
| 1989 | continue; |
| 1990 | if (rdev->raid_disk < 0) |
| 1991 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1992 | if (!reference) { |
| 1993 | /* Use the first rdev as the reference */ |
| 1994 | reference = rdev; |
| 1995 | continue; |
| 1996 | } |
| 1997 | /* does this rdev's profile match the reference profile? */ |
| 1998 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1999 | rdev->bdev->bd_disk) < 0) |
| 2000 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2001 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2002 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2003 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2004 | /* |
| 2005 | * All component devices are integrity capable and have matching |
| 2006 | * profiles, register the common profile for the md device. |
| 2007 | */ |
| 2008 | if (blk_integrity_register(mddev->gendisk, |
| 2009 | bdev_get_integrity(reference->bdev)) != 0) { |
| 2010 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 2011 | mdname(mddev)); |
| 2012 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2013 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2014 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 2015 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 2016 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 2017 | mdname(mddev)); |
| 2018 | return -EINVAL; |
| 2019 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2020 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2021 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2022 | EXPORT_SYMBOL(md_integrity_register); |
| 2023 | |
| 2024 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2025 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2026 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2027 | struct blk_integrity *bi_rdev; |
| 2028 | struct blk_integrity *bi_mddev; |
| 2029 | |
| 2030 | if (!mddev->gendisk) |
| 2031 | return; |
| 2032 | |
| 2033 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2034 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2035 | |
| 2036 | if (!bi_mddev) /* nothing to do */ |
| 2037 | return; |
| 2038 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2039 | return; |
| 2040 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2041 | rdev->bdev->bd_disk) >= 0) |
| 2042 | return; |
| 2043 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2044 | blk_integrity_unregister(mddev->gendisk); |
| 2045 | } |
| 2046 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2047 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2048 | 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] | 2049 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2050 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2051 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2052 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2053 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | |
| 2055 | if (rdev->mddev) { |
| 2056 | MD_BUG(); |
| 2057 | return -EINVAL; |
| 2058 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2059 | |
| 2060 | /* prevent duplicates */ |
| 2061 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2062 | return -EEXIST; |
| 2063 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2064 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 2065 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 2066 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2067 | if (mddev->pers) { |
| 2068 | /* Cannot change size, so fail |
| 2069 | * If mddev->level <= 0, then we don't care |
| 2070 | * about aligning sizes (e.g. linear) |
| 2071 | */ |
| 2072 | if (mddev->level > 0) |
| 2073 | return -ENOSPC; |
| 2074 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2075 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2076 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | |
| 2078 | /* Verify rdev->desc_nr is unique. |
| 2079 | * If it is -1, assign a free number, else |
| 2080 | * check number is not in use |
| 2081 | */ |
| 2082 | if (rdev->desc_nr < 0) { |
| 2083 | int choice = 0; |
| 2084 | if (mddev->pers) choice = mddev->raid_disks; |
| 2085 | while (find_rdev_nr(mddev, choice)) |
| 2086 | choice++; |
| 2087 | rdev->desc_nr = choice; |
| 2088 | } else { |
| 2089 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 2090 | return -EBUSY; |
| 2091 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2092 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 2093 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2094 | mdname(mddev), mddev->max_disks); |
| 2095 | return -EBUSY; |
| 2096 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2097 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2098 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 2099 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2102 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2103 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2104 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2105 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2106 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2107 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2108 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2109 | /* failure here is OK */; |
| 2110 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2111 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2112 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2113 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2114 | |
| 2115 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2116 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2119 | |
| 2120 | fail: |
| 2121 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2122 | b, mdname(mddev)); |
| 2123 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | } |
| 2125 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2126 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2127 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2128 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2129 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2130 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2131 | } |
| 2132 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2133 | static void unbind_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | { |
| 2135 | char b[BDEVNAME_SIZE]; |
| 2136 | if (!rdev->mddev) { |
| 2137 | MD_BUG(); |
| 2138 | return; |
| 2139 | } |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2140 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2141 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2143 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2144 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2145 | sysfs_put(rdev->sysfs_state); |
| 2146 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2147 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2148 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2149 | * writing to 'remove' to "dev/state". We also need |
| 2150 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2151 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2152 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2153 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2154 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2155 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | /* |
| 2159 | * prevent the device from being mounted, repartitioned or |
| 2160 | * otherwise reused by a RAID array (or any other kernel |
| 2161 | * subsystem), by bd_claiming the device. |
| 2162 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2163 | 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] | 2164 | { |
| 2165 | int err = 0; |
| 2166 | struct block_device *bdev; |
| 2167 | char b[BDEVNAME_SIZE]; |
| 2168 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2169 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2170 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | if (IS_ERR(bdev)) { |
| 2172 | printk(KERN_ERR "md: could not open %s.\n", |
| 2173 | __bdevname(dev, b)); |
| 2174 | return PTR_ERR(bdev); |
| 2175 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | rdev->bdev = bdev; |
| 2177 | return err; |
| 2178 | } |
| 2179 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2180 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | { |
| 2182 | struct block_device *bdev = rdev->bdev; |
| 2183 | rdev->bdev = NULL; |
| 2184 | if (!bdev) |
| 2185 | MD_BUG(); |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2186 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | void md_autodetect_dev(dev_t dev); |
| 2190 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2191 | static void export_rdev(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | { |
| 2193 | char b[BDEVNAME_SIZE]; |
| 2194 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2195 | bdevname(rdev->bdev,b)); |
| 2196 | if (rdev->mddev) |
| 2197 | MD_BUG(); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2198 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2200 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2201 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | #endif |
| 2203 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2204 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | } |
| 2206 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2207 | static void kick_rdev_from_array(struct md_rdev * rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | { |
| 2209 | unbind_rdev_from_array(rdev); |
| 2210 | export_rdev(rdev); |
| 2211 | } |
| 2212 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2213 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2215 | struct md_rdev *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2217 | rdev_for_each_safe(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | if (!rdev->mddev) { |
| 2219 | MD_BUG(); |
| 2220 | continue; |
| 2221 | } |
| 2222 | kick_rdev_from_array(rdev); |
| 2223 | } |
| 2224 | if (!list_empty(&mddev->disks)) |
| 2225 | MD_BUG(); |
| 2226 | mddev->raid_disks = 0; |
| 2227 | mddev->major_version = 0; |
| 2228 | } |
| 2229 | |
| 2230 | static void print_desc(mdp_disk_t *desc) |
| 2231 | { |
| 2232 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 2233 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 2234 | } |
| 2235 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2236 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | { |
| 2238 | int i; |
| 2239 | |
| 2240 | printk(KERN_INFO |
| 2241 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2242 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2243 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2244 | sb->ctime); |
| 2245 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2246 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2247 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2248 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2249 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2250 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2251 | sb->failed_disks, sb->spare_disks, |
| 2252 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2253 | |
| 2254 | printk(KERN_INFO); |
| 2255 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2256 | mdp_disk_t *desc; |
| 2257 | |
| 2258 | desc = sb->disks + i; |
| 2259 | if (desc->number || desc->major || desc->minor || |
| 2260 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2261 | printk(" D %2d: ", i); |
| 2262 | print_desc(desc); |
| 2263 | } |
| 2264 | } |
| 2265 | printk(KERN_INFO "md: THIS: "); |
| 2266 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | } |
| 2268 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2269 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2270 | { |
| 2271 | __u8 *uuid; |
| 2272 | |
| 2273 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2274 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2275 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2276 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2277 | le32_to_cpu(sb->major_version), |
| 2278 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2279 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2280 | sb->set_name, |
| 2281 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2282 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2283 | |
| 2284 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2285 | printk(KERN_INFO |
| 2286 | "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu" |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2287 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2288 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2289 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2290 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2291 | le32_to_cpu(sb->level), |
| 2292 | (unsigned long long)le64_to_cpu(sb->size), |
| 2293 | le32_to_cpu(sb->raid_disks), |
| 2294 | le32_to_cpu(sb->layout), |
| 2295 | le32_to_cpu(sb->chunksize), |
| 2296 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2297 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2298 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2299 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2300 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2301 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2302 | sb->devflags, |
| 2303 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2304 | (unsigned long long)le64_to_cpu(sb->events), |
| 2305 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2306 | le32_to_cpu(sb->sb_csum), |
| 2307 | le32_to_cpu(sb->max_dev) |
| 2308 | ); |
| 2309 | } |
| 2310 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2311 | static void print_rdev(struct md_rdev *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | { |
| 2313 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2314 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2315 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2316 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2317 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2319 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2320 | switch (major_version) { |
| 2321 | case 0: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2322 | print_sb_90(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2323 | break; |
| 2324 | case 1: |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2325 | print_sb_1(page_address(rdev->sb_page)); |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2326 | break; |
| 2327 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | } else |
| 2329 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2330 | } |
| 2331 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2332 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2334 | struct list_head *tmp; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2335 | struct md_rdev *rdev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2336 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | char b[BDEVNAME_SIZE]; |
| 2338 | |
| 2339 | printk("\n"); |
| 2340 | printk("md: **********************************\n"); |
| 2341 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2342 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2343 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2345 | if (mddev->bitmap) |
| 2346 | bitmap_print_sb(mddev->bitmap); |
| 2347 | else |
| 2348 | printk("%s: ", mdname(mddev)); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2349 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2351 | printk("\n"); |
| 2352 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2353 | rdev_for_each(rdev, mddev) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2354 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | } |
| 2356 | printk("md: **********************************\n"); |
| 2357 | printk("\n"); |
| 2358 | } |
| 2359 | |
| 2360 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2361 | static void sync_sbs(struct mddev * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2363 | /* Update each superblock (in-memory image), but |
| 2364 | * if we are allowed to, skip spares which already |
| 2365 | * have the right event counter, or have one earlier |
| 2366 | * (which would mean they aren't being marked as dirty |
| 2367 | * with the rest of the array) |
| 2368 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2369 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2370 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2371 | if (rdev->sb_events == mddev->events || |
| 2372 | (nospares && |
| 2373 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2374 | rdev->sb_events+1 == mddev->events)) { |
| 2375 | /* Don't update this superblock */ |
| 2376 | rdev->sb_loaded = 2; |
| 2377 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2378 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2379 | rdev->sb_loaded = 1; |
| 2380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | } |
| 2382 | } |
| 2383 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2384 | static void md_update_sb(struct mddev * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2386 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2387 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2388 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2389 | int any_badblocks_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2391 | if (mddev->ro) { |
| 2392 | if (force_change) |
| 2393 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2394 | return; |
| 2395 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2397 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2398 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2399 | if (rdev->raid_disk >= 0 && |
| 2400 | mddev->delta_disks >= 0 && |
| 2401 | !test_bit(In_sync, &rdev->flags) && |
| 2402 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2403 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2404 | |
| 2405 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2406 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2407 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2408 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2409 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2410 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2411 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2412 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2413 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2414 | md_ack_all_badblocks(&rdev->badblocks); |
| 2415 | md_error(mddev, rdev); |
| 2416 | } |
| 2417 | clear_bit(Blocked, &rdev->flags); |
| 2418 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2419 | wake_up(&rdev->blocked_wait); |
| 2420 | } |
| 2421 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2422 | wake_up(&mddev->sb_wait); |
| 2423 | return; |
| 2424 | } |
| 2425 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2426 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2427 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2428 | mddev->utime = get_seconds(); |
| 2429 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2430 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2431 | force_change = 1; |
| 2432 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2433 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2434 | * though if events isn't the right even/odd, we will have to do |
| 2435 | * spares after all |
| 2436 | */ |
| 2437 | nospares = 1; |
| 2438 | if (force_change) |
| 2439 | nospares = 0; |
| 2440 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2441 | /* If the array is degraded, then skipping spares is both |
| 2442 | * dangerous and fairly pointless. |
| 2443 | * Dangerous because a device that was removed from the array |
| 2444 | * might have a event_count that still looks up-to-date, |
| 2445 | * so it can be re-added without a resync. |
| 2446 | * Pointless because if there are any spares to skip, |
| 2447 | * then a recovery will happen and soon that array won't |
| 2448 | * be degraded any more and the spare can go back to sleep then. |
| 2449 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2450 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2451 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2452 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2453 | |
| 2454 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2455 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2456 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2457 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2458 | && mddev->can_decrease_events |
| 2459 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2460 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2461 | mddev->can_decrease_events = 0; |
| 2462 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2463 | /* otherwise we have to go forward and ... */ |
| 2464 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2465 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2466 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | |
| 2468 | if (!mddev->events) { |
| 2469 | /* |
| 2470 | * oops, this 64-bit counter should never wrap. |
| 2471 | * Either we are in around ~1 trillion A.C., assuming |
| 2472 | * 1 reboot per second, or we have a bug: |
| 2473 | */ |
| 2474 | MD_BUG(); |
| 2475 | mddev->events --; |
| 2476 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2477 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2478 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2479 | if (rdev->badblocks.changed) |
| 2480 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2481 | if (test_bit(Faulty, &rdev->flags)) |
| 2482 | set_bit(FaultRecorded, &rdev->flags); |
| 2483 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2484 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2485 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2486 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2487 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2488 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2489 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2491 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2492 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2494 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2495 | if (rdev->sb_loaded != 1) |
| 2496 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame^] | 2498 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2499 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2500 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2501 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2502 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2503 | bdevname(rdev->bdev, b), |
| 2504 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2505 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2506 | if (rdev->badblocks.size) { |
| 2507 | md_super_write(mddev, rdev, |
| 2508 | rdev->badblocks.sector, |
| 2509 | rdev->badblocks.size << 9, |
| 2510 | rdev->bb_page); |
| 2511 | rdev->badblocks.size = 0; |
| 2512 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2513 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame^] | 2514 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2515 | pr_debug("md: %s (skipping faulty)\n", |
| 2516 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2517 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2518 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | /* only need to write one superblock... */ |
| 2520 | break; |
| 2521 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2522 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2523 | /* 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] | 2524 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2525 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2526 | if (mddev->in_sync != sync_req || |
| 2527 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2528 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2529 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2530 | goto repeat; |
| 2531 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2532 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2533 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2534 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2535 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2536 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2537 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2538 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2539 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2540 | clear_bit(Blocked, &rdev->flags); |
| 2541 | |
| 2542 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2543 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2544 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2545 | wake_up(&rdev->blocked_wait); |
| 2546 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | } |
| 2548 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2549 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2550 | * We want to accept with case. For this we use cmd_match. |
| 2551 | */ |
| 2552 | static int cmd_match(const char *cmd, const char *str) |
| 2553 | { |
| 2554 | /* See if cmd, written into a sysfs file, matches |
| 2555 | * str. They must either be the same, or cmd can |
| 2556 | * have a trailing newline |
| 2557 | */ |
| 2558 | while (*cmd && *str && *cmd == *str) { |
| 2559 | cmd++; |
| 2560 | str++; |
| 2561 | } |
| 2562 | if (*cmd == '\n') |
| 2563 | cmd++; |
| 2564 | if (*str || *cmd) |
| 2565 | return 0; |
| 2566 | return 1; |
| 2567 | } |
| 2568 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2569 | struct rdev_sysfs_entry { |
| 2570 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2571 | ssize_t (*show)(struct md_rdev *, char *); |
| 2572 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2573 | }; |
| 2574 | |
| 2575 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2576 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2577 | { |
| 2578 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2579 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2580 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2581 | if (test_bit(Faulty, &rdev->flags) || |
| 2582 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2583 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2584 | sep = ","; |
| 2585 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2586 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2587 | len += sprintf(page+len, "%sin_sync",sep); |
| 2588 | sep = ","; |
| 2589 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2590 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2591 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2592 | sep = ","; |
| 2593 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2594 | if (test_bit(Blocked, &rdev->flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2595 | (rdev->badblocks.unacked_exist |
| 2596 | && !test_bit(Faulty, &rdev->flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2597 | len += sprintf(page+len, "%sblocked", sep); |
| 2598 | sep = ","; |
| 2599 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2600 | if (!test_bit(Faulty, &rdev->flags) && |
| 2601 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2602 | len += sprintf(page+len, "%sspare", sep); |
| 2603 | sep = ","; |
| 2604 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2605 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 2606 | len += sprintf(page+len, "%swrite_error", sep); |
| 2607 | sep = ","; |
| 2608 | } |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2609 | if (test_bit(WantReplacement, &rdev->flags)) { |
| 2610 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2611 | sep = ","; |
| 2612 | } |
| 2613 | if (test_bit(Replacement, &rdev->flags)) { |
| 2614 | len += sprintf(page+len, "%sreplacement", sep); |
| 2615 | sep = ","; |
| 2616 | } |
| 2617 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2618 | return len+sprintf(page+len, "\n"); |
| 2619 | } |
| 2620 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2621 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2622 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2623 | { |
| 2624 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2625 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2626 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2627 | * writemostly - sets write_mostly |
| 2628 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2629 | * blocked - sets the Blocked flags |
| 2630 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2631 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame^] | 2632 | * -insync - clear Insync for a device with a slot assigned, |
| 2633 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2634 | * write_error - sets WriteErrorSeen |
| 2635 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2636 | */ |
| 2637 | int err = -EINVAL; |
| 2638 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2639 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2640 | if (test_bit(Faulty, &rdev->flags)) |
| 2641 | err = 0; |
| 2642 | else |
| 2643 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2644 | } else if (cmd_match(buf, "remove")) { |
| 2645 | if (rdev->raid_disk >= 0) |
| 2646 | err = -EBUSY; |
| 2647 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2648 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2649 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2650 | if (mddev->pers) |
| 2651 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2652 | md_new_event(mddev); |
| 2653 | err = 0; |
| 2654 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2655 | } else if (cmd_match(buf, "writemostly")) { |
| 2656 | set_bit(WriteMostly, &rdev->flags); |
| 2657 | err = 0; |
| 2658 | } else if (cmd_match(buf, "-writemostly")) { |
| 2659 | clear_bit(WriteMostly, &rdev->flags); |
| 2660 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2661 | } else if (cmd_match(buf, "blocked")) { |
| 2662 | set_bit(Blocked, &rdev->flags); |
| 2663 | err = 0; |
| 2664 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2665 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2666 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2667 | /* metadata handler doesn't understand badblocks, |
| 2668 | * so we need to fail the device |
| 2669 | */ |
| 2670 | md_error(rdev->mddev, rdev); |
| 2671 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2672 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2673 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2674 | wake_up(&rdev->blocked_wait); |
| 2675 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2676 | md_wakeup_thread(rdev->mddev->thread); |
| 2677 | |
| 2678 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2679 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2680 | set_bit(In_sync, &rdev->flags); |
| 2681 | err = 0; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame^] | 2682 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0) { |
| 2683 | clear_bit(In_sync, &rdev->flags); |
| 2684 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2685 | rdev->raid_disk = -1; |
| 2686 | err = 0; |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2687 | } else if (cmd_match(buf, "write_error")) { |
| 2688 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2689 | err = 0; |
| 2690 | } else if (cmd_match(buf, "-write_error")) { |
| 2691 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2692 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2693 | } else if (cmd_match(buf, "want_replacement")) { |
| 2694 | /* Any non-spare device that is not a replacement can |
| 2695 | * become want_replacement at any time, but we then need to |
| 2696 | * check if recovery is needed. |
| 2697 | */ |
| 2698 | if (rdev->raid_disk >= 0 && |
| 2699 | !test_bit(Replacement, &rdev->flags)) |
| 2700 | set_bit(WantReplacement, &rdev->flags); |
| 2701 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2702 | md_wakeup_thread(rdev->mddev->thread); |
| 2703 | err = 0; |
| 2704 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2705 | /* Clearing 'want_replacement' is always allowed. |
| 2706 | * Once replacements starts it is too late though. |
| 2707 | */ |
| 2708 | err = 0; |
| 2709 | clear_bit(WantReplacement, &rdev->flags); |
| 2710 | } else if (cmd_match(buf, "replacement")) { |
| 2711 | /* Can only set a device as a replacement when array has not |
| 2712 | * yet been started. Once running, replacement is automatic |
| 2713 | * from spares, or by assigning 'slot'. |
| 2714 | */ |
| 2715 | if (rdev->mddev->pers) |
| 2716 | err = -EBUSY; |
| 2717 | else { |
| 2718 | set_bit(Replacement, &rdev->flags); |
| 2719 | err = 0; |
| 2720 | } |
| 2721 | } else if (cmd_match(buf, "-replacement")) { |
| 2722 | /* Similarly, can only clear Replacement before start */ |
| 2723 | if (rdev->mddev->pers) |
| 2724 | err = -EBUSY; |
| 2725 | else { |
| 2726 | clear_bit(Replacement, &rdev->flags); |
| 2727 | err = 0; |
| 2728 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2729 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2730 | if (!err) |
| 2731 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2732 | return err ? err : len; |
| 2733 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2734 | static struct rdev_sysfs_entry rdev_state = |
| 2735 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2736 | |
| 2737 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2738 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2739 | { |
| 2740 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2741 | } |
| 2742 | |
| 2743 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2744 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2745 | { |
| 2746 | char *e; |
| 2747 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2748 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2749 | atomic_set(&rdev->corrected_errors, n); |
| 2750 | return len; |
| 2751 | } |
| 2752 | return -EINVAL; |
| 2753 | } |
| 2754 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2755 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2756 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2757 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2758 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2759 | { |
| 2760 | if (rdev->raid_disk < 0) |
| 2761 | return sprintf(page, "none\n"); |
| 2762 | else |
| 2763 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2764 | } |
| 2765 | |
| 2766 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2767 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2768 | { |
| 2769 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2770 | int err; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2771 | int slot = simple_strtoul(buf, &e, 10); |
| 2772 | if (strncmp(buf, "none", 4)==0) |
| 2773 | slot = -1; |
| 2774 | else if (e==buf || (*e && *e!= '\n')) |
| 2775 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2776 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2777 | /* Setting 'slot' on an active array requires also |
| 2778 | * updating the 'rd%d' link, and communicating |
| 2779 | * with the personality with ->hot_*_disk. |
| 2780 | * For now we only support removing |
| 2781 | * failed/spare devices. This normally happens automatically, |
| 2782 | * but not when the metadata is externally managed. |
| 2783 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2784 | if (rdev->raid_disk == -1) |
| 2785 | return -EEXIST; |
| 2786 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2787 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2788 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2789 | clear_bit(Blocked, &rdev->flags); |
| 2790 | remove_and_add_spares(rdev->mddev, rdev); |
| 2791 | if (rdev->raid_disk >= 0) |
| 2792 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2793 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2794 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2795 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2796 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2797 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2798 | */ |
| 2799 | |
| 2800 | if (rdev->raid_disk != -1) |
| 2801 | return -EBUSY; |
| 2802 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2803 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2804 | return -EBUSY; |
| 2805 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2806 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2807 | return -EINVAL; |
| 2808 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2809 | if (slot >= rdev->mddev->raid_disks && |
| 2810 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2811 | return -ENOSPC; |
| 2812 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2813 | rdev->raid_disk = slot; |
| 2814 | if (test_bit(In_sync, &rdev->flags)) |
| 2815 | rdev->saved_raid_disk = slot; |
| 2816 | else |
| 2817 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2818 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 2819 | clear_bit(Bitmap_sync, &rdev->flags); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2820 | err = rdev->mddev->pers-> |
| 2821 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2822 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2823 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2824 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2825 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2826 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2827 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2828 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2829 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2830 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2831 | if (slot >= rdev->mddev->raid_disks && |
| 2832 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2833 | return -ENOSPC; |
| 2834 | rdev->raid_disk = slot; |
| 2835 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2836 | clear_bit(Faulty, &rdev->flags); |
| 2837 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2838 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2839 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2840 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2841 | return len; |
| 2842 | } |
| 2843 | |
| 2844 | |
| 2845 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2846 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2847 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2848 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2849 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2850 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2851 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2855 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2856 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2857 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2858 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2859 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2860 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2861 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2862 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2863 | /* Must set offset before size, so overlap checks |
| 2864 | * can be sane */ |
| 2865 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2866 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2867 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2868 | return len; |
| 2869 | } |
| 2870 | |
| 2871 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2872 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2873 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2874 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2875 | { |
| 2876 | return sprintf(page, "%llu\n", |
| 2877 | (unsigned long long)rdev->new_data_offset); |
| 2878 | } |
| 2879 | |
| 2880 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2881 | const char *buf, size_t len) |
| 2882 | { |
| 2883 | unsigned long long new_offset; |
| 2884 | struct mddev *mddev = rdev->mddev; |
| 2885 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2886 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2887 | return -EINVAL; |
| 2888 | |
| 2889 | if (mddev->sync_thread) |
| 2890 | return -EBUSY; |
| 2891 | if (new_offset == rdev->data_offset) |
| 2892 | /* reset is always permitted */ |
| 2893 | ; |
| 2894 | else if (new_offset > rdev->data_offset) { |
| 2895 | /* must not push array size beyond rdev_sectors */ |
| 2896 | if (new_offset - rdev->data_offset |
| 2897 | + mddev->dev_sectors > rdev->sectors) |
| 2898 | return -E2BIG; |
| 2899 | } |
| 2900 | /* Metadata worries about other space details. */ |
| 2901 | |
| 2902 | /* decreasing the offset is inconsistent with a backwards |
| 2903 | * reshape. |
| 2904 | */ |
| 2905 | if (new_offset < rdev->data_offset && |
| 2906 | mddev->reshape_backwards) |
| 2907 | return -EINVAL; |
| 2908 | /* Increasing offset is inconsistent with forwards |
| 2909 | * reshape. reshape_direction should be set to |
| 2910 | * 'backwards' first. |
| 2911 | */ |
| 2912 | if (new_offset > rdev->data_offset && |
| 2913 | !mddev->reshape_backwards) |
| 2914 | return -EINVAL; |
| 2915 | |
| 2916 | if (mddev->pers && mddev->persistent && |
| 2917 | !super_types[mddev->major_version] |
| 2918 | .allow_new_offset(rdev, new_offset)) |
| 2919 | return -E2BIG; |
| 2920 | rdev->new_data_offset = new_offset; |
| 2921 | if (new_offset > rdev->data_offset) |
| 2922 | mddev->reshape_backwards = 1; |
| 2923 | else if (new_offset < rdev->data_offset) |
| 2924 | mddev->reshape_backwards = 0; |
| 2925 | |
| 2926 | return len; |
| 2927 | } |
| 2928 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2929 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2930 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2931 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2932 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2933 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2934 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2935 | } |
| 2936 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2937 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2938 | { |
| 2939 | /* check if two start/length pairs overlap */ |
| 2940 | if (s1+l1 <= s2) |
| 2941 | return 0; |
| 2942 | if (s2+l2 <= s1) |
| 2943 | return 0; |
| 2944 | return 1; |
| 2945 | } |
| 2946 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2947 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2948 | { |
| 2949 | unsigned long long blocks; |
| 2950 | sector_t new; |
| 2951 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2952 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2953 | return -EINVAL; |
| 2954 | |
| 2955 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2956 | return -EINVAL; /* sector conversion overflow */ |
| 2957 | |
| 2958 | new = blocks * 2; |
| 2959 | if (new != blocks * 2) |
| 2960 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2961 | |
| 2962 | *sectors = new; |
| 2963 | return 0; |
| 2964 | } |
| 2965 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2966 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2967 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2968 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2969 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2970 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2971 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2972 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2973 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2974 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2975 | if (rdev->data_offset != rdev->new_data_offset) |
| 2976 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2977 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2978 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2979 | sectors = super_types[my_mddev->major_version]. |
| 2980 | rdev_size_change(rdev, sectors); |
| 2981 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2982 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2983 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2984 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2985 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 2986 | if (!my_mddev->pers->resize) |
| 2987 | /* Cannot change size for RAID0 or Linear etc */ |
| 2988 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2989 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2990 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2991 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2992 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2993 | rdev->sectors = sectors; |
| 2994 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2995 | /* need to check that all other rdevs with the same ->bdev |
| 2996 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2997 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2998 | * we have to change it back, we will have the lock again. |
| 2999 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3000 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3001 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3002 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3003 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3004 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 3005 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3006 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3007 | |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 3008 | mddev_lock_nointr(mddev); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3009 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 3010 | if (rdev->bdev == rdev2->bdev && |
| 3011 | rdev != rdev2 && |
| 3012 | overlaps(rdev->data_offset, rdev->sectors, |
| 3013 | rdev2->data_offset, |
| 3014 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3015 | overlap = 1; |
| 3016 | break; |
| 3017 | } |
| 3018 | mddev_unlock(mddev); |
| 3019 | if (overlap) { |
| 3020 | mddev_put(mddev); |
| 3021 | break; |
| 3022 | } |
| 3023 | } |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 3024 | mddev_lock_nointr(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3025 | if (overlap) { |
| 3026 | /* Someone else could have slipped in a size |
| 3027 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3028 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3029 | * safe, and trust userspace not to race with |
| 3030 | * itself |
| 3031 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3032 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3033 | return -EBUSY; |
| 3034 | } |
| 3035 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3036 | return len; |
| 3037 | } |
| 3038 | |
| 3039 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3040 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3041 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3042 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3043 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3044 | { |
| 3045 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3046 | |
| 3047 | if (test_bit(In_sync, &rdev->flags) || |
| 3048 | recovery_start == MaxSector) |
| 3049 | return sprintf(page, "none\n"); |
| 3050 | |
| 3051 | return sprintf(page, "%llu\n", recovery_start); |
| 3052 | } |
| 3053 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3054 | 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] | 3055 | { |
| 3056 | unsigned long long recovery_start; |
| 3057 | |
| 3058 | if (cmd_match(buf, "none")) |
| 3059 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3060 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3061 | return -EINVAL; |
| 3062 | |
| 3063 | if (rdev->mddev->pers && |
| 3064 | rdev->raid_disk >= 0) |
| 3065 | return -EBUSY; |
| 3066 | |
| 3067 | rdev->recovery_offset = recovery_start; |
| 3068 | if (recovery_start == MaxSector) |
| 3069 | set_bit(In_sync, &rdev->flags); |
| 3070 | else |
| 3071 | clear_bit(In_sync, &rdev->flags); |
| 3072 | return len; |
| 3073 | } |
| 3074 | |
| 3075 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3076 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3077 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3078 | |
| 3079 | static ssize_t |
| 3080 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 3081 | static ssize_t |
| 3082 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 3083 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3084 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3085 | { |
| 3086 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3087 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3088 | 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] | 3089 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3090 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3091 | /* Maybe that ack was all we needed */ |
| 3092 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3093 | wake_up(&rdev->blocked_wait); |
| 3094 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3095 | } |
| 3096 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3097 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3098 | |
| 3099 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3100 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3101 | { |
| 3102 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3103 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3104 | 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] | 3105 | { |
| 3106 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3107 | } |
| 3108 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3109 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3110 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3111 | static struct attribute *rdev_default_attrs[] = { |
| 3112 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3113 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3114 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3115 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3116 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3117 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3118 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3119 | &rdev_bad_blocks.attr, |
| 3120 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3121 | NULL, |
| 3122 | }; |
| 3123 | static ssize_t |
| 3124 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3125 | { |
| 3126 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3127 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3128 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3129 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3130 | |
| 3131 | if (!entry->show) |
| 3132 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3133 | |
| 3134 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 3135 | if (!rv) { |
| 3136 | if (rdev->mddev == NULL) |
| 3137 | rv = -EBUSY; |
| 3138 | else |
| 3139 | rv = entry->show(rdev, page); |
| 3140 | mddev_unlock(mddev); |
| 3141 | } |
| 3142 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | static ssize_t |
| 3146 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3147 | const char *page, size_t length) |
| 3148 | { |
| 3149 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3150 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3151 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3152 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3153 | |
| 3154 | if (!entry->store) |
| 3155 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3156 | if (!capable(CAP_SYS_ADMIN)) |
| 3157 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3158 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3159 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3160 | if (rdev->mddev == NULL) |
| 3161 | rv = -EBUSY; |
| 3162 | else |
| 3163 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3164 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3165 | } |
| 3166 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | static void rdev_free(struct kobject *ko) |
| 3170 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3171 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3172 | kfree(rdev); |
| 3173 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3174 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3175 | .show = rdev_attr_show, |
| 3176 | .store = rdev_attr_store, |
| 3177 | }; |
| 3178 | static struct kobj_type rdev_ktype = { |
| 3179 | .release = rdev_free, |
| 3180 | .sysfs_ops = &rdev_sysfs_ops, |
| 3181 | .default_attrs = rdev_default_attrs, |
| 3182 | }; |
| 3183 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3184 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3185 | { |
| 3186 | rdev->desc_nr = -1; |
| 3187 | rdev->saved_raid_disk = -1; |
| 3188 | rdev->raid_disk = -1; |
| 3189 | rdev->flags = 0; |
| 3190 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3191 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3192 | rdev->sb_events = 0; |
| 3193 | rdev->last_read_error.tv_sec = 0; |
| 3194 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3195 | rdev->sb_loaded = 0; |
| 3196 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3197 | atomic_set(&rdev->nr_pending, 0); |
| 3198 | atomic_set(&rdev->read_errors, 0); |
| 3199 | atomic_set(&rdev->corrected_errors, 0); |
| 3200 | |
| 3201 | INIT_LIST_HEAD(&rdev->same_set); |
| 3202 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3203 | |
| 3204 | /* Add space to store bad block list. |
| 3205 | * This reserves the space even on arrays where it cannot |
| 3206 | * be used - I wonder if that matters |
| 3207 | */ |
| 3208 | rdev->badblocks.count = 0; |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 3209 | rdev->badblocks.shift = -1; /* disabled until explicitly enabled */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3210 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3211 | seqlock_init(&rdev->badblocks.lock); |
| 3212 | if (rdev->badblocks.page == NULL) |
| 3213 | return -ENOMEM; |
| 3214 | |
| 3215 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3216 | } |
| 3217 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3218 | /* |
| 3219 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3220 | * |
| 3221 | * mark the device faulty if: |
| 3222 | * |
| 3223 | * - the device is nonexistent (zero size) |
| 3224 | * - the device has no valid superblock |
| 3225 | * |
| 3226 | * a faulty rdev _never_ has rdev->sb set. |
| 3227 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3228 | 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] | 3229 | { |
| 3230 | char b[BDEVNAME_SIZE]; |
| 3231 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3232 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3233 | sector_t size; |
| 3234 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3235 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | if (!rdev) { |
| 3237 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3238 | return ERR_PTR(-ENOMEM); |
| 3239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3240 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3241 | err = md_rdev_init(rdev); |
| 3242 | if (err) |
| 3243 | goto abort_free; |
| 3244 | err = alloc_disk_sb(rdev); |
| 3245 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3246 | goto abort_free; |
| 3247 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3248 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3249 | if (err) |
| 3250 | goto abort_free; |
| 3251 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3252 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3253 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3254 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3255 | if (!size) { |
| 3256 | printk(KERN_WARNING |
| 3257 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3258 | bdevname(rdev->bdev,b)); |
| 3259 | err = -EINVAL; |
| 3260 | goto abort_free; |
| 3261 | } |
| 3262 | |
| 3263 | if (super_format >= 0) { |
| 3264 | err = super_types[super_format]. |
| 3265 | load_super(rdev, NULL, super_minor); |
| 3266 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3267 | printk(KERN_WARNING |
| 3268 | "md: %s does not have a valid v%d.%d " |
| 3269 | "superblock, not importing!\n", |
| 3270 | bdevname(rdev->bdev,b), |
| 3271 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | goto abort_free; |
| 3273 | } |
| 3274 | if (err < 0) { |
| 3275 | printk(KERN_WARNING |
| 3276 | "md: could not read %s's sb, not importing!\n", |
| 3277 | bdevname(rdev->bdev,b)); |
| 3278 | goto abort_free; |
| 3279 | } |
| 3280 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | return rdev; |
| 3283 | |
| 3284 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3285 | if (rdev->bdev) |
| 3286 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3287 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | kfree(rdev); |
| 3289 | return ERR_PTR(err); |
| 3290 | } |
| 3291 | |
| 3292 | /* |
| 3293 | * Check a full RAID array for plausibility |
| 3294 | */ |
| 3295 | |
| 3296 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3297 | static void analyze_sbs(struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | { |
| 3299 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3300 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3301 | char b[BDEVNAME_SIZE]; |
| 3302 | |
| 3303 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3304 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | switch (super_types[mddev->major_version]. |
| 3306 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3307 | case 1: |
| 3308 | freshest = rdev; |
| 3309 | break; |
| 3310 | case 0: |
| 3311 | break; |
| 3312 | default: |
| 3313 | printk( KERN_ERR \ |
| 3314 | "md: fatal superblock inconsistency in %s" |
| 3315 | " -- removing from array\n", |
| 3316 | bdevname(rdev->bdev,b)); |
| 3317 | kick_rdev_from_array(rdev); |
| 3318 | } |
| 3319 | |
| 3320 | |
| 3321 | super_types[mddev->major_version]. |
| 3322 | validate_super(mddev, freshest); |
| 3323 | |
| 3324 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3325 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3326 | if (mddev->max_disks && |
| 3327 | (rdev->desc_nr >= mddev->max_disks || |
| 3328 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3329 | printk(KERN_WARNING |
| 3330 | "md: %s: %s: only %d devices permitted\n", |
| 3331 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3332 | mddev->max_disks); |
| 3333 | kick_rdev_from_array(rdev); |
| 3334 | continue; |
| 3335 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3336 | if (rdev != freshest) |
| 3337 | if (super_types[mddev->major_version]. |
| 3338 | validate_super(mddev, rdev)) { |
| 3339 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3340 | " from array!\n", |
| 3341 | bdevname(rdev->bdev,b)); |
| 3342 | kick_rdev_from_array(rdev); |
| 3343 | continue; |
| 3344 | } |
| 3345 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3346 | rdev->desc_nr = i++; |
| 3347 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3348 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 3349 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3350 | rdev->raid_disk = -1; |
| 3351 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | } |
| 3353 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | } |
| 3355 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3356 | /* Read a fixed-point number. |
| 3357 | * Numbers in sysfs attributes should be in "standard" units where |
| 3358 | * possible, so time should be in seconds. |
| 3359 | * However we internally use a a much smaller unit such as |
| 3360 | * milliseconds or jiffies. |
| 3361 | * This function takes a decimal number with a possible fractional |
| 3362 | * component, and produces an integer which is the result of |
| 3363 | * multiplying that number by 10^'scale'. |
| 3364 | * all without any floating-point arithmetic. |
| 3365 | */ |
| 3366 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3367 | { |
| 3368 | unsigned long result = 0; |
| 3369 | long decimals = -1; |
| 3370 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3371 | if (*cp == '.') |
| 3372 | decimals = 0; |
| 3373 | else if (decimals < scale) { |
| 3374 | unsigned int value; |
| 3375 | value = *cp - '0'; |
| 3376 | result = result * 10 + value; |
| 3377 | if (decimals >= 0) |
| 3378 | decimals++; |
| 3379 | } |
| 3380 | cp++; |
| 3381 | } |
| 3382 | if (*cp == '\n') |
| 3383 | cp++; |
| 3384 | if (*cp) |
| 3385 | return -EINVAL; |
| 3386 | if (decimals < 0) |
| 3387 | decimals = 0; |
| 3388 | while (decimals < scale) { |
| 3389 | result *= 10; |
| 3390 | decimals ++; |
| 3391 | } |
| 3392 | *res = result; |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
| 3396 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3397 | static void md_safemode_timeout(unsigned long data); |
| 3398 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3399 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3400 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3401 | { |
| 3402 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3403 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3404 | } |
| 3405 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3406 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3407 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3408 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3409 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3410 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3411 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3412 | if (msec == 0) |
| 3413 | mddev->safemode_delay = 0; |
| 3414 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3415 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3416 | mddev->safemode_delay = (msec*HZ)/1000; |
| 3417 | if (mddev->safemode_delay == 0) |
| 3418 | mddev->safemode_delay = 1; |
NeilBrown | 275c51c | 2013-08-08 09:20:32 +1000 | [diff] [blame] | 3419 | if (mddev->safemode_delay < old_delay || old_delay == 0) |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3420 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3421 | } |
| 3422 | return len; |
| 3423 | } |
| 3424 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3425 | __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] | 3426 | |
| 3427 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3428 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3429 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3430 | struct md_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3431 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3432 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3433 | else if (mddev->clevel[0]) |
| 3434 | return sprintf(page, "%s\n", mddev->clevel); |
| 3435 | else if (mddev->level != LEVEL_NONE) |
| 3436 | return sprintf(page, "%d\n", mddev->level); |
| 3437 | else |
| 3438 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3439 | } |
| 3440 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3441 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3442 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3443 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3444 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3445 | ssize_t rv = len; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3446 | struct md_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3447 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3448 | void *priv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3449 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3450 | |
| 3451 | if (mddev->pers == NULL) { |
| 3452 | if (len == 0) |
| 3453 | return 0; |
| 3454 | if (len >= sizeof(mddev->clevel)) |
| 3455 | return -ENOSPC; |
| 3456 | strncpy(mddev->clevel, buf, len); |
| 3457 | if (mddev->clevel[len-1] == '\n') |
| 3458 | len--; |
| 3459 | mddev->clevel[len] = 0; |
| 3460 | mddev->level = LEVEL_NONE; |
| 3461 | return rv; |
| 3462 | } |
| 3463 | |
| 3464 | /* request to change the personality. Need to ensure: |
| 3465 | * - array is not engaged in resync/recovery/reshape |
| 3466 | * - old personality can be suspended |
| 3467 | * - new personality will access other array. |
| 3468 | */ |
| 3469 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3470 | if (mddev->sync_thread || |
| 3471 | mddev->reshape_position != MaxSector || |
| 3472 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3473 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3474 | |
| 3475 | if (!mddev->pers->quiesce) { |
| 3476 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3477 | mdname(mddev), mddev->pers->name); |
| 3478 | return -EINVAL; |
| 3479 | } |
| 3480 | |
| 3481 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3482 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3483 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3484 | strncpy(clevel, buf, len); |
| 3485 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3486 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3487 | clevel[len] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3488 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3489 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3490 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3491 | if (request_module("md-%s", clevel) != 0) |
| 3492 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3493 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3494 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3495 | if (!pers || !try_module_get(pers->owner)) { |
| 3496 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3497 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3498 | return -EINVAL; |
| 3499 | } |
| 3500 | spin_unlock(&pers_lock); |
| 3501 | |
| 3502 | if (pers == mddev->pers) { |
| 3503 | /* Nothing to do! */ |
| 3504 | module_put(pers->owner); |
| 3505 | return rv; |
| 3506 | } |
| 3507 | if (!pers->takeover) { |
| 3508 | module_put(pers->owner); |
| 3509 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3510 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3511 | return -EINVAL; |
| 3512 | } |
| 3513 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3514 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3515 | rdev->new_raid_disk = rdev->raid_disk; |
| 3516 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3517 | /* ->takeover must set new_* and/or delta_disks |
| 3518 | * if it succeeds, and may set them when it fails. |
| 3519 | */ |
| 3520 | priv = pers->takeover(mddev); |
| 3521 | if (IS_ERR(priv)) { |
| 3522 | mddev->new_level = mddev->level; |
| 3523 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3524 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3525 | mddev->raid_disks -= mddev->delta_disks; |
| 3526 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3527 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3528 | module_put(pers->owner); |
| 3529 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3530 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3531 | return PTR_ERR(priv); |
| 3532 | } |
| 3533 | |
| 3534 | /* Looks like we have a winner */ |
| 3535 | mddev_suspend(mddev); |
| 3536 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3537 | |
| 3538 | if (mddev->pers->sync_request == NULL && |
| 3539 | pers->sync_request != NULL) { |
| 3540 | /* need to add the md_redundancy_group */ |
| 3541 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3542 | printk(KERN_WARNING |
| 3543 | "md: cannot register extra attributes for %s\n", |
| 3544 | mdname(mddev)); |
Tejun Heo | 388975c | 2013-09-11 23:19:13 -0400 | [diff] [blame] | 3545 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3546 | } |
| 3547 | if (mddev->pers->sync_request != NULL && |
| 3548 | pers->sync_request == NULL) { |
| 3549 | /* need to remove the md_redundancy_group */ |
| 3550 | if (mddev->to_remove == NULL) |
| 3551 | mddev->to_remove = &md_redundancy_group; |
| 3552 | } |
| 3553 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3554 | if (mddev->pers->sync_request == NULL && |
| 3555 | mddev->external) { |
| 3556 | /* We are converting from a no-redundancy array |
| 3557 | * to a redundancy array and metadata is managed |
| 3558 | * externally so we need to be sure that writes |
| 3559 | * won't block due to a need to transition |
| 3560 | * clean->dirty |
| 3561 | * until external management is started. |
| 3562 | */ |
| 3563 | mddev->in_sync = 0; |
| 3564 | mddev->safemode_delay = 0; |
| 3565 | mddev->safemode = 0; |
| 3566 | } |
| 3567 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3568 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3569 | if (rdev->raid_disk < 0) |
| 3570 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3571 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3572 | rdev->new_raid_disk = -1; |
| 3573 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3574 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3575 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3576 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3577 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3578 | if (rdev->raid_disk < 0) |
| 3579 | continue; |
| 3580 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3581 | continue; |
| 3582 | rdev->raid_disk = rdev->new_raid_disk; |
| 3583 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3584 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3585 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3586 | if (sysfs_link_rdev(mddev, rdev)) |
| 3587 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3588 | " for %s after level change\n", |
| 3589 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3590 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3594 | mddev->pers = pers; |
| 3595 | mddev->private = priv; |
| 3596 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3597 | mddev->level = mddev->new_level; |
| 3598 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3599 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3600 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3601 | mddev->reshape_backwards = 0; |
Krzysztof Wojcik | fee6872 | 2011-04-20 15:39:53 +1000 | [diff] [blame] | 3602 | mddev->degraded = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3603 | if (mddev->pers->sync_request == NULL) { |
| 3604 | /* this is now an array without redundancy, so |
| 3605 | * it must always be in_sync |
| 3606 | */ |
| 3607 | mddev->in_sync = 1; |
| 3608 | del_timer_sync(&mddev->safemode_timer); |
| 3609 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 3610 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3611 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3612 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3613 | mddev_resume(mddev); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3614 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3615 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3616 | return rv; |
| 3617 | } |
| 3618 | |
| 3619 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3620 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3621 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3622 | |
| 3623 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3624 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3625 | { |
| 3626 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3627 | if (mddev->reshape_position != MaxSector && |
| 3628 | mddev->layout != mddev->new_layout) |
| 3629 | return sprintf(page, "%d (%d)\n", |
| 3630 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3631 | return sprintf(page, "%d\n", mddev->layout); |
| 3632 | } |
| 3633 | |
| 3634 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3635 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3636 | { |
| 3637 | char *e; |
| 3638 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3639 | |
| 3640 | if (!*buf || (*e && *e != '\n')) |
| 3641 | return -EINVAL; |
| 3642 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3643 | if (mddev->pers) { |
| 3644 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3645 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3646 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3647 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3648 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3649 | if (err) { |
| 3650 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3651 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3652 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3653 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3654 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3655 | if (mddev->reshape_position == MaxSector) |
| 3656 | mddev->layout = n; |
| 3657 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3658 | return len; |
| 3659 | } |
| 3660 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3661 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3662 | |
| 3663 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3664 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3665 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3666 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3667 | if (mddev->raid_disks == 0) |
| 3668 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3669 | if (mddev->reshape_position != MaxSector && |
| 3670 | mddev->delta_disks != 0) |
| 3671 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3672 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3673 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3674 | } |
| 3675 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3676 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3677 | |
| 3678 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3679 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3680 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3681 | char *e; |
| 3682 | int rv = 0; |
| 3683 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3684 | |
| 3685 | if (!*buf || (*e && *e != '\n')) |
| 3686 | return -EINVAL; |
| 3687 | |
| 3688 | if (mddev->pers) |
| 3689 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3690 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3691 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3692 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3693 | |
| 3694 | rdev_for_each(rdev, mddev) { |
| 3695 | if (olddisks < n && |
| 3696 | rdev->data_offset < rdev->new_data_offset) |
| 3697 | return -EINVAL; |
| 3698 | if (olddisks > n && |
| 3699 | rdev->data_offset > rdev->new_data_offset) |
| 3700 | return -EINVAL; |
| 3701 | } |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3702 | mddev->delta_disks = n - olddisks; |
| 3703 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3704 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3705 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3706 | mddev->raid_disks = n; |
| 3707 | return rv ? rv : len; |
| 3708 | } |
| 3709 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3710 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3711 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3712 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3713 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3714 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3715 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3716 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3717 | return sprintf(page, "%d (%d)\n", |
| 3718 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3719 | mddev->chunk_sectors << 9); |
| 3720 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3724 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3725 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3726 | char *e; |
| 3727 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3728 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3729 | if (!*buf || (*e && *e != '\n')) |
| 3730 | return -EINVAL; |
| 3731 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3732 | if (mddev->pers) { |
| 3733 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3734 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3735 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3736 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3737 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3738 | if (err) { |
| 3739 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3740 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3741 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3742 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3743 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3744 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3745 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3746 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3747 | return len; |
| 3748 | } |
| 3749 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3750 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3751 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3752 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3753 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3754 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3755 | if (mddev->recovery_cp == MaxSector) |
| 3756 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3757 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3758 | } |
| 3759 | |
| 3760 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3761 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3762 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3763 | char *e; |
| 3764 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3765 | |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3766 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3767 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3768 | if (cmd_match(buf, "none")) |
| 3769 | n = MaxSector; |
| 3770 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3771 | return -EINVAL; |
| 3772 | |
| 3773 | mddev->recovery_cp = n; |
NeilBrown | db07d85 | 2012-10-11 14:22:17 +1100 | [diff] [blame] | 3774 | if (mddev->pers) |
| 3775 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3776 | return len; |
| 3777 | } |
| 3778 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3779 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3780 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3781 | /* |
| 3782 | * The array state can be: |
| 3783 | * |
| 3784 | * clear |
| 3785 | * No devices, no size, no level |
| 3786 | * Equivalent to STOP_ARRAY ioctl |
| 3787 | * inactive |
| 3788 | * May have some settings, but array is not active |
| 3789 | * all IO results in error |
| 3790 | * When written, doesn't tear down array, but just stops it |
| 3791 | * suspended (not supported yet) |
| 3792 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3793 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3794 | * readonly |
| 3795 | * no resync can happen. no superblocks get written. |
| 3796 | * write requests fail |
| 3797 | * read-auto |
| 3798 | * like readonly, but behaves like 'clean' on a write request. |
| 3799 | * |
| 3800 | * clean - no pending writes, but otherwise active. |
| 3801 | * When written to inactive array, starts without resync |
| 3802 | * If a write request arrives then |
| 3803 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3804 | * if not known, block and switch to write-pending |
| 3805 | * If written to an active array that has pending writes, then fails. |
| 3806 | * active |
| 3807 | * fully active: IO and resync can be happening. |
| 3808 | * When written to inactive array, starts with resync |
| 3809 | * |
| 3810 | * write-pending |
| 3811 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3812 | * |
| 3813 | * active-idle |
| 3814 | * like active, but no writes have been seen for a while (100msec). |
| 3815 | * |
| 3816 | */ |
| 3817 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3818 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3819 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3820 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3821 | "write-pending", "active-idle", NULL }; |
| 3822 | |
| 3823 | static int match_word(const char *word, char **list) |
| 3824 | { |
| 3825 | int n; |
| 3826 | for (n=0; list[n]; n++) |
| 3827 | if (cmd_match(word, list[n])) |
| 3828 | break; |
| 3829 | return n; |
| 3830 | } |
| 3831 | |
| 3832 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3833 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3834 | { |
| 3835 | enum array_state st = inactive; |
| 3836 | |
| 3837 | if (mddev->pers) |
| 3838 | switch(mddev->ro) { |
| 3839 | case 1: |
| 3840 | st = readonly; |
| 3841 | break; |
| 3842 | case 2: |
| 3843 | st = read_auto; |
| 3844 | break; |
| 3845 | case 0: |
| 3846 | if (mddev->in_sync) |
| 3847 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3848 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3849 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3850 | else if (mddev->safemode) |
| 3851 | st = active_idle; |
| 3852 | else |
| 3853 | st = active; |
| 3854 | } |
| 3855 | else { |
| 3856 | if (list_empty(&mddev->disks) && |
| 3857 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3858 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3859 | st = clear; |
| 3860 | else |
| 3861 | st = inactive; |
| 3862 | } |
| 3863 | return sprintf(page, "%s\n", array_states[st]); |
| 3864 | } |
| 3865 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3866 | static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev); |
| 3867 | static int md_set_readonly(struct mddev * mddev, struct block_device *bdev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3868 | static int do_md_run(struct mddev * mddev); |
| 3869 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3870 | |
| 3871 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3872 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3873 | { |
| 3874 | int err = -EINVAL; |
| 3875 | enum array_state st = match_word(buf, array_states); |
| 3876 | switch(st) { |
| 3877 | case bad_word: |
| 3878 | break; |
| 3879 | case clear: |
| 3880 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3881 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3882 | break; |
| 3883 | case inactive: |
| 3884 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3885 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3886 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3887 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3888 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3889 | break; |
| 3890 | case suspended: |
| 3891 | break; /* not supported yet */ |
| 3892 | case readonly: |
| 3893 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3894 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3895 | else { |
| 3896 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3897 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3898 | err = do_md_run(mddev); |
| 3899 | } |
| 3900 | break; |
| 3901 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3902 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3903 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3904 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3905 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3906 | err = restart_array(mddev); |
| 3907 | if (err == 0) { |
| 3908 | mddev->ro = 2; |
| 3909 | set_disk_ro(mddev->gendisk, 0); |
| 3910 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3911 | } else { |
| 3912 | mddev->ro = 2; |
| 3913 | err = do_md_run(mddev); |
| 3914 | } |
| 3915 | break; |
| 3916 | case clean: |
| 3917 | if (mddev->pers) { |
| 3918 | restart_array(mddev); |
| 3919 | spin_lock_irq(&mddev->write_lock); |
| 3920 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3921 | if (mddev->in_sync == 0) { |
| 3922 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3923 | if (mddev->safemode == 1) |
| 3924 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3925 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3926 | } |
| 3927 | err = 0; |
| 3928 | } else |
| 3929 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3930 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3931 | } else |
| 3932 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3933 | break; |
| 3934 | case active: |
| 3935 | if (mddev->pers) { |
| 3936 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3937 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3938 | wake_up(&mddev->sb_wait); |
| 3939 | err = 0; |
| 3940 | } else { |
| 3941 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3942 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3943 | err = do_md_run(mddev); |
| 3944 | } |
| 3945 | break; |
| 3946 | case write_pending: |
| 3947 | case active_idle: |
| 3948 | /* these cannot be set */ |
| 3949 | break; |
| 3950 | } |
| 3951 | if (err) |
| 3952 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3953 | else { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 3954 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3955 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3956 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3957 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3958 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3959 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3960 | static struct md_sysfs_entry md_array_state = |
| 3961 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3962 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3963 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3964 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3965 | return sprintf(page, "%d\n", |
| 3966 | atomic_read(&mddev->max_corr_read_errors)); |
| 3967 | } |
| 3968 | |
| 3969 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3970 | 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] | 3971 | { |
| 3972 | char *e; |
| 3973 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3974 | |
| 3975 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3976 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3977 | return len; |
| 3978 | } |
| 3979 | return -EINVAL; |
| 3980 | } |
| 3981 | |
| 3982 | static struct md_sysfs_entry max_corr_read_errors = |
| 3983 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3984 | max_corrected_read_errors_store); |
| 3985 | |
| 3986 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3987 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3988 | { |
| 3989 | return -EINVAL; |
| 3990 | } |
| 3991 | |
| 3992 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3993 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3994 | { |
| 3995 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3996 | /* The new device is added to the array. |
| 3997 | * If the array has a persistent superblock, we read the |
| 3998 | * superblock to initialise info and check validity. |
| 3999 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4000 | * which mainly checks size. |
| 4001 | */ |
| 4002 | char *e; |
| 4003 | int major = simple_strtoul(buf, &e, 10); |
| 4004 | int minor; |
| 4005 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4006 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4007 | int err; |
| 4008 | |
| 4009 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4010 | return -EINVAL; |
| 4011 | minor = simple_strtoul(e+1, &e, 10); |
| 4012 | if (*e && *e != '\n') |
| 4013 | return -EINVAL; |
| 4014 | dev = MKDEV(major, minor); |
| 4015 | if (major != MAJOR(dev) || |
| 4016 | minor != MINOR(dev)) |
| 4017 | return -EOVERFLOW; |
| 4018 | |
| 4019 | |
| 4020 | if (mddev->persistent) { |
| 4021 | rdev = md_import_device(dev, mddev->major_version, |
| 4022 | mddev->minor_version); |
| 4023 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4024 | struct md_rdev *rdev0 |
| 4025 | = list_entry(mddev->disks.next, |
| 4026 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4027 | err = super_types[mddev->major_version] |
| 4028 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4029 | if (err < 0) |
| 4030 | goto out; |
| 4031 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4032 | } else if (mddev->external) |
| 4033 | rdev = md_import_device(dev, -2, -1); |
| 4034 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4035 | rdev = md_import_device(dev, -1, -1); |
| 4036 | |
| 4037 | if (IS_ERR(rdev)) |
| 4038 | return PTR_ERR(rdev); |
| 4039 | err = bind_rdev_to_array(rdev, mddev); |
| 4040 | out: |
| 4041 | if (err) |
| 4042 | export_rdev(rdev); |
| 4043 | return err ? err : len; |
| 4044 | } |
| 4045 | |
| 4046 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4047 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4048 | |
| 4049 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4050 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4051 | { |
| 4052 | char *end; |
| 4053 | unsigned long chunk, end_chunk; |
| 4054 | |
| 4055 | if (!mddev->bitmap) |
| 4056 | goto out; |
| 4057 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4058 | while (*buf) { |
| 4059 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4060 | if (buf == end) break; |
| 4061 | if (*end == '-') { /* range */ |
| 4062 | buf = end + 1; |
| 4063 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4064 | if (buf == end) break; |
| 4065 | } |
| 4066 | if (*end && !isspace(*end)) break; |
| 4067 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4068 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4069 | } |
| 4070 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4071 | out: |
| 4072 | return len; |
| 4073 | } |
| 4074 | |
| 4075 | static struct md_sysfs_entry md_bitmap = |
| 4076 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4077 | |
| 4078 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4079 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4080 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4081 | return sprintf(page, "%llu\n", |
| 4082 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4083 | } |
| 4084 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4085 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4086 | |
| 4087 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4088 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4089 | { |
| 4090 | /* If array is inactive, we can reduce the component size, but |
| 4091 | * not increase it (except from 0). |
| 4092 | * If array is active, we can try an on-line resize |
| 4093 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4094 | sector_t sectors; |
| 4095 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4096 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4097 | if (err < 0) |
| 4098 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4099 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4100 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4101 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4102 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4103 | if (mddev->dev_sectors == 0 || |
| 4104 | mddev->dev_sectors > sectors) |
| 4105 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4106 | else |
| 4107 | err = -ENOSPC; |
| 4108 | } |
| 4109 | return err ? err : len; |
| 4110 | } |
| 4111 | |
| 4112 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4113 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4114 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4115 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4116 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4117 | * This is one of |
| 4118 | * 'none' for arrays with no metadata (good luck...) |
| 4119 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4120 | * or N.M for internally known formats |
| 4121 | */ |
| 4122 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4123 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4124 | { |
| 4125 | if (mddev->persistent) |
| 4126 | return sprintf(page, "%d.%d\n", |
| 4127 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4128 | else if (mddev->external) |
| 4129 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4130 | else |
| 4131 | return sprintf(page, "none\n"); |
| 4132 | } |
| 4133 | |
| 4134 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4135 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4136 | { |
| 4137 | int major, minor; |
| 4138 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4139 | /* Changing the details of 'external' metadata is |
| 4140 | * always permitted. Otherwise there must be |
| 4141 | * no devices attached to the array. |
| 4142 | */ |
| 4143 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4144 | ; |
| 4145 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4146 | return -EBUSY; |
| 4147 | |
| 4148 | if (cmd_match(buf, "none")) { |
| 4149 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4150 | mddev->external = 0; |
| 4151 | mddev->major_version = 0; |
| 4152 | mddev->minor_version = 90; |
| 4153 | return len; |
| 4154 | } |
| 4155 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4156 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4157 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4158 | namelen = sizeof(mddev->metadata_type)-1; |
| 4159 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4160 | mddev->metadata_type[namelen] = 0; |
| 4161 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4162 | mddev->metadata_type[--namelen] = 0; |
| 4163 | mddev->persistent = 0; |
| 4164 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4165 | mddev->major_version = 0; |
| 4166 | mddev->minor_version = 90; |
| 4167 | return len; |
| 4168 | } |
| 4169 | major = simple_strtoul(buf, &e, 10); |
| 4170 | if (e==buf || *e != '.') |
| 4171 | return -EINVAL; |
| 4172 | buf = e+1; |
| 4173 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4174 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4175 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4176 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4177 | return -ENOENT; |
| 4178 | mddev->major_version = major; |
| 4179 | mddev->minor_version = minor; |
| 4180 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4181 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4182 | return len; |
| 4183 | } |
| 4184 | |
| 4185 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4186 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4187 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4188 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4189 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4190 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4191 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4192 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 4193 | type = "frozen"; |
| 4194 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 4195 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4196 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4197 | type = "reshape"; |
| 4198 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4199 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 4200 | type = "resync"; |
| 4201 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 4202 | type = "check"; |
| 4203 | else |
| 4204 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4205 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4206 | type = "recover"; |
| 4207 | } |
| 4208 | return sprintf(page, "%s\n", type); |
| 4209 | } |
| 4210 | |
| 4211 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4212 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4213 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4214 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4215 | return -EINVAL; |
| 4216 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4217 | if (cmd_match(page, "frozen")) |
| 4218 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4219 | else |
| 4220 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4221 | |
| 4222 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4223 | if (mddev->sync_thread) { |
| 4224 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 4225 | md_reap_sync_thread(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4226 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4227 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 4228 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4229 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4230 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4231 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4232 | else if (cmd_match(page, "recover")) { |
| 4233 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 4234 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4235 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4236 | int err; |
| 4237 | if (mddev->pers->start_reshape == NULL) |
| 4238 | return -EINVAL; |
| 4239 | err = mddev->pers->start_reshape(mddev); |
| 4240 | if (err) |
| 4241 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4242 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4243 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4244 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4245 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4246 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4247 | return -EINVAL; |
| 4248 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4249 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4250 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4251 | if (mddev->ro == 2) { |
| 4252 | /* A write to sync_action is enough to justify |
| 4253 | * canceling read-auto mode |
| 4254 | */ |
| 4255 | mddev->ro = 0; |
| 4256 | md_wakeup_thread(mddev->sync_thread); |
| 4257 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4258 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4259 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4260 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4261 | return len; |
| 4262 | } |
| 4263 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4264 | static struct md_sysfs_entry md_scan_mode = |
| 4265 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
| 4266 | |
| 4267 | static ssize_t |
| 4268 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4269 | { |
| 4270 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4271 | } |
| 4272 | |
| 4273 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4274 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4275 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4276 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4277 | { |
| 4278 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4279 | (unsigned long long) |
| 4280 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4281 | } |
| 4282 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4283 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4284 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4285 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4286 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4287 | { |
| 4288 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4289 | mddev->sync_speed_min ? "local": "system"); |
| 4290 | } |
| 4291 | |
| 4292 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4293 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4294 | { |
| 4295 | int min; |
| 4296 | char *e; |
| 4297 | if (strncmp(buf, "system", 6)==0) { |
| 4298 | mddev->sync_speed_min = 0; |
| 4299 | return len; |
| 4300 | } |
| 4301 | min = simple_strtoul(buf, &e, 10); |
| 4302 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 4303 | return -EINVAL; |
| 4304 | mddev->sync_speed_min = min; |
| 4305 | return len; |
| 4306 | } |
| 4307 | |
| 4308 | static struct md_sysfs_entry md_sync_min = |
| 4309 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4310 | |
| 4311 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4312 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4313 | { |
| 4314 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4315 | mddev->sync_speed_max ? "local": "system"); |
| 4316 | } |
| 4317 | |
| 4318 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4319 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4320 | { |
| 4321 | int max; |
| 4322 | char *e; |
| 4323 | if (strncmp(buf, "system", 6)==0) { |
| 4324 | mddev->sync_speed_max = 0; |
| 4325 | return len; |
| 4326 | } |
| 4327 | max = simple_strtoul(buf, &e, 10); |
| 4328 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 4329 | return -EINVAL; |
| 4330 | mddev->sync_speed_max = max; |
| 4331 | return len; |
| 4332 | } |
| 4333 | |
| 4334 | static struct md_sysfs_entry md_sync_max = |
| 4335 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4336 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4337 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4338 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4339 | { |
| 4340 | return sprintf(page, "%d\n", mddev->degraded); |
| 4341 | } |
| 4342 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4343 | |
| 4344 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4345 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4346 | { |
| 4347 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4348 | } |
| 4349 | |
| 4350 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4351 | 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] | 4352 | { |
| 4353 | long n; |
| 4354 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4355 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4356 | return -EINVAL; |
| 4357 | |
| 4358 | if (n != 0 && n != 1) |
| 4359 | return -EINVAL; |
| 4360 | |
| 4361 | mddev->parallel_resync = n; |
| 4362 | |
| 4363 | if (mddev->sync_thread) |
| 4364 | wake_up(&resync_wait); |
| 4365 | |
| 4366 | return len; |
| 4367 | } |
| 4368 | |
| 4369 | /* force parallel resync, even with shared block devices */ |
| 4370 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4371 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4372 | sync_force_parallel_show, sync_force_parallel_store); |
| 4373 | |
| 4374 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4375 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4376 | { |
| 4377 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4378 | if (mddev->curr_resync == 0) |
| 4379 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4380 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4381 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4382 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4383 | db = resync - mddev->resync_mark_cnt; |
| 4384 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4385 | } |
| 4386 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4387 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4388 | |
| 4389 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4390 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4391 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4392 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4393 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4394 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4395 | return sprintf(page, "none\n"); |
| 4396 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4397 | if (mddev->curr_resync == 1 || |
| 4398 | mddev->curr_resync == 2) |
| 4399 | return sprintf(page, "delayed\n"); |
| 4400 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4401 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4402 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4403 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4404 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4405 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4406 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4407 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4408 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4409 | } |
| 4410 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4411 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4412 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4413 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4414 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4415 | { |
| 4416 | return sprintf(page, "%llu\n", |
| 4417 | (unsigned long long)mddev->resync_min); |
| 4418 | } |
| 4419 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4420 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4421 | { |
| 4422 | unsigned long long min; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4423 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4424 | return -EINVAL; |
| 4425 | if (min > mddev->resync_max) |
| 4426 | return -EINVAL; |
| 4427 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4428 | return -EBUSY; |
| 4429 | |
| 4430 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4431 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4432 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4433 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4434 | return -EINVAL; |
| 4435 | } |
| 4436 | mddev->resync_min = min; |
| 4437 | |
| 4438 | return len; |
| 4439 | } |
| 4440 | |
| 4441 | static struct md_sysfs_entry md_min_sync = |
| 4442 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4443 | |
| 4444 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4445 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4446 | { |
| 4447 | if (mddev->resync_max == MaxSector) |
| 4448 | return sprintf(page, "max\n"); |
| 4449 | else |
| 4450 | return sprintf(page, "%llu\n", |
| 4451 | (unsigned long long)mddev->resync_max); |
| 4452 | } |
| 4453 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4454 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4455 | { |
| 4456 | if (strncmp(buf, "max", 3) == 0) |
| 4457 | mddev->resync_max = MaxSector; |
| 4458 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4459 | unsigned long long max; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4460 | if (kstrtoull(buf, 10, &max)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4461 | return -EINVAL; |
| 4462 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4463 | return -EINVAL; |
| 4464 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4465 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4466 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4467 | return -EBUSY; |
| 4468 | |
| 4469 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4470 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4471 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4472 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4473 | return -EINVAL; |
| 4474 | } |
| 4475 | mddev->resync_max = max; |
| 4476 | } |
| 4477 | wake_up(&mddev->recovery_wait); |
| 4478 | return len; |
| 4479 | } |
| 4480 | |
| 4481 | static struct md_sysfs_entry md_max_sync = |
| 4482 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4483 | |
| 4484 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4485 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4486 | { |
| 4487 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4488 | } |
| 4489 | |
| 4490 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4491 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4492 | { |
| 4493 | char *e; |
| 4494 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4495 | unsigned long long old = mddev->suspend_lo; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4496 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4497 | if (mddev->pers == NULL || |
| 4498 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4499 | return -EINVAL; |
| 4500 | if (buf == e || (*e && *e != '\n')) |
| 4501 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4502 | |
| 4503 | mddev->suspend_lo = new; |
| 4504 | if (new >= old) |
| 4505 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4506 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4507 | else { |
| 4508 | /* Expanding suspended region - need to wait */ |
| 4509 | mddev->pers->quiesce(mddev, 1); |
| 4510 | mddev->pers->quiesce(mddev, 0); |
| 4511 | } |
| 4512 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4513 | } |
| 4514 | static struct md_sysfs_entry md_suspend_lo = |
| 4515 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4516 | |
| 4517 | |
| 4518 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4519 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4520 | { |
| 4521 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4522 | } |
| 4523 | |
| 4524 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4525 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4526 | { |
| 4527 | char *e; |
| 4528 | unsigned long long new = simple_strtoull(buf, &e, 10); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4529 | unsigned long long old = mddev->suspend_hi; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4530 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4531 | if (mddev->pers == NULL || |
| 4532 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4533 | return -EINVAL; |
| 4534 | if (buf == e || (*e && *e != '\n')) |
| 4535 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4536 | |
| 4537 | mddev->suspend_hi = new; |
| 4538 | if (new <= old) |
| 4539 | /* Shrinking suspended region */ |
| 4540 | mddev->pers->quiesce(mddev, 2); |
| 4541 | else { |
| 4542 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4543 | mddev->pers->quiesce(mddev, 1); |
| 4544 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4545 | } |
| 4546 | return len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4547 | } |
| 4548 | static struct md_sysfs_entry md_suspend_hi = |
| 4549 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4550 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4551 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4552 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4553 | { |
| 4554 | if (mddev->reshape_position != MaxSector) |
| 4555 | return sprintf(page, "%llu\n", |
| 4556 | (unsigned long long)mddev->reshape_position); |
| 4557 | strcpy(page, "none\n"); |
| 4558 | return 5; |
| 4559 | } |
| 4560 | |
| 4561 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4562 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4563 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4564 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4565 | char *e; |
| 4566 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4567 | if (mddev->pers) |
| 4568 | return -EBUSY; |
| 4569 | if (buf == e || (*e && *e != '\n')) |
| 4570 | return -EINVAL; |
| 4571 | mddev->reshape_position = new; |
| 4572 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4573 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4574 | mddev->new_level = mddev->level; |
| 4575 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4576 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4577 | rdev_for_each(rdev, mddev) |
| 4578 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4579 | return len; |
| 4580 | } |
| 4581 | |
| 4582 | static struct md_sysfs_entry md_reshape_position = |
| 4583 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4584 | reshape_position_store); |
| 4585 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4586 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4587 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4588 | { |
| 4589 | return sprintf(page, "%s\n", |
| 4590 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4591 | } |
| 4592 | |
| 4593 | static ssize_t |
| 4594 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4595 | { |
| 4596 | int backwards = 0; |
| 4597 | if (cmd_match(buf, "forwards")) |
| 4598 | backwards = 0; |
| 4599 | else if (cmd_match(buf, "backwards")) |
| 4600 | backwards = 1; |
| 4601 | else |
| 4602 | return -EINVAL; |
| 4603 | if (mddev->reshape_backwards == backwards) |
| 4604 | return len; |
| 4605 | |
| 4606 | /* check if we are allowed to change */ |
| 4607 | if (mddev->delta_disks) |
| 4608 | return -EBUSY; |
| 4609 | |
| 4610 | if (mddev->persistent && |
| 4611 | mddev->major_version == 0) |
| 4612 | return -EINVAL; |
| 4613 | |
| 4614 | mddev->reshape_backwards = backwards; |
| 4615 | return len; |
| 4616 | } |
| 4617 | |
| 4618 | static struct md_sysfs_entry md_reshape_direction = |
| 4619 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4620 | reshape_direction_store); |
| 4621 | |
| 4622 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4623 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4624 | { |
| 4625 | if (mddev->external_size) |
| 4626 | return sprintf(page, "%llu\n", |
| 4627 | (unsigned long long)mddev->array_sectors/2); |
| 4628 | else |
| 4629 | return sprintf(page, "default\n"); |
| 4630 | } |
| 4631 | |
| 4632 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4633 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4634 | { |
| 4635 | sector_t sectors; |
| 4636 | |
| 4637 | if (strncmp(buf, "default", 7) == 0) { |
| 4638 | if (mddev->pers) |
| 4639 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4640 | else |
| 4641 | sectors = mddev->array_sectors; |
| 4642 | |
| 4643 | mddev->external_size = 0; |
| 4644 | } else { |
| 4645 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4646 | return -EINVAL; |
| 4647 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4648 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4649 | |
| 4650 | mddev->external_size = 1; |
| 4651 | } |
| 4652 | |
| 4653 | mddev->array_sectors = sectors; |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4654 | if (mddev->pers) { |
| 4655 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4656 | revalidate_disk(mddev->gendisk); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4657 | } |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4658 | return len; |
| 4659 | } |
| 4660 | |
| 4661 | static struct md_sysfs_entry md_array_size = |
| 4662 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4663 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4664 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4665 | static struct attribute *md_default_attrs[] = { |
| 4666 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4667 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4668 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4669 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4670 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4671 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4672 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4673 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4674 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4675 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4676 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4677 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4678 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4679 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4680 | NULL, |
| 4681 | }; |
| 4682 | |
| 4683 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4684 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4685 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4686 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4687 | &md_sync_min.attr, |
| 4688 | &md_sync_max.attr, |
| 4689 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4690 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4691 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4692 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4693 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4694 | &md_suspend_lo.attr, |
| 4695 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4696 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4697 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4698 | NULL, |
| 4699 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4700 | static struct attribute_group md_redundancy_group = { |
| 4701 | .name = NULL, |
| 4702 | .attrs = md_redundancy_attrs, |
| 4703 | }; |
| 4704 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4705 | |
| 4706 | static ssize_t |
| 4707 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4708 | { |
| 4709 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4710 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4711 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4712 | |
| 4713 | if (!entry->show) |
| 4714 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4715 | spin_lock(&all_mddevs_lock); |
| 4716 | if (list_empty(&mddev->all_mddevs)) { |
| 4717 | spin_unlock(&all_mddevs_lock); |
| 4718 | return -EBUSY; |
| 4719 | } |
| 4720 | mddev_get(mddev); |
| 4721 | spin_unlock(&all_mddevs_lock); |
| 4722 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4723 | rv = mddev_lock(mddev); |
| 4724 | if (!rv) { |
| 4725 | rv = entry->show(mddev, page); |
| 4726 | mddev_unlock(mddev); |
| 4727 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4728 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4729 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | static ssize_t |
| 4733 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4734 | const char *page, size_t length) |
| 4735 | { |
| 4736 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4737 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4738 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4739 | |
| 4740 | if (!entry->store) |
| 4741 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4742 | if (!capable(CAP_SYS_ADMIN)) |
| 4743 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4744 | spin_lock(&all_mddevs_lock); |
| 4745 | if (list_empty(&mddev->all_mddevs)) { |
| 4746 | spin_unlock(&all_mddevs_lock); |
| 4747 | return -EBUSY; |
| 4748 | } |
| 4749 | mddev_get(mddev); |
| 4750 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 4751 | if (entry->store == new_dev_store) |
| 4752 | flush_workqueue(md_misc_wq); |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4753 | rv = mddev_lock(mddev); |
| 4754 | if (!rv) { |
| 4755 | rv = entry->store(mddev, page, length); |
| 4756 | mddev_unlock(mddev); |
| 4757 | } |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4758 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4759 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | static void md_free(struct kobject *ko) |
| 4763 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4764 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4765 | |
| 4766 | if (mddev->sysfs_state) |
| 4767 | sysfs_put(mddev->sysfs_state); |
| 4768 | |
| 4769 | if (mddev->gendisk) { |
| 4770 | del_gendisk(mddev->gendisk); |
| 4771 | put_disk(mddev->gendisk); |
| 4772 | } |
| 4773 | if (mddev->queue) |
| 4774 | blk_cleanup_queue(mddev->queue); |
| 4775 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4776 | kfree(mddev); |
| 4777 | } |
| 4778 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4779 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4780 | .show = md_attr_show, |
| 4781 | .store = md_attr_store, |
| 4782 | }; |
| 4783 | static struct kobj_type md_ktype = { |
| 4784 | .release = md_free, |
| 4785 | .sysfs_ops = &md_sysfs_ops, |
| 4786 | .default_attrs = md_default_attrs, |
| 4787 | }; |
| 4788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | int mdp_major = 0; |
| 4790 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4791 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4792 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4793 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4794 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4795 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4796 | kobject_del(&mddev->kobj); |
| 4797 | kobject_put(&mddev->kobj); |
| 4798 | } |
| 4799 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4800 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4802 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4803 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4805 | int partitioned; |
| 4806 | int shift; |
| 4807 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4808 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | |
| 4810 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4811 | return -ENODEV; |
| 4812 | |
| 4813 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4814 | shift = partitioned ? MdpMinorShift : 0; |
| 4815 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4816 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4817 | /* wait for any previous instance of this device to be |
| 4818 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4819 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4820 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4821 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4822 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4823 | error = -EEXIST; |
| 4824 | if (mddev->gendisk) |
| 4825 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4826 | |
| 4827 | if (name) { |
| 4828 | /* Need to ensure that 'name' is not a duplicate. |
| 4829 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4830 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4831 | spin_lock(&all_mddevs_lock); |
| 4832 | |
| 4833 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4834 | if (mddev2->gendisk && |
| 4835 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4836 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4837 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4838 | } |
| 4839 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4841 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4842 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4843 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4844 | if (!mddev->queue) |
| 4845 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4846 | mddev->queue->queuedata = mddev; |
| 4847 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4848 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 4849 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4850 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4851 | disk = alloc_disk(1 << shift); |
| 4852 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4853 | blk_cleanup_queue(mddev->queue); |
| 4854 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4855 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4857 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4859 | if (name) |
| 4860 | strcpy(disk->disk_name, name); |
| 4861 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4862 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4863 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4864 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | disk->fops = &md_fops; |
| 4866 | disk->private_data = mddev; |
| 4867 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4868 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4869 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4870 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4871 | * remove it now. |
| 4872 | */ |
| 4873 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4875 | /* As soon as we call add_disk(), another thread could get |
| 4876 | * through to md_open, so make sure it doesn't get too far |
| 4877 | */ |
| 4878 | mutex_lock(&mddev->open_mutex); |
| 4879 | add_disk(disk); |
| 4880 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4881 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4882 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4883 | if (error) { |
| 4884 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4885 | * __must_check, we must do something with the result |
| 4886 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4887 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4888 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4889 | error = 0; |
| 4890 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4891 | if (mddev->kobj.sd && |
| 4892 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4893 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 4894 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4895 | abort: |
| 4896 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4897 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4898 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4899 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4900 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4901 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4902 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4903 | } |
| 4904 | |
| 4905 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4906 | { |
| 4907 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4908 | return NULL; |
| 4909 | } |
| 4910 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4911 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4912 | { |
| 4913 | /* val must be "md_*" where * is not all digits. |
| 4914 | * We allocate an array with a large free minor number, and |
| 4915 | * set the name to val. val must not already be an active name. |
| 4916 | */ |
| 4917 | int len = strlen(val); |
| 4918 | char buf[DISK_NAME_LEN]; |
| 4919 | |
| 4920 | while (len && val[len-1] == '\n') |
| 4921 | len--; |
| 4922 | if (len >= DISK_NAME_LEN) |
| 4923 | return -E2BIG; |
| 4924 | strlcpy(buf, val, len+1); |
| 4925 | if (strncmp(buf, "md_", 3) != 0) |
| 4926 | return -EINVAL; |
| 4927 | return md_alloc(0, buf); |
| 4928 | } |
| 4929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | static void md_safemode_timeout(unsigned long data) |
| 4931 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4932 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4934 | if (!atomic_read(&mddev->writes_pending)) { |
| 4935 | mddev->safemode = 1; |
| 4936 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4937 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4938 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4939 | md_wakeup_thread(mddev->thread); |
| 4940 | } |
| 4941 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4942 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4943 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4944 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4945 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4946 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4947 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4948 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4950 | if (list_empty(&mddev->disks)) |
| 4951 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4952 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4953 | |
| 4954 | if (mddev->pers) |
| 4955 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4956 | /* Cannot run until previous stop completes properly */ |
| 4957 | if (mddev->sysfs_active) |
| 4958 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4960 | /* |
| 4961 | * Analyze all RAID superblock(s) |
| 4962 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4963 | if (!mddev->raid_disks) { |
| 4964 | if (!mddev->persistent) |
| 4965 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4966 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4967 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4968 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4969 | if (mddev->level != LEVEL_NONE) |
| 4970 | request_module("md-level-%d", mddev->level); |
| 4971 | else if (mddev->clevel[0]) |
| 4972 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | |
| 4974 | /* |
| 4975 | * Drop all container device buffers, from now on |
| 4976 | * the only valid external interface is through the md |
| 4977 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4978 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4979 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4980 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | continue; |
| 4982 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4983 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4984 | |
| 4985 | /* perform some consistency tests on the device. |
| 4986 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4987 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4988 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4989 | if (rdev->meta_bdev) { |
| 4990 | /* Nothing to check */; |
| 4991 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4992 | if (mddev->dev_sectors && |
| 4993 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4994 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4995 | printk("md: %s: data overlaps metadata\n", |
| 4996 | mdname(mddev)); |
| 4997 | return -EINVAL; |
| 4998 | } |
| 4999 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5000 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5001 | > rdev->data_offset) { |
| 5002 | printk("md: %s: metadata overlaps data\n", |
| 5003 | mdname(mddev)); |
| 5004 | return -EINVAL; |
| 5005 | } |
| 5006 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5007 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | } |
| 5009 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5010 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 5011 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5012 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5014 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5015 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5016 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5017 | if (mddev->level != LEVEL_NONE) |
| 5018 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5019 | mddev->level); |
| 5020 | else |
| 5021 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5022 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | return -EINVAL; |
| 5024 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5025 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5026 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5027 | if (mddev->level != pers->level) { |
| 5028 | mddev->level = pers->level; |
| 5029 | mddev->new_level = pers->level; |
| 5030 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5031 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5032 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5033 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5034 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5035 | /* This personality cannot handle reshaping... */ |
| 5036 | mddev->pers = NULL; |
| 5037 | module_put(pers->owner); |
| 5038 | return -EINVAL; |
| 5039 | } |
| 5040 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5041 | if (pers->sync_request) { |
| 5042 | /* Warn if this is a potentially silly |
| 5043 | * configuration. |
| 5044 | */ |
| 5045 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5046 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5047 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5048 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5049 | rdev_for_each(rdev, mddev) |
| 5050 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5051 | if (rdev < rdev2 && |
| 5052 | rdev->bdev->bd_contains == |
| 5053 | rdev2->bdev->bd_contains) { |
| 5054 | printk(KERN_WARNING |
| 5055 | "%s: WARNING: %s appears to be" |
| 5056 | " on the same physical disk as" |
| 5057 | " %s.\n", |
| 5058 | mdname(mddev), |
| 5059 | bdevname(rdev->bdev,b), |
| 5060 | bdevname(rdev2->bdev,b2)); |
| 5061 | warned = 1; |
| 5062 | } |
| 5063 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5064 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5065 | if (warned) |
| 5066 | printk(KERN_WARNING |
| 5067 | "True protection against single-disk" |
| 5068 | " failure might be compromised.\n"); |
| 5069 | } |
| 5070 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5071 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5072 | /* may be over-ridden by personality */ |
| 5073 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5074 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5075 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5077 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5078 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5079 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5080 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5081 | if (err) |
| 5082 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5083 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 5084 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5085 | " but 'external_size' not in effect?\n", __func__); |
| 5086 | printk(KERN_ERR |
| 5087 | "md: invalid array_size %llu > default size %llu\n", |
| 5088 | (unsigned long long)mddev->array_sectors / 2, |
| 5089 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 5090 | err = -EINVAL; |
| 5091 | mddev->pers->stop(mddev); |
| 5092 | } |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5093 | if (err == 0 && mddev->pers->sync_request && |
| 5094 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5095 | err = bitmap_create(mddev); |
| 5096 | if (err) { |
| 5097 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5098 | mdname(mddev), err); |
| 5099 | mddev->pers->stop(mddev); |
| 5100 | } |
| 5101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5103 | module_put(mddev->pers->owner); |
| 5104 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5105 | bitmap_destroy(mddev); |
| 5106 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5108 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5109 | if (mddev->kobj.sd && |
| 5110 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5111 | printk(KERN_WARNING |
| 5112 | "md: cannot register extra attributes for %s\n", |
| 5113 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5114 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5115 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5116 | mddev->ro = 0; |
| 5117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5118 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5119 | atomic_set(&mddev->max_corr_read_errors, |
| 5120 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | mddev->safemode = 0; |
| 5122 | mddev->safemode_timer.function = md_safemode_timeout; |
| 5123 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5124 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5125 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5126 | smp_wmb(); |
| 5127 | mddev->ready = 1; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5128 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5129 | if (rdev->raid_disk >= 0) |
| 5130 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5131 | /* failure here is OK */; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | |
| 5133 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5134 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5135 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5136 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5137 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5138 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5139 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5140 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5141 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | return 0; |
| 5143 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5144 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5146 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5147 | { |
| 5148 | int err; |
| 5149 | |
| 5150 | err = md_run(mddev); |
| 5151 | if (err) |
| 5152 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5153 | err = bitmap_load(mddev); |
| 5154 | if (err) { |
| 5155 | bitmap_destroy(mddev); |
| 5156 | goto out; |
| 5157 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5158 | |
| 5159 | md_wakeup_thread(mddev->thread); |
| 5160 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5161 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5162 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5163 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5164 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5165 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5166 | out: |
| 5167 | return err; |
| 5168 | } |
| 5169 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5170 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5171 | { |
| 5172 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5173 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5174 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5176 | return -ENXIO; |
| 5177 | if (!mddev->pers) |
| 5178 | return -EINVAL; |
| 5179 | if (!mddev->ro) |
| 5180 | return -EBUSY; |
| 5181 | mddev->safemode = 0; |
| 5182 | mddev->ro = 0; |
| 5183 | set_disk_ro(disk, 0); |
| 5184 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5185 | mdname(mddev)); |
| 5186 | /* Kick recovery or resync if necessary */ |
| 5187 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5188 | md_wakeup_thread(mddev->thread); |
| 5189 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5190 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5191 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | } |
| 5193 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5194 | /* similar to deny_write_access, but accounts for our holding a reference |
| 5195 | * to the file ourselves */ |
| 5196 | static int deny_bitmap_write_access(struct file * file) |
| 5197 | { |
| 5198 | struct inode *inode = file->f_mapping->host; |
| 5199 | |
| 5200 | spin_lock(&inode->i_lock); |
| 5201 | if (atomic_read(&inode->i_writecount) > 1) { |
| 5202 | spin_unlock(&inode->i_lock); |
| 5203 | return -ETXTBSY; |
| 5204 | } |
| 5205 | atomic_set(&inode->i_writecount, -1); |
| 5206 | spin_unlock(&inode->i_lock); |
| 5207 | |
| 5208 | return 0; |
| 5209 | } |
| 5210 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5211 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5212 | { |
| 5213 | struct inode *inode = file->f_mapping->host; |
| 5214 | |
| 5215 | spin_lock(&inode->i_lock); |
| 5216 | atomic_set(&inode->i_writecount, 1); |
| 5217 | spin_unlock(&inode->i_lock); |
| 5218 | } |
| 5219 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5220 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5221 | { |
| 5222 | mddev->array_sectors = 0; |
| 5223 | mddev->external_size = 0; |
| 5224 | mddev->dev_sectors = 0; |
| 5225 | mddev->raid_disks = 0; |
| 5226 | mddev->recovery_cp = 0; |
| 5227 | mddev->resync_min = 0; |
| 5228 | mddev->resync_max = MaxSector; |
| 5229 | mddev->reshape_position = MaxSector; |
| 5230 | mddev->external = 0; |
| 5231 | mddev->persistent = 0; |
| 5232 | mddev->level = LEVEL_NONE; |
| 5233 | mddev->clevel[0] = 0; |
| 5234 | mddev->flags = 0; |
| 5235 | mddev->ro = 0; |
| 5236 | mddev->metadata_type[0] = 0; |
| 5237 | mddev->chunk_sectors = 0; |
| 5238 | mddev->ctime = mddev->utime = 0; |
| 5239 | mddev->layout = 0; |
| 5240 | mddev->max_disks = 0; |
| 5241 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5242 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5243 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5244 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5245 | mddev->new_level = LEVEL_NONE; |
| 5246 | mddev->new_layout = 0; |
| 5247 | mddev->new_chunk_sectors = 0; |
| 5248 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5249 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5250 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5251 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5252 | mddev->recovery = 0; |
| 5253 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5254 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5255 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5256 | mddev->safemode = 0; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5257 | mddev->merge_check_needed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5258 | mddev->bitmap_info.offset = 0; |
| 5259 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5260 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5261 | mddev->bitmap_info.chunksize = 0; |
| 5262 | mddev->bitmap_info.daemon_sleep = 0; |
| 5263 | mddev->bitmap_info.max_write_behind = 0; |
| 5264 | } |
| 5265 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5266 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5267 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 5268 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5269 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5270 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5271 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5272 | } |
| 5273 | |
| 5274 | del_timer_sync(&mddev->safemode_timer); |
| 5275 | |
| 5276 | bitmap_flush(mddev); |
| 5277 | md_super_wait(mddev); |
| 5278 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5279 | if (mddev->ro == 0 && |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5280 | (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5281 | /* mark array as shutdown cleanly */ |
| 5282 | mddev->in_sync = 1; |
| 5283 | md_update_sb(mddev, 1); |
| 5284 | } |
| 5285 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5286 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5287 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5288 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 5289 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5290 | __md_stop_writes(mddev); |
| 5291 | mddev_unlock(mddev); |
| 5292 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5293 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5294 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5295 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5296 | { |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5297 | mddev->ready = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5298 | mddev->pers->stop(mddev); |
| 5299 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 5300 | mddev->to_remove = &md_redundancy_group; |
| 5301 | module_put(mddev->pers->owner); |
| 5302 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5303 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5304 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5305 | |
| 5306 | void md_stop(struct mddev *mddev) |
| 5307 | { |
| 5308 | /* stop the array and free an attached data structures. |
| 5309 | * This is called from dm-raid |
| 5310 | */ |
| 5311 | __md_stop(mddev); |
| 5312 | bitmap_destroy(mddev); |
| 5313 | if (mddev->bio_set) |
| 5314 | bioset_free(mddev->bio_set); |
| 5315 | } |
| 5316 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5317 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5318 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5319 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5320 | { |
| 5321 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5322 | int did_freeze = 0; |
| 5323 | |
| 5324 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5325 | did_freeze = 1; |
| 5326 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5327 | md_wakeup_thread(mddev->thread); |
| 5328 | } |
| 5329 | if (mddev->sync_thread) { |
| 5330 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5331 | /* Thread might be blocked waiting for metadata update |
| 5332 | * which will now never happen */ |
| 5333 | wake_up_process(mddev->sync_thread->tsk); |
| 5334 | } |
| 5335 | mddev_unlock(mddev); |
| 5336 | wait_event(resync_wait, mddev->sync_thread == NULL); |
| 5337 | mddev_lock_nointr(mddev); |
| 5338 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5339 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5340 | if (atomic_read(&mddev->openers) > !!bdev || |
| 5341 | mddev->sync_thread || |
| 5342 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5343 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5344 | if (did_freeze) { |
| 5345 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5346 | md_wakeup_thread(mddev->thread); |
| 5347 | } |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5348 | err = -EBUSY; |
| 5349 | goto out; |
| 5350 | } |
| 5351 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5352 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5353 | |
| 5354 | err = -ENXIO; |
| 5355 | if (mddev->ro==1) |
| 5356 | goto out; |
| 5357 | mddev->ro = 1; |
| 5358 | set_disk_ro(mddev->gendisk, 1); |
| 5359 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5360 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5361 | err = 0; |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5362 | } |
| 5363 | out: |
| 5364 | mutex_unlock(&mddev->open_mutex); |
| 5365 | return err; |
| 5366 | } |
| 5367 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5368 | /* mode: |
| 5369 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5370 | * 2 - stop but do not disassemble array |
| 5371 | */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5372 | static int do_md_stop(struct mddev * mddev, int mode, |
| 5373 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5375 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5376 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5377 | int did_freeze = 0; |
| 5378 | |
| 5379 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5380 | did_freeze = 1; |
| 5381 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5382 | md_wakeup_thread(mddev->thread); |
| 5383 | } |
| 5384 | if (mddev->sync_thread) { |
| 5385 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5386 | /* Thread might be blocked waiting for metadata update |
| 5387 | * which will now never happen */ |
| 5388 | wake_up_process(mddev->sync_thread->tsk); |
| 5389 | } |
| 5390 | mddev_unlock(mddev); |
| 5391 | wait_event(resync_wait, mddev->sync_thread == NULL); |
| 5392 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5394 | mutex_lock(&mddev->open_mutex); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5395 | if (atomic_read(&mddev->openers) > !!bdev || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5396 | mddev->sysfs_active || |
| 5397 | mddev->sync_thread || |
| 5398 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5399 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5400 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5401 | if (did_freeze) { |
| 5402 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5403 | md_wakeup_thread(mddev->thread); |
| 5404 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 5405 | return -EBUSY; |
| 5406 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5407 | if (mddev->pers) { |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5408 | if (mddev->ro) |
| 5409 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5410 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5411 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5412 | __md_stop(mddev); |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5413 | mddev->queue->merge_bvec_fn = NULL; |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5414 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5415 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5416 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5417 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5418 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5419 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5420 | if (rdev->raid_disk >= 0) |
| 5421 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5422 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5423 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5424 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5425 | mddev->changed = 1; |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5426 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5427 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5428 | if (mddev->ro) |
| 5429 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5430 | } else |
| 5431 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | /* |
| 5433 | * Free resources if final stop |
| 5434 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5435 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5437 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5438 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5439 | if (mddev->bitmap_info.file) { |
| 5440 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5441 | fput(mddev->bitmap_info.file); |
| 5442 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5443 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5444 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5446 | export_array(mddev); |
| 5447 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5448 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5449 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5450 | if (mddev->hold_active == UNTIL_STOP) |
| 5451 | mddev->hold_active = 0; |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5452 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 5453 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5454 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5455 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5456 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | } |
| 5458 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5459 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5460 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5461 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5462 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5463 | int err; |
| 5464 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5465 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5466 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | |
| 5468 | printk(KERN_INFO "md: running: "); |
| 5469 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5470 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | char b[BDEVNAME_SIZE]; |
| 5472 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5473 | } |
| 5474 | printk("\n"); |
| 5475 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5476 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5477 | if (err) { |
| 5478 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5479 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5480 | } |
| 5481 | } |
| 5482 | |
| 5483 | /* |
| 5484 | * lets try to run arrays based on all disks that have arrived |
| 5485 | * until now. (those are in pending_raid_disks) |
| 5486 | * |
| 5487 | * the method: pick the first pending disk, collect all disks with |
| 5488 | * the same UUID, remove all from the pending list and put them into |
| 5489 | * the 'same_array' list. Then order this list based on superblock |
| 5490 | * update time (freshest comes first), kick out 'old' disks and |
| 5491 | * compare superblocks. If everything's fine then run it. |
| 5492 | * |
| 5493 | * If "unit" is allocated, then bump its reference count |
| 5494 | */ |
| 5495 | static void autorun_devices(int part) |
| 5496 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5497 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5498 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | char b[BDEVNAME_SIZE]; |
| 5500 | |
| 5501 | printk(KERN_INFO "md: autorun ...\n"); |
| 5502 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5503 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5504 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5505 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5506 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5507 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | |
| 5509 | printk(KERN_INFO "md: considering %s ...\n", |
| 5510 | bdevname(rdev0->bdev,b)); |
| 5511 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5512 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5514 | printk(KERN_INFO "md: adding %s ...\n", |
| 5515 | bdevname(rdev->bdev,b)); |
| 5516 | list_move(&rdev->same_set, &candidates); |
| 5517 | } |
| 5518 | /* |
| 5519 | * now we have a set of devices, with all of them having |
| 5520 | * mostly sane superblocks. It's time to allocate the |
| 5521 | * mddev. |
| 5522 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5523 | if (part) { |
| 5524 | dev = MKDEV(mdp_major, |
| 5525 | rdev0->preferred_minor << MdpMinorShift); |
| 5526 | unit = MINOR(dev) >> MdpMinorShift; |
| 5527 | } else { |
| 5528 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5529 | unit = MINOR(dev); |
| 5530 | } |
| 5531 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5532 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5533 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5534 | break; |
| 5535 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | |
| 5537 | md_probe(dev, NULL, NULL); |
| 5538 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5539 | if (!mddev || !mddev->gendisk) { |
| 5540 | if (mddev) |
| 5541 | mddev_put(mddev); |
| 5542 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5543 | "md: cannot allocate memory for md drive.\n"); |
| 5544 | break; |
| 5545 | } |
| 5546 | if (mddev_lock(mddev)) |
| 5547 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5548 | mdname(mddev)); |
| 5549 | else if (mddev->raid_disks || mddev->major_version |
| 5550 | || !list_empty(&mddev->disks)) { |
| 5551 | printk(KERN_WARNING |
| 5552 | "md: %s already running, cannot run %s\n", |
| 5553 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5554 | mddev_unlock(mddev); |
| 5555 | } else { |
| 5556 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5557 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5558 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | list_del_init(&rdev->same_set); |
| 5560 | if (bind_rdev_to_array(rdev, mddev)) |
| 5561 | export_rdev(rdev); |
| 5562 | } |
| 5563 | autorun_array(mddev); |
| 5564 | mddev_unlock(mddev); |
| 5565 | } |
| 5566 | /* on success, candidates will be empty, on error |
| 5567 | * it won't... |
| 5568 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5569 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5570 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5571 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5572 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5573 | mddev_put(mddev); |
| 5574 | } |
| 5575 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5576 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5577 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | static int get_version(void __user * arg) |
| 5580 | { |
| 5581 | mdu_version_t ver; |
| 5582 | |
| 5583 | ver.major = MD_MAJOR_VERSION; |
| 5584 | ver.minor = MD_MINOR_VERSION; |
| 5585 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5586 | |
| 5587 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5588 | return -EFAULT; |
| 5589 | |
| 5590 | return 0; |
| 5591 | } |
| 5592 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5593 | static int get_array_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5594 | { |
| 5595 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5596 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5597 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5598 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5599 | nr = working = insync = failed = spare = 0; |
| 5600 | rcu_read_lock(); |
| 5601 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5602 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5603 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | failed++; |
| 5605 | else { |
| 5606 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5607 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5608 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5609 | else |
| 5610 | spare++; |
| 5611 | } |
| 5612 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5613 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5614 | |
| 5615 | info.major_version = mddev->major_version; |
| 5616 | info.minor_version = mddev->minor_version; |
| 5617 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 5618 | info.ctime = mddev->ctime; |
| 5619 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5620 | info.size = mddev->dev_sectors / 2; |
| 5621 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5622 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | info.nr_disks = nr; |
| 5624 | info.raid_disks = mddev->raid_disks; |
| 5625 | info.md_minor = mddev->md_minor; |
| 5626 | info.not_persistent= !mddev->persistent; |
| 5627 | |
| 5628 | info.utime = mddev->utime; |
| 5629 | info.state = 0; |
| 5630 | if (mddev->in_sync) |
| 5631 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5632 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5633 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5634 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5635 | info.working_disks = working; |
| 5636 | info.failed_disks = failed; |
| 5637 | info.spare_disks = spare; |
| 5638 | |
| 5639 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5640 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5641 | |
| 5642 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5643 | return -EFAULT; |
| 5644 | |
| 5645 | return 0; |
| 5646 | } |
| 5647 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5648 | static int get_bitmap_file(struct mddev * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5649 | { |
| 5650 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5651 | char *ptr, *buf = NULL; |
| 5652 | int err = -ENOMEM; |
| 5653 | |
NeilBrown | 60559da | 2013-07-16 16:44:29 +1000 | [diff] [blame] | 5654 | file = kmalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5655 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5656 | if (!file) |
| 5657 | goto out; |
| 5658 | |
| 5659 | /* bitmap disabled, zero the first byte and copy out */ |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 5660 | if (!mddev->bitmap || !mddev->bitmap->storage.file) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5661 | file->pathname[0] = '\0'; |
| 5662 | goto copy_out; |
| 5663 | } |
| 5664 | |
| 5665 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5666 | if (!buf) |
| 5667 | goto out; |
| 5668 | |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 5669 | ptr = d_path(&mddev->bitmap->storage.file->f_path, |
| 5670 | buf, sizeof(file->pathname)); |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5671 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5672 | goto out; |
| 5673 | |
| 5674 | strcpy(file->pathname, ptr); |
| 5675 | |
| 5676 | copy_out: |
| 5677 | err = 0; |
| 5678 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5679 | err = -EFAULT; |
| 5680 | out: |
| 5681 | kfree(buf); |
| 5682 | kfree(file); |
| 5683 | return err; |
| 5684 | } |
| 5685 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5686 | static int get_disk_info(struct mddev * mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | { |
| 5688 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5689 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5690 | |
| 5691 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5692 | return -EFAULT; |
| 5693 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5694 | rcu_read_lock(); |
| 5695 | rdev = find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | if (rdev) { |
| 5697 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5698 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5699 | info.raid_disk = rdev->raid_disk; |
| 5700 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5701 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5703 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5704 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5705 | info.state |= (1<<MD_DISK_SYNC); |
| 5706 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5707 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5708 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | } else { |
| 5710 | info.major = info.minor = 0; |
| 5711 | info.raid_disk = -1; |
| 5712 | info.state = (1<<MD_DISK_REMOVED); |
| 5713 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5714 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | |
| 5716 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5717 | return -EFAULT; |
| 5718 | |
| 5719 | return 0; |
| 5720 | } |
| 5721 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5722 | 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] | 5723 | { |
| 5724 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5725 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5726 | dev_t dev = MKDEV(info->major,info->minor); |
| 5727 | |
| 5728 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5729 | return -EOVERFLOW; |
| 5730 | |
| 5731 | if (!mddev->raid_disks) { |
| 5732 | int err; |
| 5733 | /* expecting a device which has a superblock */ |
| 5734 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5735 | if (IS_ERR(rdev)) { |
| 5736 | printk(KERN_WARNING |
| 5737 | "md: md_import_device returned %ld\n", |
| 5738 | PTR_ERR(rdev)); |
| 5739 | return PTR_ERR(rdev); |
| 5740 | } |
| 5741 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5742 | struct md_rdev *rdev0 |
| 5743 | = list_entry(mddev->disks.next, |
| 5744 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5745 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5747 | if (err < 0) { |
| 5748 | printk(KERN_WARNING |
| 5749 | "md: %s has different UUID to %s\n", |
| 5750 | bdevname(rdev->bdev,b), |
| 5751 | bdevname(rdev0->bdev,b2)); |
| 5752 | export_rdev(rdev); |
| 5753 | return -EINVAL; |
| 5754 | } |
| 5755 | } |
| 5756 | err = bind_rdev_to_array(rdev, mddev); |
| 5757 | if (err) |
| 5758 | export_rdev(rdev); |
| 5759 | return err; |
| 5760 | } |
| 5761 | |
| 5762 | /* |
| 5763 | * add_new_disk can be used once the array is assembled |
| 5764 | * to add "hot spares". They must already have a superblock |
| 5765 | * written |
| 5766 | */ |
| 5767 | if (mddev->pers) { |
| 5768 | int err; |
| 5769 | if (!mddev->pers->hot_add_disk) { |
| 5770 | printk(KERN_WARNING |
| 5771 | "%s: personality does not support diskops!\n", |
| 5772 | mdname(mddev)); |
| 5773 | return -EINVAL; |
| 5774 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5775 | if (mddev->persistent) |
| 5776 | rdev = md_import_device(dev, mddev->major_version, |
| 5777 | mddev->minor_version); |
| 5778 | else |
| 5779 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5780 | if (IS_ERR(rdev)) { |
| 5781 | printk(KERN_WARNING |
| 5782 | "md: md_import_device returned %ld\n", |
| 5783 | PTR_ERR(rdev)); |
| 5784 | return PTR_ERR(rdev); |
| 5785 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 5786 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5787 | if (!mddev->persistent) { |
| 5788 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5789 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5790 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5791 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 5792 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 5793 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5794 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame^] | 5795 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5796 | } else |
| 5797 | super_types[mddev->major_version]. |
| 5798 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5799 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 5800 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 5801 | /* This was a hot-add request, but events doesn't |
| 5802 | * match, so reject it. |
| 5803 | */ |
| 5804 | export_rdev(rdev); |
| 5805 | return -EINVAL; |
| 5806 | } |
| 5807 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5808 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5809 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5810 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5811 | else |
| 5812 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5814 | rdev->raid_disk = -1; |
| 5815 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5816 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5817 | /* If there is hot_add_disk but no hot_remove_disk |
| 5818 | * then added disks for geometry changes, |
| 5819 | * and should be added immediately. |
| 5820 | */ |
| 5821 | super_types[mddev->major_version]. |
| 5822 | validate_super(mddev, rdev); |
| 5823 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5824 | if (err) |
| 5825 | unbind_rdev_from_array(rdev); |
| 5826 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5827 | if (err) |
| 5828 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5829 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5830 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5831 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5832 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5833 | if (mddev->degraded) |
| 5834 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5835 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
马建朋 | 9864c00 | 2011-06-09 11:42:48 +1000 | [diff] [blame] | 5836 | if (!err) |
| 5837 | md_new_event(mddev); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5838 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | return err; |
| 5840 | } |
| 5841 | |
| 5842 | /* otherwise, add_new_disk is only allowed |
| 5843 | * for major_version==0 superblocks |
| 5844 | */ |
| 5845 | if (mddev->major_version != 0) { |
| 5846 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5847 | mdname(mddev)); |
| 5848 | return -EINVAL; |
| 5849 | } |
| 5850 | |
| 5851 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5852 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5853 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5854 | if (IS_ERR(rdev)) { |
| 5855 | printk(KERN_WARNING |
| 5856 | "md: error, md_import_device() returned %ld\n", |
| 5857 | PTR_ERR(rdev)); |
| 5858 | return PTR_ERR(rdev); |
| 5859 | } |
| 5860 | rdev->desc_nr = info->number; |
| 5861 | if (info->raid_disk < mddev->raid_disks) |
| 5862 | rdev->raid_disk = info->raid_disk; |
| 5863 | else |
| 5864 | rdev->raid_disk = -1; |
| 5865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5867 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5868 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5869 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5870 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5871 | set_bit(WriteMostly, &rdev->flags); |
| 5872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | if (!mddev->persistent) { |
| 5874 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5875 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 5876 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5877 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5878 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5879 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5880 | err = bind_rdev_to_array(rdev, mddev); |
| 5881 | if (err) { |
| 5882 | export_rdev(rdev); |
| 5883 | return err; |
| 5884 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | } |
| 5886 | |
| 5887 | return 0; |
| 5888 | } |
| 5889 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5890 | static int hot_remove_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5891 | { |
| 5892 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5893 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | rdev = find_rdev(mddev, dev); |
| 5896 | if (!rdev) |
| 5897 | return -ENXIO; |
| 5898 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 5899 | clear_bit(Blocked, &rdev->flags); |
| 5900 | remove_and_add_spares(mddev, rdev); |
| 5901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5902 | if (rdev->raid_disk >= 0) |
| 5903 | goto busy; |
| 5904 | |
| 5905 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5906 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5907 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5908 | |
| 5909 | return 0; |
| 5910 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5911 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5913 | return -EBUSY; |
| 5914 | } |
| 5915 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5916 | static int hot_add_disk(struct mddev * mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | { |
| 5918 | char b[BDEVNAME_SIZE]; |
| 5919 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5920 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | |
| 5922 | if (!mddev->pers) |
| 5923 | return -ENODEV; |
| 5924 | |
| 5925 | if (mddev->major_version != 0) { |
| 5926 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5927 | " version-0 superblocks.\n", |
| 5928 | mdname(mddev)); |
| 5929 | return -EINVAL; |
| 5930 | } |
| 5931 | if (!mddev->pers->hot_add_disk) { |
| 5932 | printk(KERN_WARNING |
| 5933 | "%s: personality does not support diskops!\n", |
| 5934 | mdname(mddev)); |
| 5935 | return -EINVAL; |
| 5936 | } |
| 5937 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5938 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | if (IS_ERR(rdev)) { |
| 5940 | printk(KERN_WARNING |
| 5941 | "md: error, md_import_device() returned %ld\n", |
| 5942 | PTR_ERR(rdev)); |
| 5943 | return -EINVAL; |
| 5944 | } |
| 5945 | |
| 5946 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5947 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 5949 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5951 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5953 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5954 | printk(KERN_WARNING |
| 5955 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5956 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5957 | err = -EINVAL; |
| 5958 | goto abort_export; |
| 5959 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5960 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5961 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5962 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5963 | err = bind_rdev_to_array(rdev, mddev); |
| 5964 | if (err) |
| 5965 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5966 | |
| 5967 | /* |
| 5968 | * The rest should better be atomic, we can have disk failures |
| 5969 | * noticed in interrupt contexts ... |
| 5970 | */ |
| 5971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | rdev->raid_disk = -1; |
| 5973 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5974 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5975 | |
| 5976 | /* |
| 5977 | * Kick recovery, maybe this spare has to be added to the |
| 5978 | * array immediately. |
| 5979 | */ |
| 5980 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5981 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5982 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5983 | return 0; |
| 5984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5985 | abort_export: |
| 5986 | export_rdev(rdev); |
| 5987 | return err; |
| 5988 | } |
| 5989 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5990 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5991 | { |
| 5992 | int err; |
| 5993 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5994 | if (mddev->pers) { |
| 5995 | if (!mddev->pers->quiesce) |
| 5996 | return -EBUSY; |
| 5997 | if (mddev->recovery || mddev->sync_thread) |
| 5998 | return -EBUSY; |
| 5999 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6000 | } |
| 6001 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6002 | |
| 6003 | if (fd >= 0) { |
| 6004 | if (mddev->bitmap) |
| 6005 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6006 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6007 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6008 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6009 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 6010 | mdname(mddev)); |
| 6011 | return -EBADF; |
| 6012 | } |
| 6013 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6014 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6015 | if (err) { |
| 6016 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 6017 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6018 | fput(mddev->bitmap_info.file); |
| 6019 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6020 | return err; |
| 6021 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6022 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6023 | } else if (mddev->bitmap == NULL) |
| 6024 | return -ENOENT; /* cannot remove what isn't there */ |
| 6025 | err = 0; |
| 6026 | if (mddev->pers) { |
| 6027 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6028 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6029 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6030 | if (!err) |
| 6031 | err = bitmap_load(mddev); |
| 6032 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6033 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6034 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6035 | fd = -1; /* make sure to put the file */ |
| 6036 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6037 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6038 | } |
| 6039 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6040 | if (mddev->bitmap_info.file) { |
| 6041 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 6042 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 6043 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6044 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6045 | } |
| 6046 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6047 | return err; |
| 6048 | } |
| 6049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6050 | /* |
| 6051 | * set_array_info is used two different ways |
| 6052 | * The original usage is when creating a new array. |
| 6053 | * In this usage, raid_disks is > 0 and it together with |
| 6054 | * level, size, not_persistent,layout,chunksize determine the |
| 6055 | * shape of the array. |
| 6056 | * This will always create an array with a type-0.90.0 superblock. |
| 6057 | * The newer usage is when assembling an array. |
| 6058 | * In this case raid_disks will be 0, and the major_version field is |
| 6059 | * use to determine which style super-blocks are to be found on the devices. |
| 6060 | * The minor and patch _version numbers are also kept incase the |
| 6061 | * super_block handler wishes to interpret them. |
| 6062 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6063 | 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] | 6064 | { |
| 6065 | |
| 6066 | if (info->raid_disks == 0) { |
| 6067 | /* just setting version number for superblock loading */ |
| 6068 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6069 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | super_types[info->major_version].name == NULL) { |
| 6071 | /* maybe try to auto-load a module? */ |
| 6072 | printk(KERN_INFO |
| 6073 | "md: superblock version %d not known\n", |
| 6074 | info->major_version); |
| 6075 | return -EINVAL; |
| 6076 | } |
| 6077 | mddev->major_version = info->major_version; |
| 6078 | mddev->minor_version = info->minor_version; |
| 6079 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6080 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6081 | /* ensure mddev_put doesn't delete this now that there |
| 6082 | * is some minimal configuration. |
| 6083 | */ |
| 6084 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | return 0; |
| 6086 | } |
| 6087 | mddev->major_version = MD_MAJOR_VERSION; |
| 6088 | mddev->minor_version = MD_MINOR_VERSION; |
| 6089 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 6090 | mddev->ctime = get_seconds(); |
| 6091 | |
| 6092 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6093 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6094 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6095 | mddev->raid_disks = info->raid_disks; |
| 6096 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6097 | * openned |
| 6098 | */ |
| 6099 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6100 | mddev->recovery_cp = MaxSector; |
| 6101 | else |
| 6102 | mddev->recovery_cp = 0; |
| 6103 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6104 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | |
| 6106 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6107 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | |
| 6109 | mddev->max_disks = MD_SB_DISKS; |
| 6110 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6111 | if (mddev->persistent) |
| 6112 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6113 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6114 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6115 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6116 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6117 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6118 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6119 | mddev->reshape_position = MaxSector; |
| 6120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | /* |
| 6122 | * Generate a 128 bit UUID |
| 6123 | */ |
| 6124 | get_random_bytes(mddev->uuid, 16); |
| 6125 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6126 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6127 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6128 | mddev->new_layout = mddev->layout; |
| 6129 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6130 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6132 | return 0; |
| 6133 | } |
| 6134 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6135 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6136 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6137 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6138 | |
| 6139 | if (mddev->external_size) |
| 6140 | return; |
| 6141 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6142 | mddev->array_sectors = array_sectors; |
| 6143 | } |
| 6144 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6145 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6146 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6147 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6148 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6149 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6150 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6151 | |
| 6152 | if (mddev->pers->resize == NULL) |
| 6153 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6154 | /* The "num_sectors" is the number of sectors of each device that |
| 6155 | * is used. This can only make sense for arrays with redundancy. |
| 6156 | * linear and raid0 always use whatever space is available. We can only |
| 6157 | * consider changing this number if no resync or reconstruction is |
| 6158 | * 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] | 6159 | * 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] | 6160 | * of each device. If num_sectors is zero, we find the largest size |
| 6161 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6162 | */ |
| 6163 | if (mddev->sync_thread) |
| 6164 | return -EBUSY; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6165 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6166 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6167 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6168 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6169 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6170 | num_sectors = avail; |
| 6171 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6172 | return -ENOSPC; |
| 6173 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6174 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6175 | if (!rv) |
| 6176 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6177 | return rv; |
| 6178 | } |
| 6179 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6180 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6181 | { |
| 6182 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6183 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6184 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6185 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6186 | return -EINVAL; |
| 6187 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6188 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6189 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6190 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6191 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6192 | |
| 6193 | rdev_for_each(rdev, mddev) { |
| 6194 | if (mddev->raid_disks < raid_disks && |
| 6195 | rdev->data_offset < rdev->new_data_offset) |
| 6196 | return -EINVAL; |
| 6197 | if (mddev->raid_disks > raid_disks && |
| 6198 | rdev->data_offset > rdev->new_data_offset) |
| 6199 | return -EINVAL; |
| 6200 | } |
| 6201 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6202 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6203 | if (mddev->delta_disks < 0) |
| 6204 | mddev->reshape_backwards = 1; |
| 6205 | else if (mddev->delta_disks > 0) |
| 6206 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6207 | |
| 6208 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6209 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6210 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6211 | mddev->reshape_backwards = 0; |
| 6212 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6213 | return rv; |
| 6214 | } |
| 6215 | |
| 6216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6217 | /* |
| 6218 | * update_array_info is used to change the configuration of an |
| 6219 | * on-line array. |
| 6220 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6221 | * fields in the info are checked against the array. |
| 6222 | * Any differences that cannot be handled will cause an error. |
| 6223 | * Normally, only one change can be managed at a time. |
| 6224 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6225 | 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] | 6226 | { |
| 6227 | int rv = 0; |
| 6228 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6229 | int state = 0; |
| 6230 | |
| 6231 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6232 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6233 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6234 | |
| 6235 | if (mddev->major_version != info->major_version || |
| 6236 | mddev->minor_version != info->minor_version || |
| 6237 | /* mddev->patch_version != info->patch_version || */ |
| 6238 | mddev->ctime != info->ctime || |
| 6239 | mddev->level != info->level || |
| 6240 | /* mddev->layout != info->layout || */ |
| 6241 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6242 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6243 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6244 | ((state^info->state) & 0xfffffe00) |
| 6245 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6246 | return -EINVAL; |
| 6247 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6248 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6249 | cnt++; |
| 6250 | if (mddev->raid_disks != info->raid_disks) |
| 6251 | cnt++; |
| 6252 | if (mddev->layout != info->layout) |
| 6253 | cnt++; |
| 6254 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6255 | cnt++; |
| 6256 | if (cnt == 0) |
| 6257 | return 0; |
| 6258 | if (cnt > 1) |
| 6259 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6260 | |
| 6261 | if (mddev->layout != info->layout) { |
| 6262 | /* Change layout |
| 6263 | * we don't need to do anything at the md level, the |
| 6264 | * personality will take care of it all. |
| 6265 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6266 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6267 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6268 | else { |
| 6269 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6270 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6271 | if (rv) |
| 6272 | mddev->new_layout = mddev->layout; |
| 6273 | return rv; |
| 6274 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6275 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6276 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6277 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6278 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6279 | if (mddev->raid_disks != info->raid_disks) |
| 6280 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6281 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6282 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6283 | if (mddev->pers->quiesce == NULL) |
| 6284 | return -EINVAL; |
| 6285 | if (mddev->recovery || mddev->sync_thread) |
| 6286 | return -EBUSY; |
| 6287 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 6288 | /* add the bitmap */ |
| 6289 | if (mddev->bitmap) |
| 6290 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6291 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6292 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6293 | mddev->bitmap_info.offset = |
| 6294 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6295 | mddev->bitmap_info.space = |
| 6296 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6297 | mddev->pers->quiesce(mddev, 1); |
| 6298 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6299 | if (!rv) |
| 6300 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6301 | if (rv) |
| 6302 | bitmap_destroy(mddev); |
| 6303 | mddev->pers->quiesce(mddev, 0); |
| 6304 | } else { |
| 6305 | /* remove the bitmap */ |
| 6306 | if (!mddev->bitmap) |
| 6307 | return -ENOENT; |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 6308 | if (mddev->bitmap->storage.file) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6309 | return -EINVAL; |
| 6310 | mddev->pers->quiesce(mddev, 1); |
| 6311 | bitmap_destroy(mddev); |
| 6312 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6313 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6314 | } |
| 6315 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6316 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6317 | return rv; |
| 6318 | } |
| 6319 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6320 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6321 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6322 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6323 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6324 | |
| 6325 | if (mddev->pers == NULL) |
| 6326 | return -ENODEV; |
| 6327 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6328 | rcu_read_lock(); |
| 6329 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6330 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6331 | err = -ENODEV; |
| 6332 | else { |
| 6333 | md_error(mddev, rdev); |
| 6334 | if (!test_bit(Faulty, &rdev->flags)) |
| 6335 | err = -EBUSY; |
| 6336 | } |
| 6337 | rcu_read_unlock(); |
| 6338 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6339 | } |
| 6340 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6341 | /* |
| 6342 | * We have a problem here : there is no easy way to give a CHS |
| 6343 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6344 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6345 | * dosfs just mad... ;-) |
| 6346 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6347 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6348 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6349 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6350 | |
| 6351 | geo->heads = 2; |
| 6352 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6353 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6354 | return 0; |
| 6355 | } |
| 6356 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6357 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6358 | unsigned int cmd, unsigned long arg) |
| 6359 | { |
| 6360 | int err = 0; |
| 6361 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6362 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6363 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6364 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6365 | switch (cmd) { |
| 6366 | case RAID_VERSION: |
| 6367 | case GET_ARRAY_INFO: |
| 6368 | case GET_DISK_INFO: |
| 6369 | break; |
| 6370 | default: |
| 6371 | if (!capable(CAP_SYS_ADMIN)) |
| 6372 | return -EACCES; |
| 6373 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6374 | |
| 6375 | /* |
| 6376 | * Commands dealing with the RAID driver but not any |
| 6377 | * particular array: |
| 6378 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6379 | switch (cmd) { |
| 6380 | case RAID_VERSION: |
| 6381 | err = get_version(argp); |
| 6382 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6384 | case PRINT_RAID_DEBUG: |
| 6385 | err = 0; |
| 6386 | md_print_devices(); |
| 6387 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6388 | |
| 6389 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6390 | case RAID_AUTORUN: |
| 6391 | err = 0; |
| 6392 | autostart_arrays(arg); |
| 6393 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6395 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6396 | } |
| 6397 | |
| 6398 | /* |
| 6399 | * Commands creating/starting a new array: |
| 6400 | */ |
| 6401 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6402 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6403 | |
| 6404 | if (!mddev) { |
| 6405 | BUG(); |
| 6406 | goto abort; |
| 6407 | } |
| 6408 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6409 | /* Some actions do not requires the mutex */ |
| 6410 | switch (cmd) { |
| 6411 | case GET_ARRAY_INFO: |
| 6412 | if (!mddev->raid_disks && !mddev->external) |
| 6413 | err = -ENODEV; |
| 6414 | else |
| 6415 | err = get_array_info(mddev, argp); |
| 6416 | goto abort; |
| 6417 | |
| 6418 | case GET_DISK_INFO: |
| 6419 | if (!mddev->raid_disks && !mddev->external) |
| 6420 | err = -ENODEV; |
| 6421 | else |
| 6422 | err = get_disk_info(mddev, argp); |
| 6423 | goto abort; |
| 6424 | |
| 6425 | case SET_DISK_FAULTY: |
| 6426 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 6427 | goto abort; |
| 6428 | } |
| 6429 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 6430 | if (cmd == ADD_NEW_DISK) |
| 6431 | /* need to ensure md_delayed_delete() has completed */ |
| 6432 | flush_workqueue(md_misc_wq); |
| 6433 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 6434 | if (cmd == HOT_REMOVE_DISK) |
| 6435 | /* need to ensure recovery thread has run */ |
| 6436 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 6437 | !test_bit(MD_RECOVERY_NEEDED, |
| 6438 | &mddev->flags), |
| 6439 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6440 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 6441 | /* Need to flush page cache, and ensure no-one else opens |
| 6442 | * and writes |
| 6443 | */ |
| 6444 | mutex_lock(&mddev->open_mutex); |
| 6445 | if (atomic_read(&mddev->openers) > 1) { |
| 6446 | mutex_unlock(&mddev->open_mutex); |
| 6447 | err = -EBUSY; |
| 6448 | goto abort; |
| 6449 | } |
| 6450 | set_bit(MD_STILL_CLOSED, &mddev->flags); |
| 6451 | mutex_unlock(&mddev->open_mutex); |
| 6452 | sync_blockdev(bdev); |
| 6453 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6454 | err = mddev_lock(mddev); |
| 6455 | if (err) { |
| 6456 | printk(KERN_INFO |
| 6457 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6458 | err, cmd); |
| 6459 | goto abort; |
| 6460 | } |
| 6461 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6462 | if (cmd == SET_ARRAY_INFO) { |
| 6463 | mdu_array_info_t info; |
| 6464 | if (!arg) |
| 6465 | memset(&info, 0, sizeof(info)); |
| 6466 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6467 | err = -EFAULT; |
| 6468 | goto abort_unlock; |
| 6469 | } |
| 6470 | if (mddev->pers) { |
| 6471 | err = update_array_info(mddev, &info); |
| 6472 | if (err) { |
| 6473 | printk(KERN_WARNING "md: couldn't update" |
| 6474 | " array info. %d\n", err); |
| 6475 | goto abort_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6476 | } |
| 6477 | goto done_unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6478 | } |
| 6479 | if (!list_empty(&mddev->disks)) { |
| 6480 | printk(KERN_WARNING |
| 6481 | "md: array %s already has disks!\n", |
| 6482 | mdname(mddev)); |
| 6483 | err = -EBUSY; |
| 6484 | goto abort_unlock; |
| 6485 | } |
| 6486 | if (mddev->raid_disks) { |
| 6487 | printk(KERN_WARNING |
| 6488 | "md: array %s already initialised!\n", |
| 6489 | mdname(mddev)); |
| 6490 | err = -EBUSY; |
| 6491 | goto abort_unlock; |
| 6492 | } |
| 6493 | err = set_array_info(mddev, &info); |
| 6494 | if (err) { |
| 6495 | printk(KERN_WARNING "md: couldn't set" |
| 6496 | " array info. %d\n", err); |
| 6497 | goto abort_unlock; |
| 6498 | } |
| 6499 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | } |
| 6501 | |
| 6502 | /* |
| 6503 | * Commands querying/configuring an existing array: |
| 6504 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6505 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6506 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6507 | if ((!mddev->raid_disks && !mddev->external) |
| 6508 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6509 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6510 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6511 | err = -ENODEV; |
| 6512 | goto abort_unlock; |
| 6513 | } |
| 6514 | |
| 6515 | /* |
| 6516 | * Commands even a read-only array can execute: |
| 6517 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6518 | switch (cmd) { |
| 6519 | case GET_BITMAP_FILE: |
| 6520 | err = get_bitmap_file(mddev, argp); |
| 6521 | goto done_unlock; |
| 6522 | |
| 6523 | case RESTART_ARRAY_RW: |
| 6524 | err = restart_array(mddev); |
| 6525 | goto done_unlock; |
| 6526 | |
| 6527 | case STOP_ARRAY: |
| 6528 | err = do_md_stop(mddev, 0, bdev); |
| 6529 | goto done_unlock; |
| 6530 | |
| 6531 | case STOP_ARRAY_RO: |
| 6532 | err = md_set_readonly(mddev, bdev); |
| 6533 | goto done_unlock; |
| 6534 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6535 | case HOT_REMOVE_DISK: |
| 6536 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 6537 | goto done_unlock; |
| 6538 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6539 | case ADD_NEW_DISK: |
| 6540 | /* We can support ADD_NEW_DISK on read-only arrays |
| 6541 | * on if we are re-adding a preexisting device. |
| 6542 | * So require mddev->pers and MD_DISK_SYNC. |
| 6543 | */ |
| 6544 | if (mddev->pers) { |
| 6545 | mdu_disk_info_t info; |
| 6546 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6547 | err = -EFAULT; |
| 6548 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 6549 | /* Need to clear read-only for this */ |
| 6550 | break; |
| 6551 | else |
| 6552 | err = add_new_disk(mddev, &info); |
| 6553 | goto done_unlock; |
| 6554 | } |
| 6555 | break; |
| 6556 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6557 | case BLKROSET: |
| 6558 | if (get_user(ro, (int __user *)(arg))) { |
| 6559 | err = -EFAULT; |
| 6560 | goto done_unlock; |
| 6561 | } |
| 6562 | err = -EINVAL; |
| 6563 | |
| 6564 | /* if the bdev is going readonly the value of mddev->ro |
| 6565 | * does not matter, no writes are coming |
| 6566 | */ |
| 6567 | if (ro) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6568 | goto done_unlock; |
| 6569 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6570 | /* are we are already prepared for writes? */ |
| 6571 | if (mddev->ro != 1) |
| 6572 | goto done_unlock; |
| 6573 | |
| 6574 | /* transitioning to readauto need only happen for |
| 6575 | * arrays that call md_write_start |
| 6576 | */ |
| 6577 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6578 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6579 | if (err == 0) { |
| 6580 | mddev->ro = 2; |
| 6581 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6582 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6583 | } |
| 6584 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6585 | } |
| 6586 | |
| 6587 | /* |
| 6588 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6589 | * superblock, so we do not allow them on read-only arrays. |
| 6590 | * However non-MD ioctls (e.g. get-size) will still come through |
| 6591 | * here and hit the 'default' below, so only disallow |
| 6592 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6593 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 6594 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6595 | if (mddev->ro == 2) { |
| 6596 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6597 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6598 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6599 | /* mddev_unlock will wake thread */ |
| 6600 | /* If a device failed while we were read-only, we |
| 6601 | * need to make sure the metadata is updated now. |
| 6602 | */ |
| 6603 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
| 6604 | mddev_unlock(mddev); |
| 6605 | wait_event(mddev->sb_wait, |
| 6606 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && |
| 6607 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6608 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6609 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6610 | } else { |
| 6611 | err = -EROFS; |
| 6612 | goto abort_unlock; |
| 6613 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6614 | } |
| 6615 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6616 | switch (cmd) { |
| 6617 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6618 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6619 | mdu_disk_info_t info; |
| 6620 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6621 | err = -EFAULT; |
| 6622 | else |
| 6623 | err = add_new_disk(mddev, &info); |
| 6624 | goto done_unlock; |
| 6625 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6626 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6627 | case HOT_ADD_DISK: |
| 6628 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 6629 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6630 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6631 | case RUN_ARRAY: |
| 6632 | err = do_md_run(mddev); |
| 6633 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6634 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6635 | case SET_BITMAP_FILE: |
| 6636 | err = set_bitmap_file(mddev, (int)arg); |
| 6637 | goto done_unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6638 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6639 | default: |
| 6640 | err = -EINVAL; |
| 6641 | goto abort_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6642 | } |
| 6643 | |
| 6644 | done_unlock: |
| 6645 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6646 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6647 | err != -EINVAL) |
| 6648 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6649 | mddev_unlock(mddev); |
| 6650 | |
| 6651 | return err; |
| 6652 | done: |
| 6653 | if (err) |
| 6654 | MD_BUG(); |
| 6655 | abort: |
| 6656 | return err; |
| 6657 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6658 | #ifdef CONFIG_COMPAT |
| 6659 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6660 | unsigned int cmd, unsigned long arg) |
| 6661 | { |
| 6662 | switch (cmd) { |
| 6663 | case HOT_REMOVE_DISK: |
| 6664 | case HOT_ADD_DISK: |
| 6665 | case SET_DISK_FAULTY: |
| 6666 | case SET_BITMAP_FILE: |
| 6667 | /* These take in integer arg, do not convert */ |
| 6668 | break; |
| 6669 | default: |
| 6670 | arg = (unsigned long)compat_ptr(arg); |
| 6671 | break; |
| 6672 | } |
| 6673 | |
| 6674 | return md_ioctl(bdev, mode, cmd, arg); |
| 6675 | } |
| 6676 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6677 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6678 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6679 | { |
| 6680 | /* |
| 6681 | * Succeed if we can lock the mddev, which confirms that |
| 6682 | * it isn't being stopped right now. |
| 6683 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6684 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6685 | int err; |
| 6686 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 6687 | if (!mddev) |
| 6688 | return -ENODEV; |
| 6689 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6690 | if (mddev->gendisk != bdev->bd_disk) { |
| 6691 | /* we are racing with mddev_put which is discarding this |
| 6692 | * bd_disk. |
| 6693 | */ |
| 6694 | mddev_put(mddev); |
| 6695 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6696 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6697 | /* Then retry the open from the top */ |
| 6698 | return -ERESTARTSYS; |
| 6699 | } |
| 6700 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 6701 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6702 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6703 | goto out; |
| 6704 | |
| 6705 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6706 | atomic_inc(&mddev->openers); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6707 | clear_bit(MD_STILL_CLOSED, &mddev->flags); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6708 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6710 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6711 | out: |
| 6712 | return err; |
| 6713 | } |
| 6714 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 6715 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6716 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6717 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6718 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 6719 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6720 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6721 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6722 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6723 | |
| 6724 | static int md_media_changed(struct gendisk *disk) |
| 6725 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6726 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6727 | |
| 6728 | return mddev->changed; |
| 6729 | } |
| 6730 | |
| 6731 | static int md_revalidate(struct gendisk *disk) |
| 6732 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6733 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6734 | |
| 6735 | mddev->changed = 0; |
| 6736 | return 0; |
| 6737 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 6738 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6739 | { |
| 6740 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6741 | .open = md_open, |
| 6742 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6743 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6744 | #ifdef CONFIG_COMPAT |
| 6745 | .compat_ioctl = md_compat_ioctl, |
| 6746 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6747 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6748 | .media_changed = md_media_changed, |
| 6749 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6750 | }; |
| 6751 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6752 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6753 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6754 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6756 | /* |
| 6757 | * md_thread is a 'system-thread', it's priority should be very |
| 6758 | * high. We avoid resource deadlocks individually in each |
| 6759 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6760 | * the very same RT priority as kswapd, thus we will never get |
| 6761 | * into a priority inversion deadlock. |
| 6762 | * |
| 6763 | * we definitely have to have equal or higher priority than |
| 6764 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6765 | * many dirty RAID5 blocks. |
| 6766 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6767 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6768 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6769 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6770 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6771 | /* We need to wait INTERRUPTIBLE so that |
| 6772 | * we don't add to the load-average. |
| 6773 | * That means we need to be sure no signals are |
| 6774 | * pending |
| 6775 | */ |
| 6776 | if (signal_pending(current)) |
| 6777 | flush_signals(current); |
| 6778 | |
| 6779 | wait_event_interruptible_timeout |
| 6780 | (thread->wqueue, |
| 6781 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6782 | || kthread_should_stop(), |
| 6783 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6784 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 6785 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6786 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6787 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6788 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6790 | return 0; |
| 6791 | } |
| 6792 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6793 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6794 | { |
| 6795 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6796 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6797 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6798 | wake_up(&thread->wqueue); |
| 6799 | } |
| 6800 | } |
| 6801 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6802 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 6803 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6804 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6805 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6806 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6807 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6808 | if (!thread) |
| 6809 | return NULL; |
| 6810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6811 | init_waitqueue_head(&thread->wqueue); |
| 6812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6813 | thread->run = run; |
| 6814 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6815 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6816 | thread->tsk = kthread_run(md_thread, thread, |
| 6817 | "%s_%s", |
| 6818 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 6819 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6820 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6821 | kfree(thread); |
| 6822 | return NULL; |
| 6823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6824 | return thread; |
| 6825 | } |
| 6826 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6827 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6828 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 6829 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6830 | if (!thread) |
| 6831 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 6832 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6833 | /* Locking ensures that mddev_unlock does not wake_up a |
| 6834 | * non-existent thread |
| 6835 | */ |
| 6836 | spin_lock(&pers_lock); |
| 6837 | *threadp = NULL; |
| 6838 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6839 | |
| 6840 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6841 | kfree(thread); |
| 6842 | } |
| 6843 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6844 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6845 | { |
| 6846 | if (!mddev) { |
| 6847 | MD_BUG(); |
| 6848 | return; |
| 6849 | } |
| 6850 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6851 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6852 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6853 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6854 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6855 | return; |
| 6856 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6857 | if (mddev->degraded) |
| 6858 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6859 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6860 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6861 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6862 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6863 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6864 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6865 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6866 | } |
| 6867 | |
| 6868 | /* seq_file implementation /proc/mdstat */ |
| 6869 | |
| 6870 | static void status_unused(struct seq_file *seq) |
| 6871 | { |
| 6872 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6873 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6874 | |
| 6875 | seq_printf(seq, "unused devices: "); |
| 6876 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6877 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6878 | char b[BDEVNAME_SIZE]; |
| 6879 | i++; |
| 6880 | seq_printf(seq, "%s ", |
| 6881 | bdevname(rdev->bdev,b)); |
| 6882 | } |
| 6883 | if (!i) |
| 6884 | seq_printf(seq, "<none>"); |
| 6885 | |
| 6886 | seq_printf(seq, "\n"); |
| 6887 | } |
| 6888 | |
| 6889 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6890 | static void status_resync(struct seq_file *seq, struct mddev * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6891 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6892 | sector_t max_sectors, resync, res; |
| 6893 | unsigned long dt, db; |
| 6894 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6895 | int scale; |
| 6896 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6897 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 6898 | if (mddev->curr_resync <= 3) |
| 6899 | resync = 0; |
| 6900 | else |
| 6901 | resync = mddev->curr_resync |
| 6902 | - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6903 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 6904 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 6905 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6906 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6907 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6908 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6909 | |
| 6910 | /* |
| 6911 | * Should not happen. |
| 6912 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6913 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6914 | MD_BUG(); |
| 6915 | return; |
| 6916 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6917 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6918 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6919 | * u32, as those are the requirements for sector_div. |
| 6920 | * Thus 'scale' must be at least 10 |
| 6921 | */ |
| 6922 | scale = 10; |
| 6923 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6924 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6925 | scale++; |
| 6926 | } |
| 6927 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6928 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6929 | |
| 6930 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6931 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6932 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6933 | seq_printf(seq, "["); |
| 6934 | for (i = 0; i < x; i++) |
| 6935 | seq_printf(seq, "="); |
| 6936 | seq_printf(seq, ">"); |
| 6937 | for (i = 0; i < y; i++) |
| 6938 | seq_printf(seq, "."); |
| 6939 | seq_printf(seq, "] "); |
| 6940 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6941 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6942 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6943 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6944 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6945 | "check" : |
| 6946 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6947 | "resync" : "recovery"))), |
| 6948 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6949 | (unsigned long long) resync/2, |
| 6950 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6951 | |
| 6952 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6953 | * dt: time from mark until now |
| 6954 | * db: blocks written from mark until now |
| 6955 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6956 | * |
| 6957 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6958 | * So we divide before multiply in case it is 32bit and close |
| 6959 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6960 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6961 | * near the end of resync when the number of remaining sectors |
| 6962 | * is close to 'db'. |
| 6963 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6964 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6965 | */ |
| 6966 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6967 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6968 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6969 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6971 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6972 | sector_div(rt, db/32+1); |
| 6973 | rt *= dt; |
| 6974 | rt >>= 5; |
| 6975 | |
| 6976 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6977 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6979 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6980 | } |
| 6981 | |
| 6982 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6983 | { |
| 6984 | struct list_head *tmp; |
| 6985 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6986 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6987 | |
| 6988 | if (l >= 0x10000) |
| 6989 | return NULL; |
| 6990 | if (!l--) |
| 6991 | /* header */ |
| 6992 | return (void*)1; |
| 6993 | |
| 6994 | spin_lock(&all_mddevs_lock); |
| 6995 | list_for_each(tmp,&all_mddevs) |
| 6996 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6997 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6998 | mddev_get(mddev); |
| 6999 | spin_unlock(&all_mddevs_lock); |
| 7000 | return mddev; |
| 7001 | } |
| 7002 | spin_unlock(&all_mddevs_lock); |
| 7003 | if (!l--) |
| 7004 | return (void*)2;/* tail */ |
| 7005 | return NULL; |
| 7006 | } |
| 7007 | |
| 7008 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 7009 | { |
| 7010 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7011 | struct mddev *next_mddev, *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7012 | |
| 7013 | ++*pos; |
| 7014 | if (v == (void*)2) |
| 7015 | return NULL; |
| 7016 | |
| 7017 | spin_lock(&all_mddevs_lock); |
| 7018 | if (v == (void*)1) |
| 7019 | tmp = all_mddevs.next; |
| 7020 | else |
| 7021 | tmp = mddev->all_mddevs.next; |
| 7022 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7023 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7024 | else { |
| 7025 | next_mddev = (void*)2; |
| 7026 | *pos = 0x10000; |
| 7027 | } |
| 7028 | spin_unlock(&all_mddevs_lock); |
| 7029 | |
| 7030 | if (v != (void*)1) |
| 7031 | mddev_put(mddev); |
| 7032 | return next_mddev; |
| 7033 | |
| 7034 | } |
| 7035 | |
| 7036 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 7037 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7038 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7039 | |
| 7040 | if (mddev && v != (void*)1 && v != (void*)2) |
| 7041 | mddev_put(mddev); |
| 7042 | } |
| 7043 | |
| 7044 | static int md_seq_show(struct seq_file *seq, void *v) |
| 7045 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7046 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7047 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7048 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7049 | |
| 7050 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7051 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7052 | seq_printf(seq, "Personalities : "); |
| 7053 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7054 | list_for_each_entry(pers, &pers_list, list) |
| 7055 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7056 | |
| 7057 | spin_unlock(&pers_lock); |
| 7058 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7059 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7060 | return 0; |
| 7061 | } |
| 7062 | if (v == (void*)2) { |
| 7063 | status_unused(seq); |
| 7064 | return 0; |
| 7065 | } |
| 7066 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 7067 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7068 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 7069 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7070 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7071 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7072 | mddev->pers ? "" : "in"); |
| 7073 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7074 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7075 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7076 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7077 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7078 | seq_printf(seq, " %s", mddev->pers->name); |
| 7079 | } |
| 7080 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7081 | sectors = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7082 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7083 | char b[BDEVNAME_SIZE]; |
| 7084 | seq_printf(seq, " %s[%d]", |
| 7085 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7086 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7087 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7088 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7089 | seq_printf(seq, "(F)"); |
| 7090 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7091 | } |
| 7092 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7093 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7094 | if (test_bit(Replacement, &rdev->flags)) |
| 7095 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7096 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7097 | } |
| 7098 | |
| 7099 | if (!list_empty(&mddev->disks)) { |
| 7100 | if (mddev->pers) |
| 7101 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7102 | (unsigned long long) |
| 7103 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7104 | else |
| 7105 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7106 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7107 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7108 | if (mddev->persistent) { |
| 7109 | if (mddev->major_version != 0 || |
| 7110 | mddev->minor_version != 90) { |
| 7111 | seq_printf(seq," super %d.%d", |
| 7112 | mddev->major_version, |
| 7113 | mddev->minor_version); |
| 7114 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7115 | } else if (mddev->external) |
| 7116 | seq_printf(seq, " super external:%s", |
| 7117 | mddev->metadata_type); |
| 7118 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7119 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7120 | |
| 7121 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7122 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7123 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7124 | if (mddev->pers->sync_request) { |
| 7125 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7126 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7127 | seq_printf(seq, "\n "); |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7128 | } else if (mddev->curr_resync >= 1) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7129 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 7130 | else if (mddev->recovery_cp < MaxSector) |
| 7131 | seq_printf(seq, "\tresync=PENDING\n "); |
| 7132 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7133 | } else |
| 7134 | seq_printf(seq, "\n "); |
| 7135 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7136 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7137 | |
| 7138 | seq_printf(seq, "\n"); |
| 7139 | } |
| 7140 | mddev_unlock(mddev); |
| 7141 | |
| 7142 | return 0; |
| 7143 | } |
| 7144 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7145 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7146 | .start = md_seq_start, |
| 7147 | .next = md_seq_next, |
| 7148 | .stop = md_seq_stop, |
| 7149 | .show = md_seq_show, |
| 7150 | }; |
| 7151 | |
| 7152 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7153 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7154 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7155 | int error; |
| 7156 | |
| 7157 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7158 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7159 | return error; |
| 7160 | |
| 7161 | seq = file->private_data; |
| 7162 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7163 | return error; |
| 7164 | } |
| 7165 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7166 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7167 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7168 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7169 | int mask; |
| 7170 | |
| 7171 | poll_wait(filp, &md_event_waiters, wait); |
| 7172 | |
| 7173 | /* always allow read */ |
| 7174 | mask = POLLIN | POLLRDNORM; |
| 7175 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7176 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7177 | mask |= POLLERR | POLLPRI; |
| 7178 | return mask; |
| 7179 | } |
| 7180 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7181 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7182 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7183 | .open = md_seq_open, |
| 7184 | .read = seq_read, |
| 7185 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7186 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7187 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7188 | }; |
| 7189 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7190 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7191 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7192 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7193 | list_add_tail(&p->list, &pers_list); |
| 7194 | printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7195 | spin_unlock(&pers_lock); |
| 7196 | return 0; |
| 7197 | } |
| 7198 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7199 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7200 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7201 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7202 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7203 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7204 | spin_unlock(&pers_lock); |
| 7205 | return 0; |
| 7206 | } |
| 7207 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7208 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7209 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7210 | struct md_rdev * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7211 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7212 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7213 | |
| 7214 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7215 | rcu_read_lock(); |
| 7216 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7217 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7218 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7219 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7220 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7221 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7222 | * as sync_io is counted when a request starts, and |
| 7223 | * disk_stats is counted when it completes. |
| 7224 | * So resync activity will cause curr_events to be smaller than |
| 7225 | * when there was no such activity. |
| 7226 | * non-sync IO will cause disk_stat to increase without |
| 7227 | * increasing sync_io so curr_events will (eventually) |
| 7228 | * be larger than it was before. Once it becomes |
| 7229 | * substantially larger, the test below will cause |
| 7230 | * the array to appear non-idle, and resync will slow |
| 7231 | * down. |
| 7232 | * If there is a lot of outstanding resync activity when |
| 7233 | * we set last_event to curr_events, then all that activity |
| 7234 | * completing might cause the array to appear non-idle |
| 7235 | * and resync will be slowed down even though there might |
| 7236 | * not have been non-resync activity. This will only |
| 7237 | * happen once though. 'last_events' will soon reflect |
| 7238 | * the state where there is little or no outstanding |
| 7239 | * resync requests, and further resync activity will |
| 7240 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7241 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7242 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7243 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7244 | rdev->last_events = curr_events; |
| 7245 | idle = 0; |
| 7246 | } |
| 7247 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7248 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7249 | return idle; |
| 7250 | } |
| 7251 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7252 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7253 | { |
| 7254 | /* another "blocks" (512byte) blocks have been synced */ |
| 7255 | atomic_sub(blocks, &mddev->recovery_active); |
| 7256 | wake_up(&mddev->recovery_wait); |
| 7257 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7258 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7259 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7260 | md_wakeup_thread(mddev->thread); |
| 7261 | // stop recovery, signal do_sync .... |
| 7262 | } |
| 7263 | } |
| 7264 | |
| 7265 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7266 | /* md_write_start(mddev, bi) |
| 7267 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7268 | * in superblock) before writing, schedule a superblock update |
| 7269 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7270 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7271 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7272 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7273 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7274 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7275 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7276 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7277 | BUG_ON(mddev->ro == 1); |
| 7278 | if (mddev->ro == 2) { |
| 7279 | /* need to switch to read/write */ |
| 7280 | mddev->ro = 0; |
| 7281 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7282 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7283 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7284 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7285 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7286 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7287 | if (mddev->safemode == 1) |
| 7288 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7289 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7290 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7291 | if (mddev->in_sync) { |
| 7292 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7293 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7294 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7295 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7296 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7297 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 7298 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7299 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7300 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7301 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7302 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7303 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7304 | } |
| 7305 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7306 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7307 | { |
| 7308 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7309 | if (mddev->safemode == 2) |
| 7310 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7311 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7312 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7313 | } |
| 7314 | } |
| 7315 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7316 | /* md_allow_write(mddev) |
| 7317 | * Calling this ensures that the array is marked 'active' so that writes |
| 7318 | * may proceed without blocking. It is important to call this before |
| 7319 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7320 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7321 | * |
| 7322 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 7323 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7324 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7325 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7326 | { |
| 7327 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7328 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7329 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7330 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7331 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7332 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7333 | |
| 7334 | spin_lock_irq(&mddev->write_lock); |
| 7335 | if (mddev->in_sync) { |
| 7336 | mddev->in_sync = 0; |
| 7337 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7338 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7339 | if (mddev->safemode_delay && |
| 7340 | mddev->safemode == 0) |
| 7341 | mddev->safemode = 1; |
| 7342 | spin_unlock_irq(&mddev->write_lock); |
| 7343 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7344 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7345 | } else |
| 7346 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7347 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7348 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7349 | return -EAGAIN; |
| 7350 | else |
| 7351 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7352 | } |
| 7353 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7355 | #define SYNC_MARKS 10 |
| 7356 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7357 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7358 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7359 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7360 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7361 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7362 | unsigned int currspeed = 0, |
| 7363 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7364 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7365 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7366 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7367 | sector_t mark_cnt[SYNC_MARKS]; |
| 7368 | int last_mark,m; |
| 7369 | struct list_head *tmp; |
| 7370 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7371 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7372 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7373 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7374 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7375 | |
| 7376 | /* just incase thread restarts... */ |
| 7377 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7378 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7379 | if (mddev->ro) /* never try to sync a read-only array */ |
| 7380 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7381 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7382 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7383 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7384 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7385 | action = "check"; |
| 7386 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7387 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7388 | action = "repair"; |
| 7389 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7390 | desc = "resync"; |
| 7391 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7392 | desc = "reshape"; |
| 7393 | else |
| 7394 | desc = "recovery"; |
| 7395 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7396 | mddev->last_sync_action = action ?: desc; |
| 7397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7398 | /* we overload curr_resync somewhat here. |
| 7399 | * 0 == not engaged in resync at all |
| 7400 | * 2 == checking that there is no conflict with another sync |
| 7401 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7402 | * commense |
| 7403 | * other == active in resync - this many blocks |
| 7404 | * |
| 7405 | * Before starting a resync we must have set curr_resync to |
| 7406 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7407 | * less than ours. When we find one that is the same or higher |
| 7408 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7409 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7410 | * This will mean we have to start checking from the beginning again. |
| 7411 | * |
| 7412 | */ |
| 7413 | |
| 7414 | do { |
| 7415 | mddev->curr_resync = 2; |
| 7416 | |
| 7417 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7418 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7419 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7420 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7421 | if (mddev2 == mddev) |
| 7422 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7423 | if (!mddev->parallel_resync |
| 7424 | && mddev2->curr_resync |
| 7425 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7426 | DEFINE_WAIT(wq); |
| 7427 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7428 | /* arbitrarily yield */ |
| 7429 | mddev->curr_resync = 1; |
| 7430 | wake_up(&resync_wait); |
| 7431 | } |
| 7432 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7433 | /* no need to wait here, we can wait the next |
| 7434 | * time 'round when curr_resync == 2 |
| 7435 | */ |
| 7436 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7437 | /* We need to wait 'interruptible' so as not to |
| 7438 | * contribute to the load average, and not to |
| 7439 | * be caught by 'softlockup' |
| 7440 | */ |
| 7441 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7442 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7443 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7444 | printk(KERN_INFO "md: delaying %s of %s" |
| 7445 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7446 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7447 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7448 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7449 | if (signal_pending(current)) |
| 7450 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7451 | schedule(); |
| 7452 | finish_wait(&resync_wait, &wq); |
| 7453 | goto try_again; |
| 7454 | } |
| 7455 | finish_wait(&resync_wait, &wq); |
| 7456 | } |
| 7457 | } |
| 7458 | } while (mddev->curr_resync < 2); |
| 7459 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7460 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7461 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7462 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7463 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7464 | */ |
| 7465 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 7466 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7467 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7468 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7469 | j = mddev->resync_min; |
| 7470 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7471 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7472 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7473 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7474 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7475 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7476 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7477 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7478 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7479 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7480 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7481 | if (rdev->raid_disk >= 0 && |
| 7482 | !test_bit(Faulty, &rdev->flags) && |
| 7483 | !test_bit(In_sync, &rdev->flags) && |
| 7484 | rdev->recovery_offset < j) |
| 7485 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7486 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7488 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7489 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7490 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7491 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7492 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7493 | "(but not more than %d KB/sec) for %s.\n", |
| 7494 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7495 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7496 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7497 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7498 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7499 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7500 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7501 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7502 | } |
| 7503 | last_mark = 0; |
| 7504 | mddev->resync_mark = mark[last_mark]; |
| 7505 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7506 | |
| 7507 | /* |
| 7508 | * Tune reconstruction: |
| 7509 | */ |
| 7510 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450 | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7511 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7512 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7513 | |
| 7514 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7515 | last_check = 0; |
| 7516 | |
| 7517 | if (j>2) { |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7518 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7519 | "md: resuming %s of %s from checkpoint.\n", |
| 7520 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7521 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7522 | } else |
| 7523 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7524 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7525 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 7526 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7527 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7528 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7529 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7530 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7531 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7532 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7533 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7534 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7535 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7536 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7537 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7538 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7539 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7540 | (j - mddev->curr_resync_completed)*2 |
| 7541 | >= mddev->resync_max - mddev->curr_resync_completed |
| 7542 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7543 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7544 | wait_event(mddev->recovery_wait, |
| 7545 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7546 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7547 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 7548 | j > mddev->recovery_cp) |
| 7549 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7550 | update_time = jiffies; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7551 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7552 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7553 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7554 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7555 | while (j >= mddev->resync_max && |
| 7556 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7557 | /* As this condition is controlled by user-space, |
| 7558 | * we can block indefinitely, so use '_interruptible' |
| 7559 | * to avoid triggering warnings. |
| 7560 | */ |
| 7561 | flush_signals(current); /* just in case */ |
| 7562 | wait_event_interruptible(mddev->recovery_wait, |
| 7563 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7564 | || test_bit(MD_RECOVERY_INTR, |
| 7565 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7566 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7567 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7568 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7569 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7570 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7571 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7572 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7573 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7574 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7575 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7576 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7577 | |
| 7578 | if (!skipped) { /* actual IO requested */ |
| 7579 | io_sectors += sectors; |
| 7580 | atomic_add(sectors, &mddev->recovery_active); |
| 7581 | } |
| 7582 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7583 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7584 | break; |
| 7585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7586 | j += sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7587 | if (j > 2) |
| 7588 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7589 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7590 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 7591 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7592 | * visible in /proc/mdstat |
| 7593 | */ |
| 7594 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7595 | |
| 7596 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7597 | continue; |
| 7598 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7599 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7600 | repeat: |
| 7601 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 7602 | /* step marks */ |
| 7603 | int next = (last_mark+1) % SYNC_MARKS; |
| 7604 | |
| 7605 | mddev->resync_mark = mark[next]; |
| 7606 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 7607 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7608 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7609 | last_mark = next; |
| 7610 | } |
| 7611 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7612 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7613 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7614 | |
| 7615 | /* |
| 7616 | * this loop exits only if either when we are slower than |
| 7617 | * the 'hard' speed limit, or the system was IO-idle for |
| 7618 | * a jiffy. |
| 7619 | * the system might be non-idle CPU-wise, but we only care |
| 7620 | * about not overloading the IO subsystem. (things like an |
| 7621 | * e2fsck being done on the RAID array should execute fast) |
| 7622 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7623 | cond_resched(); |
| 7624 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7625 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 7626 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7627 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 7628 | if (currspeed > speed_min(mddev)) { |
| 7629 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7630 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7631 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7632 | goto repeat; |
| 7633 | } |
| 7634 | } |
| 7635 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7636 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
| 7637 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 7638 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7639 | /* |
| 7640 | * this also signals 'finished resyncing' to md_stop |
| 7641 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7642 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7643 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 7644 | |
| 7645 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7646 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7647 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7648 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7649 | mddev->curr_resync > 2) { |
| 7650 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 7651 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7652 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 7653 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7654 | "md: checkpointing %s of %s.\n", |
| 7655 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7656 | if (test_bit(MD_RECOVERY_ERROR, |
| 7657 | &mddev->recovery)) |
| 7658 | mddev->recovery_cp = |
| 7659 | mddev->curr_resync_completed; |
| 7660 | else |
| 7661 | mddev->recovery_cp = |
| 7662 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7663 | } |
| 7664 | } else |
| 7665 | mddev->recovery_cp = MaxSector; |
| 7666 | } else { |
| 7667 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7668 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7669 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7670 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7671 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7672 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7673 | !test_bit(Faulty, &rdev->flags) && |
| 7674 | !test_bit(In_sync, &rdev->flags) && |
| 7675 | rdev->recovery_offset < mddev->curr_resync) |
| 7676 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7677 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7678 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7679 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 7680 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 7681 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7682 | |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 7683 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7684 | /* We completed so min/max setting can be forgotten if used. */ |
| 7685 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7686 | mddev->resync_min = 0; |
| 7687 | mddev->resync_max = MaxSector; |
| 7688 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7689 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7690 | mddev->curr_resync = 0; |
| 7691 | wake_up(&resync_wait); |
| 7692 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 7693 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 7694 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7695 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7696 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7697 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7698 | static int remove_and_add_spares(struct mddev *mddev, |
| 7699 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7700 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7701 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7702 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7703 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7704 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7705 | rdev_for_each(rdev, mddev) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7706 | if ((this == NULL || rdev == this) && |
| 7707 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7708 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7709 | (test_bit(Faulty, &rdev->flags) || |
| 7710 | ! test_bit(In_sync, &rdev->flags)) && |
| 7711 | atomic_read(&rdev->nr_pending)==0) { |
| 7712 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7713 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7714 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7715 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 7716 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7717 | } |
| 7718 | } |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 7719 | if (removed && mddev->kobj.sd) |
| 7720 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7721 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7722 | if (this) |
| 7723 | goto no_add; |
| 7724 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7725 | rdev_for_each(rdev, mddev) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7726 | if (rdev->raid_disk >= 0 && |
| 7727 | !test_bit(In_sync, &rdev->flags) && |
| 7728 | !test_bit(Faulty, &rdev->flags)) |
| 7729 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7730 | if (rdev->raid_disk >= 0) |
| 7731 | continue; |
| 7732 | if (test_bit(Faulty, &rdev->flags)) |
| 7733 | continue; |
| 7734 | if (mddev->ro && |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 7735 | ! (rdev->saved_raid_disk >= 0 && |
| 7736 | !test_bit(Bitmap_sync, &rdev->flags))) |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7737 | continue; |
| 7738 | |
| 7739 | rdev->recovery_offset = 0; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7740 | if (mddev->pers-> |
| 7741 | hot_add_disk(mddev, rdev) == 0) { |
| 7742 | if (sysfs_link_rdev(mddev, rdev)) |
| 7743 | /* failure here is OK */; |
| 7744 | spares++; |
| 7745 | md_new_event(mddev); |
| 7746 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7747 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7748 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7749 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 7750 | if (removed) |
| 7751 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7752 | return spares; |
| 7753 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7755 | /* |
| 7756 | * This routine is regularly called by all per-raid-array threads to |
| 7757 | * deal with generic issues like resync and super-block update. |
| 7758 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7759 | * need this as they never do any recovery or update the superblock. |
| 7760 | * |
| 7761 | * It does not do any resync itself, but rather "forks" off other threads |
| 7762 | * to do that as needed. |
| 7763 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7764 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7765 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7766 | * and wakeups up this thread which will reap the thread and finish up. |
| 7767 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7768 | * |
| 7769 | * The overall approach is: |
| 7770 | * 1/ if the superblock needs updating, update it. |
| 7771 | * 2/ If a recovery thread is running, don't do anything else. |
| 7772 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7773 | * 4/ If there are any faulty devices, remove them. |
| 7774 | * 5/ If array is degraded, try to add spares devices |
| 7775 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7776 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7777 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7778 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 7779 | if (mddev->suspended) |
| 7780 | return; |
| 7781 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7782 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7783 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7784 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7785 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7786 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7787 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7788 | mdname(mddev)); |
| 7789 | mddev->safemode = 2; |
| 7790 | } |
| 7791 | flush_signals(current); |
| 7792 | } |
| 7793 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7794 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7795 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7796 | if ( ! ( |
NeilBrown | 142d44c | 2013-11-28 10:34:18 +1100 | [diff] [blame] | 7797 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7798 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7799 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7800 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7801 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7802 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7803 | )) |
| 7804 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7805 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7806 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7807 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7808 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7809 | if (mddev->ro) { |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7810 | /* On a read-only array we can: |
| 7811 | * - remove failed devices |
| 7812 | * - add already-in_sync devices if the array itself |
| 7813 | * is in-sync. |
| 7814 | * As we only add devices that are already in-sync, |
| 7815 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7816 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7817 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 7818 | /* There is no thread, but we need to call |
| 7819 | * ->spare_active and clear saved_raid_disk |
| 7820 | */ |
| 7821 | md_reap_sync_thread(mddev); |
| 7822 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7823 | goto unlock; |
| 7824 | } |
| 7825 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7826 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7827 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7828 | spin_lock_irq(&mddev->write_lock); |
| 7829 | if (mddev->safemode && |
| 7830 | !atomic_read(&mddev->writes_pending) && |
| 7831 | !mddev->in_sync && |
| 7832 | mddev->recovery_cp == MaxSector) { |
| 7833 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7834 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7835 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7836 | } |
| 7837 | if (mddev->safemode == 1) |
| 7838 | mddev->safemode = 0; |
| 7839 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7840 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7841 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7842 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7843 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 7844 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7845 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7847 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7848 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7849 | /* resync/recovery still happening */ |
| 7850 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7851 | goto unlock; |
| 7852 | } |
| 7853 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 7854 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7855 | goto unlock; |
| 7856 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7857 | /* Set RUNNING before clearing NEEDED to avoid |
| 7858 | * any transients in the value of "sync_action". |
| 7859 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7860 | mddev->curr_resync_completed = 0; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7861 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7862 | /* Clear some bits that don't mean anything, but |
| 7863 | * might be left set |
| 7864 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7865 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7866 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7867 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 7868 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 7869 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7870 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7871 | /* no recovery is running. |
| 7872 | * remove any failed drives, then |
| 7873 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7874 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7875 | * the personality to fail the re-add. |
| 7876 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7877 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7878 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7879 | if (mddev->pers->check_reshape == NULL || |
| 7880 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7881 | /* Cannot proceed */ |
| 7882 | goto unlock; |
| 7883 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7884 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7885 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7886 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7887 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7888 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7889 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7890 | } else if (mddev->recovery_cp < MaxSector) { |
| 7891 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7892 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7893 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7894 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7895 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7896 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7897 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 7898 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7899 | /* We are adding a device or devices to an array |
| 7900 | * which has the bitmap stored on all devices. |
| 7901 | * So make sure all bitmap pages get written |
| 7902 | */ |
| 7903 | bitmap_write_all(mddev->bitmap); |
| 7904 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7905 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7906 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7907 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7908 | if (!mddev->sync_thread) { |
| 7909 | printk(KERN_ERR "%s: could not start resync" |
| 7910 | " thread...\n", |
| 7911 | mdname(mddev)); |
| 7912 | /* leave the spares where they are, it shouldn't hurt */ |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7913 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7914 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7915 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7916 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7917 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7918 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7919 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7920 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7921 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7922 | } |
| 7923 | unlock: |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7924 | wake_up(&mddev->sb_wait); |
| 7925 | |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7926 | if (!mddev->sync_thread) { |
| 7927 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7928 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7929 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7930 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7931 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7932 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7933 | mddev_unlock(mddev); |
| 7934 | } |
| 7935 | } |
| 7936 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 7937 | void md_reap_sync_thread(struct mddev *mddev) |
| 7938 | { |
| 7939 | struct md_rdev *rdev; |
| 7940 | |
| 7941 | /* resync has finished, collect result */ |
| 7942 | md_unregister_thread(&mddev->sync_thread); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7943 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 7944 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7945 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 7946 | /* success...*/ |
| 7947 | /* activate any spares */ |
| 7948 | if (mddev->pers->spare_active(mddev)) { |
| 7949 | sysfs_notify(&mddev->kobj, NULL, |
| 7950 | "degraded"); |
| 7951 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 7952 | } |
| 7953 | } |
| 7954 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7955 | mddev->pers->finish_reshape) |
| 7956 | mddev->pers->finish_reshape(mddev); |
| 7957 | |
| 7958 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame^] | 7959 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 7960 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame^] | 7961 | if (!mddev->degraded) |
| 7962 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 7963 | rdev->saved_raid_disk = -1; |
| 7964 | |
| 7965 | md_update_sb(mddev, 1); |
| 7966 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7967 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7968 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7969 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 7970 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7971 | /* flag recovery needed just to double check */ |
| 7972 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7973 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 7974 | md_new_event(mddev); |
| 7975 | if (mddev->event_work.func) |
| 7976 | queue_work(md_misc_wq, &mddev->event_work); |
| 7977 | } |
| 7978 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7979 | 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] | 7980 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7981 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7982 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7983 | !test_bit(Blocked, &rdev->flags) && |
| 7984 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7985 | msecs_to_jiffies(5000)); |
| 7986 | rdev_dec_pending(rdev, mddev); |
| 7987 | } |
| 7988 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7989 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7990 | void md_finish_reshape(struct mddev *mddev) |
| 7991 | { |
| 7992 | /* called be personality module when reshape completes. */ |
| 7993 | struct md_rdev *rdev; |
| 7994 | |
| 7995 | rdev_for_each(rdev, mddev) { |
| 7996 | if (rdev->data_offset > rdev->new_data_offset) |
| 7997 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 7998 | else |
| 7999 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 8000 | rdev->data_offset = rdev->new_data_offset; |
| 8001 | } |
| 8002 | } |
| 8003 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8004 | |
| 8005 | /* Bad block management. |
| 8006 | * We can record which blocks on each device are 'bad' and so just |
| 8007 | * fail those blocks, or that stripe, rather than the whole device. |
| 8008 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 8009 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 8010 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 8011 | * A 'shift' can be set so that larger blocks are tracked and |
| 8012 | * consequently larger devices can be covered. |
| 8013 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 8014 | * |
| 8015 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 8016 | * might need to retry if it is very unlucky. |
| 8017 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 8018 | * so we use the write_seqlock_irq variant. |
| 8019 | * |
| 8020 | * When looking for a bad block we specify a range and want to |
| 8021 | * know if any block in the range is bad. So we binary-search |
| 8022 | * to the last range that starts at-or-before the given endpoint, |
| 8023 | * (or "before the sector after the target range") |
| 8024 | * then see if it ends after the given start. |
| 8025 | * We return |
| 8026 | * 0 if there are no known bad blocks in the range |
| 8027 | * 1 if there are known bad block which are all acknowledged |
| 8028 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 8029 | * plus the start/length of the first bad section we overlap. |
| 8030 | */ |
| 8031 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 8032 | sector_t *first_bad, int *bad_sectors) |
| 8033 | { |
| 8034 | int hi; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8035 | int lo; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8036 | u64 *p = bb->page; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8037 | int rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8038 | sector_t target = s + sectors; |
| 8039 | unsigned seq; |
| 8040 | |
| 8041 | if (bb->shift > 0) { |
| 8042 | /* round the start down, and the end up */ |
| 8043 | s >>= bb->shift; |
| 8044 | target += (1<<bb->shift) - 1; |
| 8045 | target >>= bb->shift; |
| 8046 | sectors = target - s; |
| 8047 | } |
| 8048 | /* 'target' is now the first block after the bad range */ |
| 8049 | |
| 8050 | retry: |
| 8051 | seq = read_seqbegin(&bb->lock); |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8052 | lo = 0; |
| 8053 | rv = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8054 | hi = bb->count; |
| 8055 | |
| 8056 | /* Binary search between lo and hi for 'target' |
| 8057 | * i.e. for the last range that starts before 'target' |
| 8058 | */ |
| 8059 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 8060 | * are known not to be the last range before target. |
| 8061 | * VARIANT: hi-lo is the number of possible |
| 8062 | * ranges, and decreases until it reaches 1 |
| 8063 | */ |
| 8064 | while (hi - lo > 1) { |
| 8065 | int mid = (lo + hi) / 2; |
| 8066 | sector_t a = BB_OFFSET(p[mid]); |
| 8067 | if (a < target) |
| 8068 | /* This could still be the one, earlier ranges |
| 8069 | * could not. */ |
| 8070 | lo = mid; |
| 8071 | else |
| 8072 | /* This and later ranges are definitely out. */ |
| 8073 | hi = mid; |
| 8074 | } |
| 8075 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 8076 | if (hi > lo) { |
| 8077 | /* need to check all range that end after 's' to see if |
| 8078 | * any are unacknowledged. |
| 8079 | */ |
| 8080 | while (lo >= 0 && |
| 8081 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8082 | if (BB_OFFSET(p[lo]) < target) { |
| 8083 | /* starts before the end, and finishes after |
| 8084 | * the start, so they must overlap |
| 8085 | */ |
| 8086 | if (rv != -1 && BB_ACK(p[lo])) |
| 8087 | rv = 1; |
| 8088 | else |
| 8089 | rv = -1; |
| 8090 | *first_bad = BB_OFFSET(p[lo]); |
| 8091 | *bad_sectors = BB_LEN(p[lo]); |
| 8092 | } |
| 8093 | lo--; |
| 8094 | } |
| 8095 | } |
| 8096 | |
| 8097 | if (read_seqretry(&bb->lock, seq)) |
| 8098 | goto retry; |
| 8099 | |
| 8100 | return rv; |
| 8101 | } |
| 8102 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 8103 | |
| 8104 | /* |
| 8105 | * Add a range of bad blocks to the table. |
| 8106 | * This might extend the table, or might contract it |
| 8107 | * if two adjacent ranges can be merged. |
| 8108 | * We binary-search to find the 'insertion' point, then |
| 8109 | * decide how best to handle it. |
| 8110 | */ |
| 8111 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 8112 | int acknowledged) |
| 8113 | { |
| 8114 | u64 *p; |
| 8115 | int lo, hi; |
| 8116 | int rv = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8117 | unsigned long flags; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8118 | |
| 8119 | if (bb->shift < 0) |
| 8120 | /* badblocks are disabled */ |
| 8121 | return 0; |
| 8122 | |
| 8123 | if (bb->shift) { |
| 8124 | /* round the start down, and the end up */ |
| 8125 | sector_t next = s + sectors; |
| 8126 | s >>= bb->shift; |
| 8127 | next += (1<<bb->shift) - 1; |
| 8128 | next >>= bb->shift; |
| 8129 | sectors = next - s; |
| 8130 | } |
| 8131 | |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8132 | write_seqlock_irqsave(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8133 | |
| 8134 | p = bb->page; |
| 8135 | lo = 0; |
| 8136 | hi = bb->count; |
| 8137 | /* Find the last range that starts at-or-before 's' */ |
| 8138 | while (hi - lo > 1) { |
| 8139 | int mid = (lo + hi) / 2; |
| 8140 | sector_t a = BB_OFFSET(p[mid]); |
| 8141 | if (a <= s) |
| 8142 | lo = mid; |
| 8143 | else |
| 8144 | hi = mid; |
| 8145 | } |
| 8146 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 8147 | hi = lo; |
| 8148 | |
| 8149 | if (hi > lo) { |
| 8150 | /* we found a range that might merge with the start |
| 8151 | * of our new range |
| 8152 | */ |
| 8153 | sector_t a = BB_OFFSET(p[lo]); |
| 8154 | sector_t e = a + BB_LEN(p[lo]); |
| 8155 | int ack = BB_ACK(p[lo]); |
| 8156 | if (e >= s) { |
| 8157 | /* Yes, we can merge with a previous range */ |
| 8158 | if (s == a && s + sectors >= e) |
| 8159 | /* new range covers old */ |
| 8160 | ack = acknowledged; |
| 8161 | else |
| 8162 | ack = ack && acknowledged; |
| 8163 | |
| 8164 | if (e < s + sectors) |
| 8165 | e = s + sectors; |
| 8166 | if (e - a <= BB_MAX_LEN) { |
| 8167 | p[lo] = BB_MAKE(a, e-a, ack); |
| 8168 | s = e; |
| 8169 | } else { |
| 8170 | /* does not all fit in one range, |
| 8171 | * make p[lo] maximal |
| 8172 | */ |
| 8173 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8174 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8175 | s = a + BB_MAX_LEN; |
| 8176 | } |
| 8177 | sectors = e - s; |
| 8178 | } |
| 8179 | } |
| 8180 | if (sectors && hi < bb->count) { |
| 8181 | /* 'hi' points to the first range that starts after 's'. |
| 8182 | * Maybe we can merge with the start of that range */ |
| 8183 | sector_t a = BB_OFFSET(p[hi]); |
| 8184 | sector_t e = a + BB_LEN(p[hi]); |
| 8185 | int ack = BB_ACK(p[hi]); |
| 8186 | if (a <= s + sectors) { |
| 8187 | /* merging is possible */ |
| 8188 | if (e <= s + sectors) { |
| 8189 | /* full overlap */ |
| 8190 | e = s + sectors; |
| 8191 | ack = acknowledged; |
| 8192 | } else |
| 8193 | ack = ack && acknowledged; |
| 8194 | |
| 8195 | a = s; |
| 8196 | if (e - a <= BB_MAX_LEN) { |
| 8197 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8198 | s = e; |
| 8199 | } else { |
| 8200 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8201 | s = a + BB_MAX_LEN; |
| 8202 | } |
| 8203 | sectors = e - s; |
| 8204 | lo = hi; |
| 8205 | hi++; |
| 8206 | } |
| 8207 | } |
| 8208 | if (sectors == 0 && hi < bb->count) { |
| 8209 | /* we might be able to combine lo and hi */ |
| 8210 | /* Note: 's' is at the end of 'lo' */ |
| 8211 | sector_t a = BB_OFFSET(p[hi]); |
| 8212 | int lolen = BB_LEN(p[lo]); |
| 8213 | int hilen = BB_LEN(p[hi]); |
| 8214 | int newlen = lolen + hilen - (s - a); |
| 8215 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8216 | /* yes, we can combine them */ |
| 8217 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8218 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8219 | memmove(p + hi, p + hi + 1, |
| 8220 | (bb->count - hi - 1) * 8); |
| 8221 | bb->count--; |
| 8222 | } |
| 8223 | } |
| 8224 | while (sectors) { |
| 8225 | /* didn't merge (it all). |
| 8226 | * Need to add a range just before 'hi' */ |
| 8227 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8228 | /* No room for more */ |
| 8229 | rv = 0; |
| 8230 | break; |
| 8231 | } else { |
| 8232 | int this_sectors = sectors; |
| 8233 | memmove(p + hi + 1, p + hi, |
| 8234 | (bb->count - hi) * 8); |
| 8235 | bb->count++; |
| 8236 | |
| 8237 | if (this_sectors > BB_MAX_LEN) |
| 8238 | this_sectors = BB_MAX_LEN; |
| 8239 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8240 | sectors -= this_sectors; |
| 8241 | s += this_sectors; |
| 8242 | } |
| 8243 | } |
| 8244 | |
| 8245 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8246 | if (!acknowledged) |
| 8247 | bb->unacked_exist = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8248 | write_sequnlock_irqrestore(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8249 | |
| 8250 | return rv; |
| 8251 | } |
| 8252 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8253 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8254 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8255 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8256 | int rv; |
| 8257 | if (is_new) |
| 8258 | s += rdev->new_data_offset; |
| 8259 | else |
| 8260 | s += rdev->data_offset; |
| 8261 | rv = md_set_badblocks(&rdev->badblocks, |
| 8262 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8263 | if (rv) { |
| 8264 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8265 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8266 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
| 8267 | md_wakeup_thread(rdev->mddev->thread); |
| 8268 | } |
| 8269 | return rv; |
| 8270 | } |
| 8271 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8272 | |
| 8273 | /* |
| 8274 | * Remove a range of bad blocks from the table. |
| 8275 | * This may involve extending the table if we spilt a region, |
| 8276 | * but it must not fail. So if the table becomes full, we just |
| 8277 | * drop the remove request. |
| 8278 | */ |
| 8279 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8280 | { |
| 8281 | u64 *p; |
| 8282 | int lo, hi; |
| 8283 | sector_t target = s + sectors; |
| 8284 | int rv = 0; |
| 8285 | |
| 8286 | if (bb->shift > 0) { |
| 8287 | /* When clearing we round the start up and the end down. |
| 8288 | * This should not matter as the shift should align with |
| 8289 | * the block size and no rounding should ever be needed. |
| 8290 | * However it is better the think a block is bad when it |
| 8291 | * isn't than to think a block is not bad when it is. |
| 8292 | */ |
| 8293 | s += (1<<bb->shift) - 1; |
| 8294 | s >>= bb->shift; |
| 8295 | target >>= bb->shift; |
| 8296 | sectors = target - s; |
| 8297 | } |
| 8298 | |
| 8299 | write_seqlock_irq(&bb->lock); |
| 8300 | |
| 8301 | p = bb->page; |
| 8302 | lo = 0; |
| 8303 | hi = bb->count; |
| 8304 | /* Find the last range that starts before 'target' */ |
| 8305 | while (hi - lo > 1) { |
| 8306 | int mid = (lo + hi) / 2; |
| 8307 | sector_t a = BB_OFFSET(p[mid]); |
| 8308 | if (a < target) |
| 8309 | lo = mid; |
| 8310 | else |
| 8311 | hi = mid; |
| 8312 | } |
| 8313 | if (hi > lo) { |
| 8314 | /* p[lo] is the last range that could overlap the |
| 8315 | * current range. Earlier ranges could also overlap, |
| 8316 | * but only this one can overlap the end of the range. |
| 8317 | */ |
| 8318 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8319 | /* Partial overlap, leave the tail of this range */ |
| 8320 | int ack = BB_ACK(p[lo]); |
| 8321 | sector_t a = BB_OFFSET(p[lo]); |
| 8322 | sector_t end = a + BB_LEN(p[lo]); |
| 8323 | |
| 8324 | if (a < s) { |
| 8325 | /* we need to split this range */ |
| 8326 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8327 | rv = 0; |
| 8328 | goto out; |
| 8329 | } |
| 8330 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8331 | bb->count++; |
| 8332 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8333 | lo++; |
| 8334 | } |
| 8335 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8336 | /* there is no longer an overlap */ |
| 8337 | hi = lo; |
| 8338 | lo--; |
| 8339 | } |
| 8340 | while (lo >= 0 && |
| 8341 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8342 | /* This range does overlap */ |
| 8343 | if (BB_OFFSET(p[lo]) < s) { |
| 8344 | /* Keep the early parts of this range. */ |
| 8345 | int ack = BB_ACK(p[lo]); |
| 8346 | sector_t start = BB_OFFSET(p[lo]); |
| 8347 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8348 | /* now low doesn't overlap, so.. */ |
| 8349 | break; |
| 8350 | } |
| 8351 | lo--; |
| 8352 | } |
| 8353 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8354 | * anything between needs to be discarded |
| 8355 | */ |
| 8356 | if (hi - lo > 1) { |
| 8357 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8358 | bb->count -= (hi - lo - 1); |
| 8359 | } |
| 8360 | } |
| 8361 | |
| 8362 | bb->changed = 1; |
| 8363 | out: |
| 8364 | write_sequnlock_irq(&bb->lock); |
| 8365 | return rv; |
| 8366 | } |
| 8367 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8368 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8369 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8370 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8371 | if (is_new) |
| 8372 | s += rdev->new_data_offset; |
| 8373 | else |
| 8374 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8375 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8376 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8377 | } |
| 8378 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8379 | |
| 8380 | /* |
| 8381 | * Acknowledge all bad blocks in a list. |
| 8382 | * This only succeeds if ->changed is clear. It is used by |
| 8383 | * in-kernel metadata updates |
| 8384 | */ |
| 8385 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8386 | { |
| 8387 | if (bb->page == NULL || bb->changed) |
| 8388 | /* no point even trying */ |
| 8389 | return; |
| 8390 | write_seqlock_irq(&bb->lock); |
| 8391 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8392 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8393 | u64 *p = bb->page; |
| 8394 | int i; |
| 8395 | for (i = 0; i < bb->count ; i++) { |
| 8396 | if (!BB_ACK(p[i])) { |
| 8397 | sector_t start = BB_OFFSET(p[i]); |
| 8398 | int len = BB_LEN(p[i]); |
| 8399 | p[i] = BB_MAKE(start, len, 1); |
| 8400 | } |
| 8401 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8402 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8403 | } |
| 8404 | write_sequnlock_irq(&bb->lock); |
| 8405 | } |
| 8406 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8407 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8408 | /* sysfs access to bad-blocks list. |
| 8409 | * We present two files. |
| 8410 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8411 | * are recorded as bad. The list is truncated to fit within |
| 8412 | * the one-page limit of sysfs. |
| 8413 | * Writing "sector length" to this file adds an acknowledged |
| 8414 | * bad block list. |
| 8415 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8416 | * been acknowledged. Writing to this file adds bad blocks |
| 8417 | * without acknowledging them. This is largely for testing. |
| 8418 | */ |
| 8419 | |
| 8420 | static ssize_t |
| 8421 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8422 | { |
| 8423 | size_t len; |
| 8424 | int i; |
| 8425 | u64 *p = bb->page; |
| 8426 | unsigned seq; |
| 8427 | |
| 8428 | if (bb->shift < 0) |
| 8429 | return 0; |
| 8430 | |
| 8431 | retry: |
| 8432 | seq = read_seqbegin(&bb->lock); |
| 8433 | |
| 8434 | len = 0; |
| 8435 | i = 0; |
| 8436 | |
| 8437 | while (len < PAGE_SIZE && i < bb->count) { |
| 8438 | sector_t s = BB_OFFSET(p[i]); |
| 8439 | unsigned int length = BB_LEN(p[i]); |
| 8440 | int ack = BB_ACK(p[i]); |
| 8441 | i++; |
| 8442 | |
| 8443 | if (unack && ack) |
| 8444 | continue; |
| 8445 | |
| 8446 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8447 | (unsigned long long)s << bb->shift, |
| 8448 | length << bb->shift); |
| 8449 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8450 | if (unack && len == 0) |
| 8451 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8452 | |
| 8453 | if (read_seqretry(&bb->lock, seq)) |
| 8454 | goto retry; |
| 8455 | |
| 8456 | return len; |
| 8457 | } |
| 8458 | |
| 8459 | #define DO_DEBUG 1 |
| 8460 | |
| 8461 | static ssize_t |
| 8462 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8463 | { |
| 8464 | unsigned long long sector; |
| 8465 | int length; |
| 8466 | char newline; |
| 8467 | #ifdef DO_DEBUG |
| 8468 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8469 | * Normally only a successful write may clear a badblock |
| 8470 | */ |
| 8471 | int clear = 0; |
| 8472 | if (page[0] == '-') { |
| 8473 | clear = 1; |
| 8474 | page++; |
| 8475 | } |
| 8476 | #endif /* DO_DEBUG */ |
| 8477 | |
| 8478 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8479 | case 3: |
| 8480 | if (newline != '\n') |
| 8481 | return -EINVAL; |
| 8482 | case 2: |
| 8483 | if (length <= 0) |
| 8484 | return -EINVAL; |
| 8485 | break; |
| 8486 | default: |
| 8487 | return -EINVAL; |
| 8488 | } |
| 8489 | |
| 8490 | #ifdef DO_DEBUG |
| 8491 | if (clear) { |
| 8492 | md_clear_badblocks(bb, sector, length); |
| 8493 | return len; |
| 8494 | } |
| 8495 | #endif /* DO_DEBUG */ |
| 8496 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 8497 | return len; |
| 8498 | else |
| 8499 | return -ENOSPC; |
| 8500 | } |
| 8501 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8502 | static int md_notify_reboot(struct notifier_block *this, |
| 8503 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8504 | { |
| 8505 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8506 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8507 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8508 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8509 | for_each_mddev(mddev, tmp) { |
| 8510 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 8511 | if (mddev->pers) |
| 8512 | __md_stop_writes(mddev); |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8513 | mddev->safemode = 2; |
| 8514 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 8515 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8516 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8517 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8518 | /* |
| 8519 | * certain more exotic SCSI devices are known to be |
| 8520 | * volatile wrt too early system reboots. While the |
| 8521 | * right place to handle this issue is the given |
| 8522 | * driver, we do want to have a safe RAID driver ... |
| 8523 | */ |
| 8524 | if (need_delay) |
| 8525 | mdelay(1000*1); |
| 8526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8527 | return NOTIFY_DONE; |
| 8528 | } |
| 8529 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8530 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8531 | .notifier_call = md_notify_reboot, |
| 8532 | .next = NULL, |
| 8533 | .priority = INT_MAX, /* before any real devices */ |
| 8534 | }; |
| 8535 | |
| 8536 | static void md_geninit(void) |
| 8537 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8538 | 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] | 8539 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 8540 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8541 | } |
| 8542 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 8543 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8544 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8545 | int ret = -ENOMEM; |
| 8546 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 8547 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8548 | if (!md_wq) |
| 8549 | goto err_wq; |
| 8550 | |
| 8551 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 8552 | if (!md_misc_wq) |
| 8553 | goto err_misc_wq; |
| 8554 | |
| 8555 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 8556 | goto err_md; |
| 8557 | |
| 8558 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 8559 | goto err_mdp; |
| 8560 | mdp_major = ret; |
| 8561 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8562 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8563 | md_probe, NULL, NULL); |
| 8564 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8565 | md_probe, NULL, NULL); |
| 8566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8567 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 8568 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8569 | |
| 8570 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8571 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8572 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8573 | err_mdp: |
| 8574 | unregister_blkdev(MD_MAJOR, "md"); |
| 8575 | err_md: |
| 8576 | destroy_workqueue(md_misc_wq); |
| 8577 | err_misc_wq: |
| 8578 | destroy_workqueue(md_wq); |
| 8579 | err_wq: |
| 8580 | return ret; |
| 8581 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8582 | |
| 8583 | #ifndef MODULE |
| 8584 | |
| 8585 | /* |
| 8586 | * Searches all registered partitions for autorun RAID arrays |
| 8587 | * at boot time. |
| 8588 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8589 | |
| 8590 | static LIST_HEAD(all_detected_devices); |
| 8591 | struct detected_devices_node { |
| 8592 | struct list_head list; |
| 8593 | dev_t dev; |
| 8594 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8595 | |
| 8596 | void md_autodetect_dev(dev_t dev) |
| 8597 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8598 | struct detected_devices_node *node_detected_dev; |
| 8599 | |
| 8600 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 8601 | if (node_detected_dev) { |
| 8602 | node_detected_dev->dev = dev; |
| 8603 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 8604 | } else { |
| 8605 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 8606 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 8607 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8608 | } |
| 8609 | |
| 8610 | |
| 8611 | static void autostart_arrays(int part) |
| 8612 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8613 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8614 | struct detected_devices_node *node_detected_dev; |
| 8615 | dev_t dev; |
| 8616 | int i_scanned, i_passed; |
| 8617 | |
| 8618 | i_scanned = 0; |
| 8619 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8620 | |
| 8621 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 8622 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8623 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 8624 | i_scanned++; |
| 8625 | node_detected_dev = list_entry(all_detected_devices.next, |
| 8626 | struct detected_devices_node, list); |
| 8627 | list_del(&node_detected_dev->list); |
| 8628 | dev = node_detected_dev->dev; |
| 8629 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 8630 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8631 | if (IS_ERR(rdev)) |
| 8632 | continue; |
| 8633 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8634 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8635 | MD_BUG(); |
| 8636 | continue; |
| 8637 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 8638 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8639 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8640 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8641 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 8642 | |
| 8643 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 8644 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8645 | |
| 8646 | autorun_devices(part); |
| 8647 | } |
| 8648 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 8649 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8650 | |
| 8651 | static __exit void md_exit(void) |
| 8652 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8653 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8654 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 8655 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8656 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 8657 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8658 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8659 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8660 | unregister_blkdev(mdp_major, "mdp"); |
| 8661 | unregister_reboot_notifier(&md_notifier); |
| 8662 | unregister_sysctl_table(raid_table_header); |
| 8663 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8664 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8665 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8666 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8667 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8668 | destroy_workqueue(md_misc_wq); |
| 8669 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8670 | } |
| 8671 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 8672 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8673 | module_exit(md_exit) |
| 8674 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8675 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 8676 | { |
| 8677 | return sprintf(buffer, "%d", start_readonly); |
| 8678 | } |
| 8679 | static int set_ro(const char *val, struct kernel_param *kp) |
| 8680 | { |
| 8681 | char *e; |
| 8682 | int num = simple_strtoul(val, &e, 10); |
| 8683 | if (*val && (*e == '\0' || *e == '\n')) { |
| 8684 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 8685 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8686 | } |
| 8687 | return -EINVAL; |
| 8688 | } |
| 8689 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 8690 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 8691 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 8692 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 8693 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8695 | EXPORT_SYMBOL(register_md_personality); |
| 8696 | EXPORT_SYMBOL(unregister_md_personality); |
| 8697 | EXPORT_SYMBOL(md_error); |
| 8698 | EXPORT_SYMBOL(md_done_sync); |
| 8699 | EXPORT_SYMBOL(md_write_start); |
| 8700 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8701 | EXPORT_SYMBOL(md_register_thread); |
| 8702 | EXPORT_SYMBOL(md_unregister_thread); |
| 8703 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8704 | EXPORT_SYMBOL(md_check_recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8705 | EXPORT_SYMBOL(md_reap_sync_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8706 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8707 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 8708 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 8709 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |